01: package org.apache.lucene;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 org.apache.lucene.util.LuceneTestCase;
21: import org.apache.lucene.store.RAMDirectory;
22: import org.apache.lucene.index.IndexWriter;
23: import org.apache.lucene.index.Term;
24: import org.apache.lucene.analysis.WhitespaceAnalyzer;
25: import org.apache.lucene.document.Document;
26: import org.apache.lucene.document.Field;
27: import org.apache.lucene.search.IndexSearcher;
28: import org.apache.lucene.search.TermQuery;
29: import org.apache.lucene.search.Hits;
30: import org.apache.lucene.search.Hit;
31: import org.apache.lucene.search.HitIterator;
32:
33: import java.util.NoSuchElementException;
34:
35: /**
36: * This test intentionally not put in the search package in order
37: * to test HitIterator and Hit package protection.
38: */
39: public class TestHitIterator extends LuceneTestCase {
40: public void testIterator() throws Exception {
41: RAMDirectory directory = new RAMDirectory();
42:
43: IndexWriter writer = new IndexWriter(directory,
44: new WhitespaceAnalyzer(), true);
45: Document doc = new Document();
46: doc.add(new Field("field", "iterator test doc 1",
47: Field.Store.YES, Field.Index.TOKENIZED));
48: writer.addDocument(doc);
49:
50: doc = new Document();
51: doc.add(new Field("field", "iterator test doc 2",
52: Field.Store.YES, Field.Index.TOKENIZED));
53: writer.addDocument(doc);
54:
55: writer.close();
56:
57: IndexSearcher searcher = new IndexSearcher(directory);
58: Hits hits = searcher.search(new TermQuery(new Term("field",
59: "iterator")));
60:
61: HitIterator iterator = (HitIterator) hits.iterator();
62: assertEquals(2, iterator.length());
63: assertTrue(iterator.hasNext());
64: Hit hit = (Hit) iterator.next();
65: assertEquals("iterator test doc 1", hit.get("field"));
66:
67: assertTrue(iterator.hasNext());
68: hit = (Hit) iterator.next();
69: assertEquals("iterator test doc 2", hit.getDocument().get(
70: "field"));
71:
72: assertFalse(iterator.hasNext());
73:
74: boolean caughtException = false;
75: try {
76: iterator.next();
77: } catch (NoSuchElementException e) {
78: assertTrue(true);
79: caughtException = true;
80: }
81:
82: assertTrue(caughtException);
83: }
84: }
|