KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > usages > LuceneIndexTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.usages;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.Term;
30 import org.apache.lucene.search.Hit;
31 import org.apache.lucene.search.Hits;
32 import org.apache.lucene.search.IndexSearcher;
33 import org.apache.lucene.search.Searcher;
34 import org.apache.lucene.search.TermQuery;
35 import org.apache.lucene.store.Directory;
36 import org.apache.lucene.store.FSDirectory;
37 import org.netbeans.junit.NbTestCase;
38 import org.netbeans.modules.java.source.usages.ClassIndexImpl.UsageType;
39
40 /**
41  *
42  * @author Tomas Zezula
43  */

44 public class LuceneIndexTest extends NbTestCase {
45     
46     //Copied from DocumentUtil, nneds to be synchronized when changed
47
private static final String JavaDoc FIELD_RESOURCE_NAME = "resourceName"; //NOI18N
48
private static final String JavaDoc FIELD_REFERENCES = "references"; //NOI18N
49

50     private static final int REF_SIZE = UsageType.values().length;
51     
52     private File JavaDoc indexFolder1;
53     private File JavaDoc indexFolder2;
54     
55     public LuceneIndexTest (String JavaDoc testName) {
56         super (testName);
57     }
58     
59     protected @Override JavaDoc void setUp() throws Exception JavaDoc {
60         super.setUp();
61     this.clearWorkDir();
62         //Prepare indeces
63
}
64     
65     public void testIndeces () throws Exception JavaDoc {
66         if (indexFolder1 != null && indexFolder2 != null) {
67             assertTrue (indexFolder1.isDirectory());
68             assertTrue (indexFolder1.canRead());
69             assertTrue (IndexReader.indexExists(indexFolder1));
70             assertTrue (indexFolder2.isDirectory());
71             assertTrue (indexFolder2.canRead());
72             assertTrue (IndexReader.indexExists(indexFolder2));
73             compareIndeces (indexFolder1, indexFolder2);
74         }
75     }
76     
77     
78     private static void compareIndeces (final File JavaDoc file1, final File JavaDoc file2) throws IOException JavaDoc {
79         Directory dir1 = FSDirectory.getDirectory(file1,false);
80         Directory dir2 = FSDirectory.getDirectory(file2,false);
81         IndexReader in1 = IndexReader.open(dir1);
82         try {
83             IndexReader in2 = IndexReader.open(dir2);
84             try {
85                 int len1 = in1.numDocs();
86                 int len2 = in2.numDocs();
87                 assertEquals("Indeces have different size",len1, len2);
88                 Searcher search = new IndexSearcher(in2);
89                 try {
90                     for (int i=0; i< len1; i++) {
91                         Document doc1 = in1.document(i);
92                         String JavaDoc name1 = doc1.getField(FIELD_RESOURCE_NAME).stringValue();
93                         if (name1.equals("/")) {
94                             continue;
95                         }
96                         Hits hits = search.search(new TermQuery( new Term(FIELD_RESOURCE_NAME,name1)));
97                         assertEquals("No document for " + name1, 1, len2);
98                         Document doc2 = ((Hit)hits.iterator().next()).getDocument();
99                         compare(doc1,doc2);
100                     }
101                 } finally {
102                     search.close();
103                 }
104             } finally {
105                 in2.close();
106             }
107         } finally {
108             in1.close();
109         }
110     }
111     
112     private static void compare (Document doc1, Document doc2) throws IOException JavaDoc {
113         Field[] f1 = doc1.getFields(FIELD_REFERENCES);
114         Field[] f2 = doc2.getFields(FIELD_REFERENCES);
115         assertEquals("Reference size not equal for:" + doc1 + " and: " + doc2, f1.length,f2.length);
116         Map JavaDoc<String JavaDoc,String JavaDoc> m1 = fill (f1);
117         Map JavaDoc<String JavaDoc,String JavaDoc> m2 = fill (f2);
118         for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> e : m1.entrySet()) {
119             String JavaDoc key = e.getKey();
120             String JavaDoc value1 = e.getValue();
121             String JavaDoc value2= m2.get(key);
122             assertNotNull("Unknown reference: " + key,value2);
123             assertEquals("Different usage types",value1,value2);
124         }
125     }
126     
127     private static Map JavaDoc<String JavaDoc,String JavaDoc> fill (Field[] fs) {
128         Map JavaDoc<String JavaDoc,String JavaDoc> m1 = new HashMap JavaDoc<String JavaDoc, String JavaDoc> ();
129         for (Field f : fs) {
130             String JavaDoc ru = f.stringValue();
131             int index = ru.length() - REF_SIZE;
132             String JavaDoc key = ru.substring(0,index);
133             String JavaDoc value = ru.substring(index);
134             m1.put (key, value);
135         }
136         return m1;
137     }
138     
139 }
140
Popular Tags