KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > TestAnalyzers


1 package org.apache.lucene.analysis;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.*;
20 import junit.framework.*;
21
22 import org.apache.lucene.*;
23 import org.apache.lucene.analysis.*;
24
25 public class TestAnalyzers extends TestCase {
26
27    public TestAnalyzers(String JavaDoc name) {
28       super(name);
29    }
30
31   public void assertAnalyzesTo(Analyzer a,
32                                String JavaDoc input,
33                                String JavaDoc[] output) throws Exception JavaDoc {
34     TokenStream ts = a.tokenStream("dummy", new StringReader(input));
35     for (int i=0; i<output.length; i++) {
36       Token t = ts.next();
37       assertNotNull(t);
38       assertEquals(t.termText(), output[i]);
39     }
40     assertNull(ts.next());
41     ts.close();
42   }
43
44   public void testSimple() throws Exception JavaDoc {
45     Analyzer a = new SimpleAnalyzer();
46     assertAnalyzesTo(a, "foo bar FOO BAR",
47                      new String JavaDoc[] { "foo", "bar", "foo", "bar" });
48     assertAnalyzesTo(a, "foo bar . FOO <> BAR",
49                      new String JavaDoc[] { "foo", "bar", "foo", "bar" });
50     assertAnalyzesTo(a, "foo.bar.FOO.BAR",
51                      new String JavaDoc[] { "foo", "bar", "foo", "bar" });
52     assertAnalyzesTo(a, "U.S.A.",
53                      new String JavaDoc[] { "u", "s", "a" });
54     assertAnalyzesTo(a, "C++",
55                      new String JavaDoc[] { "c" });
56     assertAnalyzesTo(a, "B2B",
57                      new String JavaDoc[] { "b", "b" });
58     assertAnalyzesTo(a, "2B",
59                      new String JavaDoc[] { "b" });
60     assertAnalyzesTo(a, "\"QUOTED\" word",
61                      new String JavaDoc[] { "quoted", "word" });
62   }
63
64   public void testNull() throws Exception JavaDoc {
65     Analyzer a = new WhitespaceAnalyzer();
66     assertAnalyzesTo(a, "foo bar FOO BAR",
67                      new String JavaDoc[] { "foo", "bar", "FOO", "BAR" });
68     assertAnalyzesTo(a, "foo bar . FOO <> BAR",
69                      new String JavaDoc[] { "foo", "bar", ".", "FOO", "<>", "BAR" });
70     assertAnalyzesTo(a, "foo.bar.FOO.BAR",
71                      new String JavaDoc[] { "foo.bar.FOO.BAR" });
72     assertAnalyzesTo(a, "U.S.A.",
73                      new String JavaDoc[] { "U.S.A." });
74     assertAnalyzesTo(a, "C++",
75                      new String JavaDoc[] { "C++" });
76     assertAnalyzesTo(a, "B2B",
77                      new String JavaDoc[] { "B2B" });
78     assertAnalyzesTo(a, "2B",
79                      new String JavaDoc[] { "2B" });
80     assertAnalyzesTo(a, "\"QUOTED\" word",
81                      new String JavaDoc[] { "\"QUOTED\"", "word" });
82   }
83
84   public void testStop() throws Exception JavaDoc {
85     Analyzer a = new StopAnalyzer();
86     assertAnalyzesTo(a, "foo bar FOO BAR",
87                      new String JavaDoc[] { "foo", "bar", "foo", "bar" });
88     assertAnalyzesTo(a, "foo a bar such FOO THESE BAR",
89                      new String JavaDoc[] { "foo", "bar", "foo", "bar" });
90   }
91 }
92
93
Popular Tags