1 package org.apache.lucene.facet.index.streaming;
3 import java.io.IOException;
5 import org.apache.lucene.analysis.TokenStream;
6 import org.apache.lucene.store.Directory;
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;
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
32 * http://www.apache.org/licenses/LICENSE-2.0
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.
41 public class CategoryParentsStreamTest extends CategoryContainerTestBase {
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.
51 public void testStreamDefaultParams() throws IOException {
52 Directory directory = newDirectory();
53 TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
55 CategoryParentsStream stream = new CategoryParentsStream(
56 new CategoryAttributesStream(categoryContainer),
57 taxonomyWriter, new DefaultFacetIndexingParams());
59 // count the number of tokens
61 for (nTokens = 0; stream.incrementToken(); nTokens++) {
63 // should be 6 - all categories and parents
64 assertEquals("Wrong number of tokens", 6, nTokens);
66 taxonomyWriter.close();
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.
78 public void testStreamNonTopLevelParams() throws IOException {
79 Directory directory = newDirectory();
80 final TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
82 FacetIndexingParams indexingParams = new DefaultFacetIndexingParams() {
84 protected OrdinalPolicy fixedOrdinalPolicy() {
85 return new NonTopLevelOrdinalPolicy();
88 protected PathPolicy fixedPathPolicy() {
89 return new NonTopLevelPathPolicy();
93 CategoryParentsStream stream = new CategoryParentsStream(
94 new CategoryAttributesStream(categoryContainer),
95 taxonomyWriter, indexingParams);
97 // count the number of tokens
99 for (nTokens = 0; stream.incrementToken(); nTokens++) {
102 * should be 4: 3 non top level ("two", "three" and "six"), and one
103 * explicit top level ("four")
105 assertEquals("Wrong number of tokens", 4, nTokens);
107 taxonomyWriter.close();
112 * Verifies the correctness when no attributes in parents are retained in
113 * {@link CategoryParentsStream}.
115 * @throws IOException
116 * @throws FacetException
119 public void testNoRetainableAttributes() throws IOException, FacetException {
120 Directory directory = newDirectory();
121 TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(directory);
123 new CategoryParentsStream(new CategoryAttributesStream(categoryContainer),
124 taxonomyWriter, new DefaultFacetIndexingParams());
126 // add DummyAttribute, but do not retain, only one expected
127 categoryContainer.addCategory(initialCatgeories[0], new DummyProperty());
129 CategoryParentsStream stream = new CategoryParentsStream(new CategoryAttributesStream(
130 categoryContainer), taxonomyWriter,
131 new DefaultFacetIndexingParams());
134 while (stream.incrementToken()) {
135 if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
139 assertEquals("Wrong number of tokens with attributes", 1, nAttributes);
141 taxonomyWriter.close();
146 * Verifies the correctness when attributes in parents are retained in
147 * {@link CategoryParentsStream}.
149 * @throws IOException
150 * @throws FacetException
153 public void testRetainableAttributes() throws IOException, FacetException {
154 Directory directory = newDirectory();
155 TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(
158 FacetIndexingParams indexingParams = new DefaultFacetIndexingParams();
159 new CategoryParentsStream(new CategoryAttributesStream(
160 categoryContainer), taxonomyWriter, indexingParams);
162 // add DummyAttribute and retain it, three expected
163 categoryContainer.clear();
165 .addCategory(initialCatgeories[0], new DummyProperty());
166 CategoryParentsStream stream = new CategoryParentsStream(
167 new CategoryAttributesStream(categoryContainer),
168 taxonomyWriter, new DefaultFacetIndexingParams());
169 stream.addRetainableProperty(DummyProperty.class);
171 MyCategoryListTokenizer tokenizer = new MyCategoryListTokenizer(stream,
176 while (tokenizer.incrementToken()) {
177 if (stream.categoryAttribute.getProperty(DummyProperty.class) != null) {
181 } catch (IOException e) {
182 fail("Properties retained after stream closed");
184 assertEquals("Wrong number of tokens with attributes", 3, nAttributes);
186 taxonomyWriter.close();
190 private final class MyCategoryListTokenizer extends CategoryListTokenizer {
192 public MyCategoryListTokenizer(TokenStream input,
193 FacetIndexingParams indexingParams) {
194 super(input, indexingParams);
198 public boolean incrementToken() throws IOException {
199 if (input.incrementToken()) {
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");