PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/testcase/TestIndex.java

#
Java | 183 lines | 152 code | 25 blank | 6 comment | 7 complexity | c1492def286ab9b76560497d38c66302 MD5 | raw file
  1package testcase;
  2
  3import java.io.File;
  4import java.io.FileReader;
  5import java.io.FilenameFilter;
  6import java.io.IOException;
  7import java.util.Iterator;
  8import java.util.regex.Matcher;
  9import java.util.regex.Pattern;
 10
 11import junit.framework.Test;
 12import junit.framework.TestCase;
 13
 14import org.apache.lucene.analysis.Analyzer;
 15import org.apache.lucene.analysis.standard.StandardAnalyzer;
 16import org.apache.lucene.document.Document;
 17import org.apache.lucene.document.Field;
 18import org.apache.lucene.document.Field.Index;
 19import org.apache.lucene.document.Field.Store;
 20import org.apache.lucene.index.IndexModifier;
 21import org.apache.lucene.index.IndexReader;
 22import org.apache.lucene.queryParser.ParseException;
 23import org.apache.lucene.queryParser.QueryParser;
 24import org.apache.lucene.search.Hit;
 25import org.apache.lucene.search.Hits;
 26import org.apache.lucene.search.IndexSearcher;
 27import org.apache.lucene.search.Query;
 28import org.apache.lucene.search.Searcher;
 29import org.apache.lucene.store.Directory;
 30import org.apache.lucene.store.FSDirectory;
 31
 32/**
 33 * JUnit 3.x testcase of Lucene classes with no jedit dependencies.
 34 * 
 35 * @author ezust
 36 *
 37 */
 38
 39public class TestIndex extends TestCase
 40{
 41
 42	static String userDir = "/home/ezust/.jedit";
 43	static String jEditHome = "/home/ezust/workspace/jEditCVS/build"; 
 44	
 45	Analyzer analyzer = new StandardAnalyzer();
 46	QueryParser parser = new QueryParser("content", analyzer);
 47	IndexReader indexReader = null;
 48	IndexModifier indexModifier = null;
 49	Searcher searcher;
 50	Query query;
 51	
 52	public TestIndex() {
 53		super("testSearch");
 54	}
 55	public static Test suite() {
 56		return new TestIndex();
 57	}
 58
 59	@Override 
 60	public void setUp() {
 61		indexModifier = getWriter("help");
 62		indexStaticHelp();
 63		indexReader = getReader("help");
 64		System.out.println("setup complete");
 65	}
 66	
 67	public void testSearch() throws IOException {
 68		assertEquals(1, 1);
 69		assertTrue(true);
 70		assertFalse(false);
 71		Hits hits = search("fold");
 72		Iterator<Hit> hitr = hits.iterator();
 73		int numHits = 0;
 74		while (hitr.hasNext()) {
 75			numHits ++;
 76			Hit hit = hitr.next();
 77			System.out.println("id: " + hit.getId() + " ");
 78		}
 79		assertTrue(numHits>0);
 80	}
 81
 82	public void indexStaticHelp() 
 83	{
 84		String[] components = new String[] {jEditHome, "doc", "users-guide"};
 85		String path = StringList.join(components, File.separator);
 86		Field nameField = new Field("name", "jEdit Users Guide", Store.YES, Index.NO);
 87		Field groupField = new Field("group", "help", Store.YES, Index.NO);
 88		Field fields[] = new Field[] {nameField, groupField};
 89		indexDirectory(fields, path);
 90	}
 91	
 92	public Hits search(String queryString) {
 93		String indexName = "help";
 94		try {
 95			query = parser.parse(queryString);
 96			Hits hits;
 97			indexReader = getReader(indexName);
 98			searcher = new IndexSearcher(indexReader);
 99			hits = searcher.search(query);
100			return hits;
101		}
102		catch (ParseException pe) {
103			throw new RuntimeException("Unable to parse: " + queryString , pe);
104		}
105		catch (IOException ioe) {
106			throw new RuntimeException( "Unable to search index", ioe);
107		}
108	}
109	
110	public IndexReader getReader(String name) {
111		try {
112			if (indexReader != null) return indexReader;
113			String path = getPath(name);
114			indexReader = IndexReader.open(path);
115			return indexReader;
116		}
117		catch (IOException ioe) {return null;}
118		
119	}
120
121	static final Pattern filenamePattern = Pattern.compile("\\.(txt|html)?$");	
122
123	void indexDirectory(Field[] fields, String dir)  
124	{
125		getWriter("help");
126		String[] files = null;
127		FilenameFilter fnf = new FilenameFilter() {
128			public boolean accept(File dir, String name)	{
129				Matcher m = filenamePattern.matcher(name);
130				return m.matches();
131			}
132		};
133		File dirFile = new File(dir);
134		files = dirFile.list(fnf);
135		
136		for (String fileName: files) try 	{
137			File f = new File(fileName);
138			
139			FileReader fr = new FileReader(f);
140			Field content= new Field("content", fr);
141			Field url = new Field("url", "file://" + f.getCanonicalPath(), Store.YES, Index.NO);
142			Document doc = new Document();
143			for (Field field: fields) doc.add(field);
144			doc.add(content);
145			doc.add(url);
146			indexModifier.addDocument(doc);
147		}
148		catch (IOException ioe) {
149			throw new RuntimeException("Unable to index file: " + fileName, ioe);
150		}
151	}
152
153	private IndexModifier getWriter( String name ) {
154		
155		if (indexModifier != null) return indexModifier;
156		
157		String path = getPath(name);
158		Analyzer sa = new StandardAnalyzer();
159		Directory d = null;
160		try {
161			d = FSDirectory.getDirectory(path, false);
162			indexModifier = new IndexModifier(d, sa, false);
163		}
164		catch (IOException ioe) { // Directory doesn't exist
165			try {
166				d = FSDirectory.getDirectory(path, true);
167				indexModifier = new IndexModifier(d, sa, true);
168			}
169			catch (IOException ioe1) {
170				throw new RuntimeException("Can't create indexModifier: " + name, ioe1);
171			}
172		}
173		
174		return indexModifier;
175	}
176
177	public static String getPath(String indexName) {
178		String[] components = new String[] {userDir, "lucene", indexName};
179		return StringList.join(components, File.separator);
180	}
181
182
183}