View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.data.lucene;
    20
    21  import org.owasp.dependencycheck.data.lucene.SearchFieldAnalyzer;
    22  import org.owasp.dependencycheck.data.lucene.FieldAnalyzer;
    23  import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
    24  import java.util.HashMap;
    25  import org.apache.lucene.queryparser.classic.QueryParser;
    26  import org.apache.lucene.analysis.standard.StandardAnalyzer;
    27  import org.apache.lucene.analysis.Analyzer;
    28  import org.apache.lucene.search.ScoreDoc;
    29  import org.apache.lucene.search.TopScoreDocCollector;
    30  import org.apache.lucene.search.IndexSearcher;
    31  import org.apache.lucene.index.IndexReader;
    32  import org.apache.lucene.search.Query;
    33  import java.io.IOException;
    34  import org.apache.lucene.document.Field;
    35  import org.apache.lucene.document.TextField;
    36  import org.apache.lucene.document.Document;
    37  import org.apache.lucene.index.DirectoryReader;
    38  import org.apache.lucene.index.IndexWriter;
    39  import org.apache.lucene.index.IndexWriterConfig;
    40  import org.apache.lucene.store.RAMDirectory;
    41  import org.apache.lucene.store.Directory;
    42  import org.apache.lucene.util.Version;
    43  import org.junit.After;
    44  import org.junit.AfterClass;
    45  import org.junit.Before;
    46  import org.junit.BeforeClass;
    47  import org.junit.Test;
    48  import static org.junit.Assert.*;
    49
    50  /**
    51   *
    52   * @author Jeremy Long (jeremy.long@owasp.org)
    53   */
    54  public class FieldAnalyzerTest {
    55
    56      @BeforeClass
    57      public static void setUpClass() throws Exception {
    58      }
    59
    60      @AfterClass
    61      public static void tearDownClass() throws Exception {
    62      }
    63
    64      @Before
    65      public void setUp() {
    66      }
    67
    68      @After
    69      public void tearDown() {
    70      }
    71
    72      @Test
    73      public void testAnalyzers() throws Exception {
    74
    75          Analyzer analyzer = new FieldAnalyzer(Version.LUCENE_43);
    76          Directory index = new RAMDirectory();
    77
    78          String field1 = "product";
    79          String text1 = "springframework";
    80
    81          String field2 = "vendor";
    82          String text2 = "springsource";
    83
    84          createIndex(analyzer, index, field1, text1, field2, text2);
    85
    86          //Analyzer searchingAnalyzer = new SearchFieldAnalyzer(Version.LUCENE_43);
    87          String querystr = "product:\"(Spring Framework Core)\" vendor:(SpringSource)";
    88
    89          SearchFieldAnalyzer searchAnalyzerProduct = new SearchFieldAnalyzer(Version.LUCENE_43);
    90          SearchFieldAnalyzer searchAnalyzerVendor = new SearchFieldAnalyzer(Version.LUCENE_43);
    91          HashMap<String, Analyzer> map = new HashMap<String, Analyzer>();
    92          map.put(field1, searchAnalyzerProduct);
    93          map.put(field2, searchAnalyzerVendor);
    94          PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_43), map);
    95          QueryParser parser = new QueryParser(Version.LUCENE_43, field1, wrapper);
    96
    97          Query q = parser.parse(querystr);
    98          //System.out.println(q.toString());
    99
    100         int hitsPerPage = 10;
    101
    102         IndexReader reader = DirectoryReader.open(index);
    103         IndexSearcher searcher = new IndexSearcher(reader);
    104         TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    105         searcher.search(q, collector);
    106         ScoreDoc[] hits = collector.topDocs().scoreDocs;
    107
    108         assertEquals("Did not find 1 document?", 1, hits.length);
    109
    110         searchAnalyzerProduct.clear(); //ensure we don't have anything left over from the previous search.
    111         searchAnalyzerVendor.clear();
    112         querystr = "product:(Apache Struts) vendor:(Apache)";
    113         Query q2 = parser.parse(querystr);
    114         //System.out.println(q2.toString());
    115         assertFalse("second parsing contains previousWord from the TokenPairConcatenatingFilter", q2.toString().contains("core"));
    116     }
    117
    118     private void createIndex(Analyzer analyzer, Directory index, String field1, String text1, String field2, String text2) throws IOException {
    119         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, analyzer);
    120         IndexWriter w = new IndexWriter(index, config);
    121         addDoc(w, field1, text1, field2, text2);
    122         w.close();
    123     }
    124
    125     private static void addDoc(IndexWriter w, String field1, String text1, String field2, String text2) throws IOException {
    126         Document doc = new Document();
    127         doc.add(new TextField(field1, text1, Field.Store.YES));
    128         doc.add(new TextField(field2, text2, Field.Store.YES));
    129         w.addDocument(doc);
    130     }
    131 }