~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/test/org/apache/lucene/facet/index/CategoryContainerTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.facet.index;
2
 
 
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;
9
 
 
10
 
import org.junit.Test;
11
 
 
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;
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 CategoryContainerTest extends CategoryContainerTestBase {
39
 
 
40
 
  @Test
41
 
  public void basicTest() {
42
 
    assertEquals("Wrong number of categories in the container", 3,
43
 
        categoryContainer.size());
44
 
 
45
 
    categoryContainer.clear();
46
 
    assertEquals("Container should not contain categories after clear", 0,
47
 
        categoryContainer.size());
48
 
  }
49
 
 
50
 
  @Test
51
 
  public void testIterator() throws FacetException {
52
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
53
 
 
54
 
    // count the number of tokens
55
 
    int nCategories;
56
 
    for (nCategories = 0; iterator.hasNext(); nCategories++) {
57
 
      iterator.next();
58
 
    }
59
 
    assertEquals("Wrong number of tokens", 3, nCategories);
60
 
  }
61
 
 
62
 
  @Test
63
 
  public void testExistingNewCategoryWithProperty() throws FacetException {
64
 
    categoryContainer.addCategory(new CategoryPath("five", "six"),
65
 
        new DummyProperty());
66
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
67
 
 
68
 
    // count the number of tokens, and check there is one DummyAttribute
69
 
    int nCategories;
70
 
    int nProperties = 0;
71
 
    for (nCategories = 0; iterator.hasNext(); nCategories++) {
72
 
      CategoryAttribute attribute = iterator.next();
73
 
      if (attribute.getProperty(DummyProperty.class) != null) {
74
 
        nProperties++;
75
 
      }
76
 
    }
77
 
    assertEquals("Wrong number of tokens", 3, nCategories);
78
 
    assertEquals("Wrong number of tokens with properties", 1, nProperties);
79
 
  }
80
 
 
81
 
  @Test
82
 
  public void testMultipleCategoriesWithProperties() throws FacetException {
83
 
    AssociationProperty associationProperty = new AssociationIntProperty(
84
 
        49);
85
 
    categoryContainer.addCategory(new CategoryPath("five", "six"),
86
 
        new DummyProperty(), associationProperty);
87
 
    categoryContainer.addCategory(new CategoryPath("seven", "eight"),
88
 
        new DummyProperty());
89
 
    associationProperty = new AssociationIntProperty(123);
90
 
    categoryContainer.addCategory(new CategoryPath("nine"),
91
 
        associationProperty, new DummyProperty());
92
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
93
 
 
94
 
    // count the number of tokens, and check there is one DummyAttribute
95
 
    int nCategories;
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) {
101
 
        nDummyAttributes++;
102
 
      }
103
 
      if (attribute.getProperty(AssociationIntProperty.class) != null) {
104
 
        nAssocAttributes++;
105
 
      }
106
 
    }
107
 
    assertEquals("Wrong number of tokens", 5, nCategories);
108
 
    assertEquals("Wrong number of tokens with dummy properties", 3,
109
 
        nDummyAttributes);
110
 
    assertEquals("Wrong number of tokens with association properties", 2,
111
 
        nAssocAttributes);
112
 
  }
113
 
 
114
 
  @Test
115
 
  public void testAddNewCategoryWithProperty() throws FacetException {
116
 
    categoryContainer.addCategory(new CategoryPath("seven", "eight"),
117
 
        new DummyProperty());
118
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
119
 
 
120
 
    // count the number of tokens, and check there is one DummyAttribute
121
 
    int nCategories;
122
 
    int nProperties = 0;
123
 
    for (nCategories = 0; iterator.hasNext(); nCategories++) {
124
 
      CategoryAttribute attribute = iterator.next();
125
 
      if (attribute.getProperty(DummyProperty.class) != null) {
126
 
        nProperties++;
127
 
      }
128
 
    }
129
 
    assertEquals("Wrong number of tokens", 4, nCategories);
130
 
    assertEquals("Wrong number of tokens with properties", 1, nProperties);
131
 
  }
132
 
 
133
 
  /**
134
 
   * Test addition of {@link CategoryAttribute} object without properties to a
135
 
   * {@link CategoryContainer}.
136
 
   * 
137
 
   * @throws FacetException
138
 
   */
139
 
  @Test
140
 
  public void testAddCategoryAttributeWithoutProperties()
141
 
      throws FacetException {
142
 
    CategoryAttribute newCA = new CategoryAttributeImpl(new CategoryPath(
143
 
        "seven", "eight"));
144
 
    categoryContainer.addCategory(newCA);
145
 
  }
146
 
 
147
 
  /**
148
 
   * Test addition of {@link CategoryAttribute} object with property to a
149
 
   * {@link CategoryContainer}.
150
 
   * 
151
 
   * @throws FacetException
152
 
   */
153
 
  @Test
154
 
  public void testAddCategoryAttributeWithProperty() throws FacetException {
155
 
    CategoryAttribute newCA = new CategoryAttributeImpl(new CategoryPath(
156
 
        "seven", "eight"));
157
 
    newCA.addProperty(new DummyProperty());
158
 
    categoryContainer.addCategory(newCA);
159
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
160
 
 
161
 
    // count the number of tokens, and check there is one DummyAttribute
162
 
    int nCategories;
163
 
    int nProperties = 0;
164
 
    for (nCategories = 0; iterator.hasNext(); nCategories++) {
165
 
      CategoryAttribute attribute = iterator.next();
166
 
      if (attribute.getProperty(DummyProperty.class) != null) {
167
 
        nProperties++;
168
 
      }
169
 
    }
170
 
    assertEquals("Wrong number of tokens", 4, nCategories);
171
 
    assertEquals("Wrong number of tokens with properties", 1, nProperties);
172
 
  }
173
 
 
174
 
  /**
175
 
   * Verifies that a {@link CategoryAttributesStream} can be constructed from
176
 
   * {@link CategoryContainer} and produce the correct number of tokens.
177
 
   * 
178
 
   * @throws IOException
179
 
   */
180
 
  @Test
181
 
  public void testCategoryAttributesStream() throws IOException {
182
 
    CategoryAttributesStream stream = new CategoryAttributesStream(
183
 
        categoryContainer);
184
 
    // count the number of tokens
185
 
    int nTokens;
186
 
    for (nTokens = 0; stream.incrementToken(); nTokens++) {
187
 
    }
188
 
    assertEquals("Wrong number of tokens", 3, nTokens);
189
 
  }
190
 
 
191
 
  /**
192
 
   * Test that {@link CategoryContainer} merges properties.
193
 
   * 
194
 
   * @throws FacetException
195
 
   */
196
 
  @Test
197
 
  public void testCategoryAttributeMerge() throws FacetException {
198
 
    categoryContainer.addCategory(initialCatgeories[0],
199
 
        new AssociationIntProperty(2));
200
 
    categoryContainer.addCategory(initialCatgeories[0],
201
 
        new AssociationIntProperty(15));
202
 
 
203
 
    Iterator<CategoryAttribute> iterator = categoryContainer.iterator();
204
 
 
205
 
    int nCategories;
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);
211
 
      if (aa != null) {
212
 
        assertEquals("Wrong association value", 17, aa.getAssociation());
213
 
        nAssociations++;
214
 
      }
215
 
    }
216
 
    assertEquals("Wrong number of tokens", 3, nCategories);
217
 
    assertEquals("Wrong number of tokens with associations", 1,
218
 
        nAssociations);
219
 
  }
220
 
  
221
 
  @Test
222
 
  public void testSerialization() throws Exception {
223
 
    AssociationProperty associationProperty = new AssociationIntProperty(
224
 
        49);
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());
232
 
    
233
 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
234
 
    ObjectOutputStream out = new ObjectOutputStream(baos);
235
 
    out.writeObject(categoryContainer);
236
 
    out.close();
237
 
    
238
 
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
239
 
    ObjectInputStream in = new ObjectInputStream(bais);
240
 
    assertEquals(
241
 
        "Original and deserialized CategoryContainer are different",
242
 
        categoryContainer, in.readObject());
243
 
  }
244
 
}
 
 
b'\\ No newline at end of file'