16
16
*/
17
17
package org .apache .lucene .analysis .hunspell ;
18
18
19
+ import java .io .IOException ;
19
20
import java .io .InputStream ;
20
- import java .net .URL ;
21
21
import java .nio .file .Files ;
22
22
import java .nio .file .Path ;
23
+ import java .text .ParseException ;
23
24
import java .util .List ;
24
- import java .util .Objects ;
25
25
import java .util .stream .Collectors ;
26
26
import org .apache .lucene .store .ByteBuffersDirectory ;
27
27
import org .apache .lucene .util .IOUtils ;
28
- import org .junit .Test ;
29
28
30
29
public class SpellCheckerTest extends StemmerTestBase {
31
- @ Test
32
- public void base () throws Exception {
30
+
31
+ public void testBase () throws Exception {
33
32
doTest ("base" );
34
33
}
35
34
36
- @ Test
37
- public void baseUtf () throws Exception {
35
+ public void testBaseUtf () throws Exception {
38
36
doTest ("base_utf" );
39
37
}
40
38
41
- @ Test
42
- public void keepcase () throws Exception {
39
+ public void testKeepcase () throws Exception {
43
40
doTest ("keepcase" );
44
41
}
45
42
46
- @ Test
47
- public void allcaps () throws Exception {
43
+ public void testAllcaps () throws Exception {
48
44
doTest ("allcaps" );
49
45
}
50
46
51
47
public void rep () throws Exception {
52
48
doTest ("rep" );
53
49
}
54
50
55
- @ Test
56
- public void forceUCase () throws Exception {
51
+ public void testForceUCase () throws Exception {
57
52
doTest ("forceucase" );
58
53
}
59
54
60
- @ Test
61
- public void checkSharpS () throws Exception {
55
+ public void testCheckSharpS () throws Exception {
62
56
doTest ("checksharps" );
63
57
}
64
58
65
- @ Test
66
- public void IJ () throws Exception {
59
+ public void testIJ () throws Exception {
67
60
doTest ("IJ" );
68
61
}
69
62
70
- @ Test
71
- public void i53643_numbersWithSeparators () throws Exception {
63
+ public void testI53643_numbersWithSeparators () throws Exception {
72
64
doTest ("i53643" );
73
65
}
74
66
75
- @ Test
76
- public void dotless_i () throws Exception {
67
+ public void testDotless_i () throws Exception {
77
68
doTest ("dotless_i" );
78
69
}
79
70
80
- @ Test
81
- public void needAffixOnAffixes () throws Exception {
71
+ public void testNeedAffixOnAffixes () throws Exception {
82
72
doTest ("needaffix5" );
83
73
}
84
74
85
- @ Test
86
- public void compoundFlag () throws Exception {
75
+ public void testCompoundFlag () throws Exception {
87
76
doTest ("compoundflag" );
88
77
}
89
78
90
- @ Test
91
- public void checkCompoundCase () throws Exception {
79
+ public void testCheckCompoundCase () throws Exception {
92
80
doTest ("checkcompoundcase" );
93
81
}
94
82
95
- @ Test
96
- public void checkCompoundDup () throws Exception {
83
+ public void testCheckCompoundDup () throws Exception {
97
84
doTest ("checkcompounddup" );
98
85
}
99
86
100
- @ Test
101
- public void checkCompoundTriple () throws Exception {
87
+ public void testCheckCompoundTriple () throws Exception {
102
88
doTest ("checkcompoundtriple" );
103
89
}
104
90
105
- @ Test
106
- public void simplifiedTriple () throws Exception {
91
+ public void testSimplifiedTriple () throws Exception {
107
92
doTest ("simplifiedtriple" );
108
93
}
109
94
110
- @ Test
111
- public void compoundForbid () throws Exception {
95
+ public void testCompoundForbid () throws Exception {
112
96
doTest ("compoundforbid" );
113
97
}
114
98
@@ -161,10 +145,14 @@ public void testGermanCompounding() throws Exception {
161
145
}
162
146
163
147
protected void doTest (String name ) throws Exception {
164
- InputStream affixStream =
165
- Objects .requireNonNull (getClass ().getResourceAsStream (name + ".aff" ), name );
166
- InputStream dictStream =
167
- Objects .requireNonNull (getClass ().getResourceAsStream (name + ".dic" ), name );
148
+ checkSpellCheckerExpectations (
149
+ Path .of (getClass ().getResource (name + ".aff" ).toURI ()).getParent ().resolve (name ), true );
150
+ }
151
+
152
+ static void checkSpellCheckerExpectations (Path basePath , boolean checkSuggestions )
153
+ throws IOException , ParseException {
154
+ InputStream affixStream = Files .newInputStream (Path .of (basePath .toString () + ".aff" ));
155
+ InputStream dictStream = Files .newInputStream (Path .of (basePath .toString () + ".dic" ));
168
156
169
157
SpellChecker speller ;
170
158
try {
@@ -176,30 +164,30 @@ protected void doTest(String name) throws Exception {
176
164
IOUtils .closeWhileHandlingException (dictStream );
177
165
}
178
166
179
- URL good = StemmerTestBase . class . getResource ( name + ".good" );
180
- if (good != null ) {
181
- for (String word : Files .readAllLines (Path . of ( good . toURI ()) )) {
182
- assertTrue ("Unexpectedly considered misspelled: " + word , speller .spell (word ));
167
+ Path good = Path . of ( basePath + ".good" );
168
+ if (Files . exists ( good ) ) {
169
+ for (String word : Files .readAllLines (good )) {
170
+ assertTrue ("Unexpectedly considered misspelled: " + word , speller .spell (word . trim () ));
183
171
}
184
172
}
185
173
186
- URL wrong = StemmerTestBase . class . getResource ( name + ".wrong" );
187
- URL sug = StemmerTestBase . class . getResource ( name + ".sug" );
188
- if (wrong != null ) {
189
- List <String > wrongWords = Files .readAllLines (Path . of ( wrong . toURI ()) );
174
+ Path wrong = Path . of ( basePath + ".wrong" );
175
+ Path sug = Path . of ( basePath + ".sug" );
176
+ if (Files . exists ( wrong ) ) {
177
+ List <String > wrongWords = Files .readAllLines (wrong );
190
178
for (String word : wrongWords ) {
191
- assertFalse ("Unexpectedly considered correct: " + word , speller .spell (word ));
179
+ assertFalse ("Unexpectedly considered correct: " + word , speller .spell (word . trim () ));
192
180
}
193
- if (sug != null ) {
181
+ if (Files . exists ( sug ) && checkSuggestions ) {
194
182
String suggestions =
195
183
wrongWords .stream ()
196
184
.map (s -> String .join (", " , speller .suggest (s )))
197
185
.filter (s -> !s .isEmpty ())
198
186
.collect (Collectors .joining ("\n " ));
199
- assertEquals (Files .readString (Path . of ( sug . toURI ()) ).trim (), suggestions );
187
+ assertEquals (Files .readString (sug ).trim (), suggestions );
200
188
}
201
189
} else {
202
- assertNull (".sug file without .wrong file!" , sug );
190
+ assertFalse (".sug file without .wrong file!" , Files . exists ( sug ) );
203
191
}
204
192
}
205
193
}
0 commit comments