add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / ngram / EdgeNGramTokenizerTest.java
1 package org.apache.lucene.analysis.ngram;
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
21 import java.io.StringReader;
22
23 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
24
25 /**
26  * Tests {@link EdgeNGramTokenizer} for correctness.
27  */
28 public class EdgeNGramTokenizerTest extends BaseTokenStreamTestCase {
29   private StringReader input;
30
31   @Override
32   public void setUp() throws Exception {
33     super.setUp();
34     input = new StringReader("abcde");
35   }
36
37   public void testInvalidInput() throws Exception {
38     boolean gotException = false;
39     try {        
40       new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 0, 0);
41     } catch (IllegalArgumentException e) {
42       gotException = true;
43     }
44     assertTrue(gotException);
45   }
46
47   public void testInvalidInput2() throws Exception {
48     boolean gotException = false;
49     try {        
50       new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 2, 1);
51     } catch (IllegalArgumentException e) {
52       gotException = true;
53     }
54     assertTrue(gotException);
55   }
56
57   public void testInvalidInput3() throws Exception {
58     boolean gotException = false;
59     try {        
60       new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, -1, 2);
61     } catch (IllegalArgumentException e) {
62       gotException = true;
63     }
64     assertTrue(gotException);
65   }
66
67   public void testFrontUnigram() throws Exception {
68     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 1);
69     assertTokenStreamContents(tokenizer, new String[]{"a"}, new int[]{0}, new int[]{1}, 5 /* abcde */);
70   }
71
72   public void testBackUnigram() throws Exception {
73     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.BACK, 1, 1);
74     assertTokenStreamContents(tokenizer, new String[]{"e"}, new int[]{4}, new int[]{5}, 5 /* abcde */);
75   }
76
77   public void testOversizedNgrams() throws Exception {
78     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 6, 6);
79     assertTokenStreamContents(tokenizer, new String[0], new int[0], new int[0], 5 /* abcde */);
80   }
81
82   public void testFrontRangeOfNgrams() throws Exception {
83     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 3);
84     assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);
85   }
86
87   public void testBackRangeOfNgrams() throws Exception {
88     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.BACK, 1, 3);
89     assertTokenStreamContents(tokenizer, new String[]{"e","de","cde"}, new int[]{4,3,2}, new int[]{5,5,5}, 5 /* abcde */);
90   }
91   
92   public void testReset() throws Exception {
93     EdgeNGramTokenizer tokenizer = new EdgeNGramTokenizer(input, EdgeNGramTokenizer.Side.FRONT, 1, 3);
94     assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);
95     tokenizer.reset(new StringReader("abcde"));
96     assertTokenStreamContents(tokenizer, new String[]{"a","ab","abc"}, new int[]{0,0,0}, new int[]{1,2,3}, 5 /* abcde */);
97   }
98 }