Merge remote-tracking branch 'upstream/master' into JAVA-3570
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.java14.newfeatues;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MultilineUnitTest {
|
||||
|
||||
@SuppressWarnings("preview")
|
||||
String multiline = """
|
||||
A quick brown fox jumps over a lazy dog; \
|
||||
the lazy dog howls loudly.""";
|
||||
|
||||
@SuppressWarnings("preview")
|
||||
String anotherMultiline = """
|
||||
A quick brown fox jumps over a lazy dog;
|
||||
the lazy dog howls loudly.""";
|
||||
|
||||
@Test
|
||||
public void givenMultilineString_whenSlashUsed_thenNoNewLine() {
|
||||
assertFalse(multiline.contains("\n"));
|
||||
assertTrue(anotherMultiline.contains("\n"));
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.java14.newfeatues;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RecordUnitTest {
|
||||
|
||||
@SuppressWarnings("preview")
|
||||
public record User(int id, String password) {
|
||||
};
|
||||
|
||||
private User user1 = new User(0, "UserOne");
|
||||
|
||||
@Test
|
||||
public void givenRecord_whenObjInitialized_thenValuesCanBeFetchedWithGetters() {
|
||||
|
||||
assertEquals(0, user1.id());
|
||||
assertEquals("UserOne", user1.password());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRecord_thenEqualsImplemented() {
|
||||
|
||||
User user2 = user1;
|
||||
|
||||
assertEquals(user1, user2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRecord_thenToStringImplemented() {
|
||||
|
||||
assertTrue(user1.toString()
|
||||
.contains("UserOne"));
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.java14.newfeatues;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SwitchExprUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDay_whenSunday_thenWeekend() {
|
||||
assertTrue(isTodayHolidayInJava8("SUNDAY"));
|
||||
|
||||
assertTrue(isTodayHolidayInJava14("SUNDAY"));
|
||||
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava8("SOMEDAY"));
|
||||
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> isTodayHolidayInJava14("SOMEDAY"));
|
||||
}
|
||||
|
||||
private boolean isTodayHolidayInJava8(String day) {
|
||||
|
||||
boolean isTodayHoliday;
|
||||
switch (day) {
|
||||
case "MONDAY":
|
||||
case "TUESDAY":
|
||||
case "WEDNESDAY":
|
||||
case "THURSDAY":
|
||||
case "FRIDAY":
|
||||
isTodayHoliday = false;
|
||||
break;
|
||||
case "SATURDAY":
|
||||
case "SUNDAY":
|
||||
isTodayHoliday = true;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("What's a " + day);
|
||||
}
|
||||
return isTodayHoliday;
|
||||
}
|
||||
|
||||
private boolean isTodayHolidayInJava14(String day) {
|
||||
return switch (day) {
|
||||
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> false;
|
||||
case "SATURDAY", "SUNDAY" -> true;
|
||||
default -> throw new IllegalArgumentException("What's a " + day);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
=========
|
||||
|
||||
## Core Java Collections Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [ArrayList vs. LinkedList vs. HashMap in Java](https://www.baeldung.com/java-arraylist-vs-linkedlist-vs-hashmap)
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<artifactId>core-java-collections-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-4</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.18.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.collections.comparation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArrayListUnitTest {
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenItemAddedToSpecificIndex_thenItCanBeRetrieved() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Daniel");
|
||||
list.add(0, "Marko");
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0)).isEqualTo("Marko");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayList_whenItemRemovedViaIndex_thenListSizeIsReduced() {
|
||||
List<String> list = new ArrayList<>(Arrays.asList("Daniel", "Marko"));
|
||||
list.remove(1);
|
||||
assertThat(list).hasSize(1);
|
||||
assertThat(list).doesNotContain("Marko");
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.collections.comparation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class HashMapUnitTest {
|
||||
|
||||
@Test
|
||||
void givenHashMap_whenItemAddedByKey_thenItCanBeRetrieved() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("123456", "Daniel");
|
||||
map.put("654321", "Marko");
|
||||
assertThat(map.get("654321")).isEqualTo("Marko");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenHashMap_whenItemRemovedByKey_thenMapSizeIsReduced() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("123456", "Daniel");
|
||||
map.put("654321", "Marko");
|
||||
map.remove("654321");
|
||||
assertThat(map).hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.collections.comparation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LinkedListUnitTest {
|
||||
|
||||
@Test
|
||||
void givenLinkedList_whenItemIsAppended_thenItCanBeRetrieved() {
|
||||
LinkedList<String> list = new LinkedList<>();
|
||||
list.addLast("Daniel");
|
||||
list.addFirst("Marko");
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.getLast()).isEqualTo("Daniel");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLinkedList_whenItemIsRemoved_thenListSizeIsReduced() {
|
||||
LinkedList<String> list = new LinkedList<>(Arrays.asList("Daniel", "Marko", "David"));
|
||||
list.removeFirst();
|
||||
list.removeLast();
|
||||
assertThat(list).hasSize(1);
|
||||
assertThat(list).containsExactly("Marko");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenLinkedList_whenItemInserted_thenItCanBeRetrievedAndDeleted() {
|
||||
LinkedList<String> list = new LinkedList<>();
|
||||
list.push("Daniel");
|
||||
list.push("Marko");
|
||||
assertThat(list.poll()).isEqualTo("Marko");
|
||||
assertThat(list).hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.collections.comparation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class ListVsMapUnitTest {
|
||||
|
||||
@Test
|
||||
void givenList_whenIteratingTroughValues_thenEachValueIsPresent() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("Daniel");
|
||||
list.add("Marko");
|
||||
for (String name : list) {
|
||||
assertThat(name).isIn(list);
|
||||
}
|
||||
assertThat(list).containsExactly("Daniel", "Marko");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMap_whenIteratingTroughValues_thenEachValueIsPresent() {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
map.put(1, "Daniel");
|
||||
map.put(2, "Marko");
|
||||
for (String name : map.values()) {
|
||||
assertThat(name).isIn(map.values());
|
||||
}
|
||||
assertThat(map.values()).containsExactlyInAnyOrder("Daniel", "Marko");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,4 +13,5 @@ This module contains articles about core Java input and output (IO)
|
||||
- [Reading a Line at a Given Line Number From a File in Java](https://www.baeldung.com/java-read-line-at-number)
|
||||
- [Find the Last Modified File in a Directory with Java](https://www.baeldung.com/java-last-modified-file)
|
||||
- [Get a Filename Without the Extension in Java](https://www.baeldung.com/java-filename-without-extension)
|
||||
- [Writing byte[] to a File in Java](https://www.baeldung.com/java-write-byte-array-file)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.writebytearray;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import com.google.common.io.ByteSink;
|
||||
import com.google.common.io.MoreFiles;
|
||||
|
||||
public class WriteByteArrayUnitTest {
|
||||
private static byte[] dataForWriting;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException {
|
||||
dataForWriting = Files.readAllBytes(Paths.get("src/test/resources/example-image.jpg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFileOutputStream_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-fos.jpg");
|
||||
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
|
||||
outputStream.write(dataForWriting);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingNioFiles_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-nio-files.jpg");
|
||||
Files.write(outputFile.toPath(), dataForWriting);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGuavaFiles_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-guava-files.jpg");
|
||||
com.google.common.io.Files.write(dataForWriting, outputFile);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGuavaByteSink_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-guava-bs.jpg");
|
||||
ByteSink byteSink = com.google.common.io.Files.asByteSink(outputFile);
|
||||
byteSink.write(dataForWriting);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGuavaByteSinkMoreFiles_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-guava-bs.jpg");
|
||||
ByteSink byteSink = MoreFiles.asByteSink(outputFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
|
||||
byteSink.write(dataForWriting);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCommonsIo_thenByteArrayIsWritten() throws IOException {
|
||||
File outputFile = tempFolder.newFile("example-file-utils.jpg");
|
||||
FileUtils.writeByteArrayToFile(outputFile, dataForWriting);
|
||||
assertThat(outputFile).hasBinaryContent(dataForWriting);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
@@ -14,4 +14,5 @@ This module contains articles about core Java Security
|
||||
- [Get a List of Trusted Certificates in Java](https://www.baeldung.com/java-list-trusted-certificates)
|
||||
- [Security Context Basics: User, Subject and Principal](https://www.baeldung.com/security-context-basics)
|
||||
- [Java AES Encryption and Decryption](https://www.baeldung.com/java-aes-encryption-decryption)
|
||||
- [InvalidAlgorithmParameterException: Wrong IV Length](https://www.baeldung.com/java-invalidalgorithmparameter-exception)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security)
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
<module>core-java-collections</module>
|
||||
<module>core-java-collections-2</module>
|
||||
<module>core-java-collections-3</module>
|
||||
<module>core-java-collections-4</module>
|
||||
<module>core-java-collections-array-list</module>
|
||||
<module>core-java-collections-list</module>
|
||||
<module>core-java-collections-list-2</module>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.map.bytearrays;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class BytesKey {
|
||||
private final byte[] array;
|
||||
|
||||
public BytesKey(byte[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public byte[] getArray() {
|
||||
return array.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
BytesKey bytesKey = (BytesKey) o;
|
||||
return Arrays.equals(array, bytesKey.array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(array);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package com.baeldung.map.bytearrays;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ByteArrayKeyUnitTest {
|
||||
@Test
|
||||
void givenPrimitiveByteArrayKey_whenRetrievingFromMap_shouldRetrieveDifferentObjects() {
|
||||
// given
|
||||
byte[] key1 = {1, 2, 3};
|
||||
byte[] key2 = {1, 2, 3};
|
||||
String value1 = "value1";
|
||||
String value2 = "value2";
|
||||
Map<byte[], String> map = new HashMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
|
||||
// when
|
||||
String retrievedValue1 = map.get(key1);
|
||||
String retrievedValue2 = map.get(key2);
|
||||
String retrievedValue3 = map.get(new byte[]{1, 2, 3});
|
||||
|
||||
// then
|
||||
assertThat(retrievedValue1).isEqualTo(value1);
|
||||
assertThat(retrievedValue2).isEqualTo(value2);
|
||||
assertThat(retrievedValue3).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEncodedStringKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() {
|
||||
// given
|
||||
String key1 = Base64.getEncoder().encodeToString(new byte[]{1, 2, 3});
|
||||
String key2 = Base64.getEncoder().encodeToString(new byte[]{1, 2, 3});
|
||||
String value1 = "value1";
|
||||
String value2 = "value2";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
|
||||
// when
|
||||
String retrievedValue1 = map.get(key1);
|
||||
String retrievedValue2 = map.get(key2);
|
||||
|
||||
// then
|
||||
assertThat(key1).isEqualTo(key2);
|
||||
assertThat(retrievedValue1).isEqualTo(value2);
|
||||
assertThat(retrievedValue2).isEqualTo(value2);
|
||||
assertThat(retrievedValue1).isEqualTo(retrievedValue2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenByteListKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() {
|
||||
// given
|
||||
List<Byte> key1 = ImmutableList.of((byte)1, (byte)2, (byte)3);
|
||||
List<Byte> key2 = ImmutableList.of((byte)1, (byte)2, (byte)3);
|
||||
String value1 = "value1";
|
||||
String value2 = "value2";
|
||||
Map<List<Byte>, String> map = new HashMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
|
||||
// when
|
||||
String retrievedValue1 = map.get(key1);
|
||||
String retrievedValue2 = map.get(key2);
|
||||
|
||||
// then
|
||||
assertThat(key1).isEqualTo(key2);
|
||||
assertThat(retrievedValue1).isEqualTo(value2);
|
||||
assertThat(retrievedValue2).isEqualTo(value2);
|
||||
assertThat(retrievedValue1).isEqualTo(retrievedValue2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCustomWrapperKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() {
|
||||
// given
|
||||
BytesKey key1 = new BytesKey(new byte[]{1, 2, 3});
|
||||
BytesKey key2 = new BytesKey(new byte[]{1, 2, 3});
|
||||
String value1 = "value1";
|
||||
String value2 = "value2";
|
||||
Map<BytesKey, String> map = new HashMap<>();
|
||||
map.put(key1, value1);
|
||||
map.put(key2, value2);
|
||||
|
||||
// when
|
||||
String retrievedValue1 = map.get(key1);
|
||||
String retrievedValue2 = map.get(key2);
|
||||
String retrievedValue3 = map.get(new BytesKey(new byte[]{1, 2, 3}));
|
||||
|
||||
// then
|
||||
assertThat(key1).isEqualTo(key2);
|
||||
assertThat(retrievedValue1).isEqualTo(value2);
|
||||
assertThat(retrievedValue2).isEqualTo(value2);
|
||||
assertThat(retrievedValue1).isEqualTo(retrievedValue2);
|
||||
assertThat(retrievedValue3).isEqualTo(value2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,22 +6,21 @@ import static org.junit.Assert.assertEquals;
|
||||
public class PerfectSquareUnitTest {
|
||||
|
||||
@Test
|
||||
public void test0xFFAssignedToInteger() {
|
||||
public void testIsNumberPerfectSquare() {
|
||||
long n = 18676209273604L; // 18676209273604 = 43621598 * 43621598
|
||||
boolean expectedValue = true;
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test0xFFAssignedToByte() {
|
||||
long n = 549790047707L; // prime number
|
||||
boolean expectedValue = false;
|
||||
n = 549790047707L; // prime number
|
||||
expectedValue = false;
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingSqrt(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingBinarySearch(1, Integer.MAX_VALUE, n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareByUsingNewtonMethod(n));
|
||||
assertEquals(expectedValue, PerfectSquareUtil.isPerfectSquareWithOptimization(n));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Getting Database URL From JDBC Connection Object](https://www.baeldung.com/jdbc-get-url-from-connection)
|
||||
- [JDBC URL Format For Different Databases](https://www.baeldung.com/java-jdbc-url-format)
|
||||
|
||||
@@ -20,10 +20,43 @@
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.driver.version}</version>
|
||||
</dependency>
|
||||
<!--
|
||||
==================================================================
|
||||
This jar is not in maven central repo due to some license issues.
|
||||
We need to manually download and install to maven repository.
|
||||
==================================================================
|
||||
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc14</artifactId>
|
||||
<version>${oracle.driver.version}</version>
|
||||
</dependency>
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
<version>${mssql.driver.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.200</h2.version>
|
||||
<postgresql.version>42.2.5.jre7</postgresql.version>
|
||||
<mssql.driver.version>8.4.1.jre11</mssql.driver.version>
|
||||
<oracle.driver.version>10.2.0.4.0</oracle.driver.version>
|
||||
<mysql.driver.version>8.0.22</mysql.driver.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.jdbcurlformat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class JdbcUrlFormatLiveTest {
|
||||
@Test
|
||||
public void givenOracleSID_thenCreateConnectionObject() {
|
||||
String oracleJdbcUrl = "jdbc:oracle:thin:@myoracle.db.server:1521:my_sid";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOracleServiceName_thenCreateConnectionObject() {
|
||||
String oracleJdbcUrl = "jdbc:oracle:thin:@//myoracle.db.server:1521/my_servicename";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOracleTnsnames_thenCreateConnectionObject() {
|
||||
String oracleJdbcUrl = "jdbc:oracle:thin:@" +
|
||||
"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" +
|
||||
"(HOST=myoracle.db.server)(PORT=1521))" +
|
||||
"(CONNECT_DATA=(SERVICE_NAME=my_servicename)))";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(oracleJdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMysqlDb_thenCreateConnectionObject() {
|
||||
String jdbcUrl = "jdbc:mysql://mysql.db.server:3306/my_database?useSSL=false&serverTimezone=UTC";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMssqlDb_thenCreateConnectionObject() {
|
||||
String jdbcUrl = "jdbc:sqlserver://mssql.db.server\\mssql_instance;databaseName=my_database";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPostgreSqlDb_thenCreateConnectionObject() {
|
||||
String jdbcUrl = "jdbc:postgresql://postgresql.db.server:5430/my_database?ssl=true&loglevel=2";
|
||||
String username = "dbUser";
|
||||
String password = "1234567";
|
||||
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
|
||||
assertNotNull(conn);
|
||||
} catch (SQLException e) {
|
||||
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -662,8 +662,7 @@
|
||||
|
||||
<module>spring-mobile</module>
|
||||
<module>spring-mockito</module>
|
||||
|
||||
<module>spring-mvc-basics</module>
|
||||
|
||||
<module>spring-mvc-basics-2</module>
|
||||
<module>spring-mvc-basics-3</module>
|
||||
<module>spring-mvc-basics-4</module>
|
||||
@@ -714,6 +713,7 @@
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
|
||||
<module>spring-web-modules</module>
|
||||
<module>spring-webflux-amqp</module> <!-- long -->
|
||||
<module>spring-websockets</module>
|
||||
|
||||
@@ -1148,8 +1148,7 @@
|
||||
|
||||
<module>spring-mobile</module>
|
||||
<module>spring-mockito</module>
|
||||
|
||||
<module>spring-mvc-basics</module>
|
||||
|
||||
<module>spring-mvc-basics-2</module>
|
||||
<module>spring-mvc-basics-3</module>
|
||||
<module>spring-mvc-basics-4</module>
|
||||
@@ -1199,6 +1198,7 @@
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
|
||||
<module>spring-web-modules</module>
|
||||
<module>spring-webflux-amqp</module> <!-- long -->
|
||||
<module>spring-websockets</module>
|
||||
|
||||
|
||||
@@ -47,6 +47,17 @@
|
||||
<artifactId>camel-spring-javaconfig</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-jackson</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-test</artifactId>
|
||||
<version>${env.camel.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.camel.jackson;
|
||||
|
||||
public class Fruit {
|
||||
|
||||
private String name;
|
||||
private int id;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.camel.jackson;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FruitList {
|
||||
|
||||
private List<Fruit> fruits;
|
||||
|
||||
public List<Fruit> getFruits() {
|
||||
return fruits;
|
||||
}
|
||||
|
||||
public void setFruits(List<Fruit> fruits) {
|
||||
this.fruits = fruits;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.camel.jackson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.component.jackson.ListJacksonDataFormat;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.junit4.CamelTestSupport;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FruitArrayJacksonUnmarshalUnitTest extends CamelTestSupport {
|
||||
|
||||
@Test
|
||||
public void givenJsonFruitArray_whenUnmarshalled_thenSuccess() throws Exception {
|
||||
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
|
||||
mock.expectedMessageCount(1);
|
||||
mock.message(0).body().isInstanceOf(List.class);
|
||||
|
||||
String json = readJsonFromFile("/json/fruit-array.json");
|
||||
template.sendBody("direct:jsonInput", json);
|
||||
assertMockEndpointsSatisfied();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Fruit> fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
|
||||
assertNotNull("Fruit lists should not be null", fruitList);
|
||||
|
||||
assertEquals("There should be two fruits", 2, fruitList.size());
|
||||
|
||||
Fruit fruit = fruitList.get(0);
|
||||
assertEquals("Fruit name", "Banana", fruit.getName());
|
||||
assertEquals("Fruit id", 100, fruit.getId());
|
||||
|
||||
fruit = fruitList.get(1);
|
||||
assertEquals("Fruit name", "Apple", fruit.getName());
|
||||
assertEquals("Fruit id", 101, fruit.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RouteBuilder createRouteBuilder() throws Exception {
|
||||
return new RouteBuilder() {
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
|
||||
.to("mock:marshalledObject");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
|
||||
URL resource = FruitArrayJacksonUnmarshalUnitTest.class.getResource(path);
|
||||
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.camel.jackson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.component.jackson.JacksonDataFormat;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.junit4.CamelTestSupport;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FruitListJacksonUnmarshalUnitTest extends CamelTestSupport {
|
||||
|
||||
@Test
|
||||
public void givenJsonFruitList_whenUnmarshalled_thenSuccess() throws Exception {
|
||||
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
|
||||
mock.expectedMessageCount(1);
|
||||
mock.message(0).body().isInstanceOf(FruitList.class);
|
||||
|
||||
String json = readJsonFromFile("/json/fruit-list.json");
|
||||
template.sendBody("direct:jsonInput", json);
|
||||
assertMockEndpointsSatisfied();
|
||||
|
||||
FruitList fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(FruitList.class);
|
||||
assertNotNull("Fruit lists should not be null", fruitList);
|
||||
|
||||
List<Fruit> fruits = fruitList.getFruits();
|
||||
assertEquals("There should be two fruits", 2, fruits.size());
|
||||
|
||||
Fruit fruit = fruits.get(0);
|
||||
assertEquals("Fruit name", "Banana", fruit.getName());
|
||||
assertEquals("Fruit id", 100, fruit.getId());
|
||||
|
||||
fruit = fruits.get(1);
|
||||
assertEquals("Fruit name", "Apple", fruit.getName());
|
||||
assertEquals("Fruit id", 101, fruit.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RouteBuilder createRouteBuilder() throws Exception {
|
||||
return new RouteBuilder() {
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
from("direct:jsonInput").unmarshal(new JacksonDataFormat(FruitList.class))
|
||||
.to("mock:marshalledObject");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
|
||||
URL resource = FruitListJacksonUnmarshalUnitTest.class.getResource(path);
|
||||
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"id": 100,
|
||||
"name": "Banana"
|
||||
},
|
||||
{
|
||||
"id": 101,
|
||||
"name": "Apple"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"fruits": [
|
||||
{
|
||||
"id": 100,
|
||||
"name": "Banana"
|
||||
},
|
||||
{
|
||||
"id": 101,
|
||||
"name": "Apple"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -69,6 +69,7 @@
|
||||
<module>spring-boot-vue</module>
|
||||
<module>spring-boot-xml</module>
|
||||
<module>spring-boot-actuator</module>
|
||||
<module>spring-boot-data-2</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-boot-data-2</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user