pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / index / categorypolicy / PathPolicyTest.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.DefaultPathPolicy;
8 import org.apache.lucene.facet.index.categorypolicy.NonTopLevelPathPolicy;
9 import org.apache.lucene.facet.index.categorypolicy.PathPolicy;
10 import org.apache.lucene.facet.taxonomy.CategoryPath;
11 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
12 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
13
14 /**
15  * Licensed to the Apache Software Foundation (ASF) under one or more
16  * contributor license agreements.  See the NOTICE file distributed with
17  * this work for additional information regarding copyright ownership.
18  * The ASF licenses this file to You under the Apache License, Version 2.0
19  * (the "License"); you may not use this file except in compliance with
20  * the License.  You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30
31 public class PathPolicyTest extends LuceneTestCase {
32
33   @Test
34   public void testDefaultPathPolicy() {
35     // check path policy
36     CategoryPath cp = new CategoryPath();
37     PathPolicy pathPolicy = new DefaultPathPolicy();
38     assertFalse("default path policy should not accept root", 
39         pathPolicy.shouldAdd(cp));
40     for (int i = 0; i < 300; i++) {
41       int nComponents = 1 + random.nextInt(10);
42       String[] components = new String[nComponents];
43       for (int j = 0; j < components.length; j++) {
44         components[j] = (Integer.valueOf(random.nextInt(30))).toString();
45       }
46       cp = new CategoryPath(components);
47       assertTrue("default path policy should accept "
48           + cp.toString('/'),
49           pathPolicy.shouldAdd(cp));
50     }
51   }
52
53   @Test
54   public void testNonTopLevelPathPolicy() throws Exception {
55     Directory dir = newDirectory();
56     TaxonomyWriter taxonomy = null;
57     taxonomy = new DirectoryTaxonomyWriter(dir);
58
59     CategoryPath[] topLevelPaths = new CategoryPath[10];
60     String[] topLevelStrings = new String[10];
61     for (int i = 0; i < 10; i++) {
62       topLevelStrings[i] = Integer.valueOf(random.nextInt(30)).toString();
63
64       topLevelPaths[i] = new CategoryPath(topLevelStrings[i]);
65       taxonomy.addCategory(topLevelPaths[i]);
66     }
67     CategoryPath[] nonTopLevelPaths = new CategoryPath[300];
68     for (int i = 0; i < 300; i++) {
69       int nComponents = 2 + random.nextInt(10);
70       String[] components = new String[nComponents];
71       components[0] = topLevelStrings[i % 10];
72       for (int j = 1; j < components.length; j++) {
73         components[j] = (Integer.valueOf(random.nextInt(30))).toString();
74       }
75       nonTopLevelPaths[i] = new CategoryPath(components);
76       taxonomy.addCategory(nonTopLevelPaths[i]);
77     }
78     // check ordinal policy
79     PathPolicy pathPolicy = new NonTopLevelPathPolicy();
80     assertFalse("top level path policy should not match root",
81         pathPolicy.shouldAdd(new CategoryPath()));
82     for (int i = 0; i < 10; i++) {
83       assertFalse("top level path policy should not match "
84           + topLevelPaths[i],
85           pathPolicy.shouldAdd(topLevelPaths[i]));
86     }
87     for (int i = 0; i < 300; i++) {
88       assertTrue("top level path policy should match "
89           + nonTopLevelPaths[i],
90           pathPolicy.shouldAdd(nonTopLevelPaths[i]));
91     }
92     taxonomy.close();
93     dir.close();
94   }
95 }