[BAEL-18364] move spring-rest-*** articles - 1

This commit is contained in:
Sjmillington
2019-10-17 17:17:31 +01:00
parent db85c8f275
commit f1673ce653
20415 changed files with 1639515 additions and 0 deletions
@@ -0,0 +1,49 @@
package com.baeldung.string.wordcount;
import java.util.StringTokenizer;
public class WordCounter {
static final int WORD = 0;
static final int SEPARATOR = 1;
public static int countWordsUsingRegex(String arg) {
if (arg == null) {
return 0;
}
final String[] words = arg.split("[\\pP\\s&&[^']]+");
return words.length;
}
public static int countWordsUsingTokenizer(String arg) {
if (arg == null) {
return 0;
}
final StringTokenizer stringTokenizer = new StringTokenizer(arg);
return stringTokenizer.countTokens();
}
public static int countWordsManually(String arg) {
if (arg == null) {
return 0;
}
int flag = SEPARATOR;
int count = 0;
int stringLength = arg.length();
int characterCounter = 0;
while (characterCounter < stringLength) {
if (isAllowedInWord(arg.charAt(characterCounter)) && flag == SEPARATOR) {
flag = WORD;
count++;
} else if (!isAllowedInWord(arg.charAt(characterCounter))) {
flag = SEPARATOR;
}
characterCounter++;
}
return count;
}
private static boolean isAllowedInWord(char charAt) {
return charAt == '\'' || Character.isLetter(charAt);
}
}
@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
@@ -0,0 +1,26 @@
package com.baeldung.string.equalsIgnoreCase;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringEqualsIgnoreCaseUnitTest {
private String string1 = "equals ignore case";
private String string2 = "EQUALS IGNORE CASE";
@Test
public void givenEqualStringsWithDifferentCase_whenUsingEqualsIgnoreCase_ThenTheyAreEqual() {
assertThat(string1.equalsIgnoreCase(string2)).isTrue();
}
@Test
public void givenEqualStringsWithDifferentCase_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreEqual() {
assertThat(StringUtils.equalsIgnoreCase(string1, string2)).isTrue();
}
@Test
public void givenAStringAndNullValue_whenUsingApacheCommonsEqualsIgnoreCase_ThenTheyAreNotEqual() {
assertThat(StringUtils.equalsIgnoreCase(string1, null)).isFalse();
}
}
@@ -0,0 +1,35 @@
package com.baeldung.string.wordcount;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.StringTokenizer;
public class WordCountUnitTest {
private String string1 = "This is a test sentence with eight words";
private String string2 = "This#is%a test sentence with eight words";
@Test
public void givenStringWith8Words_whenUsingRegexCount_ThenResultEqual8() {
assertEquals(8, WordCounter.countWordsUsingRegex(string2));
assertEquals(9, WordCounter.countWordsUsingRegex("no&one#should%ever-write-like,this;but:well"));
assertEquals(7, WordCounter.countWordsUsingRegex("the farmer's wife--she was from Albuquerque"));
}
@Test
public void givenStringWith8Words_whenUsingManualMethod_ThenWordCountEqual8() {
assertEquals(8, WordCounter.countWordsManually(string1));
assertEquals(9, WordCounter.countWordsManually("no&one#should%ever-write-like,this but well"));
assertEquals(7, WordCounter.countWordsManually("the farmer's wife--she was from Albuquerque"));
}
@Test
public void givenAStringWith8Words_whenUsingTokenizer_ThenWordCountEqual8() {
assertEquals(8, WordCounter.countWordsUsingTokenizer(string1));
assertEquals(3, new StringTokenizer("three blind mice").countTokens());
assertEquals(4, new StringTokenizer("see\thow\tthey\trun").countTokens());
assertEquals(7, new StringTokenizer("the farmer's wife--she was from Albuquerque", " -").countTokens());
assertEquals(10, new StringTokenizer("did,you,ever,see,such,a,sight,in,your,life", ",").countTokens());
}
}
@@ -0,0 +1,73 @@
package com.baeldung.stringdiff;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class StringDiffBenchmarkUnitTest {
private DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
private List<String> inputs = randomizeInputs(10000);
public static void main(String[] args) throws RunnerException {
Options opts = new OptionsBuilder().include(".*")
.warmupIterations(1)
.measurementIterations(50)
.jvmArgs("-Xms2g", "-Xmx2g")
.shouldDoGC(true)
.forks(1)
.build();
new Runner(opts).run();
}
@Benchmark
public int diffMatchPatch() {
for (int i = 0; i < inputs.size() - 1; i++) {
diffMatchPatch.diffMain(inputs.get(i), inputs.get(i + 1), false);
}
return inputs.size();
}
@Benchmark
public int stringUtils() {
for (int i = 0; i < inputs.size() - 1; i++) {
StringUtils.difference(inputs.get(i), inputs.get(i + 1));
}
return inputs.size();
}
/**
* Creates a list of a given size, containing 30 character long strings,
* each starting with a static prefix of 10 characters and followed by
* a random 20 character suffix
*
* @return a {@link List} of randomised strings
*/
private List<String> randomizeInputs(int size) {
String staticPart = "ABCDEF1234";
List<String> inputs = new ArrayList<>();
for (int i = 0; i < size; i++) {
inputs.add(staticPart + RandomStringUtils.randomAlphabetic(20));
}
return inputs;
}
}
@@ -0,0 +1,39 @@
package com.baeldung.stringdiff;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.apache.commons.lang3.StringUtils;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.Operation;
import org.junit.Test;
public class StringDiffUnitTest {
private DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
// Test samples
private final String text1 = "ABCDELMN";
private final String text2 = "ABCFGLMN";
@Test
public void givenTwoStrings_whenDiffMatchPatch_thenReturnCorrectDiff() {
assertThat(diffMatchPatch.diffMain(text1, text2, false), containsInAnyOrder(
new DiffMatchPatch.Diff(Operation.EQUAL, "ABC"),
new DiffMatchPatch.Diff(Operation.DELETE, "DE"),
new DiffMatchPatch.Diff(Operation.INSERT, "FG"),
new DiffMatchPatch.Diff(Operation.EQUAL, "LMN")));
assertThat(diffMatchPatch.diffMain(text2, text1, false), containsInAnyOrder(
new DiffMatchPatch.Diff(Operation.EQUAL, "ABC"),
new DiffMatchPatch.Diff(Operation.INSERT, "DE"),
new DiffMatchPatch.Diff(Operation.DELETE, "FG"),
new DiffMatchPatch.Diff(Operation.EQUAL, "LMN")));
}
@Test
public void givenTwoStrings_whenStringUtilsDifference_thenReturnCorrectDiff() {
assertThat(StringUtils.difference(text1, text2), is("FGLMN"));
assertThat(StringUtils.difference(text2, text1), is("DELMN"));
}
}
@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear