1
package org.apache.lucene.facet.index;
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.io.ObjectInputStream;
7
import java.io.ObjectOutputStream;
8
import java.util.Iterator;
10
import org.junit.Test;
12
import org.apache.lucene.facet.FacetException;
13
import org.apache.lucene.facet.enhancements.association.AssociationIntProperty;
14
import org.apache.lucene.facet.enhancements.association.AssociationProperty;
15
import org.apache.lucene.facet.index.CategoryContainer;
16
import org.apache.lucene.facet.index.attributes.CategoryAttribute;
17
import org.apache.lucene.facet.index.attributes.CategoryAttributeImpl;
18
import org.apache.lucene.facet.index.streaming.CategoryAttributesStream;
19
import org.apache.lucene.facet.taxonomy.CategoryPath;
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
29
* http://www.apache.org/licenses/LICENSE-2.0
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.
38
public class CategoryContainerTest extends CategoryContainerTestBase {
41
public void basicTest() {
42
assertEquals("Wrong number of categories in the container", 3,
43
categoryContainer.size());
45
categoryContainer.clear();
46
assertEquals("Container should not contain categories after clear", 0,
47
categoryContainer.size());
51
public void testIterator() throws FacetException {
52
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
54
// count the number of tokens
56
for (nCategories = 0; iterator.hasNext(); nCategories++) {
59
assertEquals("Wrong number of tokens", 3, nCategories);
63
public void testExistingNewCategoryWithProperty() throws FacetException {
64
categoryContainer.addCategory(new CategoryPath("five", "six"),
66
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
68
// count the number of tokens, and check there is one DummyAttribute
71
for (nCategories = 0; iterator.hasNext(); nCategories++) {
72
CategoryAttribute attribute = iterator.next();
73
if (attribute.getProperty(DummyProperty.class) != null) {
77
assertEquals("Wrong number of tokens", 3, nCategories);
78
assertEquals("Wrong number of tokens with properties", 1, nProperties);
82
public void testMultipleCategoriesWithProperties() throws FacetException {
83
AssociationProperty associationProperty = new AssociationIntProperty(
85
categoryContainer.addCategory(new CategoryPath("five", "six"),
86
new DummyProperty(), associationProperty);
87
categoryContainer.addCategory(new CategoryPath("seven", "eight"),
89
associationProperty = new AssociationIntProperty(123);
90
categoryContainer.addCategory(new CategoryPath("nine"),
91
associationProperty, new DummyProperty());
92
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
94
// count the number of tokens, and check there is one DummyAttribute
96
int nDummyAttributes = 0;
97
int nAssocAttributes = 0;
98
for (nCategories = 0; iterator.hasNext(); nCategories++) {
99
CategoryAttribute attribute = iterator.next();
100
if (attribute.getProperty(DummyProperty.class) != null) {
103
if (attribute.getProperty(AssociationIntProperty.class) != null) {
107
assertEquals("Wrong number of tokens", 5, nCategories);
108
assertEquals("Wrong number of tokens with dummy properties", 3,
110
assertEquals("Wrong number of tokens with association properties", 2,
115
public void testAddNewCategoryWithProperty() throws FacetException {
116
categoryContainer.addCategory(new CategoryPath("seven", "eight"),
117
new DummyProperty());
118
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
120
// count the number of tokens, and check there is one DummyAttribute
123
for (nCategories = 0; iterator.hasNext(); nCategories++) {
124
CategoryAttribute attribute = iterator.next();
125
if (attribute.getProperty(DummyProperty.class) != null) {
129
assertEquals("Wrong number of tokens", 4, nCategories);
130
assertEquals("Wrong number of tokens with properties", 1, nProperties);
134
* Test addition of {@link CategoryAttribute} object without properties to a
135
* {@link CategoryContainer}.
137
* @throws FacetException
140
public void testAddCategoryAttributeWithoutProperties()
141
throws FacetException {
142
CategoryAttribute newCA = new CategoryAttributeImpl(new CategoryPath(
144
categoryContainer.addCategory(newCA);
148
* Test addition of {@link CategoryAttribute} object with property to a
149
* {@link CategoryContainer}.
151
* @throws FacetException
154
public void testAddCategoryAttributeWithProperty() throws FacetException {
155
CategoryAttribute newCA = new CategoryAttributeImpl(new CategoryPath(
157
newCA.addProperty(new DummyProperty());
158
categoryContainer.addCategory(newCA);
159
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
161
// count the number of tokens, and check there is one DummyAttribute
164
for (nCategories = 0; iterator.hasNext(); nCategories++) {
165
CategoryAttribute attribute = iterator.next();
166
if (attribute.getProperty(DummyProperty.class) != null) {
170
assertEquals("Wrong number of tokens", 4, nCategories);
171
assertEquals("Wrong number of tokens with properties", 1, nProperties);
175
* Verifies that a {@link CategoryAttributesStream} can be constructed from
176
* {@link CategoryContainer} and produce the correct number of tokens.
178
* @throws IOException
181
public void testCategoryAttributesStream() throws IOException {
182
CategoryAttributesStream stream = new CategoryAttributesStream(
184
// count the number of tokens
186
for (nTokens = 0; stream.incrementToken(); nTokens++) {
188
assertEquals("Wrong number of tokens", 3, nTokens);
192
* Test that {@link CategoryContainer} merges properties.
194
* @throws FacetException
197
public void testCategoryAttributeMerge() throws FacetException {
198
categoryContainer.addCategory(initialCatgeories[0],
199
new AssociationIntProperty(2));
200
categoryContainer.addCategory(initialCatgeories[0],
201
new AssociationIntProperty(15));
203
Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
206
int nAssociations = 0;
207
for (nCategories = 0; iterator.hasNext(); nCategories++) {
208
CategoryAttribute ca = iterator.next();
209
AssociationProperty aa = (AssociationProperty) ca
210
.getProperty(AssociationIntProperty.class);
212
assertEquals("Wrong association value", 17, aa.getAssociation());
216
assertEquals("Wrong number of tokens", 3, nCategories);
217
assertEquals("Wrong number of tokens with associations", 1,
222
public void testSerialization() throws Exception {
223
AssociationProperty associationProperty = new AssociationIntProperty(
225
categoryContainer.addCategory(new CategoryPath("five", "six"),
226
new DummyProperty(), associationProperty);
227
categoryContainer.addCategory(new CategoryPath("seven", "eight"),
228
new DummyProperty());
229
associationProperty = new AssociationIntProperty(123);
230
categoryContainer.addCategory(new CategoryPath("nine"),
231
associationProperty, new DummyProperty());
233
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
234
ObjectOutputStream out = new ObjectOutputStream(baos);
235
out.writeObject(categoryContainer);
238
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
239
ObjectInputStream in = new ObjectInputStream(bais);
241
"Original and deserialized CategoryContainer are different",
242
categoryContainer, in.readObject());
b'\\ No newline at end of file'