add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestWordlistLoader.java
1 package org.apache.lucene.index;
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.BufferedReader;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.lucene.util.LuceneTestCase;
27
28 import org.apache.lucene.analysis.WordlistLoader;
29
30 public class TestWordlistLoader extends LuceneTestCase {
31
32   public void testWordlistLoading() throws IOException {
33     String s = "ONE\n  two \nthree";
34     HashSet<String> wordSet1 = WordlistLoader.getWordSet(new StringReader(s));
35     checkSet(wordSet1);
36     HashSet<String> wordSet2 = WordlistLoader.getWordSet(new BufferedReader(new StringReader(s)));
37     checkSet(wordSet2);
38   }
39
40   public void testComments() throws Exception {
41     String s = "ONE\n  two \nthree\n#comment";
42     HashSet<String> wordSet1 = WordlistLoader.getWordSet(new StringReader(s), "#");
43     checkSet(wordSet1);
44     assertFalse(wordSet1.contains("#comment"));
45     assertFalse(wordSet1.contains("comment"));
46   }
47
48
49   private void checkSet(HashSet<String> wordset) {
50     assertEquals(3, wordset.size());
51     assertTrue(wordset.contains("ONE"));                // case is not modified
52     assertTrue(wordset.contains("two"));                // surrounding whitespace is removed
53     assertTrue(wordset.contains("three"));
54     assertFalse(wordset.contains("four"));
55   }
56
57   /**
58    * Test stopwords in snowball format
59    */
60   public void testSnowballListLoading() throws IOException {
61     String s = 
62       "|comment\n" + // commented line
63       " |comment\n" + // commented line with leading whitespace
64       "\n" + // blank line
65       "  \t\n" + // line with only whitespace
66       " |comment | comment\n" + // commented line with comment
67       "ONE\n" + // stopword, in uppercase
68       "   two   \n" + // stopword with leading/trailing space
69       " three   four five \n" + // multiple stopwords
70       "six seven | comment\n"; //multiple stopwords + comment
71     Set<String> wordset = WordlistLoader.getSnowballWordSet(new StringReader(s));
72     assertEquals(7, wordset.size());
73     assertTrue(wordset.contains("ONE"));
74     assertTrue(wordset.contains("two"));
75     assertTrue(wordset.contains("three"));
76     assertTrue(wordset.contains("four"));
77     assertTrue(wordset.contains("five"));
78     assertTrue(wordset.contains("six"));
79     assertTrue(wordset.contains("seven"));
80   }
81 }