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