pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / index / streaming / CategoryParentsStreamTest.java
1 package org.apache.lucene.facet.index.streaming;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.analysis.TokenStream;
6 import org.apache.lucene.store.Directory;
7 import org.junit.Test;
8
9 import org.apache.lucene.facet.FacetException;
10 import org.apache.lucene.facet.index.CategoryContainerTestBase;
11 import org.apache.lucene.facet.index.DummyProperty;
12 import org.apache.lucene.facet.index.categorypolicy.NonTopLevelOrdinalPolicy;
13 import org.apache.lucene.facet.index.categorypolicy.NonTopLevelPathPolicy;
14 import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;
15 import org.apache.lucene.facet.index.categorypolicy.PathPolicy;
16 import org.apache.lucene.facet.index.params.DefaultFacetIndexingParams;
17 import org.apache.lucene.facet.index.params.FacetIndexingParams;
18 import org.apache.lucene.facet.index.streaming.CategoryAttributesStream;
19 import org.apache.lucene.facet.index.streaming.CategoryListTokenizer;
20 import org.apache.lucene.facet.index.streaming.CategoryParentsStream;
21 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
22 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
23
24 /**
25  * Licensed to the Apache Software Foundation (ASF) under one or more
26  * contributor license agreements.  See the NOTICE file distributed with
27  * this work for additional information regarding copyright ownership.
28  * The ASF licenses this file to You under the Apache License, Version 2.0
29  * (the "License"); you may not use this file except in compliance with
30  * the License.  You may obtain a copy of the License at
31  *
32  *     http://www.apache.org/licenses/LICENSE-2.0
33  *
34  * Unless required by applicable law or agreed to in writing, software
35  * distributed under the License is distributed on an "AS IS" BASIS,
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37  * See the License for the specific language governing permissions and
38  * limitations under the License.
39  */
40
41 public class CategoryParentsStreamTest extends CategoryContainerTestBase {
42
43   /**
44    * Verifies that a {@link CategoryParentsStream} can be constructed from
45    * {@link CategoryAttributesStream} and produces the correct number of
46    * tokens with default facet indexing params.
47    * 
48    * @throws IOException
49    */
50   @Test
51   public void testStreamDefaultParams() throws IOException {
52     Directory directory = newDirectory();
53     TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
54         directory);
55     CategoryParentsStream stream = new CategoryParentsStream(
56         new CategoryAttributesStream(categoryContainer),
57         taxonomyWriter, new DefaultFacetIndexingParams());
58
59     // count the number of tokens
60     int nTokens;
61     for (nTokens = 0; stream.incrementToken(); nTokens++) {
62     }
63     // should be 6 - all categories and parents
64     assertEquals("Wrong number of tokens", 6, nTokens);
65
66     taxonomyWriter.close();
67     directory.close();
68   }
69
70   /**
71    * Verifies that a {@link CategoryParentsStream} can be constructed from
72    * {@link CategoryAttributesStream} and produces the correct number of
73    * tokens with non top level facet indexing params.
74    * 
75    * @throws IOException
76    */
77   @Test
78   public void testStreamNonTopLevelParams() throws IOException {
79     Directory directory = newDirectory();
80     final TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
81         directory);
82     FacetIndexingParams indexingParams = new DefaultFacetIndexingParams() {
83       @Override
84       protected OrdinalPolicy fixedOrdinalPolicy() {
85         return new NonTopLevelOrdinalPolicy();
86       }
87       @Override
88       protected PathPolicy fixedPathPolicy() {
89         return new NonTopLevelPathPolicy();
90       }
91     };
92     
93     CategoryParentsStream stream = new CategoryParentsStream(
94         new CategoryAttributesStream(categoryContainer),
95         taxonomyWriter, indexingParams);
96
97     // count the number of tokens
98     int nTokens;
99     for (nTokens = 0; stream.incrementToken(); nTokens++) {
100     }
101     /*
102      * should be 4: 3 non top level ("two", "three" and "six"), and one
103      * explicit top level ("four")
104      */
105     assertEquals("Wrong number of tokens", 4, nTokens);
106
107     taxonomyWriter.close();
108     directory.close();
109   }
110
111   /**
112    * Verifies the correctness when no attributes in parents are retained in
113    * {@link CategoryParentsStream}.
114    * 
115    * @throws IOException
116    * @throws FacetException 
117    */
118   @Test
119   public void testNoRetainableAttributes() throws IOException, FacetException {
120     Directory directory = newDirectory();
121     TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(directory);
122
123     new CategoryParentsStream(new CategoryAttributesStream(categoryContainer),
124         taxonomyWriter, new DefaultFacetIndexingParams());
125
126     // add DummyAttribute, but do not retain, only one expected
127     categoryContainer.addCategory(initialCatgeories[0], new DummyProperty());
128
129     CategoryParentsStream stream = new CategoryParentsStream(new CategoryAttributesStream(
130         categoryContainer), taxonomyWriter,
131         new DefaultFacetIndexingParams());
132
133     int nAttributes = 0;
134     while (stream.incrementToken()) {
135       if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
136         nAttributes++;
137       }
138     }
139     assertEquals("Wrong number of tokens with attributes", 1, nAttributes);
140
141     taxonomyWriter.close();
142     directory.close();
143   }
144
145   /**
146    * Verifies the correctness when attributes in parents are retained in
147    * {@link CategoryParentsStream}.
148    * 
149    * @throws IOException
150    * @throws FacetException 
151    */
152   @Test
153   public void testRetainableAttributes() throws IOException, FacetException {
154     Directory directory = newDirectory();
155     TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
156         directory);
157
158     FacetIndexingParams indexingParams = new DefaultFacetIndexingParams();
159     new CategoryParentsStream(new CategoryAttributesStream(
160         categoryContainer), taxonomyWriter, indexingParams);
161
162     // add DummyAttribute and retain it, three expected
163     categoryContainer.clear();
164     categoryContainer
165         .addCategory(initialCatgeories[0], new DummyProperty());
166     CategoryParentsStream stream = new CategoryParentsStream(
167         new CategoryAttributesStream(categoryContainer),
168         taxonomyWriter, new DefaultFacetIndexingParams());
169     stream.addRetainableProperty(DummyProperty.class);
170
171     MyCategoryListTokenizer tokenizer = new MyCategoryListTokenizer(stream,
172         indexingParams);
173
174     int nAttributes = 0;
175     try {
176       while (tokenizer.incrementToken()) {
177         if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
178           nAttributes++;
179         }
180       }
181     } catch (IOException e) {
182       fail("Properties retained after stream closed");
183     }
184     assertEquals("Wrong number of tokens with attributes", 3, nAttributes);
185
186     taxonomyWriter.close();
187     directory.close();
188   }
189
190   private final class MyCategoryListTokenizer extends CategoryListTokenizer {
191
192     public MyCategoryListTokenizer(TokenStream input,
193         FacetIndexingParams indexingParams) {
194       super(input, indexingParams);
195     }
196
197     @Override
198     public boolean incrementToken() throws IOException {
199       if (input.incrementToken()) {
200         return true;
201       }
202       if (categoryAttribute != null) {
203         if (categoryAttribute.getCategoryPath() == null) {
204           if (categoryAttribute.getProperty(DummyProperty.class) != null) {
205             throw new IOException(
206                 "Properties not cleared properly from parents stream");
207           }
208         }
209       }
210       return false;
211     }
212
213   }
214 }