BAEL-3200 Error handling with Spring AMQP

This commit is contained in:
Alexander Molochko
2019-10-17 03:01:56 +03:00
parent db85c8f275
commit 70d5e7c57d
20386 changed files with 1639315 additions and 0 deletions
@@ -0,0 +1,72 @@
package com.baeldung.string.charArrayToString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import com.google.common.base.Joiner;
import org.junit.Test;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CharArrayToStringConversionUnitTest {
@Test
public void whenStringConstructor_thenOK() {
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = new String(charArray);
assertThat(string, is("baeldung"));
}
@Test
public void whenStringCopyValueOf_thenOK() {
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = String.copyValueOf(charArray);
assertThat(string, is("baeldung"));
}
@Test
public void whenStringValueOf_thenOK() {
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = String.valueOf(charArray);
assertThat(string, is("baeldung"));
}
@Test
public void whenStringBuilder_thenOK() {
final char[][] arrayOfCharArray = { { 'b', 'a' }, { 'e', 'l', 'd', 'u' }, { 'n', 'g' } };
StringBuilder sb = new StringBuilder();
for (char[] subArray : arrayOfCharArray) {
sb.append(subArray);
}
assertThat(sb.toString(), is("baeldung"));
}
@Test
public void whenStreamCollectors_thenOK() {
final Character[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
Stream<Character> charStream = Arrays.stream(charArray);
String string = charStream.map(String::valueOf).collect(Collectors.joining());
assertThat(string, is("baeldung"));
}
@Test
public void whenGoogleCommonBaseJoiners_thenOK() {
final Character[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = Joiner.on("|").join(charArray);
assertThat(string, is("b|a|e|l|d|u|n|g"));
}
}
@@ -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,56 @@
package com.baeldung.string.todouble;
import static org.junit.Assert.assertEquals;
import java.text.DecimalFormat;
import java.text.ParseException;
import org.junit.Test;
public class StringToDoubleConversionUnitTest {
@Test
public void givenValidString_WhenParseDouble_ThenResultIsPrimitiveDouble() {
assertEquals(1.23, Double.parseDouble("1.23"), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenParseDouble_ThenNullPointerExceptionIsThrown() {
Double.parseDouble(null);
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenParseDouble_ThenNumberFormatExceptionIsThrown() {
Double.parseDouble("&");
}
@Test
public void givenValidString_WhenValueOf_ThenResultIsPrimitiveDouble() {
assertEquals(1.23, Double.valueOf("1.23"), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenValueOf_ThenNullPointerExceptionIsThrown() {
Double.valueOf(null);
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenValueOf_ThenNumberFormatExceptionIsThrown() {
Double.valueOf("&");
}
@Test
public void givenValidString_WhenDecimalFormat_ThenResultIsValidDouble() throws ParseException {
assertEquals(1.23, new DecimalFormat("#").parse("1.23").doubleValue(), 0.000001);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenDecimalFormat_ThenNullPointerExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse(null);
}
@Test(expected = ParseException.class)
public void givenInvalidString_WhenDecimalFormat_ThenParseExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse("&");
}
}
@@ -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"));
}
}