Commit f02e284
Improve dictionary processing: fix critical bugs and enhance null-safety (#2958)
* Improve dictionary processing code quality and fix critical bugs
This commit addresses multiple code quality issues and critical bugs in the dictionary processing implementation:
**Critical Bug Fixes (P0):**
- Fix PagingList.removeAll() logic error: method was calling retainAll() instead of removeAll()
- Fix resource leaks in all 6 Updater constructors: FileOutputStream not properly closed on error
- Fix CharMappingItem.equals(): remove state mutation (calling sort()) and add null safety
**High Priority Improvements (P1):**
- Add null-safe equals/hashCode in StopwordsItem and ProtwordsItem using Objects.equals()
- Add defensive copying for exposed mutable arrays in CharMappingItem and SynonymItem getters
**Files Modified:**
- DictionaryFile.java: Fixed removeAll() bug
- CharMappingFile.java, KuromojiFile.java, ProtwordsFile.java, StemmerOverrideFile.java, StopwordsFile.java, SynonymFile.java: Fixed resource leaks
- CharMappingItem.java: Fixed equals(), added defensive copying
- StopwordsItem.java, ProtwordsItem.java: Null-safe equals/hashCode
- SynonymItem.java: Added defensive copying
These improvements enhance robustness, prevent potential NPEs and resource leaks, and follow security best practices.
* Fix and enhance dictionary Item tests for null-safe implementations
Updated tests to align with null-safe equals/hashCode implementations:
**Test Fixes:**
- ProtwordsItemTest: Fixed test_hashCode_withNullInput and test_equals_withNullInput to expect null-safe behavior instead of NullPointerException
- StopwordsItemTest: Added test_hashCode_withNullInput and test_equals_withNullInput for comprehensive null handling coverage
- CharMappingItemTest: Added test_hashCode_withNullOutput and test_equals_withNullOutput for null output scenarios
**Test Enhancements:**
- SynonymItemTest: Added 5 defensive copy tests to verify immutability of returned arrays
- CharMappingItemTest: Added 3 defensive copy tests for inputs and newInputs arrays
**Files Modified:**
- ProtwordsItemTest.java: Updated 2 null-related tests
- StopwordsItemTest.java: Added 2 new null-safe tests
- CharMappingItemTest.java: Added 5 new tests (2 null tests + 3 defensive copy tests)
- SynonymItemTest.java: Added 5 defensive copy tests
These tests ensure the null-safe implementations work correctly and verify that defensive copying prevents external modification of internal state.
* Fix CharMappingItemTest to use assertArrayEquals for defensive copy
Changed test_constructor_withIdZero to use assertArrayEquals instead of assertSame
since getNewInputs() now returns a defensive copy (clone) of the array rather than
the original array reference. This is the correct behavior for encapsulation and
security, and the test should verify content equality, not reference equality.
* Add missing assertArrayEquals import to CharMappingItemTest
Added static import for org.junit.Assert.assertArrayEquals to fix compilation
error when using assertArrayEquals method in test_constructor_withIdZero.
---------
Co-authored-by: Claude <[email protected]>1 parent d8622d7 commit f02e284
File tree
15 files changed
+322
-55
lines changed- src
- main/java/org/codelibs/fess/dict
- kuromoji
- mapping
- protwords
- stemmeroverride
- stopwords
- synonym
- test/java/org/codelibs/fess/dict
- mapping
- protwords
- stopwords
- synonym
15 files changed
+322
-55
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
259 | | - | |
| 259 | + | |
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
255 | 256 | | |
256 | 257 | | |
257 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
258 | 261 | | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
259 | 269 | | |
260 | 270 | | |
261 | 271 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
| 324 | + | |
324 | 325 | | |
325 | 326 | | |
326 | | - | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
327 | 330 | | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
328 | 338 | | |
329 | 339 | | |
330 | 340 | | |
| |||
Lines changed: 9 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | | - | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| 118 | + | |
117 | 119 | | |
118 | | - | |
| 120 | + | |
119 | 121 | | |
120 | 122 | | |
121 | | - | |
| 123 | + | |
122 | 124 | | |
123 | 125 | | |
124 | 126 | | |
| |||
188 | 190 | | |
189 | 191 | | |
190 | 192 | | |
| 193 | + | |
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| |||
201 | 204 | | |
202 | 205 | | |
203 | 206 | | |
204 | | - | |
205 | | - | |
206 | | - | |
| 207 | + | |
207 | 208 | | |
208 | 209 | | |
209 | | - | |
| 210 | + | |
210 | 211 | | |
211 | 212 | | |
212 | 213 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
253 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
254 | 264 | | |
255 | 265 | | |
256 | 266 | | |
| |||
Lines changed: 2 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
101 | | - | |
| 99 | + | |
102 | 100 | | |
103 | 101 | | |
104 | 102 | | |
| |||
110 | 108 | | |
111 | 109 | | |
112 | 110 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 111 | + | |
117 | 112 | | |
118 | 113 | | |
119 | 114 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| 279 | + | |
279 | 280 | | |
280 | 281 | | |
281 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
282 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
283 | 293 | | |
284 | 294 | | |
285 | 295 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| 266 | + | |
266 | 267 | | |
267 | 268 | | |
268 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
269 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
270 | 280 | | |
271 | 281 | | |
272 | 282 | | |
| |||
Lines changed: 2 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
109 | | - | |
110 | | - | |
| 108 | + | |
111 | 109 | | |
112 | 110 | | |
113 | 111 | | |
| |||
119 | 117 | | |
120 | 118 | | |
121 | 119 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
| 120 | + | |
126 | 121 | | |
127 | 122 | | |
128 | 123 | | |
| |||
Lines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
340 | 340 | | |
341 | 341 | | |
342 | 342 | | |
| 343 | + | |
343 | 344 | | |
344 | 345 | | |
345 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
346 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
347 | 357 | | |
348 | 358 | | |
349 | 359 | | |
| |||
0 commit comments