View Javadoc
1 package net.sf.flock.lucene; 2 3 import java.io.IOException; 4 5 import org.apache.lucene.document.Document; 6 import org.apache.lucene.document.Field; 7 import org.apache.lucene.queryParser.ParseException; 8 9 import junit.framework.TestCase; 10 11 public class IndexServiceTest extends TestCase { 12 13 private final static String BODY_FOO = "Fairly long body with all sorts of crap and foo."; 14 private final static String BODY_BAR = "Fairly long body with all sorts of crap and bar."; 15 16 private final static int ITERATIONS = 50; 17 18 private IndexService is; 19 20 public IndexServiceTest(String name) { 21 super(name); 22 } 23 24 protected void setUp() throws Exception { 25 is = new IndexService(System.getProperty("java.io.tmpdir") + "/flockTest", true); 26 } 27 28 protected void tearDown() throws Exception { 29 is.close(); 30 } 31 32 public void testIndex() throws ParseException, IOException { 33 34 is.add( this.createDocument("foo", BODY_FOO) ); 35 assertEquals(1, is.search("body", "foo").length()); 36 37 is.update("id", this.createDocument("foo", BODY_BAR) ); 38 assertEquals(0, is.search("body", "foo").length()); 39 assertEquals(1, is.search("body", "bar").length()); 40 41 assertEquals(1, is.delete("id", "foo") ); 42 assertEquals(0, is.search("body", "foo").length()); 43 assertEquals(0, is.search("body", "bar").length()); 44 45 long s = System.currentTimeMillis(); 46 for (int i=0; i<ITERATIONS; i++) { 47 is.add( this.createDocument("foo"+i, BODY_FOO) ); 48 is.add( this.createDocument("bar"+i, BODY_BAR) ); 49 } 50 System.out.println( ITERATIONS + "x2 adds took "+ (System.currentTimeMillis() - s) ); 51 52 s = System.currentTimeMillis(); 53 for (int i=0; i<ITERATIONS; i++) { 54 assertEquals(ITERATIONS, is.search("body", "foo").length()); 55 assertEquals(ITERATIONS, is.search("body", "bar").length()); 56 } 57 System.out.println( ITERATIONS + "x2 searches took "+ (System.currentTimeMillis() - s) ); 58 59 s = System.currentTimeMillis(); 60 for (int i=0; i<ITERATIONS; i++) { 61 assertEquals(1, is.delete("id", "foo"+i)); 62 assertEquals(1, is.delete("id", "bar"+i)); 63 } 64 System.out.println( ITERATIONS + "x2 deletes took "+ (System.currentTimeMillis() - s) ); 65 66 assertEquals(0, is.search("body", "foo").length()); 67 assertEquals(0, is.search("body", "bar").length()); 68 69 } 70 71 private Document createDocument(String id, String body) { 72 Document doc = new Document(); 73 doc.add(Field.Keyword("id", id)); 74 doc.add(Field.Text("body", body)); 75 return doc; 76 } 77 78 }

This page was automatically generated by Maven