add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / analysis / TestMockAnalyzer.java
1 package org.apache.lucene.analysis;
2
3 import java.io.StringReader;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 public class TestMockAnalyzer extends BaseTokenStreamTestCase {
23
24   /** Test a configuration that behaves a lot like WhitespaceAnalyzer */
25   public void testWhitespace() throws Exception {
26     Analyzer a = new MockAnalyzer(random);
27     assertAnalyzesTo(a, "A bc defg hiJklmn opqrstuv wxy z ",
28         new String[] { "a", "bc", "defg", "hijklmn", "opqrstuv", "wxy", "z" });
29     assertAnalyzesToReuse(a, "aba cadaba shazam",
30         new String[] { "aba", "cadaba", "shazam" });
31     assertAnalyzesToReuse(a, "break on whitespace",
32         new String[] { "break", "on", "whitespace" });
33   }
34   
35   /** Test a configuration that behaves a lot like SimpleAnalyzer */
36   public void testSimple() throws Exception {
37     Analyzer a = new MockAnalyzer(random, MockTokenizer.SIMPLE, true);
38     assertAnalyzesTo(a, "a-bc123 defg+hijklmn567opqrstuv78wxy_z ",
39         new String[] { "a", "bc", "defg", "hijklmn", "opqrstuv", "wxy", "z" });
40     assertAnalyzesToReuse(a, "aba4cadaba-Shazam",
41         new String[] { "aba", "cadaba", "shazam" });
42     assertAnalyzesToReuse(a, "break+on/Letters",
43         new String[] { "break", "on", "letters" });
44   }
45   
46   /** Test a configuration that behaves a lot like KeywordAnalyzer */
47   public void testKeyword() throws Exception {
48     Analyzer a = new MockAnalyzer(random, MockTokenizer.KEYWORD, false);
49     assertAnalyzesTo(a, "a-bc123 defg+hijklmn567opqrstuv78wxy_z ",
50         new String[] { "a-bc123 defg+hijklmn567opqrstuv78wxy_z " });
51     assertAnalyzesToReuse(a, "aba4cadaba-Shazam",
52         new String[] { "aba4cadaba-Shazam" });
53     assertAnalyzesToReuse(a, "break+on/Nothing",
54         new String[] { "break+on/Nothing" });
55   }
56   
57   /** Test a configuration that behaves a lot like StopAnalyzer */
58   public void testStop() throws Exception {
59     Analyzer a = new MockAnalyzer(random, MockTokenizer.SIMPLE, true, (CharArraySet) StopAnalyzer.ENGLISH_STOP_WORDS_SET, true);
60     assertAnalyzesTo(a, "the quick brown a fox",
61         new String[] { "quick", "brown", "fox" },
62         new int[] { 2, 1, 2 });
63     
64     // disable positions
65     a = new MockAnalyzer(random, MockTokenizer.SIMPLE, true, (CharArraySet) StopAnalyzer.ENGLISH_STOP_WORDS_SET, false);
66     assertAnalyzesTo(a, "the quick brown a fox",
67         new String[] { "quick", "brown", "fox" },
68         new int[] { 1, 1, 1 });
69   }
70   
71   public void testLUCENE_3042() throws Exception {
72     String testString = "t";
73     
74     Analyzer analyzer = new MockAnalyzer(random);
75     TokenStream stream = analyzer.reusableTokenStream("dummy", new StringReader(testString));
76     stream.reset();
77     while (stream.incrementToken()) {
78       // consume
79     }
80     stream.end();
81     stream.close();
82     
83     assertAnalyzesToReuse(analyzer, testString, new String[] { "t" });
84   }
85
86   /** blast some random strings through the analyzer */
87   public void testRandomStrings() throws Exception {
88     checkRandomData(random, new MockAnalyzer(random), atLeast(1000));
89   }
90 }