001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.usages;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.HashMap;
047: import java.util.Map;
048: import org.apache.lucene.document.Document;
049: import org.apache.lucene.document.Field;
050: import org.apache.lucene.index.IndexReader;
051: import org.apache.lucene.index.Term;
052: import org.apache.lucene.search.Hit;
053: import org.apache.lucene.search.Hits;
054: import org.apache.lucene.search.IndexSearcher;
055: import org.apache.lucene.search.Searcher;
056: import org.apache.lucene.search.TermQuery;
057: import org.apache.lucene.store.Directory;
058: import org.apache.lucene.store.FSDirectory;
059: import org.netbeans.junit.NbTestCase;
060: import org.netbeans.modules.java.source.usages.ClassIndexImpl.UsageType;
061:
062: /**
063: *
064: * @author Tomas Zezula
065: */
066: public class LuceneIndexTest extends NbTestCase {
067:
068: //Copied from DocumentUtil, nneds to be synchronized when changed
069: private static final String FIELD_RESOURCE_NAME = "resourceName"; //NOI18N
070: private static final String FIELD_REFERENCES = "references"; //NOI18N
071:
072: private static final int REF_SIZE = UsageType.values().length;
073:
074: private File indexFolder1;
075: private File indexFolder2;
076:
077: public LuceneIndexTest(String testName) {
078: super (testName);
079: }
080:
081: protected @Override
082: void setUp() throws Exception {
083: super .setUp();
084: this .clearWorkDir();
085: //Prepare indeces
086: }
087:
088: public void testIndeces() throws Exception {
089: if (indexFolder1 != null && indexFolder2 != null) {
090: assertTrue(indexFolder1.isDirectory());
091: assertTrue(indexFolder1.canRead());
092: assertTrue(IndexReader.indexExists(indexFolder1));
093: assertTrue(indexFolder2.isDirectory());
094: assertTrue(indexFolder2.canRead());
095: assertTrue(IndexReader.indexExists(indexFolder2));
096: compareIndeces(indexFolder1, indexFolder2);
097: }
098: }
099:
100: private static void compareIndeces(final File file1,
101: final File file2) throws IOException {
102: Directory dir1 = FSDirectory.getDirectory(file1, false);
103: Directory dir2 = FSDirectory.getDirectory(file2, false);
104: IndexReader in1 = IndexReader.open(dir1);
105: try {
106: IndexReader in2 = IndexReader.open(dir2);
107: try {
108: int len1 = in1.numDocs();
109: int len2 = in2.numDocs();
110: assertEquals("Indeces have different size", len1, len2);
111: Searcher search = new IndexSearcher(in2);
112: try {
113: for (int i = 0; i < len1; i++) {
114: Document doc1 = in1.document(i);
115: String name1 = doc1.getField(
116: FIELD_RESOURCE_NAME).stringValue();
117: if (name1.equals("/")) {
118: continue;
119: }
120: Hits hits = search.search(new TermQuery(
121: new Term(FIELD_RESOURCE_NAME, name1)));
122: assertEquals("No document for " + name1, 1,
123: len2);
124: Document doc2 = ((Hit) hits.iterator().next())
125: .getDocument();
126: compare(doc1, doc2);
127: }
128: } finally {
129: search.close();
130: }
131: } finally {
132: in2.close();
133: }
134: } finally {
135: in1.close();
136: }
137: }
138:
139: private static void compare(Document doc1, Document doc2)
140: throws IOException {
141: Field[] f1 = doc1.getFields(FIELD_REFERENCES);
142: Field[] f2 = doc2.getFields(FIELD_REFERENCES);
143: assertEquals("Reference size not equal for:" + doc1 + " and: "
144: + doc2, f1.length, f2.length);
145: Map<String, String> m1 = fill(f1);
146: Map<String, String> m2 = fill(f2);
147: for (Map.Entry<String, String> e : m1.entrySet()) {
148: String key = e.getKey();
149: String value1 = e.getValue();
150: String value2 = m2.get(key);
151: assertNotNull("Unknown reference: " + key, value2);
152: assertEquals("Different usage types", value1, value2);
153: }
154: }
155:
156: private static Map<String, String> fill(Field[] fs) {
157: Map<String, String> m1 = new HashMap<String, String>();
158: for (Field f : fs) {
159: String ru = f.stringValue();
160: int index = ru.length() - REF_SIZE;
161: String key = ru.substring(0, index);
162: String value = ru.substring(index);
163: m1.put(key, value);
164: }
165: return m1;
166: }
167:
168: }
|