1
2
3
4
5 package com.atlassian.bonnie;
6
7 import junit.framework.TestCase;
8 import org.apache.lucene.analysis.SimpleAnalyzer;
9 import org.apache.lucene.store.Directory;
10 import org.apache.lucene.store.RAMDirectory;
11
12 import java.io.File;
13
14 public class TestLuceneConnectionFactory extends TestCase
15 {
16 public void testGetFromPath()
17 {
18 assertNotNull(LuceneConnectionFactory.get());
19 assertTrue(LuceneConnectionFactory.get() == LuceneConnectionFactory.get());
20 assertTrue(LuceneConnectionFactory.get() == LuceneConnectionFactory.get());
21 assertTrue(LuceneConnectionFactory.get() == LuceneConnectionFactory.get());
22 assertTrue(LuceneConnectionFactory.get() == LuceneConnectionFactory.get());
23 }
24
25 public void testGetLuceneConnectionFromPath() throws Exception
26 {
27 File tempFile = File.createTempFile(getClass().getName(), ".test");
28 tempFile.delete();
29 tempFile.mkdir();
30 try
31 {
32 ILuceneConnection luceneConnection = LuceneConnectionFactory.get().createLuceneConnection(tempFile.getAbsolutePath(), null,
33 ILuceneConnection.DEFAULT_CONFIGURATION);
34 assertNotNull(luceneConnection);
35 assertTrue(luceneConnection instanceof ConcurrentLuceneConnection);
36 ILuceneConnection anotherLuceneConnection = LuceneConnectionFactory.get().createLuceneConnection(tempFile.getAbsolutePath(), null,
37 ILuceneConnection.DEFAULT_CONFIGURATION);
38 assertFalse(luceneConnection == anotherLuceneConnection);
39 assertFalse(anotherLuceneConnection == LuceneConnectionFactory.get().createLuceneConnection(tempFile.getAbsolutePath(),
40 new SimpleAnalyzer(), ILuceneConnection.DEFAULT_CONFIGURATION));
41 }
42 finally
43 {
44 tempFile.delete();
45 }
46 }
47
48 public void testGetLuceneConnectionInvalidPath() throws Exception
49 {
50 File tempFile = File.createTempFile(getClass().getName(), ".test");
51 tempFile.delete();
52
53 try
54 {
55 ILuceneConnection luceneConnection = LuceneConnectionFactory.get().createLuceneConnection(tempFile.getAbsolutePath(), new SimpleAnalyzer(),
56 ILuceneConnection.DEFAULT_CONFIGURATION);
57
58 luceneConnection.withWriter(null);
59
60 fail("LuceneException should have been thrown.");
61 }
62 catch (LuceneException yay)
63 {}
64 }
65
66 public void testGetLuceneConnectionFromDirectory()
67 {
68 final Directory dir = new RAMDirectory();
69 ILuceneConnection luceneConnection = LuceneConnectionFactory.get().createLuceneConnection(dir, null, ILuceneConnection.DEFAULT_CONFIGURATION);
70 assertNotNull(luceneConnection);
71 assertTrue(luceneConnection instanceof ConcurrentLuceneConnection);
72 ILuceneConnection anotherLuceneConnection = LuceneConnectionFactory.get().createLuceneConnection(dir, null,
73 ILuceneConnection.DEFAULT_CONFIGURATION);
74 assertFalse(luceneConnection == anotherLuceneConnection);
75 assertFalse(anotherLuceneConnection == LuceneConnectionFactory.get().createLuceneConnection(dir, new SimpleAnalyzer(),
76 ILuceneConnection.DEFAULT_CONFIGURATION));
77 }
78 }