Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
55   165   3   18.33
0   84   0.05   3
3     1  
1    
 
  TestDateFilter       Line # 36 55 3 100% 1.0
 
  (2)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import junit.framework.TestCase;
20    import org.apache.lucene.analysis.SimpleAnalyzer;
21    import org.apache.lucene.document.DateTools;
22    import org.apache.lucene.document.Document;
23    import org.apache.lucene.document.Field;
24    import org.apache.lucene.index.IndexWriter;
25    import org.apache.lucene.index.Term;
26    import org.apache.lucene.store.RAMDirectory;
27   
28    import java.io.IOException;
29   
30    /**
31    * DateFilter JUnit tests.
32    *
33    * @author Otis Gospodnetic
34    * @version $Revision: 387550 $
35    */
 
36    public class TestDateFilter
37    extends TestCase
38    {
 
39  2 toggle public TestDateFilter(String name)
40    {
41  2 super(name);
42    }
43   
44    /**
45    *
46    */
 
47  1 toggle public static void testBefore()
48    throws IOException
49    {
50    // create an index
51  1 RAMDirectory indexStore = new RAMDirectory();
52  1 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
53   
54  1 long now = System.currentTimeMillis();
55   
56  1 Document doc = new Document();
57    // add time that is in the past
58  1 doc.add(new Field("datefield", DateTools.timeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED));
59  1 doc.add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED));
60  1 writer.addDocument(doc);
61  1 writer.optimize();
62  1 writer.close();
63   
64  1 IndexSearcher searcher = new IndexSearcher(indexStore);
65   
66    // filter that should preserve matches
67    //DateFilter df1 = DateFilter.Before("datefield", now);
68  1 RangeFilter df1 = new RangeFilter("datefield", DateTools.timeToString(now - 2000, DateTools.Resolution.MILLISECOND),
69    DateTools.timeToString(now, DateTools.Resolution.MILLISECOND), false, true);
70    // filter that should discard matches
71    //DateFilter df2 = DateFilter.Before("datefield", now - 999999);
72  1 RangeFilter df2 = new RangeFilter("datefield", DateTools.timeToString(0, DateTools.Resolution.MILLISECOND),
73    DateTools.timeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, false);
74   
75    // search something that doesn't exist with DateFilter
76  1 Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
77   
78    // search for something that does exists
79  1 Query query2 = new TermQuery(new Term("body", "sunny"));
80   
81  1 Hits result;
82   
83    // ensure that queries return expected results without DateFilter first
84  1 result = searcher.search(query1);
85  1 assertEquals(0, result.length());
86   
87  1 result = searcher.search(query2);
88  1 assertEquals(1, result.length());
89   
90   
91    // run queries with DateFilter
92  1 result = searcher.search(query1, df1);
93  1 assertEquals(0, result.length());
94   
95  1 result = searcher.search(query1, df2);
96  1 assertEquals(0, result.length());
97   
98  1 result = searcher.search(query2, df1);
99  1 assertEquals(1, result.length());
100   
101  1 result = searcher.search(query2, df2);
102  1 assertEquals(0, result.length());
103    }
104   
105    /**
106    *
107    */
 
108  1 toggle public static void testAfter()
109    throws IOException
110    {
111    // create an index
112  1 RAMDirectory indexStore = new RAMDirectory();
113  1 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
114   
115  1 long now = System.currentTimeMillis();
116   
117  1 Document doc = new Document();
118    // add time that is in the future
119  1 doc.add(new Field("datefield", DateTools.timeToString(now + 888888, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.UN_TOKENIZED));
120  1 doc.add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.TOKENIZED));
121  1 writer.addDocument(doc);
122  1 writer.optimize();
123  1 writer.close();
124   
125  1 IndexSearcher searcher = new IndexSearcher(indexStore);
126   
127    // filter that should preserve matches
128    //DateFilter df1 = DateFilter.After("datefield", now);
129  1 RangeFilter df1 = new RangeFilter("datefield", DateTools.timeToString(now, DateTools.Resolution.MILLISECOND),
130    DateTools.timeToString(now + 999999, DateTools.Resolution.MILLISECOND), true, false);
131    // filter that should discard matches
132    //DateFilter df2 = DateFilter.After("datefield", now + 999999);
133  1 RangeFilter df2 = new RangeFilter("datefield", DateTools.timeToString(now + 999999, DateTools.Resolution.MILLISECOND),
134    DateTools.timeToString(now + 999999999, DateTools.Resolution.MILLISECOND), false, true);
135   
136    // search something that doesn't exist with DateFilter
137  1 Query query1 = new TermQuery(new Term("body", "NoMatchForThis"));
138   
139    // search for something that does exists
140  1 Query query2 = new TermQuery(new Term("body", "sunny"));
141   
142  1 Hits result;
143   
144    // ensure that queries return expected results without DateFilter first
145  1 result = searcher.search(query1);
146  1 assertEquals(0, result.length());
147   
148  1 result = searcher.search(query2);
149  1 assertEquals(1, result.length());
150   
151   
152    // run queries with DateFilter
153  1 result = searcher.search(query1, df1);
154  1 assertEquals(0, result.length());
155   
156  1 result = searcher.search(query1, df2);
157  1 assertEquals(0, result.length());
158   
159  1 result = searcher.search(query2, df1);
160  1 assertEquals(1, result.length());
161   
162  1 result = searcher.search(query2, df2);
163  1 assertEquals(0, result.length());
164    }
165    }