Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,21 @@
package com.baeldung.localization;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class App {
/**
* Runs all available formatter
* @throws ParseException
*/
public static void main(String[] args) {
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
Localization.run(locales);
JavaSEFormat.run(locales);
ICUFormat.run(locales);
}
}
@@ -0,0 +1,29 @@
package com.baeldung.localization;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import com.ibm.icu.text.MessageFormat;
public class ICUFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
String format = bundle.getString("label-icu");
MessageFormat formatter = new MessageFormat(format, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("ICU formatter");
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
}
}
@@ -0,0 +1,24 @@
package com.baeldung.localization;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class JavaSEFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
final String pattern = bundle.getString("label");
final MessageFormat formatter = new MessageFormat(pattern, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("Java formatter");
final Date date = new Date(System.currentTimeMillis());
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
}
}
@@ -0,0 +1,18 @@
package com.baeldung.localization;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class Localization {
public static String getLabel(Locale locale) {
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
return bundle.getString("label");
}
public static void run(List<Locale> locales) {
locales.forEach(locale -> System.out.println(getLabel(locale)));
}
}
@@ -0,0 +1,67 @@
package com.baeldung.string.multiline;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
public class MultiLineString {
String newLine = System.getProperty("line.separator");
public String stringConcatenation() {
return "Get busy living"
.concat(newLine)
.concat("or")
.concat(newLine)
.concat("get busy dying.")
.concat(newLine)
.concat("--Stephen King");
}
public String stringJoin() {
return String.join(newLine,
"Get busy living",
"or",
"get busy dying.",
"--Stephen King");
}
public String stringBuilder() {
return new StringBuilder()
.append("Get busy living")
.append(newLine)
.append("or")
.append(newLine)
.append("get busy dying.")
.append(newLine)
.append("--Stephen King")
.toString();
}
public String stringWriter() {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
printWriter.println("Get busy living");
printWriter.println("or");
printWriter.println("get busy dying.");
printWriter.println("--Stephen King");
return stringWriter.toString();
}
public String guavaJoiner() {
return Joiner.on(newLine).join(ImmutableList.of("Get busy living",
"or",
"get busy dying.",
"--Stephen King"));
}
public String loadFromFile() throws IOException {
return new String(Files.readAllBytes(Paths.get("src/main/resources/stephenking.txt")));
}
}
@@ -0,0 +1,2 @@
label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}.
label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}.
@@ -0,0 +1,2 @@
label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoy\u00e9 {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}.
label-icu={0} {2, plural, =0 {ne } other {}}vous a envoy\u00e9 {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}.
@@ -0,0 +1,2 @@
label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}.
label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}.
@@ -0,0 +1,2 @@
label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}.
label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}.
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1 @@
label=Alice has sent you a message.
@@ -0,0 +1 @@
label=Alice vous a envoy\u00e9 un message.
@@ -0,0 +1 @@
label=Alice ti ha inviato un messaggio.
@@ -0,0 +1 @@
label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107.
@@ -0,0 +1,4 @@
Get busy living
or
get busy dying.
--Stephen King
@@ -0,0 +1,74 @@
package com.baeldung.localization;
import static org.junit.Assert.assertEquals;
import java.util.Locale;
import org.junit.Test;
import com.baeldung.localization.ICUFormat;
public class ICUFormatUnitTest {
@Test
public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice nie wysłała ci żadnej wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice wysłała ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob wysłał ci 6 wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 }));
}
}
@@ -0,0 +1,19 @@
package com.baeldung.string.interview;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class LocaleUnitTest {
@Test
public void whenUsingLocal_thenCorrectResultsForDifferentLocale() {
Locale usLocale = Locale.US;
BigDecimal number = new BigDecimal(102_300.456d);
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);
assertEquals(usNumberFormat.format(number), "$102,300.46");
}
}
@@ -0,0 +1,29 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
public class StringAnagramUnitTest {
public boolean isAnagram(String s1, String s2) {
if(s1.length() != s2.length())
return false;
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
@Test
public void whenTestAnagrams_thenTestingCorrectly() {
assertThat(isAnagram("car", "arc")).isTrue();
assertThat(isAnagram("west", "stew")).isTrue();
assertThat(isAnagram("west", "east")).isFalse();
}
}
@@ -0,0 +1,20 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringChangeCaseUnitTest {
@Test
public void givenString_whenChangingToUppercase_thenCaseChanged() {
String s = "Welcome to Baeldung!";
assertEquals("WELCOME TO BAELDUNG!", s.toUpperCase());
}
@Test
public void givenString_whenChangingToLowerrcase_thenCaseChanged() {
String s = "Welcome to Baeldung!";
assertEquals("welcome to baeldung!", s.toLowerCase());
}
}
@@ -0,0 +1,28 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringCountOccurrencesUnitTest {
public int countOccurrences(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}
@Test
public void givenString_whenCountingFrequencyOfChar_thenCountCorrect() {
assertEquals(3, countOccurrences("united states", 't'));
}
public void givenString_whenUsingJava8_thenCountingOfCharCorrect() {
String str = "united states";
long count = str.chars().filter(ch -> (char)ch == 't').count();
assertEquals(3, count);
}
}
@@ -0,0 +1,14 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringFormatUnitTest {
@Test
public void givenString_whenUsingStringFormat_thenStringFormatted() {
String title = "Baeldung";
String formatted = String.format("Title is %s", title);
assertEquals(formatted, "Title is Baeldung");
}
}
@@ -0,0 +1,17 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class StringInternUnitTest {
@Test
public void whenCallingStringIntern_thenStringsInterned() {
String s1 = "Baeldung";
String s2 = new String("Baeldung");
String s3 = new String("Baeldung").intern();
assertThat(s1 == s2).isFalse();
assertThat(s1 == s3).isTrue();
}
}
@@ -0,0 +1,18 @@
package com.baeldung.string.interview;
import java.util.StringJoiner;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringJoinerUnitTest {
@Test
public void whenUsingStringJoiner_thenStringsJoined() {
StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("Red")
.add("Green")
.add("Blue");
assertEquals(joiner.toString(), "[Red,Green,Blue]");
}
}
@@ -0,0 +1,29 @@
package com.baeldung.string.interview;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class StringPalindromeUnitTest {
public boolean isPalindrome(String text) {
int forward = 0;
int backward = text.length() - 1;
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return true;
}
@Test
public void givenIsPalindromeMethod_whenCheckingString_thenFindIfPalindrome() {
assertThat(isPalindrome("madam")).isTrue();
assertThat(isPalindrome("radar")).isTrue();
assertThat(isPalindrome("level")).isTrue();
assertThat(isPalindrome("baeldung")).isFalse();
}
}
@@ -0,0 +1,13 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringReverseUnitTest {
@Test
public void whenUsingInbuildMethods_thenStringReversed() {
String reversed = new StringBuilder("baeldung").reverse().toString();
assertEquals("gnudleab", reversed);
}
}
@@ -0,0 +1,32 @@
package com.baeldung.string.interview;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class StringSplitUnitTest {
@Test
public void givenCoreJava_whenSplittingStrings_thenSplitted() {
String expected[] = {
"john",
"peter",
"mary"
};
String[] splitted = "john,peter,mary".split(",");
assertArrayEquals( expected, splitted );
}
@Test
public void givenApacheCommons_whenSplittingStrings_thenSplitted() {
String expected[] = {
"john",
"peter",
"mary"
};
String[] splitted = StringUtils.split("john peter mary");
assertArrayEquals( expected, splitted );
}
}
@@ -0,0 +1,24 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertArrayEquals;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenGetBytes_thenCorrect() throws UnsupportedEncodingException {
byte[] byteArray1 = "abcd".getBytes();
byte[] byteArray2 = "efgh".getBytes(StandardCharsets.US_ASCII);
byte[] byteArray3 = "ijkl".getBytes("UTF-8");
byte[] expected1 = new byte[] { 97, 98, 99, 100 };
byte[] expected2 = new byte[] { 101, 102, 103, 104 };
byte[] expected3 = new byte[] { 105, 106, 107, 108 };
assertArrayEquals(expected1, byteArray1);
assertArrayEquals(expected2, byteArray2);
assertArrayEquals(expected3, byteArray3);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.string.interview;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
public class StringToCharArrayUnitTest {
@Test
public void whenConvertingStringToCharArray_thenConversionSuccessful() {
String beforeConvStr = "hello";
char[] afterConvCharArr = { 'h', 'e', 'l', 'l', 'o' };
assertEquals(Arrays.equals(beforeConvStr.toCharArray(), afterConvCharArr), true);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.string.interview;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringToIntegerUnitTest {
@Test
public void givenString_whenParsingInt_shouldConvertToInt() {
String givenString = "42";
int result = Integer.parseInt(givenString);
assertThat(result).isEqualTo(42);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.string.multiline;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import com.baeldung.string.multiline.MultiLineString;
public class MultiLineStringUnitTest {
@Test
public void whenCompareMultiLineStrings_thenTheyAreAllTheSame() throws IOException {
MultiLineString ms = new MultiLineString();
assertEquals(ms.stringConcatenation(), ms.stringJoin());
assertEquals(ms.stringJoin(), ms.stringBuilder());
assertEquals(ms.stringBuilder(), ms.guavaJoiner());
assertEquals(ms.guavaJoiner(), ms.loadFromFile());
}
}
@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear