Skip to content

Commit d3442f7

Browse files
authored
Add unit test for PathHierarchyTokenizerFactory (#24984)
1 parent a9014df commit d3442f7

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.analysis;
21+
22+
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
23+
24+
import org.apache.lucene.analysis.Tokenizer;
25+
import org.elasticsearch.common.settings.Settings;
26+
import org.elasticsearch.index.Index;
27+
import org.elasticsearch.test.ESTokenStreamTestCase;
28+
import org.elasticsearch.test.IndexSettingsModule;
29+
30+
import java.io.IOException;
31+
import java.io.StringReader;
32+
33+
public class PathHierarchyTokenizerFactoryTests extends ESTokenStreamTestCase {
34+
35+
public void testDefaults() throws IOException {
36+
final Index index = new Index("test", "_na_");
37+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
38+
Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
39+
"path-hierarchy-tokenizer", Settings.EMPTY).create();
40+
tokenizer.setReader(new StringReader("/one/two/three"));
41+
assertTokenStreamContents(tokenizer, new String[] {"/one", "/one/two", "/one/two/three"});
42+
}
43+
44+
public void testReverse() throws IOException {
45+
final Index index = new Index("test", "_na_");
46+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
47+
Settings settings = newAnalysisSettingsBuilder().put("reverse", true).build();
48+
Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
49+
"path-hierarchy-tokenizer", settings).create();
50+
tokenizer.setReader(new StringReader("/one/two/three"));
51+
assertTokenStreamContents(tokenizer, new String[] {"/one/two/three", "one/two/three", "two/three", "three"});
52+
}
53+
54+
public void testDelimiter() throws IOException {
55+
final Index index = new Index("test", "_na_");
56+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
57+
Settings settings = newAnalysisSettingsBuilder().put("delimiter", "-").build();
58+
Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
59+
"path-hierarchy-tokenizer", settings).create();
60+
tokenizer.setReader(new StringReader("/one/two/three"));
61+
assertTokenStreamContents(tokenizer, new String[] {"/one/two/three"});
62+
tokenizer.setReader(new StringReader("one-two-three"));
63+
assertTokenStreamContents(tokenizer, new String[] {"one", "one-two", "one-two-three"});
64+
}
65+
66+
public void testReplace() throws IOException {
67+
final Index index = new Index("test", "_na_");
68+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
69+
Settings settings = newAnalysisSettingsBuilder().put("replacement", "-").build();
70+
Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
71+
"path-hierarchy-tokenizer", settings).create();
72+
tokenizer.setReader(new StringReader("/one/two/three"));
73+
assertTokenStreamContents(tokenizer, new String[] {"-one", "-one-two", "-one-two-three"});
74+
tokenizer.setReader(new StringReader("one-two-three"));
75+
assertTokenStreamContents(tokenizer, new String[] {"one-two-three"});
76+
}
77+
78+
public void testSkip() throws IOException {
79+
final Index index = new Index("test", "_na_");
80+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
81+
Settings settings = newAnalysisSettingsBuilder().put("skip", 2).build();
82+
Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
83+
"path-hierarchy-tokenizer", settings).create();
84+
tokenizer.setReader(new StringReader("/one/two/three/four/five"));
85+
assertTokenStreamContents(tokenizer, new String[] {"/three", "/three/four", "/three/four/five"});
86+
}
87+
88+
public void testDelimiterExceptions() {
89+
final Index index = new Index("test", "_na_");
90+
final Settings indexSettings = newAnalysisSettingsBuilder().build();
91+
{
92+
String delimiter = RandomPicks.randomFrom(random(), new String[] {"--", ""});
93+
Settings settings = newAnalysisSettingsBuilder().put("delimiter", delimiter).build();
94+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
95+
() -> new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
96+
"path-hierarchy-tokenizer", settings).create());
97+
assertEquals("delimiter must be a one char value", e.getMessage());
98+
}
99+
{
100+
String replacement = RandomPicks.randomFrom(random(), new String[] {"--", ""});
101+
Settings settings = newAnalysisSettingsBuilder().put("replacement", replacement).build();
102+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
103+
() -> new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null,
104+
"path-hierarchy-tokenizer", settings).create());
105+
assertEquals("replacement must be a one char value", e.getMessage());
106+
}
107+
}
108+
}

test/framework/src/main/java/org/elasticsearch/test/ESTokenStreamTestCase.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.carrotsearch.randomizedtesting.annotations.Listeners;
2323
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
24+
2425
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
2526
import org.apache.lucene.util.LuceneTestCase;
2627
import org.apache.lucene.util.TimeUnits;
@@ -51,10 +52,6 @@ public abstract class ESTokenStreamTestCase extends BaseTokenStreamTestCase {
5152
BootstrapForTesting.ensureInitialized();
5253
}
5354

54-
public static Version randomVersion() {
55-
return VersionUtils.randomVersion(random());
56-
}
57-
5855
public Settings.Builder newAnalysisSettingsBuilder() {
5956
return Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT);
6057
}

0 commit comments

Comments
 (0)