File tree Expand file tree Collapse file tree
src/test/java/com/baeldung/text Expand file tree Collapse file tree Original file line number Diff line number Diff line change 9090 <artifactId >commons-lang3</artifactId >
9191 <version >${commons-lang.version} </version >
9292 </dependency >
93+ <dependency >
94+ <groupId >org.apache.commons</groupId >
95+ <artifactId >commons-text</artifactId >
96+ <version >${commons-text.version} </version >
97+ </dependency >
9398 <dependency >
9499 <groupId >org.apache.commons</groupId >
95100 <artifactId >commons-collections4</artifactId >
366371 <multiverse .version>0.7.0</multiverse .version>
367372 <cglib .version>3.2.4</cglib .version>
368373 <commons-lang .version>3.5</commons-lang .version>
374+ <commons-text .version>1.1</commons-text .version>
369375 <commons-beanutils .version>1.9.3</commons-beanutils .version>
370376 <jasypt .version>1.9.2</jasypt .version>
371377 <javatuples .version>1.2</javatuples .version>
Original file line number Diff line number Diff line change 1+ package com .baeldung .text ;
2+
3+ import org .apache .commons .text .diff .EditScript ;
4+ import org .apache .commons .text .diff .StringsComparator ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+
8+ public class DiffTest {
9+
10+ @ Test
11+ public void whenEditScript_thenCorrect () {
12+ StringsComparator cmp = new StringsComparator ("ABCFGH" , "BCDEFG" );
13+ EditScript <Character > script = cmp .getScript ();
14+ int mod = script .getModifications ();
15+
16+ Assert .assertEquals (4 , mod );
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .text ;
2+
3+ import org .apache .commons .text .similarity .LongestCommonSubsequence ;
4+ import org .apache .commons .text .similarity .LongestCommonSubsequenceDistance ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+
8+ public class LongestCommonSubsequenceTest {
9+
10+ @ Test
11+ public void whenCompare_thenCorrect () {
12+ LongestCommonSubsequence lcs = new LongestCommonSubsequence ();
13+ int countLcs = lcs .apply ("New York" , "New Hampshire" );
14+
15+ Assert .assertEquals (5 , countLcs );
16+ }
17+
18+ @ Test
19+ public void whenCalculateDistance_thenCorrect () {
20+ LongestCommonSubsequenceDistance lcsd = new LongestCommonSubsequenceDistance ();
21+ int countLcsd = lcsd .apply ("New York" , "New Hampshire" );
22+
23+ Assert .assertEquals (11 , countLcsd );
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .text ;
2+
3+ import org .apache .commons .text .StrBuilder ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+
7+ public class StrBuilderTest {
8+
9+ @ Test
10+ public void whenReplaced_thenCorrect () {
11+ StrBuilder strBuilder = new StrBuilder ("example StrBuilder!" );
12+ strBuilder .replaceAll ("example" , "new" );
13+
14+ Assert .assertEquals (new StrBuilder ("new StrBuilder!" ), strBuilder );
15+ }
16+
17+ @ Test
18+ public void whenCleared_thenEmpty () {
19+ StrBuilder strBuilder = new StrBuilder ("example StrBuilder!" );
20+ strBuilder .clear ();
21+
22+ Assert .assertEquals (new StrBuilder ("" ), strBuilder );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .text ;
2+
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+
6+ import org .apache .commons .text .StrSubstitutor ;
7+ import org .junit .Assert ;
8+ import org .junit .Test ;
9+
10+ public class StrSubstitutorTest {
11+
12+ @ Test
13+ public void whenSubstituted_thenCorrect () {
14+ Map <String , String > substitutes = new HashMap <>();
15+ substitutes .put ("name" , "John" );
16+ substitutes .put ("college" , "University of Stanford" );
17+ String templateString = "My name is ${name} and I am a student at the ${college}." ;
18+ StrSubstitutor sub = new StrSubstitutor (substitutes );
19+ String result = sub .replace (templateString );
20+
21+ Assert .assertEquals ("My name is John and I am a student at the University of Stanford." , result );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .text ;
2+
3+ import org .apache .commons .text .WordUtils ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+
7+ public class WordUtilsTest {
8+
9+ @ Test
10+ public void whenCapitalized_thenCorrect () {
11+ String toBeCapitalized = "to be capitalized!" ;
12+ String result = WordUtils .capitalize (toBeCapitalized );
13+
14+ Assert .assertEquals ("To Be Capitalized!" , result );
15+ }
16+
17+ @ Test
18+ public void whenContainsWords_thenCorrect () {
19+ boolean containsWords = WordUtils .containsAllWords ("String to search" , "to" , "search" );
20+
21+ Assert .assertTrue (containsWords );
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments