1 |
| package photospace.search; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import java.util.*; |
5 |
| import org.apache.commons.io.*; |
6 |
| import org.apache.commons.logging.*; |
7 |
| import org.apache.lucene.index.*; |
8 |
| import junit.framework.*; |
9 |
| import photospace.meta.*; |
10 |
| |
11 |
| public class IndexerTest |
12 |
| extends TestCase |
13 |
| { |
14 |
| private static final Log log = LogFactory.getLog(IndexerTest.class); |
15 |
| |
16 |
| SearchIndex searchIndex; |
17 |
| Indexer indexer; |
18 |
| File index; |
19 |
| |
20 |
3
| public void setUp() throws Exception
|
21 |
| { |
22 |
3
| index = new File(System.getProperty("java.io.tmpdir"), "test-index");
|
23 |
3
| searchIndex = new SearchIndex(index);
|
24 |
| |
25 |
3
| indexer = new Indexer();
|
26 |
3
| indexer.setIndex(searchIndex);
|
27 |
| } |
28 |
| |
29 |
3
| public void tearDown() throws Exception
|
30 |
| { |
31 |
3
| searchIndex.closeReader();
|
32 |
3
| FileUtils.deleteDirectory(index);
|
33 |
| } |
34 |
| |
35 |
1
| public void testInit() throws Exception
|
36 |
| { |
37 |
1
| indexer.init();
|
38 |
1
| assertNotNull(searchIndex.getReader());
|
39 |
| } |
40 |
| |
41 |
1
| public void testIndex() throws Exception
|
42 |
| { |
43 |
1
| Meta meta = DocumentFactoryTest.getPhotoMeta();
|
44 |
1
| List metas = new ArrayList();
|
45 |
1
| metas.add(meta);
|
46 |
| |
47 |
1
| indexer.index(metas, true);
|
48 |
| |
49 |
1
| Meta fromIndex = getMetaFromIndex(meta.getPath());
|
50 |
1
| assertEquals(meta, fromIndex);
|
51 |
| |
52 |
1
| IndexReader reader = searchIndex.getReader();
|
53 |
| |
54 |
1
| assertEquals(1, reader.numDocs());
|
55 |
1
| assertEquals(DocumentFactory.ALL_VALUE, reader.document(0).getValues(DocumentFactory.ALL_FIELD)[0]);
|
56 |
1
| assertTrue(reader.getFieldNames().contains("title"));
|
57 |
| |
58 |
1
| FolderMeta root = new FolderMeta();
|
59 |
1
| FolderMeta folder = new FolderMeta();
|
60 |
1
| PhotoMeta photo1 = new PhotoMeta();
|
61 |
1
| PhotoMeta photo2 = new PhotoMeta();
|
62 |
1
| PhotoMeta photo3 = new PhotoMeta();
|
63 |
1
| PhotoMeta photo4 = new PhotoMeta();
|
64 |
1
| PhotoMeta photo5 = new PhotoMeta();
|
65 |
| |
66 |
1
| root.setPath("/");
|
67 |
1
| folder.setPath("/folder/");
|
68 |
1
| photo1.setPath("/folder/photo1.jpg");
|
69 |
1
| photo2.setPath("/folder/photo2.jpg");
|
70 |
1
| photo3.setPath("/folder/photo3.jpg");
|
71 |
1
| photo4.setPath("/folder/photo4.jpg");
|
72 |
1
| photo5.setPath("/folder/photo5.jpg");
|
73 |
| |
74 |
1
| indexer.index(Arrays.asList(new Meta[] { root, folder, photo1, photo2, photo3, photo4, photo5 }), true);
|
75 |
| |
76 |
1
| searchIndex.closeReader();
|
77 |
1
| reader = searchIndex.getReader();
|
78 |
| |
79 |
1
| assertEquals(7, reader.numDocs());
|
80 |
| } |
81 |
| |
82 |
1
| public void testMerge() throws Exception
|
83 |
| { |
84 |
1
| PhotoMeta meta = new PhotoMeta();
|
85 |
1
| meta.setPath("/foo.jpg");
|
86 |
1
| meta.setRatingCount(new Integer(2));
|
87 |
| |
88 |
1
| indexer.index(Arrays.asList(new Meta[] { meta }), true);
|
89 |
| |
90 |
1
| Meta fromIndex = getMetaFromIndex(meta.getPath());
|
91 |
1
| assertEquals(meta, fromIndex);
|
92 |
| |
93 |
1
| IndexReader reader = searchIndex.getReader();
|
94 |
1
| Term pathTerm = new Term("path", meta.getPath());
|
95 |
1
| int docCount = reader.numDocs();
|
96 |
1
| assertEquals(1, reader.docFreq(pathTerm));
|
97 |
| |
98 |
1
| meta.setRatingCount(new Integer(6));
|
99 |
1
| meta.setUpdated(new Date());
|
100 |
| |
101 |
1
| List toMerge = new ArrayList();
|
102 |
1
| toMerge.add(meta);
|
103 |
| |
104 |
1
| indexer.merge(toMerge);
|
105 |
| |
106 |
1
| searchIndex.closeReader();
|
107 |
1
| reader = searchIndex.getReader();
|
108 |
| |
109 |
1
| assertEquals(docCount, reader.numDocs());
|
110 |
1
| assertEquals(1, reader.docFreq(pathTerm));
|
111 |
| |
112 |
1
| Meta fromMerge = getMetaFromIndex(meta.getPath());
|
113 |
1
| assertEquals(meta, fromMerge);
|
114 |
| } |
115 |
| |
116 |
3
| private Meta getMetaFromIndex(String path) throws Exception
|
117 |
| { |
118 |
3
| Term pathTerm = new Term("path", path);
|
119 |
3
| IndexReader reader = searchIndex.getReader();
|
120 |
3
| TermDocs docs = reader.termDocs(pathTerm);
|
121 |
3
| docs.next();
|
122 |
3
| return new DocumentFactory().createMeta(reader.document(docs.doc()));
|
123 |
| } |
124 |
| |
125 |
| |
126 |
| } |