pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / path / TestPathHierarchyTokenizer.java
1 package org.apache.lucene.analysis.path;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.StringReader;
21
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;
26
27 public class TestPathHierarchyTokenizer extends BaseTokenStreamTestCase {
28
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"},
34         new int[]{0, 0, 0},
35         new int[]{2, 4, 6},
36         new int[]{1, 0, 0},
37         path.length());
38   }
39
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},
48         path.length());
49   }
50
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"},
56         new int[]{0, 0, 0},
57         new int[]{1, 3, 5},
58         new int[]{1, 0, 0},
59         path.length());
60   }
61
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},
70         path.length());
71   }
72
73   public void testOnlyDelimiter() throws Exception {
74     String path = "/";
75     PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
76     assertTokenStreamContents(t,
77         new String[]{"/"},
78         new int[]{0},
79         new int[]{1},
80         new int[]{1},
81         path.length());
82   }
83
84   public void testOnlyDelimiters() throws Exception {
85     String path = "//";
86     PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path) );
87     assertTokenStreamContents(t,
88         new String[]{"/", "//"},
89         new int[]{0, 0},
90         new int[]{1, 2},
91         new int[]{1, 0},
92         path.length());
93   }
94
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"},
100         new int[]{0, 0, 0},
101         new int[]{2, 4, 6},
102         new int[]{1, 0, 0},
103         path.length());
104   }
105
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},
114         path.length());
115   }
116
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},
128         path.length());
129   }
130
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"},
136         new int[]{2, 2},
137         new int[]{4, 6},
138         new int[]{1, 0},
139         path.length());
140   }
141
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/"},
147         new int[]{2, 2, 2},
148         new int[]{4, 6, 7},
149         new int[]{1, 0, 0},
150         path.length());
151   }
152
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"},
158         new int[]{1, 1},
159         new int[]{3, 5},
160         new int[]{1, 0},
161         path.length());
162   }
163
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/"},
169         new int[]{1, 1, 1},
170         new int[]{3, 5, 6},
171         new int[]{1, 0, 0},
172         path.length());
173   }
174
175   public void testOnlyDelimiterSkip() throws Exception {
176     String path = "/";
177     PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
178     assertTokenStreamContents(t,
179         new String[]{},
180         new int[]{},
181         new int[]{},
182         new int[]{},
183         path.length());
184   }
185
186   public void testOnlyDelimitersSkip() throws Exception {
187     String path = "//";
188     PathHierarchyTokenizer t = new PathHierarchyTokenizer( new StringReader(path), 1 );
189     assertTokenStreamContents(t,
190         new String[]{"/"},
191         new int[]{1},
192         new int[]{2},
193         new int[]{1},
194         path.length());
195   }
196 }