KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
20
21 import java.io.StringReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.HashSet JavaDoc;
25
26 public class TestStopAnalyzer extends TestCase {
27   private StopAnalyzer stop = new StopAnalyzer();
28
29   private Set JavaDoc inValidTokens = new HashSet JavaDoc();
30   public TestStopAnalyzer(String JavaDoc s) {
31     super(s);
32   }
33
34   protected void setUp() {
35     for (int i = 0; i < StopAnalyzer.ENGLISH_STOP_WORDS.length; i++) {
36       inValidTokens.add(StopAnalyzer.ENGLISH_STOP_WORDS[i]);
37     }
38   }
39
40   public void testDefaults() {
41     assertTrue(stop != null);
42     StringReader JavaDoc reader = new StringReader JavaDoc("This is a test of the english stop analyzer");
43     TokenStream stream = stop.tokenStream("test", reader);
44     assertTrue(stream != null);
45     Token token = null;
46     try {
47       while ((token = stream.next()) != null)
48       {
49         assertTrue(inValidTokens.contains(token.termText()) == false);
50       }
51     } catch (IOException JavaDoc e) {
52       assertTrue(false);
53     }
54   }
55
56   public void testStopList() {
57     Set JavaDoc stopWordsSet = new HashSet JavaDoc();
58     stopWordsSet.add("good");
59     stopWordsSet.add("test");
60     stopWordsSet.add("analyzer");
61     StopAnalyzer newStop = new StopAnalyzer((String JavaDoc[])stopWordsSet.toArray(new String JavaDoc[3]));
62     StringReader JavaDoc reader = new StringReader JavaDoc("This is a good test of the english stop analyzer");
63     TokenStream stream = newStop.tokenStream("test", reader);
64     assertTrue(stream != null);
65     Token token = null;
66     try {
67       while ((token = stream.next()) != null)
68       {
69         String JavaDoc text = token.termText();
70         assertTrue(stopWordsSet.contains(text) == false);
71       }
72     } catch (IOException JavaDoc e) {
73       assertTrue(false);
74     }
75   }
76 }
77
Popular Tags