add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / index / categorypolicy / OrdinalPolicyTest.java
1 package org.apache.lucene.facet.index.categorypolicy;
2
3 import org.apache.lucene.store.Directory;
4 import org.junit.Test;
5
6 import org.apache.lucene.util.LuceneTestCase;
7 import org.apache.lucene.facet.index.categorypolicy.DefaultOrdinalPolicy;
8 import org.apache.lucene.facet.index.categorypolicy.NonTopLevelOrdinalPolicy;
9 import org.apache.lucene.facet.index.categorypolicy.OrdinalPolicy;
10 import org.apache.lucene.facet.taxonomy.CategoryPath;
11 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
12 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
13 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
14
15 /**
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
22  *
23  *     http://www.apache.org/licenses/LICENSE-2.0
24  *
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.
30  */
31
32 public class OrdinalPolicyTest extends LuceneTestCase {
33
34   @Test
35   public void testDefaultOrdinalPolicy() {
36     // check ordinal policy
37     OrdinalPolicy ordinalPolicy = new DefaultOrdinalPolicy();
38     assertFalse("default ordinal policy should not match root", ordinalPolicy
39         .shouldAdd(TaxonomyReader.ROOT_ORDINAL));
40     for (int i = 0; i < 300; i++) {
41       int ordinal = 1 + random.nextInt(Integer.MAX_VALUE - 1);
42       assertTrue("default ordinal policy should match " + ordinal,
43           ordinalPolicy.shouldAdd(ordinal));
44     }
45   }
46
47   @Test
48   public void testNonTopLevelOrdinalPolicy() throws Exception {
49     Directory dir = newDirectory();
50     TaxonomyWriter taxonomy = null;
51     taxonomy = new LuceneTaxonomyWriter(dir);
52
53     int[] topLevelOrdinals = new int[10];
54     String[] topLevelStrings = new String[10];
55     for (int i = 0; i < 10; i++) {
56       topLevelStrings[i] = Integer.valueOf(random.nextInt(30)).toString();
57       topLevelOrdinals[i] = taxonomy.addCategory(new CategoryPath(
58           topLevelStrings[i]));
59     }
60     int[] nonTopLevelOrdinals = new int[300];
61     for (int i = 0; i < 300; i++) {
62       int nComponents = 2 + random.nextInt(10);
63       String[] components = new String[nComponents];
64       components[0] = topLevelStrings[i % 10];
65       for (int j = 1; j < components.length; j++) {
66         components[j] = (Integer.valueOf(random.nextInt(30))).toString();
67       }
68       nonTopLevelOrdinals[i] = taxonomy.addCategory(new CategoryPath(
69           components));
70     }
71     // check ordinal policy
72     OrdinalPolicy ordinalPolicy = new NonTopLevelOrdinalPolicy();
73     ordinalPolicy.init(taxonomy);
74     assertFalse("top level ordinal policy should not match root", ordinalPolicy
75         .shouldAdd(TaxonomyReader.ROOT_ORDINAL));
76     for (int i = 0; i < 10; i++) {
77       assertFalse("top level ordinal policy should not match "
78           + topLevelOrdinals[i],
79           ordinalPolicy.shouldAdd(topLevelOrdinals[i]));
80     }
81     for (int i = 0; i < 300; i++) {
82       assertTrue("top level ordinal policy should match "
83           + nonTopLevelOrdinals[i],
84           ordinalPolicy.shouldAdd(nonTopLevelOrdinals[i]));
85     }
86
87     // check illegal ordinal
88     assertFalse("Should not add illegal ordinal", ordinalPolicy.shouldAdd(100000));
89     taxonomy.close();
90     dir.close();
91   }
92
93 }