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