BAEL-972 - Apache Commons Text (#2178)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article
This commit is contained in:
Ahmed Tawila 2017-06-30 18:00:18 +03:00 committed by Grzegorz Piwowarek
parent 69c330eb6c
commit c2574997f7
6 changed files with 119 additions and 0 deletions

View File

@ -90,6 +90,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
@ -366,6 +371,7 @@
<multiverse.version>0.7.0</multiverse.version>
<cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version>
<commons-text.version>1.1</commons-text.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<jasypt.version>1.9.2</jasypt.version>
<javatuples.version>1.2</javatuples.version>

View File

@ -0,0 +1,18 @@
package com.baeldung.text;
import org.apache.commons.text.diff.EditScript;
import org.apache.commons.text.diff.StringsComparator;
import org.junit.Assert;
import org.junit.Test;
public class DiffTest {
@Test
public void whenEditScript_thenCorrect() {
StringsComparator cmp = new StringsComparator("ABCFGH", "BCDEFG");
EditScript<Character> script = cmp.getScript();
int mod = script.getModifications();
Assert.assertEquals(4, mod);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.text;
import org.apache.commons.text.similarity.LongestCommonSubsequence;
import org.apache.commons.text.similarity.LongestCommonSubsequenceDistance;
import org.junit.Assert;
import org.junit.Test;
public class LongestCommonSubsequenceTest {
@Test
public void whenCompare_thenCorrect() {
LongestCommonSubsequence lcs = new LongestCommonSubsequence();
int countLcs = lcs.apply("New York", "New Hampshire");
Assert.assertEquals(5, countLcs);
}
@Test
public void whenCalculateDistance_thenCorrect() {
LongestCommonSubsequenceDistance lcsd = new LongestCommonSubsequenceDistance();
int countLcsd = lcsd.apply("New York", "New Hampshire");
Assert.assertEquals(11, countLcsd);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.text;
import org.apache.commons.text.StrBuilder;
import org.junit.Assert;
import org.junit.Test;
public class StrBuilderTest {
@Test
public void whenReplaced_thenCorrect() {
StrBuilder strBuilder = new StrBuilder("example StrBuilder!");
strBuilder.replaceAll("example", "new");
Assert.assertEquals(new StrBuilder("new StrBuilder!"), strBuilder);
}
@Test
public void whenCleared_thenEmpty() {
StrBuilder strBuilder = new StrBuilder("example StrBuilder!");
strBuilder.clear();
Assert.assertEquals(new StrBuilder(""), strBuilder);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.text;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.text.StrSubstitutor;
import org.junit.Assert;
import org.junit.Test;
public class StrSubstitutorTest {
@Test
public void whenSubstituted_thenCorrect() {
Map<String, String> substitutes = new HashMap<>();
substitutes.put("name", "John");
substitutes.put("college", "University of Stanford");
String templateString = "My name is ${name} and I am a student at the ${college}.";
StrSubstitutor sub = new StrSubstitutor(substitutes);
String result = sub.replace(templateString);
Assert.assertEquals("My name is John and I am a student at the University of Stanford.", result);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.text;
import org.apache.commons.text.WordUtils;
import org.junit.Assert;
import org.junit.Test;
public class WordUtilsTest {
@Test
public void whenCapitalized_thenCorrect() {
String toBeCapitalized = "to be capitalized!";
String result = WordUtils.capitalize(toBeCapitalized);
Assert.assertEquals("To Be Capitalized!", result);
}
@Test
public void whenContainsWords_thenCorrect() {
boolean containsWords = WordUtils.containsAllWords("String to search", "to", "search");
Assert.assertTrue(containsWords);
}
}