add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / byTask / feeds / TrecContentSourceTest.java
1 package org.apache.lucene.benchmark.byTask.feeds;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.text.ParseException;
25 import java.util.Arrays;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.Properties;
29
30 import org.apache.lucene.benchmark.byTask.feeds.TrecDocParser.ParsePathType;
31 import org.apache.lucene.benchmark.byTask.utils.Config;
32 import org.apache.lucene.document.DateTools;
33 import org.apache.lucene.util.LuceneTestCase;
34 import org.apache.lucene.util._TestUtil;
35
36 public class TrecContentSourceTest extends LuceneTestCase {
37
38   /** A TrecDocMaker which works on a String and not files. */
39   private static class StringableTrecSource extends TrecContentSource {
40   
41     private String docs = null;
42     
43     public StringableTrecSource(String docs, boolean forever) {
44       this.docs = docs;
45       this.forever = forever;
46     }
47     
48     @Override
49     void openNextFile() throws NoMoreDataException, IOException {
50       if (reader != null) {
51         if (!forever) {
52           throw new NoMoreDataException();
53         }
54         ++iteration;
55       }
56       
57       reader = new BufferedReader(new StringReader(docs));
58     }
59     
60     @Override
61     public void setConfig(Config config) {
62       htmlParser = new DemoHTMLParser();
63     }
64   }
65   
66   private void assertDocData(DocData dd, String expName, String expTitle,
67                              String expBody, Date expDate)
68       throws ParseException {
69     assertNotNull(dd);
70     assertEquals(expName, dd.getName());
71     assertEquals(expTitle, dd.getTitle());
72     assertTrue(dd.getBody().indexOf(expBody) != -1);
73     Date date = dd.getDate() != null ? DateTools.stringToDate(dd.getDate()) : null;
74     assertEquals(expDate, date);
75   }
76   
77   private void assertNoMoreDataException(StringableTrecSource stdm) throws Exception {
78     boolean thrown = false;
79     try {
80       stdm.getNextDocData(null);
81     } catch (NoMoreDataException e) {
82       thrown = true;
83     }
84     assertTrue("Expecting NoMoreDataException", thrown);
85   }
86   
87   public void testOneDocument() throws Exception {
88     String docs = "<DOC>\r\n" + 
89                   "<DOCNO>TEST-000</DOCNO>\r\n" + 
90                   "<DOCHDR>\r\n" + 
91                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
92                   "HTTP/1.1 200 OK\r\n" + 
93                   "Date: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
94                   "Server: Apache/1.3.27 (Unix)\r\n" + 
95                   "Last-Modified: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
96                   "Content-Length: 614\r\n" + 
97                   "Connection: close\r\n" + 
98                   "Content-Type: text/html\r\n" + 
99                   "</DOCHDR>\r\n" + 
100                   "<html>\r\n" + 
101                   "\r\n" + 
102                   "<head>\r\n" + 
103                   "<title>\r\n" + 
104                   "TEST-000 title\r\n" + 
105                   "</title>\r\n" + 
106                   "</head>\r\n" + 
107                   "\r\n" + 
108                   "<body>\r\n" + 
109                   "TEST-000 text\r\n" + 
110                   "\r\n" + 
111                   "</body>\r\n" + 
112                   "\r\n" + 
113                   "</DOC>";
114     StringableTrecSource source = new StringableTrecSource(docs, false);
115     source.setConfig(null);
116
117     DocData dd = source.getNextDocData(new DocData());
118     assertDocData(dd, "TEST-000_0", "TEST-000 title", "TEST-000 text", source
119         .parseDate("Sun, 11 Jan 2009 08:00:00 GMT"));
120     
121     assertNoMoreDataException(source);
122   }
123   
124   public void testTwoDocuments() throws Exception {
125     String docs = "<DOC>\r\n" + 
126                   "<DOCNO>TEST-000</DOCNO>\r\n" + 
127                   "<DOCHDR>\r\n" + 
128                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
129                   "HTTP/1.1 200 OK\r\n" + 
130                   "Date: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
131                   "Server: Apache/1.3.27 (Unix)\r\n" + 
132                   "Last-Modified: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
133                   "Content-Length: 614\r\n" + 
134                   "Connection: close\r\n" + 
135                   "Content-Type: text/html\r\n" + 
136                   "</DOCHDR>\r\n" + 
137                   "<html>\r\n" + 
138                   "\r\n" + 
139                   "<head>\r\n" + 
140                   "<title>\r\n" + 
141                   "TEST-000 title\r\n" + 
142                   "</title>\r\n" + 
143                   "</head>\r\n" + 
144                   "\r\n" + 
145                   "<body>\r\n" + 
146                   "TEST-000 text\r\n" + 
147                   "\r\n" + 
148                   "</body>\r\n" + 
149                   "\r\n" + 
150                   "</DOC>\r\n" +
151                   "<DOC>\r\n" + 
152                   "<DOCNO>TEST-001</DOCNO>\r\n" + 
153                   "<DOCHDR>\r\n" + 
154                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
155                   "HTTP/1.1 200 OK\r\n" + 
156                   "Date: Sun, 11 Jan 2009 08:01:00 GMT\r\n" + 
157                   "Server: Apache/1.3.27 (Unix)\r\n" + 
158                   "Last-Modified: Sun, 11 Jan 2008 08:01:00 GMT\r\n" + 
159                   "Content-Length: 614\r\n" + 
160                   "Connection: close\r\n" + 
161                   "Content-Type: text/html\r\n" + 
162                   "</DOCHDR>\r\n" + 
163                   "<html>\r\n" + 
164                   "\r\n" + 
165                   "<head>\r\n" + 
166                   "<title>\r\n" + 
167                   "TEST-001 title\r\n" + 
168                   "</title>\r\n" + 
169                   "</head>\r\n" + 
170                   "\r\n" + 
171                   "<body>\r\n" + 
172                   "TEST-001 text\r\n" + 
173                   "\r\n" + 
174                   "</body>\r\n" + 
175                   "\r\n" + 
176                   "</DOC>";
177     StringableTrecSource source = new StringableTrecSource(docs, false);
178     source.setConfig(null);
179
180     DocData dd = source.getNextDocData(new DocData());
181     assertDocData(dd, "TEST-000_0", "TEST-000 title", "TEST-000 text", source
182         .parseDate("Sun, 11 Jan 2009 08:00:00 GMT"));
183     
184     dd = source.getNextDocData(dd);
185     assertDocData(dd, "TEST-001_0", "TEST-001 title", "TEST-001 text", source
186         .parseDate("Sun, 11 Jan 2009 08:01:00 GMT"));
187     
188     assertNoMoreDataException(source);
189   }
190
191   // If a Date: attribute is missing, make sure the document is not skipped, but
192   // rather that null Data is assigned.
193   public void testMissingDate() throws Exception {
194     String docs = "<DOC>\r\n" + 
195                   "<DOCNO>TEST-000</DOCNO>\r\n" + 
196                   "<DOCHDR>\r\n" + 
197                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
198                   "HTTP/1.1 200 OK\r\n" + 
199                   "Server: Apache/1.3.27 (Unix)\r\n" + 
200                   "Last-Modified: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
201                   "Content-Length: 614\r\n" + 
202                   "Connection: close\r\n" + 
203                   "Content-Type: text/html\r\n" + 
204                   "</DOCHDR>\r\n" + 
205                   "<html>\r\n" + 
206                   "\r\n" + 
207                   "<head>\r\n" + 
208                   "<title>\r\n" + 
209                   "TEST-000 title\r\n" + 
210                   "</title>\r\n" + 
211                   "</head>\r\n" + 
212                   "\r\n" + 
213                   "<body>\r\n" + 
214                   "TEST-000 text\r\n" + 
215                   "\r\n" + 
216                   "</body>\r\n" + 
217                   "\r\n" + 
218                   "</DOC>\r\n" +
219                   "<DOC>\r\n" + 
220                   "<DOCNO>TEST-001</DOCNO>\r\n" + 
221                   "<DOCHDR>\r\n" + 
222                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
223                   "HTTP/1.1 200 OK\r\n" + 
224                   "Date: Sun, 11 Jan 2009 08:01:00 GMT\r\n" + 
225                   "Server: Apache/1.3.27 (Unix)\r\n" + 
226                   "Last-Modified: Sun, 11 Jan 2009 08:01:00 GMT\r\n" + 
227                   "Content-Length: 614\r\n" + 
228                   "Connection: close\r\n" + 
229                   "Content-Type: text/html\r\n" + 
230                   "</DOCHDR>\r\n" + 
231                   "<html>\r\n" + 
232                   "\r\n" + 
233                   "<head>\r\n" + 
234                   "<title>\r\n" + 
235                   "TEST-001 title\r\n" + 
236                   "</title>\r\n" + 
237                   "</head>\r\n" + 
238                   "\r\n" + 
239                   "<body>\r\n" + 
240                   "TEST-001 text\r\n" + 
241                   "\r\n" + 
242                   "</body>\r\n" + 
243                   "\r\n" + 
244                   "</DOC>";
245     StringableTrecSource source = new StringableTrecSource(docs, false);
246     source.setConfig(null);
247
248     DocData dd = source.getNextDocData(new DocData());
249     assertDocData(dd, "TEST-000_0", "TEST-000 title", "TEST-000 text", null);
250     
251     dd = source.getNextDocData(dd);
252     assertDocData(dd, "TEST-001_0", "TEST-001 title", "TEST-001 text", source
253         .parseDate("Sun, 11 Jan 2009 08:01:00 GMT"));
254     
255     assertNoMoreDataException(source);
256   }
257
258   // When a 'bad date' is input (unparsable date), make sure the DocData date is
259   // assigned null.
260   public void testBadDate() throws Exception {
261     String docs = "<DOC>\r\n" + 
262                   "<DOCNO>TEST-000</DOCNO>\r\n" + 
263                   "<DOCHDR>\r\n" + 
264                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
265                   "HTTP/1.1 200 OK\r\n" + 
266                   "Date: Bad Date\r\n" + 
267                   "Server: Apache/1.3.27 (Unix)\r\n" + 
268                   "Last-Modified: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
269                   "Content-Length: 614\r\n" + 
270                   "Connection: close\r\n" + 
271                   "Content-Type: text/html\r\n" + 
272                   "</DOCHDR>\r\n" + 
273                   "<html>\r\n" + 
274                   "\r\n" + 
275                   "<head>\r\n" + 
276                   "<title>\r\n" + 
277                   "TEST-000 title\r\n" + 
278                   "</title>\r\n" + 
279                   "</head>\r\n" + 
280                   "\r\n" + 
281                   "<body>\r\n" + 
282                   "TEST-000 text\r\n" + 
283                   "\r\n" + 
284                   "</body>\r\n" + 
285                   "\r\n" + 
286                   "</DOC>";
287     StringableTrecSource source = new StringableTrecSource(docs, false);
288     source.setConfig(null);
289
290     DocData dd = source.getNextDocData(new DocData());
291     assertDocData(dd, "TEST-000_0", "TEST-000 title", "TEST-000 text", null);
292     
293     assertNoMoreDataException(source);
294   }
295
296   public void testForever() throws Exception {
297     String docs = "<DOC>\r\n" + 
298                   "<DOCNO>TEST-000</DOCNO>\r\n" + 
299                   "<DOCHDR>\r\n" + 
300                   "http://lucene.apache.org.trecdocmaker.test\r\n" + 
301                   "HTTP/1.1 200 OK\r\n" + 
302                   "Date: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
303                   "Server: Apache/1.3.27 (Unix)\r\n" + 
304                   "Last-Modified: Sun, 11 Jan 2009 08:00:00 GMT\r\n" + 
305                   "Content-Length: 614\r\n" + 
306                   "Connection: close\r\n" + 
307                   "Content-Type: text/html\r\n" + 
308                   "</DOCHDR>\r\n" + 
309                   "<html>\r\n" + 
310                   "\r\n" + 
311                   "<head>\r\n" + 
312                   "<title>\r\n" + 
313                   "TEST-000 title\r\n" + 
314                   "</title>\r\n" + 
315                   "</head>\r\n" + 
316                   "\r\n" + 
317                   "<body>\r\n" + 
318                   "TEST-000 text\r\n" + 
319                   "\r\n" + 
320                   "</body>\r\n" + 
321                   "\r\n" + 
322                   "</DOC>";
323     StringableTrecSource source = new StringableTrecSource(docs, true);
324     source.setConfig(null);
325
326     DocData dd = source.getNextDocData(new DocData());
327     assertDocData(dd, "TEST-000_0", "TEST-000 title", "TEST-000 text", source
328         .parseDate("Sun, 11 Jan 2009 08:00:00 GMT"));
329     
330     // same document, but the second iteration changes the name.
331     dd = source.getNextDocData(dd);
332     assertDocData(dd, "TEST-000_1", "TEST-000 title", "TEST-000 text", source
333         .parseDate("Sun, 11 Jan 2009 08:00:00 GMT"));
334
335     // Don't test that NoMoreDataException is thrown, since the forever flag is
336     // turned on.
337   }
338   
339   /** 
340    * Open a trec content source over a directory with files of all trec path types and all
341    * supported formats - bzip, gzip, txt. 
342    */
343   public void testTrecFeedDirAllTypes() throws Exception {
344     File dataDir =  _TestUtil.getTempDir("trecFeedAllTypes");
345     _TestUtil.unzip(getDataFile("trecdocs.zip"), dataDir);
346     TrecContentSource tcs = new TrecContentSource();
347     Properties props = new Properties();
348     props.setProperty("print.props", "false");
349     props.setProperty("content.source.verbose", "false");
350     props.setProperty("content.source.excludeIteration", "true");
351     props.setProperty("doc.maker.forever", "false");
352     props.setProperty("docs.dir", dataDir.getCanonicalPath().replace('\\','/')); 
353     props.setProperty("trec.doc.parser", TrecParserByPath.class.getName());
354     props.setProperty("content.source.forever", "false");
355     tcs.setConfig(new Config(props));
356     tcs.resetInputs();
357     DocData dd = new DocData();
358     int n = 0;
359     boolean gotExpectedException = false;
360     HashSet<ParsePathType> unseenTypes = new HashSet<ParsePathType>(Arrays.asList(ParsePathType.values()));
361     try {
362       while (n<100) { // arbiterary limit to prevent looping forever in case of test failure
363         dd = tcs.getNextDocData(dd);
364         ++n;
365         assertNotNull("doc data "+n+" should not be null!", dd);
366         unseenTypes.remove(tcs.currPathType);
367         switch(tcs.currPathType) {
368           case GOV2:
369             assertDocData(dd, "TEST-000", "TEST-000 title", "TEST-000 text", tcs.parseDate("Sun, 11 Jan 2009 08:00:00 GMT"));
370             break;
371           case FBIS:
372             assertDocData(dd, "TEST-001", "TEST-001 Title", "TEST-001 text", tcs.parseDate("1 January 1991"));
373             break;
374           case FR94:
375             // no title extraction in this source for now
376             assertDocData(dd, "TEST-002", null, "DEPARTMENT OF SOMETHING", tcs.parseDate("February 3, 1994"));
377             break;
378           case FT:
379             assertDocData(dd, "TEST-003", "Test-003 title", "Some pub text", tcs.parseDate("980424"));
380             break;
381           case LATIMES:
382             assertDocData(dd, "TEST-004", "Test-004 Title", "Some paragraph", tcs.parseDate("January 17, 1997, Sunday"));
383             break;
384           default:
385             assertTrue("Should never get here!", false);
386         }
387       }
388     } catch (NoMoreDataException e) {
389       gotExpectedException = true;
390     }
391     assertTrue("Should have gotten NoMoreDataException!", gotExpectedException);
392     assertEquals("Wrong number of documents created by source!",5,n);
393     assertTrue("Did not see all types!",unseenTypes.isEmpty());
394   }
395
396 }