1
package org.apache.lucene.facet.index.categorypolicy;
3
import org.apache.lucene.store.Directory;
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;
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
22
* http://www.apache.org/licenses/LICENSE-2.0
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.
31
public class PathPolicyTest extends LuceneTestCase {
34
public void testDefaultPathPolicy() {
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();
46
cp = new CategoryPath(components);
47
assertTrue("default path policy should accept "
49
pathPolicy.shouldAdd(cp));
54
public void testNonTopLevelPathPolicy() throws Exception {
55
Directory dir = newDirectory();
56
TaxonomyWriter taxonomy = null;
57
taxonomy = new DirectoryTaxonomyWriter(dir);
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();
64
topLevelPaths[i] = new CategoryPath(topLevelStrings[i]);
65
taxonomy.addCategory(topLevelPaths[i]);
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();
75
nonTopLevelPaths[i] = new CategoryPath(components);
76
taxonomy.addCategory(nonTopLevelPaths[i]);
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 "
85
pathPolicy.shouldAdd(topLevelPaths[i]));
87
for (int i = 0; i < 300; i++) {
88
assertTrue("top level path policy should match "
89
+ nonTopLevelPaths[i],
90
pathPolicy.shouldAdd(nonTopLevelPaths[i]));