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  import org.owasp.dependencycheck.BaseTest;
47  
48  /**
49   *
50   * @author Jeremy Long
51   */
52  public class FieldAnalyzerTest extends BaseTest {
53  
54      @Test
55      public void testAnalyzers() throws Exception {
56  
57          Analyzer analyzer = new FieldAnalyzer(LuceneUtils.CURRENT_VERSION);
58          Directory index = new RAMDirectory();
59  
60          String field1 = "product";
61          String text1 = "springframework";
62  
63          String field2 = "vendor";
64          String text2 = "springsource";
65  
66          createIndex(analyzer, index, field1, text1, field2, text2);
67  
68          //Analyzer searchingAnalyzer = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
69          String querystr = "product:\"(Spring Framework Core)\" vendor:(SpringSource)";
70  
71          SearchFieldAnalyzer searchAnalyzerProduct = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
72          SearchFieldAnalyzer searchAnalyzerVendor = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
73          HashMap<String, Analyzer> map = new HashMap<String, Analyzer>();
74          map.put(field1, searchAnalyzerProduct);
75          map.put(field2, searchAnalyzerVendor);
76          PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(LuceneUtils.CURRENT_VERSION), map);
77          QueryParser parser = new QueryParser(LuceneUtils.CURRENT_VERSION, field1, wrapper);
78  
79          Query q = parser.parse(querystr);
80          //System.out.println(q.toString());
81  
82          int hitsPerPage = 10;
83  
84          IndexReader reader = DirectoryReader.open(index);
85          IndexSearcher searcher = new IndexSearcher(reader);
86          TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
87          searcher.search(q, collector);
88          ScoreDoc[] hits = collector.topDocs().scoreDocs;
89  
90          assertEquals("Did not find 1 document?", 1, hits.length);
91  
92          searchAnalyzerProduct.clear(); //ensure we don't have anything left over from the previous search.
93          searchAnalyzerVendor.clear();
94          querystr = "product:(Apache Struts) vendor:(Apache)";
95          Query q2 = parser.parse(querystr);
96          //System.out.println(q2.toString());
97          assertFalse("second parsing contains previousWord from the TokenPairConcatenatingFilter", q2.toString().contains("core"));
98      }
99  
100     private void createIndex(Analyzer analyzer, Directory index, String field1, String text1, String field2, String text2) throws IOException {
101         IndexWriterConfig config = new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer);
102         IndexWriter w = new IndexWriter(index, config);
103         addDoc(w, field1, text1, field2, text2);
104         w.close();
105     }
106 
107     private static void addDoc(IndexWriter w, String field1, String text1, String field2, String text2) throws IOException {
108         Document doc = new Document();
109         doc.add(new TextField(field1, text1, Field.Store.YES));
110         doc.add(new TextField(field2, text2, Field.Store.YES));
111         w.addDocument(doc);
112     }
113 }