Clover coverage report -
Coverage timestamp: Fri Nov 19 2004 13:41:51 PST
file stats: LOC: 273   Methods: 12
NCLOC: 224   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SearcherTest.java - 100% 100% 100%
coverage
 1    package photospace.search;
 2   
 3    import java.io.*;
 4    import java.util.*;
 5    import org.apache.commons.collections.*;
 6    import org.apache.commons.logging.*;
 7    import org.apache.lucene.index.*;
 8    import org.apache.lucene.search.*;
 9    import junit.framework.*;
 10    import photospace.beans.*;
 11    import photospace.meta.*;
 12   
 13    public class SearcherTest
 14    extends TestCase
 15    {
 16    private static final Log log = LogFactory.getLog(SearcherTest.class);
 17   
 18    Searcher searcher;
 19    Indexer indexer;
 20    File index;
 21    SearchIndex searchIndex;
 22   
 23  9 public void setUp() throws Exception
 24    {
 25  9 index = new File(System.getProperty("java.io.tmpdir"), "test-index");
 26   
 27  9 searchIndex = new SearchIndex(index);
 28   
 29  9 indexer = new Indexer();
 30  9 indexer.setIndex(searchIndex);
 31   
 32  9 searcher = new Searcher();
 33  9 searcher.setIndex(searchIndex);
 34    }
 35   
 36  9 public void tearDown() throws Exception
 37    {
 38  9 searchIndex.closeReader();
 39    }
 40   
 41  1 public void testSearchEmpty() throws Exception
 42    {
 43  1 Meta meta = new PhotoMeta();
 44  1 meta.setPath("/photo.jpg");
 45  1 meta.setTitle("");
 46  1 indexer.index(Arrays.asList(new Meta[] { meta }), true);
 47   
 48  1 assertTrue(searcher.search(new TermQuery(new Term("title", ""))).containsFile(meta));
 49    }
 50   
 51  1 public void testStringQuery() throws Exception
 52    {
 53  1 Meta meta = DocumentFactoryTest.getPhotoMeta();
 54  1 indexer.index(Arrays.asList(new Meta[] { meta }), true);
 55   
 56  1 String query = meta.getTitle();
 57   
 58  1 assertTrue(searcher.search(query, Searcher.DESCENDING, 0, -1).containsFile(meta));
 59   
 60  1 query = DocumentFactory.TEXT_FIELD + ":\"" + meta.getTitle() + "\"";
 61  1 assertTrue(searcher.search(query, Searcher.DESCENDING, 0, -1).containsFile(meta));
 62   
 63  1 query = "path:" + meta.getPath() + "";
 64  1 assertTrue(searcher.search(query, Searcher.DESCENDING, 0, -1).containsFile(meta));
 65   
 66  1 query = "title:\"foo bar\" OR (created-date:[20040101 TO 20040201] AND " + DocumentFactory.TYPE_FIELD + ":" + DocumentFactory.PHOTO_TYPE + ")";
 67  1 assertTrue(searcher.search(query, Searcher.DESCENDING, 0, -1).containsFile(meta));
 68    }
 69   
 70  1 public void testPaging() throws Exception
 71    {
 72  1 Meta first = DocumentFactoryTest.getPhotoMeta();
 73  1 first.setPath("/first.jpg");
 74  1 Meta last = DocumentFactoryTest.getPhotoMeta();
 75  1 last.setPath("/last.jpg");
 76  1 Meta middle = DocumentFactoryTest.getPhotoMeta();
 77  1 middle.setPath("/middle.jpg");
 78   
 79  1 Meta[] metas = new Meta[] { first, last, middle };
 80  1 indexer.index(Arrays.asList(metas), true);
 81   
 82  1 SearchResult all = searcher.search(findAllQuery(), null, Searcher.CREATED_DESCENDING);
 83  1 assertEquals(metas.length, all.getFiles().length);
 84  1 assertEquals(0, all.getStart());
 85  1 assertEquals(2, all.getEnd());
 86  1 assertEquals(metas.length, all.getTotal());
 87   
 88  1 SearchResult none = searcher.search(findAllQuery(), null, Searcher.CREATED_DESCENDING, -1, -1);
 89  1 assertEquals(0, none.getFiles().length);
 90  1 assertEquals(-1, none.getStart());
 91  1 assertEquals(-1, none.getEnd());
 92  1 assertEquals(metas.length, none.getTotal());
 93   
 94  1 SearchResult paged = searcher.search(findAllQuery(), null, Searcher.CREATED_DESCENDING, 0, 1);
 95  1 assertEquals(0, paged.getStart());
 96  1 assertEquals(1, paged.getEnd());
 97  1 assertEquals(3, paged.getTotal());
 98  1 assertEquals(2, paged.getFiles().length);
 99    }
 100   
 101  1 public void testSorting() throws Exception
 102    {
 103  1 Meta first = new PhotoMeta();
 104  1 first.setPath("/first/");
 105  1 first.setCreated(new Date());
 106  1 Meta last = new PhotoMeta();
 107  1 last.setPath("/last/");
 108  1 Meta middle = new PhotoMeta();
 109  1 middle.setPath("/middle/");
 110  1 Meta empty = new PhotoMeta();
 111  1 empty.setPath("/empty/");
 112  1 middle.setCreated(new Date(first.getCreated().getTime() + 1 * 60 * 1000));
 113  1 last.setCreated(new Date(first.getCreated().getTime() + 2 * 60 * 1000));
 114  1 empty.setCreated(null);
 115   
 116  1 Meta[] metas = new Meta[] { first, empty, last, middle };
 117  1 indexer.index(Arrays.asList(metas), true);
 118   
 119  1 Meta[] results = searcher.search(findAllQuery(), null, Searcher.CREATED_DESCENDING).getFiles();
 120  1 assertEquals(last, results[0]);
 121  1 assertEquals(middle, results[1]);
 122  1 assertEquals(first, results[2]);
 123  1 assertEquals(empty, results[3]);
 124   
 125  1 results = searcher.search(findAllQuery(), null, Searcher.CREATED_ASCENDING).getFiles();
 126  1 assertEquals(empty, results[0]);
 127  1 assertEquals(first, results[1]);
 128  1 assertEquals(middle, results[2]);
 129  1 assertEquals(last, results[3]);
 130    }
 131   
 132  1 public void testFindAll() throws Exception
 133    {
 134  1 FolderMeta root = new FolderMeta();
 135  1 FolderMeta folder = new FolderMeta();
 136  1 PhotoMeta photo1 = new PhotoMeta();
 137  1 PhotoMeta photo2 = new PhotoMeta();
 138  1 PhotoMeta photo3 = new PhotoMeta();
 139  1 PhotoMeta photo4 = new PhotoMeta();
 140  1 PhotoMeta photo5 = new PhotoMeta();
 141   
 142  1 root.setPath("/");
 143  1 folder.setPath("/folder/");
 144  1 photo1.setPath("/folder/photo1.jpg");
 145  1 photo2.setPath("/folder/photo2.jpg");
 146  1 photo3.setPath("/folder/photo3.jpg");
 147  1 photo4.setPath("/folder/photo4.jpg");
 148  1 photo5.setPath("/folder/photo5.jpg");
 149  1 indexer.index(Arrays.asList(new Meta[] { root, folder, photo1, photo2, photo3, photo4, photo5 }), true);
 150   
 151  1 Meta[] results = searcher.search(findAllQuery()).getFiles();
 152  1 assertEquals(7, results.length);
 153    }
 154   
 155  1 public void testMatches() throws Exception
 156    {
 157  1 PhotoMeta meta1 = new PhotoMeta();
 158  1 meta1.setPath("/folder/photo1.jpg");
 159  1 meta1.setLabels(new String[] {"alon", "kathryn"});
 160   
 161  1 PhotoMeta meta2 = new PhotoMeta();
 162  1 meta2.setPath("/folder/photo2.jpg");
 163  1 meta2.setLabels(new String[] {"alon"});
 164  1 indexer.index(Arrays.asList(new Meta[] { meta1, meta2 }), true);
 165   
 166  1 Collection labels = searcher.getFieldMatches("labels");
 167  1 assertEquals(new HashSet(Arrays.asList(meta1.getLabels())), CollectionUtils.collect(labels, new PropertyTransformer("value"), new HashSet()));
 168  1 assertEquals(2, ((Match) CollectionUtils.find(labels, new PropertyValuePredicate("value", "alon"))).getCount());
 169  1 assertEquals(1, ((Match) CollectionUtils.find(labels, new PropertyValuePredicate("value", "kathryn"))).getCount());
 170   
 171  1 Collection pathMatches = searcher.getDocumentMatches(meta1.getPath(), "path");
 172  1 assertTrue(pathMatches.contains(new Match(new Term("path", meta1.getPath()), 1)));
 173  1 assertFalse(pathMatches.contains(new Match(new Term("path", meta2.getPath()), 1)));
 174   
 175  1 Collection labelsMatches = searcher.getDocumentMatches(meta1.getPath(), "labels");
 176  1 assertTrue(labelsMatches.contains(new Match(new Term("labels", "alon"), 2)));
 177  1 assertTrue(labelsMatches.contains(new Match(new Term("labels", "kathryn"), 1)));
 178   
 179  1 Collection docMatches = searcher.getDocumentMatches(meta1.getPath());
 180  1 Collection labelMatches = CollectionUtils.select(docMatches, new PropertyValuePredicate("field", "labels"));
 181  1 assertEquals(2, ((Match) CollectionUtils.find(labelMatches, new PropertyValuePredicate("value", "alon"))).getCount());
 182  1 assertEquals(1, ((Match) CollectionUtils.find(labelMatches, new PropertyValuePredicate("value", "kathryn"))).getCount());
 183    }
 184   
 185  1 public void testBrowse() throws Exception
 186    {
 187  1 FolderMeta root = new FolderMeta();
 188  1 FolderMeta folder = new FolderMeta();
 189  1 PhotoMeta photo1 = new PhotoMeta();
 190  1 PhotoMeta photo2 = new PhotoMeta();
 191  1 PhotoMeta photo3 = new PhotoMeta();
 192  1 PhotoMeta photo4 = new PhotoMeta();
 193  1 PhotoMeta photo5 = new PhotoMeta();
 194   
 195  1 root.setPath("/");
 196  1 folder.setPath("/folder/");
 197  1 photo1.setPath("/folder/photo1.jpg");
 198  1 photo2.setPath("/folder/photo2.jpg");
 199  1 photo3.setPath("/folder/photo3.jpg");
 200  1 photo4.setPath("/folder/photo4.jpg");
 201  1 photo5.setPath("/folder/photo5.jpg");
 202  1 indexer.index(Arrays.asList(new Meta[] { root, folder, photo1, photo2, photo3, photo4, photo5 }), true);
 203   
 204  1 Meta bogus = searcher.browse("/bogus", Searcher.DESCENDING, 0, -1);
 205  1 assertNull(bogus);
 206   
 207  1 PhotoMeta photoResult = (PhotoMeta) searcher.browse(photo1.getPath(), Searcher.DESCENDING, 0, -1);
 208  1 assertEquals(photo1, photoResult);
 209  1 assertEquals(folder, photoResult.getParent());
 210  1 assertEquals(root, photoResult.getParent().getParent());
 211  1 assertEquals(-1, photoResult.getParent().getStart());
 212  1 assertEquals(-1, photoResult.getParent().getEnd());
 213  1 assertEquals(5, photoResult.getParent().getTotal());
 214   
 215  1 FolderMeta folderResult = (FolderMeta) searcher.browse(folder.getPath(), Searcher.DESCENDING, 0, -1);
 216  1 assertTrue(Arrays.asList(folderResult.getFiles()).containsAll(Arrays.asList(new Meta[] {photo1, photo2, photo3, photo4, photo5 })));
 217  1 assertEquals(root, folderResult.getParent());
 218   
 219  1 folderResult = (FolderMeta) searcher.browse(folder.getPath(), Searcher.DESCENDING, 0, 1);
 220  1 assertEquals(2, folderResult.getFiles().length);
 221  1 assertEquals(0, folderResult.getStart());
 222  1 assertEquals(1, folderResult.getEnd());
 223  1 assertEquals(root, folderResult.getParent());
 224   
 225  1 FolderMeta rootResult = (FolderMeta) searcher.browse(root.getPath(), Searcher.DESCENDING, 0, -1);
 226  1 assertEquals(1, rootResult.getFiles().length);
 227  1 assertTrue(rootResult.containsFile(folder));
 228  1 FolderMeta child = (FolderMeta) rootResult.getFiles()[0];
 229  1 assertEquals(4, child.getFiles().length);
 230    }
 231   
 232  1 public void testConstrain() throws Exception
 233    {
 234  1 FolderMeta root = new FolderMeta();
 235  1 FolderMeta folder = new FolderMeta();
 236  1 PhotoMeta photo1 = new PhotoMeta();
 237   
 238  1 root.setPath("/");
 239  1 folder.setPath("/folder/");
 240  1 photo1.setPath("/photo1.jpg");
 241  1 indexer.index(Arrays.asList(new Meta[] { root, folder, photo1 }), true);
 242   
 243  1 Query constraint = new TermQuery(new Term(DocumentFactory.TYPE_FIELD, DocumentFactory.PHOTO_TYPE));
 244  1 SearchResult result = searcher.search("ALL:1", constraint, Searcher.DESCENDING, -1, -1);
 245  1 assertEquals(1, result.getTotal());
 246   
 247  1 FolderMeta browsed = (FolderMeta) searcher.browse("/", constraint, Searcher.DESCENDING, -1, -1);
 248  1 assertEquals(1, browsed.getTotal());
 249    }
 250   
 251  1 public void testSpacialSearch() throws Exception
 252    {
 253  1 PhotoMeta photo1 = new PhotoMeta();
 254  1 photo1.setPath("/folder/photo1.jpg");
 255  1 photo1.getPosition().setLatitude(new Double(37.75));
 256  1 photo1.getPosition().setLongitude(new Double(122.68));
 257  1 PhotoMeta photo2 = new PhotoMeta();
 258  1 photo2.setPath("/folder/photo2.jpg");
 259  1 photo2.getPosition().setLatitude(new Double(-37.75));
 260  1 photo2.getPosition().setLongitude(new Double(122.68));
 261   
 262  1 indexer.index(Arrays.asList(new Meta[] { photo1, photo2 }), true);
 263   
 264  1 SearchResult result = searcher.search("point:37.7,122.6 radius:100", Searcher.DESCENDING, 0, -1);
 265  1 assertEquals(1, result.getTotal());
 266  1 assertTrue(result.containsFile(photo1));
 267    }
 268   
 269  6 public Query findAllQuery()
 270    {
 271  6 return new TermQuery(new Term(DocumentFactory.ALL_FIELD, DocumentFactory.ALL_VALUE));
 272    }
 273    }