1616 */
1717package org .apache .commons .text .similarity ;
1818
19+ import static org .junit .jupiter .api .Assertions .assertNull ;
20+ import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertThrows ;
22+
23+ import java .util .Arrays ;
24+ import java .util .stream .Stream ;
25+
26+ import org .junit .jupiter .api .BeforeAll ;
1927import org .junit .jupiter .api .Test ;
2028import org .junit .jupiter .params .ParameterizedTest ;
2129import org .junit .jupiter .params .provider .Arguments ;
2230import org .junit .jupiter .params .provider .MethodSource ;
2331
24- import java .util .Arrays ;
25- import java .util .stream .Stream ;
32+ public class DamerauLevenshteinDistanceTest {
2633
27- import static org .junit .jupiter .api .Assertions .*;
28- import static org .junit .jupiter .api .Assertions .assertNull ;
34+ private static DamerauLevenshteinDistance defaultInstance ;
2935
30- public class DamerauLevenshteinDistanceTest {
36+ @ BeforeAll
37+ static void createInstance () {
38+ defaultInstance = new DamerauLevenshteinDistance ();
39+ }
3140
3241 @ Test
3342 void testGetThresholdDirectlyAfterObjectInstantiation () {
34- assertNull (LevenshteinDistance . getDefaultInstance () .getThreshold ());
43+ assertNull (defaultInstance .getThreshold ());
3544 }
3645
3746 @ Test
3847 void testGetThresholdIsCorrect () {
39- LevenshteinDistance distance = new LevenshteinDistance (10 );
48+ DamerauLevenshteinDistance distance = new DamerauLevenshteinDistance (10 );
4049
4150 assertEquals (10 , distance .getThreshold ());
4251 }
4352
4453 @ Test
4554 void testNullInputsThrowUnlimited () {
46- DamerauLevenshteinDistance instance = DamerauLevenshteinDistance .getDefaultInstance ();
47-
48- assertThrows (IllegalArgumentException .class , () -> instance .apply (null , "test" ));
49- assertThrows (IllegalArgumentException .class , () -> instance .apply ("test" , null ));
50- assertThrows (IllegalArgumentException .class , () -> instance .apply (null , SimilarityInput .input ("test" )));
51- assertThrows (IllegalArgumentException .class , () -> instance .apply (SimilarityInput .input ("test" ), null ));
55+ assertThrows (IllegalArgumentException .class , () -> defaultInstance .apply (null , "test" ));
56+ assertThrows (IllegalArgumentException .class , () -> defaultInstance .apply ("test" , null ));
57+ assertThrows (IllegalArgumentException .class , () -> defaultInstance .apply (null , SimilarityInput .input ("test" )));
58+ assertThrows (IllegalArgumentException .class , () -> defaultInstance .apply (SimilarityInput .input ("test" ), null ));
5259 }
5360
5461 @ Test
@@ -69,10 +76,8 @@ void testInvalidThresholdThrows() {
6976 @ ParameterizedTest (name = "DamerauLevenshteinDistance.unlimitedCompare(\" {0}\" , \" {1}\" ) should return {2}" )
7077 @ MethodSource ("unlimitedDamerauLevenshteinDistanceTestCases" )
7178 void testCalculateDamerauLevenshteinDistance (String left , String right , int expectedDistance ) {
72- DamerauLevenshteinDistance instance = DamerauLevenshteinDistance .getDefaultInstance ();
73-
74- int leftRightDistance = instance .apply (left , right );
75- int rightLeftDistance = instance .apply (right , left );
79+ int leftRightDistance = defaultInstance .apply (left , right );
80+ int rightLeftDistance = defaultInstance .apply (right , left );
7681
7782 assertEquals (expectedDistance , leftRightDistance );
7883 assertEquals (expectedDistance , rightLeftDistance );
@@ -81,13 +86,11 @@ void testCalculateDamerauLevenshteinDistance(String left, String right, int expe
8186 @ ParameterizedTest (name = "DamerauLevenshteinDistance.unlimitedCompare(\" {0}\" , \" {1}\" ) should return {2} ({3})" )
8287 @ MethodSource ("unlimitedDamerauLevenshteinDistanceTestCases_SimilarityInput" )
8388 void testCalculateDamerauLevenshteinDistance_SimilarityInput (String left , String right , int expectedDistance , final Class <?> cls ) {
84- DamerauLevenshteinDistance instance = DamerauLevenshteinDistance .getDefaultInstance ();
85-
8689 SimilarityInput <Object > leftInput = SimilarityInputTest .build (cls , left );
8790 SimilarityInput <Object > rightInput = SimilarityInputTest .build (cls , right );
8891
89- int leftRightDistance = instance .apply (leftInput , rightInput );
90- int rightLeftDistance = instance .apply (rightInput , leftInput );
92+ int leftRightDistance = defaultInstance .apply (leftInput , rightInput );
93+ int rightLeftDistance = defaultInstance .apply (rightInput , leftInput );
9194
9295 assertEquals (expectedDistance , leftRightDistance );
9396 assertEquals (expectedDistance , rightLeftDistance );
0 commit comments