add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / index / streaming / CategoryAttributesStreamTest.java
1 package org.apache.lucene.facet.index.streaming;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.junit.Test;
9
10 import org.apache.lucene.facet.index.CategoryContainerTestBase;
11 import org.apache.lucene.facet.index.attributes.CategoryAttribute;
12 import org.apache.lucene.facet.index.attributes.CategoryAttributeImpl;
13 import org.apache.lucene.facet.index.streaming.CategoryAttributesStream;
14 import org.apache.lucene.facet.taxonomy.CategoryPath;
15
16 /**
17  * Licensed to the Apache Software Foundation (ASF) under one or more
18  * contributor license agreements.  See the NOTICE file distributed with
19  * this work for additional information regarding copyright ownership.
20  * The ASF licenses this file to You under the Apache License, Version 2.0
21  * (the "License"); you may not use this file except in compliance with
22  * the License.  You may obtain a copy of the License at
23  *
24  *     http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32
33 public class CategoryAttributesStreamTest extends CategoryContainerTestBase {
34
35   /**
36    * Verifies that a {@link CategoryAttributesStream} accepts
37    * {@link CategoryAttribute} and passes them on as tokens.
38    * 
39    * @throws IOException
40    */
41   @Test
42   public void testStream() throws IOException {
43     ArrayList<CategoryAttribute> attributesList = new ArrayList<CategoryAttribute>();
44     for (int i = 0; i < initialCatgeories.length; i++) {
45       attributesList.add(new CategoryAttributeImpl(initialCatgeories[i]));
46     }
47
48     // test number of tokens
49     CategoryAttributesStream stream = new CategoryAttributesStream(
50         attributesList);
51     int nTokens = 0;
52     while (stream.incrementToken()) {
53       nTokens++;
54     }
55     assertEquals("Wrong number of tokens", 3, nTokens);
56
57     // test reset
58     stream.reset();
59     nTokens = 0;
60     while (stream.incrementToken()) {
61       nTokens++;
62     }
63     assertEquals("Wrong number of tokens", 3, nTokens);
64
65     // test reset and contents
66     Set<CategoryPath> pathsSet = new HashSet<CategoryPath>();
67     for (int i = 0; i < initialCatgeories.length; i++) {
68       pathsSet.add(initialCatgeories[i]);
69     }
70     stream.reset();
71     while (stream.incrementToken()) {
72       CategoryAttribute fromStream = stream
73           .getAttribute(CategoryAttribute.class);
74       if (!pathsSet.remove(fromStream.getCategoryPath())) {
75         fail("Unexpected category path: "
76             + fromStream.getCategoryPath().toString(':'));
77       }
78     }
79     assertTrue("all category paths should have been found", pathsSet
80         .isEmpty());
81   }
82 }