1
package org.apache.lucene.facet.index.attributes;
3
import java.util.ArrayList;
8
import org.apache.lucene.util.LuceneTestCase;
9
import org.apache.lucene.facet.FacetException;
10
import org.apache.lucene.facet.index.DummyProperty;
11
import org.apache.lucene.facet.index.attributes.CategoryAttribute;
12
import org.apache.lucene.facet.index.attributes.CategoryAttributeImpl;
13
import org.apache.lucene.facet.taxonomy.CategoryPath;
16
* Licensed to the Apache Software Foundation (ASF) under one or more
17
* contributor license agreements. See the NOTICE file distributed with
18
* this work for additional information regarding copyright ownership.
19
* The ASF licenses this file to You under the Apache License, Version 2.0
20
* (the "License"); you may not use this file except in compliance with
21
* the License. You may obtain a copy of the License at
23
* http://www.apache.org/licenses/LICENSE-2.0
25
* Unless required by applicable law or agreed to in writing, software
26
* distributed under the License is distributed on an "AS IS" BASIS,
27
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
* See the License for the specific language governing permissions and
29
* limitations under the License.
32
public class CategoryAttributeImplTest extends LuceneTestCase {
35
public void testCategoryPath() {
36
CategoryAttribute ca = new CategoryAttributeImpl();
38
assertNull("Category Path should be null", ca.getCategoryPath());
40
CategoryPath cp = new CategoryPath("a", "b");
41
ca.setCategoryPath(cp);
43
assertEquals("Wrong Category Path", cp, ca.getCategoryPath());
45
ca.setCategoryPath(null);
46
assertNull("Category Path should be null", ca.getCategoryPath());
48
ca = new CategoryAttributeImpl(cp);
49
assertEquals("Wrong Category Path", cp, ca.getCategoryPath());
53
public void testProperties() throws FacetException {
54
CategoryAttribute ca = new CategoryAttributeImpl();
56
assertNull("Attribute should be null", ca
57
.getProperty(DummyProperty.class));
58
assertNull("Attribute classes should be null", ca.getPropertyClasses());
60
ca.addProperty(new DummyProperty());
61
assertEquals("DummyProperty should be in properties",
62
new DummyProperty(), ca.getProperty(DummyProperty.class));
63
assertEquals("Attribute classes should contain 1 element", 1, ca
64
.getPropertyClasses().size());
66
boolean failed = false;
68
ca.addProperty(new DummyProperty());
69
} catch (UnsupportedOperationException e) {
74
fail("Two DummyAttributes added to the same CategoryAttribute");
78
assertNull("Attribute classes should be null", ca.getPropertyClasses());
80
ca.addProperty(new DummyProperty());
81
assertEquals("DummyProperty should be in properties",
82
new DummyProperty(), ca.getProperty(DummyProperty.class));
83
ca.remove(DummyProperty.class);
84
assertEquals("DummyProperty should not be in properties", null, ca
85
.getProperty(DummyProperty.class));
86
assertNull("Attribute classes should be null", ca.getPropertyClasses());
88
ca.addProperty(new DummyProperty());
89
List<Class<? extends CategoryProperty>> propertyClasses = new ArrayList<Class<? extends CategoryProperty>>();
90
assertEquals("No property expected when no classes given", null, ca
91
.getProperty(propertyClasses));
92
propertyClasses.add(DummyProperty.class);
93
assertEquals("DummyProperty should be in properties",
94
new DummyProperty(), ca.getProperty(propertyClasses));
95
propertyClasses.add(OrdinalProperty.class);
96
assertEquals("DummyProperty should be in properties",
97
new DummyProperty(), ca.getProperty(propertyClasses));
98
propertyClasses.clear();
99
propertyClasses.add(OrdinalProperty.class);
100
assertEquals("No ordinal property expected", null, ca
101
.getProperty(propertyClasses));
105
public void testCloneCopyToAndSet() throws FacetException {
106
CategoryAttributeImpl ca1 = new CategoryAttributeImpl();
108
CategoryPath cp = new CategoryPath("a", "b");
109
ca1.setCategoryPath(cp);
110
ca1.addProperty(new DummyProperty());
112
CategoryAttribute ca2 = ca1.clone();
113
assertEquals("Error in cloning", ca1, ca2);
115
CategoryAttributeImpl ca3 = new CategoryAttributeImpl();
116
assertNotSame("Should not be the same", ca1, ca3);
118
assertEquals("Error in cloning", ca1, ca3);
120
ca2.setCategoryPath(null);
121
assertNotSame("Should not be the same", ca1, ca2);
123
assertEquals("Error in cloning", ca1, ca2);