1   package com.atlassian.bonnie.search;
2   
3   import com.atlassian.bonnie.LuceneConnection;
4   import com.atlassian.bonnie.Searchable;
5   import com.atlassian.bonnie.Searcher;
6   import com.atlassian.bonnie.analyzer.DefaultLuceneAnalyzer;
7   import com.atlassian.bonnie.analyzer.DefaultLuceneAnalyzerFactory;
8   import com.atlassian.bonnie.analyzer.LuceneAnalyzerFactory;
9   import com.atlassian.bonnie.search.extractor.XmlConfiguredExtractor;
10  import junit.framework.TestCase;
11  import org.apache.lucene.index.IndexWriter;
12  import org.apache.lucene.store.RAMDirectory;
13  
14  import java.io.IOException;
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  public abstract class BaseLuceneTest extends TestCase
19  {
20  	protected LuceneIndexer luceneIndexer;
21  	protected Searcher luceneSearcher;
22  	protected LuceneConnection luceneConnection;
23  	private LuceneAnalyzerFactory luceneAnalyzerFactory;
24  
25  	public BaseLuceneTest()
26  	{
27  
28  	}
29  
30  	public BaseLuceneTest(String testName)
31  	{
32  		super(testName);
33  	}
34  
35  	protected void setUp() throws Exception
36  	{
37  		super.setUp();
38  
39  		luceneAnalyzerFactory = new DefaultLuceneAnalyzerFactory();
40  
41  		luceneConnection = new LuceneConnection(new RAMDirectory(), new DefaultLuceneAnalyzer());
42  
43  		luceneIndexer = new LuceneIndexer();
44  		luceneIndexer.setLuceneConnection(luceneConnection);
45  
46  		List extractors = new ArrayList();
47  		extractors.add(new XmlConfiguredExtractor());
48  
49  		luceneIndexer.setDocumentBuilder(new DocumentBuilderForTests(extractors));
50  
51  		// do this little trickery so that our tests use the Searcher interface not LuceneSearcher
52  		LuceneSearcher lSearcher = new LuceneSearcher();
53  		lSearcher.setLuceneConnection(luceneConnection);
54  		lSearcher.setLuceneAnalyzerFactory(luceneAnalyzerFactory);
55  		luceneSearcher = lSearcher;
56  	}
57  
58  	protected MockSearchableObject createSearchableObject(int id, String title, String description)
59  	{
60  		return initSearchableObject(new MockSearchableObject(), id, title, description);
61  	}
62  
63  	protected MockSearchableObject createSearchableObjectDifferentClass(int id, String title, String description)
64  	{
65  		return initSearchableObject(new MockSearchableObject()
66  		{
67  		}, id, title, description);
68  	}
69  
70  	protected MockSearchableObject createUnindexable(int id, String title, String description)
71  	{
72  		MockSearchableObject obj = createSearchableObject(id, title, description);
73  		obj.setIndexable(false);
74  		return obj;
75  	}
76  
77  	private MockSearchableObject initSearchableObject(MockSearchableObject obj, int id, String title, String description)
78  	{
79  		obj.setId(id);
80  		obj.setTitle(title);
81  		obj.setDescription(description);
82  		return obj;
83  	}
84  
85  	protected void index(final Searchable s)
86  	{
87  		luceneConnection.withWriter(new LuceneConnection.WriterAction()
88  		{
89  			public void perform(IndexWriter writer) throws IOException
90  			{
91  				luceneIndexer.index(s);
92  			}
93  		});
94  	}
95  
96  	protected void index(final List s)
97  	{
98  		luceneConnection.withWriter(new LuceneConnection.WriterAction()
99  		{
100 			public void perform(IndexWriter writer) throws IOException
101 			{
102 				for (int i = 0; i < s.size(); i++)
103 				{
104 					luceneIndexer.index((Searchable) s.get(i));
105 				}
106 			}
107 		});
108 	}
109 }