View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.lucene;
19  
20  import java.io.IOException;
21  import java.util.HashMap;
22  import org.apache.lucene.analysis.Analyzer;
23  import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
24  import org.apache.lucene.analysis.standard.StandardAnalyzer;
25  import org.apache.lucene.document.Document;
26  import org.apache.lucene.document.Field;
27  import org.apache.lucene.document.TextField;
28  import org.apache.lucene.index.DirectoryReader;
29  import org.apache.lucene.index.IndexReader;
30  import org.apache.lucene.index.IndexWriter;
31  import org.apache.lucene.index.IndexWriterConfig;
32  import org.apache.lucene.queryparser.classic.QueryParser;
33  import org.apache.lucene.search.IndexSearcher;
34  import org.apache.lucene.search.Query;
35  import org.apache.lucene.search.ScoreDoc;
36  import org.apache.lucene.search.TopScoreDocCollector;
37  import org.apache.lucene.store.Directory;
38  import org.apache.lucene.store.RAMDirectory;
39  import org.junit.After;
40  import org.junit.AfterClass;
41  import static org.junit.Assert.assertEquals;
42  import static org.junit.Assert.assertFalse;
43  import org.junit.Before;
44  import org.junit.BeforeClass;
45  import org.junit.Test;
46  
47  /**
48   *
49   * @author Jeremy Long
50   */
51  public class FieldAnalyzerTest {
52  
53      @BeforeClass
54      public static void setUpClass() throws Exception {
55      }
56  
57      @AfterClass
58      public static void tearDownClass() throws Exception {
59      }
60  
61      @Before
62      public void setUp() {
63      }
64  
65      @After
66      public void tearDown() {
67      }
68  
69      @Test
70      public void testAnalyzers() throws Exception {
71  
72          Analyzer analyzer = new FieldAnalyzer(LuceneUtils.CURRENT_VERSION);
73          Directory index = new RAMDirectory();
74  
75          String field1 = "product";
76          String text1 = "springframework";
77  
78          String field2 = "vendor";
79          String text2 = "springsource";
80  
81          createIndex(analyzer, index, field1, text1, field2, text2);
82  
83          //Analyzer searchingAnalyzer = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
84          String querystr = "product:\"(Spring Framework Core)\" vendor:(SpringSource)";
85  
86          SearchFieldAnalyzer searchAnalyzerProduct = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
87          SearchFieldAnalyzer searchAnalyzerVendor = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
88          HashMap<String, Analyzer> map = new HashMap<String, Analyzer>();
89          map.put(field1, searchAnalyzerProduct);
90          map.put(field2, searchAnalyzerVendor);
91          PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(LuceneUtils.CURRENT_VERSION), map);
92          QueryParser parser = new QueryParser(LuceneUtils.CURRENT_VERSION, field1, wrapper);
93  
94          Query q = parser.parse(querystr);
95          //System.out.println(q.toString());
96  
97          int hitsPerPage = 10;
98  
99          IndexReader reader = DirectoryReader.open(index);
100         IndexSearcher searcher = new IndexSearcher(reader);
101         TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
102         searcher.search(q, collector);
103         ScoreDoc[] hits = collector.topDocs().scoreDocs;
104 
105         assertEquals("Did not find 1 document?", 1, hits.length);
106 
107         searchAnalyzerProduct.clear(); //ensure we don't have anything left over from the previous search.
108         searchAnalyzerVendor.clear();
109         querystr = "product:(Apache Struts) vendor:(Apache)";
110         Query q2 = parser.parse(querystr);
111         //System.out.println(q2.toString());
112         assertFalse("second parsing contains previousWord from the TokenPairConcatenatingFilter", q2.toString().contains("core"));
113     }
114 
115     private void createIndex(Analyzer analyzer, Directory index, String field1, String text1, String field2, String text2) throws IOException {
116         IndexWriterConfig config = new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer);
117         IndexWriter w = new IndexWriter(index, config);
118         addDoc(w, field1, text1, field2, text2);
119         w.close();
120     }
121 
122     private static void addDoc(IndexWriter w, String field1, String text1, String field2, String text2) throws IOException {
123         Document doc = new Document();
124         doc.add(new TextField(field1, text1, Field.Store.YES));
125         doc.add(new TextField(field2, text2, Field.Store.YES));
126         w.addDocument(doc);
127     }
128 }