1 package org.apache.lucene.analysis.path;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.StringReader;
22 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
23 import org.apache.lucene.analysis.CharStream;
24 import org.apache.lucene.analysis.MappingCharFilter;
25 import org.apache.lucene.analysis.NormalizeCharMap;
27 public class TestPathHierarchyTokenizer extends BaseTokenStreamTestCase {
29 public void testBasic() throws Exception {
30 String path = "/a/b/c";
31 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
32 assertTokenStreamContents(t,
33 new String[]{"/a", "/a/b", "/a/b/c"},
40 public void testEndOfDelimiter() throws Exception {
41 String path = "/a/b/c/";
42 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
43 assertTokenStreamContents(t,
44 new String[]{"/a", "/a/b", "/a/b/c", "/a/b/c/"},
45 new int[]{0, 0, 0, 0},
46 new int[]{2, 4, 6, 7},
47 new int[]{1, 0, 0, 0},
51 public void testStartOfChar() throws Exception {
52 String path = "a/b/c";
53 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
54 assertTokenStreamContents(t,
55 new String[]{"a", "a/b", "a/b/c"},
62 public void testStartOfCharEndOfDelimiter() throws Exception {
63 String path = "a/b/c/";
64 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
65 assertTokenStreamContents(t,
66 new String[]{"a", "a/b", "a/b/c", "a/b/c/"},
67 new int[]{0, 0, 0, 0},
68 new int[]{1, 3, 5, 6},
69 new int[]{1, 0, 0, 0},
73 public void testOnlyDelimiter() throws Exception {
75 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
76 assertTokenStreamContents(t,
84 public void testOnlyDelimiters() throws Exception {
86 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
87 assertTokenStreamContents(t,
88 new String[]{"/", "//"},
95 public void testReplace() throws Exception {
96 String path = "/a/b/c";
97 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), '/', '\\' );
98 assertTokenStreamContents(t,
99 new String[]{"\\a", "\\a\\b", "\\a\\b\\c"},
106 public void testWindowsPath() throws Exception {
107 String path = "c:\\a\\b\\c";
108 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), '\\', '\\' );
109 assertTokenStreamContents(t,
110 new String[]{"c:", "c:\\a", "c:\\a\\b", "c:\\a\\b\\c"},
111 new int[]{0, 0, 0, 0},
112 new int[]{2, 4, 6, 8},
113 new int[]{1, 0, 0, 0},
117 public void testNormalizeWinDelimToLinuxDelim() throws Exception {
118 NormalizeCharMap normMap = new NormalizeCharMap();
119 normMap.add("\\", "/");
120 String path = "c:\\a\\b\\c";
121 CharStream cs = new MappingCharFilter(normMap, new StringReader(path));
122 PathHierarchyTokenizer t = new PathHierarchyTokenizer( cs );
123 assertTokenStreamContents(t,
124 new String[]{"c:", "c:/a", "c:/a/b", "c:/a/b/c"},
125 new int[]{0, 0, 0, 0},
126 new int[]{2, 4, 6, 8},
127 new int[]{1, 0, 0, 0},
131 public void testBasicSkip() throws Exception {
132 String path = "/a/b/c";
133 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
134 assertTokenStreamContents(t,
135 new String[]{"/b", "/b/c"},
142 public void testEndOfDelimiterSkip() throws Exception {
143 String path = "/a/b/c/";
144 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
145 assertTokenStreamContents(t,
146 new String[]{"/b", "/b/c", "/b/c/"},
153 public void testStartOfCharSkip() throws Exception {
154 String path = "a/b/c";
155 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
156 assertTokenStreamContents(t,
157 new String[]{"/b", "/b/c"},
164 public void testStartOfCharEndOfDelimiterSkip() throws Exception {
165 String path = "a/b/c/";
166 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
167 assertTokenStreamContents(t,
168 new String[]{"/b", "/b/c", "/b/c/"},
175 public void testOnlyDelimiterSkip() throws Exception {
177 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
178 assertTokenStreamContents(t,
186 public void testOnlyDelimitersSkip() throws Exception {
188 PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
189 assertTokenStreamContents(t,