add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / enhancements / EnhancementsPayloadIteratorTest.java
1 package org.apache.lucene.facet.enhancements;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.Term;
7 import org.apache.lucene.store.Directory;
8 import org.junit.AfterClass;
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11
12 import org.apache.lucene.util.LuceneTestCase;
13 import org.apache.lucene.facet.enhancements.EnhancementsPayloadIterator;
14 import org.apache.lucene.facet.enhancements.association.AssociationEnhancement;
15 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
16 import org.apache.lucene.facet.example.association.AssociationIndexer;
17 import org.apache.lucene.facet.example.association.AssociationUtils;
18 import org.apache.lucene.facet.search.DrillDown;
19 import org.apache.lucene.facet.taxonomy.CategoryPath;
20
21 /**
22  * Licensed to the Apache Software Foundation (ASF) under one or more
23  * contributor license agreements.  See the NOTICE file distributed with
24  * this work for additional information regarding copyright ownership.
25  * The ASF licenses this file to You under the Apache License, Version 2.0
26  * (the "License"); you may not use this file except in compliance with
27  * the License.  You may obtain a copy of the License at
28  *
29  *     http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS,
33  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  */
37
38 public class EnhancementsPayloadIteratorTest extends LuceneTestCase {
39
40   private static Directory indexDir;
41   private static Directory taxoDir;
42   private static EnhancementsIndexingParams indexingParams;
43   private static AssociationEnhancement associationEnhancement;
44
45   @BeforeClass
46   public static void buildAssociationIndex() throws Exception {
47     // create Directories for the search index and for the taxonomy index
48     indexDir = newDirectory();
49     taxoDir = newDirectory();
50
51     // index the sample documents
52     if (VERBOSE) {
53       System.out.println("index the sample documents...");
54     }
55     AssociationIndexer.index(indexDir, taxoDir);
56
57     indexingParams = AssociationUtils.assocIndexingParams;
58     associationEnhancement = (AssociationEnhancement) indexingParams
59         .getCategoryEnhancements().get(0);
60   }
61
62   @Test
63   public void testFullIterator() throws IOException {
64     IndexReader indexReader = IndexReader.open(indexDir);
65     Term term = DrillDown.term(indexingParams, new CategoryPath("tags", "lucene"));
66     EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
67         indexingParams.getCategoryEnhancements(), indexReader, term);
68     assertTrue("Unexpected failure of init()", iterator.init());
69     assertTrue("Missing instance of tags/lucene in doc 0", iterator.setdoc(0));
70     int assoc = (Integer) iterator.getCategoryData(associationEnhancement);
71     assertEquals("Unexpected association value for tags/lucene in doc 0", 3, assoc, 1E-5);
72     assertTrue("Missing instance of tags/lucene in doc 1", iterator.setdoc(1));
73     assoc = (Integer) iterator.getCategoryData(associationEnhancement);
74     assertEquals("Unexpected association value for tags/lucene in doc 1", 1, assoc, 1E-5);
75     indexReader.close();
76   }
77
78   @Test
79   public void testEmptyIterator() throws IOException {
80     IndexReader indexReader = IndexReader.open(indexDir);
81     Term term = DrillDown.term(indexingParams, new CategoryPath("root","a", "f2"));
82     EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
83         indexingParams.getCategoryEnhancements(), indexReader, term);
84     assertTrue("Unexpected failure of init()", iterator.init());
85     assertFalse("Unexpected payload for root/a/f2 in doc 0", iterator.setdoc(0));
86     assertFalse("Unexpected instance of root/a/f2 in doc 1", iterator.setdoc(1));
87     indexReader.close();
88   }
89
90   @Test
91   public void testPartialIterator() throws IOException {
92     IndexReader indexReader = IndexReader.open(indexDir);
93     Term term = DrillDown.term(indexingParams, new CategoryPath("genre","software"));
94     EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
95         indexingParams.getCategoryEnhancements(), indexReader, term);
96     assertTrue("Unexpected failure of init()", iterator.init());
97     assertFalse("Unexpected payload for genre/computing in doc 0", iterator.setdoc(0));
98     assertTrue("Missing instance of genre/computing in doc 1", iterator.setdoc(1));
99     float assoc = Float.intBitsToFloat((Integer) iterator
100         .getCategoryData(associationEnhancement));
101     assertEquals("Unexpected association value for genre/computing in doc 1", 0.34f, assoc, 0.001);
102     indexReader.close();
103   }
104
105   @AfterClass
106   public static void closeDirectories() throws IOException {
107     indexDir.close();
108     indexDir = null;
109     taxoDir.close();
110     taxoDir = null;
111   }
112 }