1 19 20 package org.netbeans.modules.java.source.usages; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 44 public class LuceneIndexTest extends NbTestCase { 45 46 private static final String FIELD_RESOURCE_NAME = "resourceName"; private static final String FIELD_REFERENCES = "references"; 50 private static final int REF_SIZE = UsageType.values().length; 51 52 private File indexFolder1; 53 private File indexFolder2; 54 55 public LuceneIndexTest (String testName) { 56 super (testName); 57 } 58 59 protected @Override void setUp() throws Exception { 60 super.setUp(); 61 this.clearWorkDir(); 62 } 64 65 public void testIndeces () throws Exception { 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 file1, final File file2) throws IOException { 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 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 { 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 <String ,String > m1 = fill (f1); 117 Map <String ,String > m2 = fill (f2); 118 for (Map.Entry <String ,String > e : m1.entrySet()) { 119 String key = e.getKey(); 120 String value1 = e.getValue(); 121 String value2= m2.get(key); 122 assertNotNull("Unknown reference: " + key,value2); 123 assertEquals("Different usage types",value1,value2); 124 } 125 } 126 127 private static Map <String ,String > fill (Field[] fs) { 128 Map <String ,String > m1 = new HashMap <String , String > (); 129 for (Field f : fs) { 130 String ru = f.stringValue(); 131 int index = ru.length() - REF_SIZE; 132 String key = ru.substring(0,index); 133 String value = ru.substring(index); 134 m1.put (key, value); 135 } 136 return m1; 137 } 138 139 } 140 | Popular Tags |