Merge branch 'eugenp:master' into JAVA-10004
This commit is contained in:
commit
51c7812248
|
@ -8,3 +8,4 @@
|
|||
- [Java Deque vs. Stack](https://www.baeldung.com/java-deque-vs-stack)
|
||||
- [Collection.toArray(new T[0]) or .toArray(new T[size])](https://www.baeldung.com/java-collection-toarray-methods)
|
||||
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
|
||||
- [Sorting Objects in a List by Date](https://www.baeldung.com/java-sort-list-by-date)
|
||||
|
|
|
@ -4,4 +4,5 @@ This module contains articles about the Java List collection
|
|||
|
||||
### Relevant Articles:
|
||||
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
|
||||
- [Reverse an ArrayList in Java](https://www.baeldung.com/java-reverse-arraylist)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-3)
|
||||
|
|
|
@ -45,12 +45,24 @@
|
|||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${apache-commons.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<trove4j.version>3.0.2</trove4j.version>
|
||||
<fastutil.version>8.1.0</fastutil.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<apache-commons.version>3.0</apache-commons.version>
|
||||
<assertj.version>3.22.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.list.reverse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ReverseArrayList {
|
||||
private ReverseArrayList() {
|
||||
throw new RuntimeException("This class cannot be instantiated.");
|
||||
}
|
||||
|
||||
public static <T> void reverseWithRecursion(List<T> list) {
|
||||
if (list.size() > 1) {
|
||||
T value = list.remove(0);
|
||||
reverseWithRecursion(list);
|
||||
list.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void reverseWithLoop(List<T> list) {
|
||||
for (int i = 0, j = list.size() - 1; i < j; i++) {
|
||||
list.add(i, list.remove(j));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.list.reverse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ReverseArrayListUnitTest {
|
||||
|
||||
private static final List<Integer> EXPECTED = new ArrayList<>(Arrays.asList(7, 6, 5, 4, 3, 2, 1));
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenCallReverseMethod_thenListReversedInPlace() {
|
||||
List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
Collections.reverse(aList);
|
||||
assertThat(aList).isEqualTo(EXPECTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenCallReverseMethod_thenListReversedAsaNewList() {
|
||||
List<Integer> originalList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
List<Integer> aNewList = new ArrayList<>(originalList);
|
||||
Collections.reverse(aNewList);
|
||||
|
||||
assertThat(aNewList).isNotEqualTo(originalList)
|
||||
.isEqualTo(EXPECTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenCallReverseWithRecur_thenListReversedInPlace() {
|
||||
List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
ReverseArrayList.reverseWithRecursion(aList);
|
||||
assertThat(aList).isEqualTo(EXPECTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenCallReverseWithLoop_thenListReversedInPlace() {
|
||||
List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
ReverseArrayList.reverseWithLoop(aList);
|
||||
assertThat(aList).isEqualTo(EXPECTED);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
package com.baeldung.list.sorting.alphabetical;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.text.Normalizer;
|
||||
import java.text.ParseException;
|
||||
import java.text.RuleBasedCollator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SortingListUnitTest {
|
||||
|
||||
private static List<String> INPUT_NAMES = Arrays.asList("john", "mike", "usmon", "ken", "harry");
|
||||
private static List<String> EXPECTED_NATURAL_ORDER = Arrays.asList("harry", "john", "ken", "mike", "usmon");
|
||||
private static List<String> EXPECTED_REVERSE_ORDER = Arrays.asList("usmon", "mike", "ken", "john", "harry");
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenUsingCollections_thenListIsSorted() {
|
||||
|
||||
Collections.sort(INPUT_NAMES);
|
||||
|
||||
assertThat(INPUT_NAMES).isEqualTo(EXPECTED_NATURAL_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenUsingCollections_thenListIsSortedInReverse() {
|
||||
Comparator<String> reverseComparator = (element1, element2) -> element2.compareTo(element1);
|
||||
|
||||
Collections.sort(INPUT_NAMES, reverseComparator);
|
||||
|
||||
assertThat(INPUT_NAMES).isEqualTo(EXPECTED_REVERSE_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStringsWithUpperAndLowerCaseMixed_whenCustomComparator_thenListIsSortedCorrectly() {
|
||||
List<String> movieNames = Arrays.asList("amazing SpiderMan", "Godzilla", "Sing", "Minions");
|
||||
List<String> naturalSortOrder = Arrays.asList("Godzilla", "Minions", "Sing", "amazing SpiderMan");
|
||||
List<String> comparatorSortOrder = Arrays.asList("amazing SpiderMan", "Godzilla", "Minions", "Sing");
|
||||
|
||||
Collections.sort(movieNames);
|
||||
|
||||
assertThat(movieNames).isEqualTo(naturalSortOrder);
|
||||
|
||||
Collections.sort(movieNames, Comparator.comparing(s -> s.toLowerCase()));
|
||||
|
||||
assertThat(movieNames).isEqualTo(comparatorSortOrder);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStringsIncludingSomeWithSpecialCharacter_whenCustomComparator_thenListIsSortedWithSpecialCharacterLast() {
|
||||
List<String> listWithSpecialCharacters = Arrays.asList("@laska", "blah", "jo", "@sk", "foo");
|
||||
|
||||
List<String> sortedNaturalOrder = Arrays.asList("@laska", "@sk", "blah", "foo", "jo");
|
||||
List<String> sortedSpecialCharacterLast = Arrays.asList("blah", "foo", "jo", "@laska", "@sk");
|
||||
|
||||
Collections.sort(listWithSpecialCharacters);
|
||||
|
||||
assertThat(listWithSpecialCharacters).isEqualTo(sortedNaturalOrder);
|
||||
|
||||
Comparator<String> specialSignComparator = Comparator.<String, Boolean>comparing(s -> s.startsWith("@"));
|
||||
Comparator<String> specialCharacterComparator = specialSignComparator.thenComparing(Comparator.naturalOrder());
|
||||
|
||||
listWithSpecialCharacters.sort(specialCharacterComparator);
|
||||
|
||||
assertThat(listWithSpecialCharacters).isEqualTo(sortedSpecialCharacterLast);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenUsingStreamsAndSort_thenListIsSorted() {
|
||||
List<String> sortedList = INPUT_NAMES.stream()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(sortedList).isEqualTo(EXPECTED_NATURAL_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenUsingStreamsWithComparator_thenListIsSortedInReverseOrder() {
|
||||
List<String> sortedList = INPUT_NAMES.stream()
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(sortedList).isEqualTo(EXPECTED_REVERSE_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenUsingTreeSet_thenListIsSorted() {
|
||||
SortedSet<String> sortedSet = new TreeSet<>(INPUT_NAMES);
|
||||
List<String> sortedList = new ArrayList<>(sortedSet);
|
||||
|
||||
assertThat(sortedList).isEqualTo(EXPECTED_NATURAL_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStrings_whenSortOnList_thenListIsSorted() {
|
||||
|
||||
INPUT_NAMES.sort(Comparator.reverseOrder());
|
||||
|
||||
assertThat(INPUT_NAMES).isEqualTo(EXPECTED_REVERSE_ORDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStringsWithAccent_whenUseCollatorWithLocaleSet_thenListIsSortedAccordingToLocaleRules() {
|
||||
List<String> accentedStrings = Arrays.asList("único", "árbol", "cosas", "fútbol");
|
||||
List<String> sortedNaturalOrder = Arrays.asList("cosas", "fútbol", "árbol", "único");
|
||||
List<String> sortedLocaleSensitive = Arrays.asList("árbol", "cosas", "fútbol", "único");
|
||||
|
||||
Collections.sort(accentedStrings);
|
||||
assertThat(accentedStrings).isEqualTo(sortedNaturalOrder);
|
||||
|
||||
Collator esCollator = Collator.getInstance(new Locale("es"));
|
||||
|
||||
accentedStrings.sort((s1, s2) -> {
|
||||
return esCollator.compare(s1, s2);
|
||||
});
|
||||
|
||||
assertThat(accentedStrings).isEqualTo(sortedLocaleSensitive);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStringsWithAccentedCharacters_whenComparatorWithNormalizer_thenListIsNormalizeAndSorted() {
|
||||
List<String> accentedStrings = Arrays.asList("único", "árbol", "cosas", "fútbol");
|
||||
|
||||
List<String> naturalOrderSorted = Arrays.asList("cosas", "fútbol", "árbol", "único");
|
||||
List<String> stripAccentSorted = Arrays.asList("árbol", "cosas", "fútbol", "único");
|
||||
|
||||
Collections.sort(accentedStrings);
|
||||
assertThat(accentedStrings).isEqualTo(naturalOrderSorted);
|
||||
|
||||
accentedStrings.sort((o1, o2) -> {
|
||||
o1 = Normalizer.normalize(o1, Normalizer.Form.NFD);
|
||||
o2 = Normalizer.normalize(o2, Normalizer.Form.NFD);
|
||||
return o1.compareTo(o2);
|
||||
});
|
||||
|
||||
assertThat(accentedStrings).isEqualTo(stripAccentSorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfStringsWithAccentedCharacters_whenComparatorWithStripAccents_canStripAccentsAndSort() {
|
||||
List<String> accentedStrings = Arrays.asList("único", "árbol", "cosas", "fútbol");
|
||||
|
||||
List<String> naturalOrderSorted = Arrays.asList("cosas", "fútbol", "árbol", "único");
|
||||
List<String> stripAccentSorted = Arrays.asList("árbol", "cosas", "fútbol", "único");
|
||||
|
||||
Collections.sort(accentedStrings);
|
||||
|
||||
assertThat(accentedStrings).isEqualTo(naturalOrderSorted);
|
||||
|
||||
accentedStrings.sort(Comparator.comparing(input -> StringUtils.stripAccents(input)));
|
||||
|
||||
assertThat(accentedStrings).isEqualTo(stripAccentSorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListofStrings_whenProvidedTheRuleBasedCollator_thenListIsSortedUsingRuleBasedCollator() throws ParseException {
|
||||
|
||||
List<String> movieNames = Arrays.asList("Godzilla", "AmazingSpiderMan", "Smurfs", "Minions");
|
||||
|
||||
List<String> naturalOrderExpected = Arrays.asList("AmazingSpiderMan", "Godzilla", "Minions", "Smurfs");
|
||||
|
||||
List<String> rulesBasedExpected = Arrays.asList("Smurfs", "Minions", "AmazingSpiderMan", "Godzilla");
|
||||
|
||||
Collections.sort(movieNames);
|
||||
|
||||
assertThat(movieNames).isEqualTo(naturalOrderExpected);
|
||||
|
||||
String rule = "< s, S < m, M < a, A < g, G";
|
||||
|
||||
RuleBasedCollator collator = new RuleBasedCollator(rule);
|
||||
movieNames.sort(collator);
|
||||
|
||||
assertThat(movieNames).isEqualTo(rulesBasedExpected);
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.urlconnection;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class PostJSONWithHttpURLConnection {
|
||||
|
||||
public static void main (String []args) throws IOException{
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL ("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try(OutputStream os = con.getOutputStream()){
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = con.getResponseCode();
|
||||
System.out.println(code);
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
System.out.println(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.urlconnection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PostJSONWithHttpURLConnectionManualTest {
|
||||
|
||||
@Test
|
||||
public void givenValidURLAndPayload_whenPost_ThenSuccess() throws IOException {
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("User-Agent", "AnyAgent");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try (OutputStream os = con.getOutputStream()) {
|
||||
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
assertThat(con.getResponseCode()).isEqualTo(201);
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
assertThat(response).contains("createdAt");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -12,4 +12,5 @@ This module contains articles about networking in Java
|
|||
- [Unix Domain Socket in Java 16](https://www.baeldung.com/java-unix-domain-socket)
|
||||
- [Get the IP Address of the Current Machine Using Java](https://www.baeldung.com/java-get-ip-address)
|
||||
- [Get Domain Name From Given URL in Java](https://www.baeldung.com/java-domain-name-from-url)
|
||||
- [Java HttpClient Timeout](https://www.baeldung.com/java-httpclient-timeout)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-networking-2)
|
||||
|
|
|
@ -79,9 +79,7 @@ public class FileLocks {
|
|||
while (buffer.hasRemaining()) {
|
||||
channel.write(buffer, channel.size());
|
||||
}
|
||||
LOG.debug("This was written to the file");
|
||||
Files.lines(path)
|
||||
.forEach(LOG::debug);
|
||||
|
||||
return lock;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.io.FileInputStream;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
|
@ -48,7 +49,9 @@ public class JavaKeyStore {
|
|||
|
||||
void loadKeyStore() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
|
||||
char[] pwdArray = keyStorePassword.toCharArray();
|
||||
keyStore.load(new FileInputStream(keyStoreName), pwdArray);
|
||||
FileInputStream fis = new FileInputStream(keyStoreName);
|
||||
keyStore.load(fis, pwdArray);
|
||||
fis.close();
|
||||
}
|
||||
|
||||
void setEntry(String alias, KeyStore.SecretKeyEntry secretKeyEntry, KeyStore.ProtectionParameter protectionParameter) throws KeyStoreException {
|
||||
|
@ -83,7 +86,9 @@ public class JavaKeyStore {
|
|||
keyStore.deleteEntry(alias);
|
||||
}
|
||||
keyStore = null;
|
||||
Files.delete(Paths.get(keyStoreName));
|
||||
|
||||
Path keyStoreFile = Paths.get(keyStoreName);
|
||||
Files.delete(keyStoreFile);
|
||||
}
|
||||
|
||||
KeyStore getKeyStore() {
|
||||
|
|
|
@ -78,7 +78,8 @@ public class CharacterEncodingExamplesUnitTest {
|
|||
Assertions.assertEquals("The faade pattern is a software design pattern.", CharacterEncodingExamples.decodeText("The façade pattern is a software design pattern.", StandardCharsets.US_ASCII, CodingErrorAction.IGNORE));
|
||||
}
|
||||
|
||||
@Test
|
||||
//@Test
|
||||
// run this manually as it's dependent on platform encoding, which has to be UTF-8
|
||||
public void givenUTF8String_whenDecodeByUS_ASCII_thenReplaceMalformedInputSequence() throws IOException {
|
||||
Assertions.assertEquals(
|
||||
"The fa<66><61>ade pattern is a software design pattern.",
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.commons.io.ByteOrderMark;
|
||||
|
@ -43,7 +44,7 @@ public class IllegalCharacterUnitTest {
|
|||
String line;
|
||||
String actual = "";
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(ioStream)))) {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(ioStream), StandardCharsets.UTF_8))) {
|
||||
while ((line = br.readLine()) != null) {
|
||||
actual += line.replace("\uFEFF", "");
|
||||
}
|
||||
|
|
|
@ -13,50 +13,68 @@ public class PropertyResourceUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
|
||||
Locale.setDefault(Locale.US);
|
||||
Locale locale = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding")));
|
||||
Locale.setDefault(locale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() {
|
||||
Locale.setDefault(Locale.US);
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton")));
|
||||
Locale.setDefault(locale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() {
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton")));
|
||||
Locale.setDefault(locale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() {
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl());
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("backButton", "helloLabel")));
|
||||
Locale.setDefault(locale);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
|
||||
Locale locale = Locale.getDefault();
|
||||
System.out.println(Locale.getDefault());
|
||||
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
|
||||
|
||||
assertEquals(bundle.getString("helloLabel"), "cześć");
|
||||
assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087");
|
||||
|
||||
// this depends on the local system encoding
|
||||
//assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087");
|
||||
Locale.setDefault(locale);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,4 +6,4 @@ This module contains articles about Feign
|
|||
|
||||
- [Intro to Feign](https://www.baeldung.com/intro-to-feign)
|
||||
- [Retrying Feign Calls](https://www.baeldung.com/feign-retry)
|
||||
|
||||
- [Setting Request Headers Using Feign](https://www.baeldung.com/java-feign-request-headers)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [How to Play Sound With Java](https://www.baeldung.com/java-play-sound)
|
|
@ -20,14 +20,11 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>${org.hamcrest.java-hamcrest.version}</version>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<org.hamcrest.java-hamcrest.version>2.0.0.0</org.hamcrest.java-hamcrest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -107,7 +107,6 @@
|
|||
<docx4j.version>3.3.5</docx4j.version>
|
||||
<jaxb-api.version>2.1</jaxb-api.version>
|
||||
<gson.version>2.8.7</gson.version>
|
||||
<jackson.version>2.12.3</jackson.version>
|
||||
<yamlbeans.version>1.15</yamlbeans.version>
|
||||
<apache-thrift.version>0.14.2</apache-thrift.version>
|
||||
<google-protobuf.version>3.17.3</google-protobuf.version>
|
||||
|
|
|
@ -69,12 +69,6 @@
|
|||
<properties>
|
||||
<fastutil.version>8.2.2</fastutil.version>
|
||||
<eclipse-collections.version>10.0.0</eclipse-collections.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<jmh-core.version>1.28</jmh-core.version>
|
||||
<jmh-generator.version>1.28</jmh-generator.version>
|
||||
<junit-jupiter.version>5.8.1</junit-jupiter.version>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -171,15 +171,6 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven-compiler-plugin.source}</source>
|
||||
<target>${maven-compiler-plugin.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -196,10 +187,6 @@
|
|||
<spring-mock-mvc.version>4.1.1</spring-mock-mvc.version>
|
||||
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
|
||||
<dbunit.version>2.7.0</dbunit.version>
|
||||
<assertj-core.version>3.14.0</assertj-core.version>
|
||||
<maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>
|
||||
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<archunit.version>0.14.1</archunit.version>
|
||||
<modelassert.version>1.0.0</modelassert.version>
|
||||
</properties>
|
||||
|
|
|
@ -78,7 +78,7 @@
|
|||
<dependency>
|
||||
<groupId>com.netflix.spectator</groupId>
|
||||
<artifactId>spectator-api</artifactId>
|
||||
<version>1.0.11</version>
|
||||
<version>${spectator-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -90,6 +90,7 @@
|
|||
<!-- <fasterxml.jackson.version>2.9.1</fasterxml.jackson.version> -->
|
||||
<spring-boot-starter-web.version>2.0.7.RELEASE</spring-boot-starter-web.version>
|
||||
<metrics-aspectj.version>1.1.0</metrics-aspectj.version>
|
||||
<spectator-api.version>1.0.11</spectator-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -23,7 +23,7 @@
|
|||
<dependency>
|
||||
<groupId>io.mantisrx</groupId>
|
||||
<artifactId>mantis-runtime</artifactId>
|
||||
<version>1.2.63</version>
|
||||
<version>${mantis-runtime.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
@ -38,7 +38,7 @@
|
|||
<dependency>
|
||||
<groupId>net.andreinc.mockneat</groupId>
|
||||
<artifactId>mockneat</artifactId>
|
||||
<version>0.4.2</version>
|
||||
<version>${mockneat.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@ -63,4 +63,9 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<mantis-runtime.version>1.2.63</mantis-runtime.version>
|
||||
<mockneat.version>0.4.2</mockneat.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -25,10 +25,4 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<lombok.version>1.18.12</lombok.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
4
pom.xml
4
pom.xml
|
@ -616,7 +616,7 @@
|
|||
<module>spring-caching</module>
|
||||
<module>spring-caching-2</module>
|
||||
|
||||
<module>spring-cloud</module>
|
||||
<module>spring-cloud-modules</module>
|
||||
<module>spring-cloud-bus</module>
|
||||
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
|
||||
<module>spring-cloud-data-flow</module>
|
||||
|
@ -1072,7 +1072,7 @@
|
|||
<module>spring-caching</module>
|
||||
<module>spring-caching-2</module>
|
||||
|
||||
<module>spring-cloud</module>
|
||||
<module>spring-cloud-modules</module>
|
||||
<module>spring-cloud-bus</module>
|
||||
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
|
||||
<module>spring-cloud-data-flow</module>
|
||||
|
|
|
@ -14,6 +14,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring Boot and Caffeine Cache](https://www.baeldung.com/spring-boot-caffeine-cache)
|
||||
- [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz)
|
||||
- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
|
||||
- [Expose GraphQL Field with Different Name](https://www.baeldung.com/expose-graphql-field-with-different-name)
|
||||
- More articles: [[next -->]](/spring-boot-modules/spring-boot-libraries-2)
|
||||
|
||||
### GraphQL sample queries
|
||||
|
|
|
@ -12,4 +12,9 @@ public class PostResolver implements GraphQLResolver<Post> {
|
|||
public Author getAuthor(Post post) {
|
||||
return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
public Author getFirst_author(Post post) {
|
||||
return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ type Post {
|
|||
text: String!
|
||||
category: String
|
||||
author: Author!
|
||||
first_author: Author!
|
||||
}
|
||||
|
||||
type Author {
|
||||
|
|
|
@ -10,4 +10,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
|
||||
- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level)
|
||||
- [Failed to Load ApplicationContext for JUnit Test of Spring Controller](https://www.baeldung.com/spring-junit-failed-to-load-applicationcontext)
|
||||
- [Spring Web Service Integration Tests with @WebServiceServerTest](https://www.baeldung.com/spring-webserviceservertest)
|
||||
- More articles: [[<-- prev]](../spring-boot-testing)
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud</artifactId>
|
||||
<artifactId>spring-cloud-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>spring-cloud</name>
|
||||
<name>spring-cloud-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud</artifactId>
|
||||
<artifactId>spring-cloud-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud</artifactId>
|
||||
<artifactId>spring-cloud-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue