diff --git a/core-java-modules/core-java-17/pom.xml b/core-java-modules/core-java-17/pom.xml
index f1f4edc484..e422bd62bb 100644
--- a/core-java-modules/core-java-17/pom.xml
+++ b/core-java-modules/core-java-17/pom.xml
@@ -23,6 +23,18 @@
${assertj.version}
test
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ ${junit-jupiter.version}
+ test
+
@@ -38,32 +50,35 @@
${maven.compiler.target.version}
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${surefire.plugin.version}
-
- --enable-preview
- 1
-
-
-
- org.apache.maven.surefire
- surefire-api
- ${surefire.plugin.version}
-
-
-
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ ${surefire.plugin.version}
+
+
+ --enable-preview
+ --enable-native-access=core.java
+ 1
+
+
+
+
+ org.apache.maven.surefire
+ surefire-api
+ ${surefire.plugin.version}
+
+
+
17
17
- 17
- 3.8.1
- 3.0.0-M5
- 3.17.2
+ 17
+ 3.8.1
+ 3.0.0-M5
+ 3.17.2
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java
new file mode 100644
index 0000000000..0890b025be
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP356.java
@@ -0,0 +1,20 @@
+package com.baeldung.features;
+
+import java.util.random.RandomGeneratorFactory;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+public class JEP356 {
+
+ public Stream getAllAlgorithms() {
+ return RandomGeneratorFactory.all().map(RandomGeneratorFactory::name);
+ }
+
+ public IntStream getPseudoInts(String algorithm, int streamSize) {
+ // returns an IntStream with size @streamSize of random numbers generated using the @algorithm
+ // where the lower bound is 0 and the upper is 100 (exclusive)
+ return RandomGeneratorFactory.of(algorithm)
+ .create()
+ .ints(streamSize, 0,100);
+ }
+}
diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java
new file mode 100644
index 0000000000..2bc3a664d1
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP406.java
@@ -0,0 +1,28 @@
+package com.baeldung.features;
+
+import com.baeldung.features.JEP409.Circle;
+import com.baeldung.features.JEP409.Shape;
+import com.baeldung.features.JEP409.Triangle;
+
+public class JEP406 {
+
+ static record Human (String name, int age, String profession) {}
+
+ public String checkObject(Object obj) {
+ return switch (obj) {
+ case Human h -> "Name: %s, age: %s and profession: %s".formatted(h.name(), h.age(), h.profession());
+ case Circle c -> "This is a circle";
+ case Shape s -> "It is just a shape";
+ case null -> "It is null";
+ default -> "It is an object";
+ };
+ }
+
+ public String checkShape(Shape shape) {
+ return switch (shape) {
+ case Triangle t && (t.getNumberOfSides() != 3) -> "This is a weird triangle";
+ case Circle c && (c.getNumberOfSides() != 0) -> "This is a weird circle";
+ default -> "Just a normal shape";
+ };
+ }
+}
diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java
new file mode 100644
index 0000000000..0fc3d6f467
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP409.java
@@ -0,0 +1,38 @@
+package com.baeldung.features;
+
+public class JEP409 {
+
+ sealed interface Shape permits Rectangle, Circle, Square, Triangle {
+ int getNumberOfSides();
+ }
+
+ static final class Rectangle implements Shape {
+ @Override
+ public int getNumberOfSides() {
+ return 4;
+ }
+ }
+
+ static final class Circle implements Shape {
+ @Override
+ public int getNumberOfSides() {
+ return 0;
+ }
+ }
+
+ static final class Square implements Shape {
+ @Override
+ public int getNumberOfSides() {
+ return 4;
+ }
+ }
+
+ static non-sealed class Triangle implements Shape {
+
+ @Override
+ public int getNumberOfSides() {
+ return 3;
+ }
+ }
+
+}
diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java
new file mode 100644
index 0000000000..8c998629c9
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP412.java
@@ -0,0 +1,56 @@
+package com.baeldung.features;
+
+import jdk.incubator.foreign.CLinker;
+import jdk.incubator.foreign.FunctionDescriptor;
+import jdk.incubator.foreign.MemoryAddress;
+import jdk.incubator.foreign.SymbolLookup;
+
+import java.io.IOException;
+import java.lang.invoke.MethodType;
+
+import static jdk.incubator.foreign.ResourceScope.newImplicitScope;
+
+public class JEP412 {
+
+ private static final SymbolLookup libLookup;
+
+ static {
+ var resource = JEP412.class.getResource("/compile_c.sh");
+ try {
+ var process = new ProcessBuilder("sh", resource.getPath()).start();
+ while (process.isAlive()) {}
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+
+ var path = JEP412.class.getResource("/print_name.so").getPath();
+ System.load(path);
+ libLookup = SymbolLookup.loaderLookup();
+ }
+
+ public String getPrintNameFormat(String name){
+
+ var printMethod = libLookup.lookup("printName");
+
+ if (printMethod.isPresent()) {
+ var methodReference = CLinker.getInstance()
+ .downcallHandle(
+ printMethod.get(),
+ MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
+ FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER)
+ );
+
+ try {
+ var nativeString = CLinker.toCString(name, newImplicitScope());
+ var invokeReturn = methodReference.invoke(nativeString.address());
+ var memoryAddress = (MemoryAddress) invokeReturn;
+ return CLinker.toJavaString(memoryAddress);
+ } catch (Throwable throwable) {
+ throw new RuntimeException(throwable);
+ }
+ }
+ throw new RuntimeException("printName function not found.");
+ }
+}
+
+
diff --git a/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java
new file mode 100644
index 0000000000..aaccc4a665
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/com/baeldung/features/JEP414.java
@@ -0,0 +1,27 @@
+package com.baeldung.features;
+
+import jdk.incubator.vector.FloatVector;
+import jdk.incubator.vector.VectorSpecies;
+
+public class JEP414 {
+
+ private static final VectorSpecies SPECIES = FloatVector.SPECIES_PREFERRED;
+
+
+ public void newVectorComputation(float[] a, float[] b, float[] c) {
+ for (var i = 0; i < a.length; i += SPECIES.length()) {
+ var m = SPECIES.indexInRange(i, a.length);
+ var va = FloatVector.fromArray(SPECIES, a, i, m);
+ var vb = FloatVector.fromArray(SPECIES, b, i, m);
+ var vc = va.mul(vb);
+ vc.intoArray(c, i, m);
+ }
+ }
+
+ public void commonVectorComputation(float[] a, float[] b, float[] c) {
+ for (var i = 0; i < a.length; i ++) {
+ c[i] = a[i] * b[i];
+ }
+ }
+
+}
diff --git a/core-java-modules/core-java-17/src/main/java/module-info.java b/core-java-modules/core-java-17/src/main/java/module-info.java
new file mode 100644
index 0000000000..b1ce62aa68
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/java/module-info.java
@@ -0,0 +1,4 @@
+module core.java {
+ requires jdk.incubator.vector;
+ requires jdk.incubator.foreign;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/main/resources/compile_c.sh b/core-java-modules/core-java-17/src/main/resources/compile_c.sh
new file mode 100755
index 0000000000..d2e0629cc5
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/resources/compile_c.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
+
+gcc -c -fPIC $SCRIPTPATH/print_name.c
+gcc -shared -rdynamic -o print_name.so print_name.o
+
+mv print_name.so $SCRIPTPATH/
+mv print_name.o $SCRIPTPATH/
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/main/resources/print_name.c b/core-java-modules/core-java-17/src/main/resources/print_name.c
new file mode 100644
index 0000000000..7bda12e352
--- /dev/null
+++ b/core-java-modules/core-java-17/src/main/resources/print_name.c
@@ -0,0 +1,8 @@
+#include
+#include
+
+char* printName(char *name) {
+ char* newString = (char*)malloc((15 + sizeof(name))*sizeof(char));
+ sprintf(newString, "Your name is %s", name);
+ return newString;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java
new file mode 100644
index 0000000000..b4f61a6f24
--- /dev/null
+++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP356UnitTest.java
@@ -0,0 +1,19 @@
+package com.baeldung.features;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JEP356UnitTest {
+
+ @Test
+ void getPseudoInts_whenUsingAlgorithmXoroshiro128PlusPlus_shouldReturnStreamOfRandomInteger() {
+ var algorithm = "Xoshiro256PlusPlus";
+ var streamSize = 100;
+
+ JEP356 jep356 = new JEP356();
+
+ jep356.getPseudoInts(algorithm, streamSize)
+ .forEach(value -> assertThat(value).isLessThanOrEqualTo(99));
+ }
+}
diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java
new file mode 100644
index 0000000000..87e6f3bec6
--- /dev/null
+++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP406UnitTest.java
@@ -0,0 +1,33 @@
+package com.baeldung.features;
+
+import com.baeldung.features.JEP406.Human;
+import com.baeldung.features.JEP409.Square;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class JEP406UnitTest {
+
+ @Test
+ void checkObject_shouldMatchWithHuman() {
+ var jep406 = new JEP406();
+
+ var human = new Human("John", 31, "Developer");
+
+ var checkResult = jep406.checkObject(human);
+
+ assertEquals("Name: John, age: 31 and profession: Developer", checkResult);
+ }
+
+ @Test
+ void checkShape_shouldMatchWithShape() {
+ var jep406 = new JEP406();
+
+ var square = new Square();
+
+ var checkResult = jep406.checkShape(square);
+
+ assertEquals("Just a normal shape", checkResult);
+ }
+
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java
new file mode 100644
index 0000000000..41380ec2bb
--- /dev/null
+++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP409UnitTest.java
@@ -0,0 +1,39 @@
+package com.baeldung.features;
+
+import com.baeldung.features.JEP409.*;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+class JEP409UnitTest {
+
+ static class WeirdTriangle extends Triangle {
+ @Override public int getNumberOfSides() {
+ return 40;
+ }
+ }
+
+ @Test
+ void testSealedClass_shouldInvokeRightClass() {
+
+ Shape shape = spy(new WeirdTriangle());
+
+ int numberOfSides = getNumberOfSides(shape);
+
+ assertEquals(40, numberOfSides);
+ verify(shape).getNumberOfSides();
+ }
+
+ int getNumberOfSides(Shape shape) {
+ return switch (shape) {
+ case WeirdTriangle t -> t.getNumberOfSides();
+ case Circle c -> c.getNumberOfSides();
+ case Triangle t -> t.getNumberOfSides();
+ case Rectangle r -> r.getNumberOfSides();
+ case Square s -> s.getNumberOfSides();
+ };
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java
new file mode 100644
index 0000000000..66e7d952c7
--- /dev/null
+++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP412UnitTest.java
@@ -0,0 +1,17 @@
+package com.baeldung.features;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class JEP412UnitTest {
+
+ @Test
+ void getPrintNameFormat_whenPassingAName_shouldReceiveItFormatted() {
+ var jep412 = new JEP412();
+
+ var formattedName = jep412.getPrintNameFormat("John");
+
+ assertEquals("Your name is John", formattedName);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java
new file mode 100644
index 0000000000..459344d907
--- /dev/null
+++ b/core-java-modules/core-java-17/src/test/java/com/baeldung/features/JEP414UnitTest.java
@@ -0,0 +1,38 @@
+package com.baeldung.features;
+
+import org.junit.jupiter.api.Test;
+
+import static java.util.stream.Collectors.toList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class JEP414UnitTest {
+
+ @Test
+ void vectorComputation_shouldMultiplyVectors() {
+ var jep414 = new JEP414();
+
+ float[] a = initArray(100);
+ float[] b = initArray(100);
+ float[] c = new float[100];
+ float[] d = new float[100];
+
+ jep414.newVectorComputation(a, b, c);
+ jep414.commonVectorComputation(a, b, d);
+
+ for (int i = 0; i < a.length; i++) {
+ assertEquals(c[i], d[i]);
+ }
+ }
+
+ private float[] initArray(int size) {
+ final var jep356 = new JEP356();
+ final var values = new float[size];
+ final var pseudoInts = jep356.getPseudoInts("Xoshiro256PlusPlus", size).mapToObj(Float::valueOf).collect(toList());
+
+ for (int i = 0; i < pseudoInts.size(); i++) {
+ values[i] = pseudoInts.get(i);
+ }
+
+ return values;
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-maps-4/pom.xml b/core-java-modules/core-java-collections-maps-4/pom.xml
index 1835e3ceac..93ec782c94 100644
--- a/core-java-modules/core-java-collections-maps-4/pom.xml
+++ b/core-java-modules/core-java-collections-maps-4/pom.xml
@@ -1,34 +1,55 @@
- 4.0.0
- core-java-collections-maps-4
- 0.1.0-SNAPSHOT
- core-java-collections-maps-4
- jar
+ 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">
+ 4.0.0
+ core-java-collections-maps-4
+ 0.1.0-SNAPSHOT
+ core-java-collections-maps-4
+ jar
-
- com.baeldung.core-java-modules
- core-java-modules
- 0.0.1-SNAPSHOT
- ../pom.xml
-
+
+ com.baeldung.core-java-modules
+ core-java-modules
+ 0.0.1-SNAPSHOT
+ ../pom.xml
+
+
+
+ UTF-8
+ 1.8
+ 1.8
+
-
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- 3.0.0-M5
-
-
- !performance
-
-
-
-
-
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+ org.hamcrest
+ hamcrest-all
+ 1.3
+ test
+
+
-
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0-M5
+
+
+ !performance
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Address.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Address.java
new file mode 100644
index 0000000000..410758405e
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Address.java
@@ -0,0 +1,32 @@
+package com.baeldung.nestedhashmaps;
+
+public class Address {
+
+ private Integer addressId;
+ private String addressLocation;
+
+ public Address() {
+ }
+
+ public Address(Integer addressId, String addressLocation) {
+ this.addressId = addressId;
+ this.addressLocation = addressLocation;
+ }
+
+ public Integer getAddressId() {
+ return addressId;
+ }
+
+ public void setAddressId(Integer addressId) {
+ this.addressId = addressId;
+ }
+
+ public String getAddressLocation() {
+ return addressLocation;
+ }
+
+ public void setAddressLocation(String addressLocation) {
+ this.addressLocation = addressLocation;
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Employee.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Employee.java
new file mode 100644
index 0000000000..2ab54ff17c
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/Employee.java
@@ -0,0 +1,43 @@
+package com.baeldung.nestedhashmaps;
+
+public class Employee {
+
+ private Integer employeeId;
+ private Address address;
+ private String employeeName;
+
+ public Employee() {
+ super();
+ }
+
+ public Employee(Integer employeeId, Address address, String employeeName) {
+ this.employeeId = employeeId;
+ this.address = address;
+ this.employeeName = employeeName;
+ }
+
+ public Integer getEmployeeId() {
+ return employeeId;
+ }
+
+ public void setEmployeeId(Integer employeeId) {
+ this.employeeId = employeeId;
+ }
+
+ public Address getAddress() {
+ return address;
+ }
+
+ public void setAddress(Address address) {
+ this.address = address;
+ }
+
+ public String getEmployeeName() {
+ return employeeName;
+ }
+
+ public void setEmployeeName(String employeeName) {
+ this.employeeName = employeeName;
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/MapsUtil.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/MapsUtil.java
new file mode 100644
index 0000000000..53781b0bc1
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/MapsUtil.java
@@ -0,0 +1,57 @@
+package com.baeldung.nestedhashmaps;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+public class MapsUtil {
+
+ MapsUtil() {
+ super();
+ }
+
+ public Map buildInnerMap(List batterList) {
+ Map innerBatterMap = new HashMap();
+
+ int index = 1;
+ for (String item : batterList) {
+ innerBatterMap.put(index, item);
+ index++;
+ }
+
+ return innerBatterMap;
+ }
+
+ public Map> createNestedMapfromStream(List listEmployee) {
+ Map> employeeAddressMap = listEmployee.stream()
+ .collect(Collectors.groupingBy(e -> e.getAddress().getAddressId(),
+ Collectors.toMap(f -> f.getAddress().getAddressLocation(), Employee::getEmployeeName)));
+ return employeeAddressMap;
+ }
+
+ public Map> createNestedObjectMap(List listEmployee) {
+ Map> employeeMap = new HashMap<>();
+
+ employeeMap = listEmployee.stream().collect(Collectors.groupingBy((Employee emp) -> emp.getEmployeeId(),
+ Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
+
+ return employeeMap;
+ }
+
+ public Map flattenMap(Map, ?> source) {
+ Map converted = new HashMap<>();
+
+ for (Entry, ?> entry : source.entrySet()) {
+ if (entry.getValue() instanceof Map) {
+ flattenMap((Map) entry.getValue())
+ .forEach((key, value) -> converted.put(entry.getKey() + "." + key, value));
+ } else {
+ converted.put(entry.getKey().toString(), entry.getValue().toString());
+ }
+ }
+ return converted;
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClass.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClass.java
new file mode 100644
index 0000000000..ce63b72527
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClass.java
@@ -0,0 +1,118 @@
+package com.baeldung.nestedhashmaps;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import java.util.List;
+
+public class NestedHashMapExamplesClass {
+ public static void main(String[] args) {
+
+ MapsUtil mUtil = new MapsUtil();
+
+ List batterList = new ArrayList<>();
+ Map> outerBakedGoodsMap = new HashMap<>();
+ Map> outerBakedGoodsMap2 = new HashMap<>();
+ Map> outerBakedGoodsMap3 = new HashMap<>();
+ Map> outerBakedGoodsMap4 = new HashMap<>();
+
+ batterList.add("Mulberry");
+ batterList.add("Cranberry");
+ batterList.add("Blackberry");
+ batterList.add("Mixed fruit");
+ batterList.add("Orange");
+
+ outerBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
+
+ batterList.clear();
+ batterList.add("Candy");
+ batterList.add("Dark Chocolate");
+ batterList.add("Chocolate");
+ batterList.add("Jam filled");
+ batterList.add("Pineapple");
+
+ outerBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
+
+ outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
+ outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
+
+ outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
+ batterList.clear();
+ batterList.add("Banana");
+ batterList.add("Red Velvet");
+ batterList.add("Blackberry");
+ batterList.add("Passion fruit");
+ batterList.add("Kiwi");
+
+ outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
+
+ outerBakedGoodsMap4.putAll(outerBakedGoodsMap);
+
+ System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap2));
+ System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap3));
+ System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap4));
+
+ outerBakedGoodsMap.get("Cake")
+ .put(6, "Cranberry");
+ System.out.println(outerBakedGoodsMap);
+
+ outerBakedGoodsMap.get("Cake")
+ .remove(5);
+ System.out.println(outerBakedGoodsMap);
+
+ outerBakedGoodsMap.put("Eclair", new HashMap() {
+ {
+ put(1, "Dark Chocolate");
+ }
+ });
+
+ System.out.println(outerBakedGoodsMap);
+ outerBakedGoodsMap.remove("Eclair");
+ System.out.println(outerBakedGoodsMap);
+ System.out.println("Baked Goods Map Flattened: " + mUtil.flattenMap(outerBakedGoodsMap));
+
+ // Employees Map
+ List listEmployee = new ArrayList();
+
+ listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
+ listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
+ listEmployee.add(new Employee(3, new Address(156, "Bramles Lane"), "Bofur"));
+ listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
+ listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
+
+ Map> employeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
+
+ Map> employeeMap = mUtil.createNestedObjectMap(listEmployee);
+ Map> employeeMap2 = mUtil.createNestedObjectMap(listEmployee);
+
+ listEmployee.clear();
+ listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
+ listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
+ listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
+ listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
+ listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
+
+ Map> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
+
+ Map> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
+
+ System.out.println(employeeMap.equals(employeeMap1));
+ System.out.println(employeeMap.equals(employeeMap2));
+
+ for (Map.Entry> outerBakedGoodsMapEntrySet : outerBakedGoodsMap.entrySet()) {
+ Map valueMap = outerBakedGoodsMapEntrySet.getValue();
+ System.out.println(valueMap.entrySet());
+ }
+
+ for (Map.Entry> employeeEntrySet : employeeAddressMap.entrySet()) {
+ Map valueMap = employeeEntrySet.getValue();
+ System.out.println(valueMap.entrySet());
+ }
+
+ System.out.println("Employee Address Map Flattened: " + mUtil.flattenMap(employeeAddressMap));
+
+ System.out.println(employeeAddressMap.equals(employeeAddressMap1));
+ }
+
+}
diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClassUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClassUnitTest.java
new file mode 100644
index 0000000000..a86e329bea
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/nestedhashmaps/NestedHashMapExamplesClassUnitTest.java
@@ -0,0 +1,243 @@
+package com.baeldung.nestedhashmaps;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertThat;
+import org.hamcrest.collection.IsMapContaining;
+
+public class NestedHashMapExamplesClassUnitTest {
+ private MapsUtil mUtil = new MapsUtil();
+ private List batterList = new ArrayList<>();
+ private List listEmployee = new ArrayList();
+ private Map> actualBakedGoodsMap = new HashMap<>();
+ private Map> actualEmployeeAddressMap = new HashMap<>();
+ private Map> actualEmployeeMap = new HashMap<>();
+
+ @Test
+ public void whenCreateNestedHashMap_thenNestedMap() {
+ assertThat(mUtil.buildInnerMap(batterList), is(notNullValue()));
+ Assert.assertEquals(actualBakedGoodsMap.keySet().size(), 2);
+ Assert.assertThat(actualBakedGoodsMap, IsMapContaining.hasValue(equalTo(mUtil.buildInnerMap(batterList))));
+ }
+
+ private Map> setup() {
+ Map> expectedMap = new HashMap<>();
+ expectedMap.put(Integer.valueOf(100), new HashMap() {
+ {
+ put("Misty Lanes", "Balin");
+ }
+ });
+ expectedMap.put(Integer.valueOf(200), new HashMap() {
+ {
+ put("Bag End", "Bilbo Baggins");
+ }
+ });
+ expectedMap.put(Integer.valueOf(156), new HashMap() {
+ {
+ put("Brambles Lane", "Bofur");
+ }
+ });
+ expectedMap.put(Integer.valueOf(124), new HashMap() {
+ {
+ put("Timbuktoo", "Thorin Oakenshield");
+ }
+ });
+ expectedMap.put(Integer.valueOf(23), new HashMap() {
+ {
+ put("Rivendell", "Elrond");
+ }
+ });
+
+ return expectedMap;
+ }
+
+ @Test
+ public void whenCreateNestedHashMapwithStreams_thenNestedMap() {
+
+ Map> expectedMap = setup();
+
+ assertThat(actualEmployeeAddressMap, equalTo(expectedMap));
+ }
+
+ @Test
+ public void whenCompareTwoHashMapswithDifferenctValues_usingEquals_thenFail() {
+ Map> outerBakedGoodsMap2 = new HashMap<>();
+ outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
+ outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
+ assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
+
+ Map> outerBakedGoodsMap3 = new HashMap>();
+ outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
+ batterList = new ArrayList<>();
+ batterList = Arrays.asList("Banana", "Red Velvet", "Blackberry", "Passion fruit", "Kiwi");
+
+ outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
+
+ assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
+
+ listEmployee.clear();
+ listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
+ listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
+ listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
+ listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
+ listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
+
+ Map> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
+
+ Map> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
+
+ assertNotEquals(employeeAddressMap1, actualEmployeeAddressMap);
+
+ assertNotEquals(employeeMap1, actualEmployeeMap);
+ }
+
+ @Test
+ public void whencomparingDifferentObjectValuesUsingEquals_thenFail() {
+ listEmployee.clear();
+ listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
+ listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
+ listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
+ listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
+ listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
+
+ Map> employeeMap1 = listEmployee.stream().collect(Collectors.groupingBy(
+ (Employee emp) -> emp.getEmployeeId(),
+ Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
+
+ assertNotSame(employeeMap1, actualEmployeeMap);
+ assertNotEquals(employeeMap1, actualEmployeeMap);
+
+ Map> expectedMap = setupAddressObjectMap();
+ assertNotSame(expectedMap, actualEmployeeMap);
+ assertNotEquals(expectedMap, actualEmployeeMap);
+
+ }
+
+ @Test
+ public void whenCompareTwoHashMapsUsingEquals_thenSuccess() {
+ Map> outerBakedGoodsMap4 = new HashMap<>();
+ outerBakedGoodsMap4.putAll(actualBakedGoodsMap);
+
+ assertEquals(actualBakedGoodsMap, outerBakedGoodsMap4);
+
+ Map> employeeMap1 = new HashMap<>();
+ employeeMap1.putAll(actualEmployeeMap);
+ assertEquals(actualEmployeeMap, employeeMap1);
+ }
+
+ @Test
+ public void whenAddElementinHashMaps_thenSuccess() {
+ assertEquals(actualBakedGoodsMap.get("Cake").size(), 5);
+ actualBakedGoodsMap.get("Cake").put(6, "Cranberry");
+ assertEquals(actualBakedGoodsMap.get("Cake").size(), 6);
+ }
+
+ @Test
+ public void whenDeleteElementinHashMaps_thenSuccess() {
+ assertNotEquals(actualBakedGoodsMap.get("Cake").get(5), null);
+ actualBakedGoodsMap.get("Cake").remove(5);
+ assertEquals(actualBakedGoodsMap.get("Cake").get(5), null);
+
+ actualBakedGoodsMap.put("Eclair", new HashMap() {
+ {
+ put(1, "Dark Chocolate");
+ }
+ });
+
+ assertNotEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
+ actualBakedGoodsMap.get("Eclair").remove(1);
+ assertEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
+
+ actualBakedGoodsMap.put("Eclair", new HashMap() {
+ {
+ put(1, "Dark Chocolate");
+ }
+ });
+
+ assertNotEquals(actualBakedGoodsMap.get("Eclair"), null);
+ actualBakedGoodsMap.remove("Eclair");
+ assertEquals(actualBakedGoodsMap.get("Eclair"), null);
+ }
+
+ @Test
+ public void whenFlattenMap_thenRemovesNesting() {
+
+ Map flattenedBakedGoodsMap = mUtil.flattenMap(actualBakedGoodsMap);
+ assertThat(flattenedBakedGoodsMap, IsMapContaining.hasKey("Donut.2"));
+
+ Map flattenedEmployeeAddressMap = mUtil.flattenMap(actualEmployeeAddressMap);
+ assertThat(flattenedEmployeeAddressMap, IsMapContaining.hasKey("200.Bag End"));
+ }
+
+ @Before
+ public void buildMaps() {
+
+ batterList = Arrays.asList("Mulberry", "Cranberry", "Blackberry", "Mixed fruit", "Orange");
+
+ actualBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
+
+ batterList = new ArrayList<>();
+ batterList = Arrays.asList("Candy", "Dark Chocolate", "Chocolate", "Jam filled", "Pineapple");
+
+ actualBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
+
+ listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
+ listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
+ listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
+ listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
+ listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
+
+ actualEmployeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
+
+ actualEmployeeMap = mUtil.createNestedObjectMap(listEmployee);
+
+ }
+
+ private Map> setupAddressObjectMap() {
+
+ Map> expectedMap = new HashMap<>();
+
+ expectedMap.put(1, new HashMap() {
+ {
+ put(124, new Address(124, "Timbuktoo"));
+ }
+ });
+ expectedMap.put(2, new HashMap() {
+ {
+ put(100, new Address(100, "Misty Lanes"));
+ }
+ });
+ expectedMap.put(3, new HashMap() {
+ {
+ put(156, new Address(156, "Brambles Lane"));
+ }
+ });
+ expectedMap.put(4, new HashMap() {
+ {
+ put(200, new Address(200, "Bag End"));
+ }
+ });
+ expectedMap.put(5, new HashMap() {
+ {
+ put(23, new Address(23, "Rivendell"));
+ }
+ });
+ return expectedMap;
+ }
+
+}
diff --git a/core-java-modules/core-java-string-algorithms-2/src/test/java/com/baeldung/isuppercase/StringFirstCharacterUppercase.java b/core-java-modules/core-java-string-algorithms-2/src/test/java/com/baeldung/isuppercase/StringFirstCharacterUppercase.java
new file mode 100644
index 0000000000..0d9d3b6431
--- /dev/null
+++ b/core-java-modules/core-java-string-algorithms-2/src/test/java/com/baeldung/isuppercase/StringFirstCharacterUppercase.java
@@ -0,0 +1,32 @@
+package com.baeldung.isuppercase;
+
+import com.google.common.base.Ascii;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.matchesPattern;
+
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+public class StringFirstCharacterUppercase {
+
+ @Test
+ public void givenString_whenCheckingWithCharacterIsUpperCase_thenStringCapitalized() {
+ String example = "Katie";
+ Assertions.assertTrue(Character.isUpperCase(example.charAt(0)));
+ }
+
+ @Test
+ public void givenString_whenCheckingWithRegex_thenStringCapitalized() {
+ String example = "Katie";
+ String regEx = "[A-Z]\\w*";
+ assertThat(example, matchesPattern(regEx));
+ }
+
+ @Test
+ public void givenString_whenCheckingWithGuava_thenStringCapitalized() {
+ String example = "Katie";
+ Assertions.assertTrue(Ascii.isUpperCase(example.charAt(0)));
+ }
+}
diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/multipledelimiterssplit/MultipleDelimitersSplitUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/multipledelimiterssplit/MultipleDelimitersSplitUnitTest.java
index c8f8fc2d98..2da8955645 100644
--- a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/multipledelimiterssplit/MultipleDelimitersSplitUnitTest.java
+++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/multipledelimiterssplit/MultipleDelimitersSplitUnitTest.java
@@ -17,7 +17,7 @@ public class MultipleDelimitersSplitUnitTest {
@Test
public void givenString_whenSplittingByMultipleDelimitersWithRegEx_thenStringSplit() {
String example = "Mary;Thomas:Jane-Kate";
- String[] names = example.split(";|:|-");
+ String[] names = example.split("[;:-]");
String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"};
Assertions.assertEquals(4, names.length);
Assertions.assertArrayEquals(expectedNames, names);
@@ -38,7 +38,7 @@ public class MultipleDelimitersSplitUnitTest {
String example = "Mary;Thomas:Jane-Kate";
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
Iterable expected = Arrays.asList(expectedArray);
- Iterable names = Splitter.on(Pattern.compile(";|:|-")).split(example);
+ Iterable names = Splitter.on(Pattern.compile("[;:-]")).split(example);
Assertions.assertEquals(4, Iterators.size(names.iterator()));
Assertions.assertIterableEquals(expected, names);
}
@@ -48,7 +48,7 @@ public class MultipleDelimitersSplitUnitTest {
String example = "Mary;Thomas:Jane-Kate";
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
Iterable expected = Arrays.asList(expectedArray);
- Iterable names = Splitter.onPattern(";|:|-").split(example);
+ Iterable names = Splitter.onPattern("[;:-]").split(example);
Assertions.assertEquals(4, Iterators.size(names.iterator()));
Assertions.assertIterableEquals(expected, names);
}
diff --git a/core-java-modules/core-java/pom.xml b/core-java-modules/core-java/pom.xml
index db2b1edc70..42262be29a 100644
--- a/core-java-modules/core-java/pom.xml
+++ b/core-java-modules/core-java/pom.xml
@@ -61,6 +61,11 @@
moneta
${javamoney.moneta.version}
+
+ org.springframework
+ spring-core
+ ${spring.core.version}
+
@@ -187,6 +192,7 @@
3.0.0-M1
1.8
1.8
+ 4.3.20.RELEASE
\ No newline at end of file
diff --git a/core-java-modules/core-java/src/main/java/com/baeldung/util/MySerializationUtils.java b/core-java-modules/core-java/src/main/java/com/baeldung/util/MySerializationUtils.java
new file mode 100644
index 0000000000..bfaa91313c
--- /dev/null
+++ b/core-java-modules/core-java/src/main/java/com/baeldung/util/MySerializationUtils.java
@@ -0,0 +1,44 @@
+package com.baeldung.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+public class MySerializationUtils {
+
+ public static byte[] serialize(T obj) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(obj);
+ oos.close();
+ return baos.toByteArray();
+ }
+
+ public static T deserialize(byte[] b, Class cl) throws IOException, ClassNotFoundException {
+ ByteArrayInputStream bais = new ByteArrayInputStream(b);
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ Object o = ois.readObject();
+ return cl.cast(o);
+ }
+
+ public static boolean isSerializable(Class> it) {
+ boolean serializable = it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it);
+ if (!serializable) {
+ return serializable;
+ }
+ Field[] declaredFields = it.getDeclaredFields();
+ for (Field field : declaredFields) {
+ if (Modifier.isVolatile(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
+ continue;
+ }
+ Class> fieldType = field.getType();
+ return isSerializable(fieldType);
+ }
+ return serializable;
+ }
+}
diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/serialization/SerializationUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/serialization/SerializationUnitTest.java
new file mode 100644
index 0000000000..a8c4009386
--- /dev/null
+++ b/core-java-modules/core-java/src/test/java/com/baeldung/serialization/SerializationUnitTest.java
@@ -0,0 +1,111 @@
+package com.baeldung.serialization;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+import org.apache.commons.lang3.SerializationUtils;
+import org.junit.Test;
+
+import com.baeldung.util.MySerializationUtils;
+
+public class SerializationUnitTest {
+
+ @Test(expected = NotSerializableException.class)
+ public void whenSerializing_ThenThrowsError() throws IOException {
+ Address address = new Address();
+ address.setHouseNumber(10);
+ FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
+ try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
+ objectOutputStream.writeObject(address);
+ }
+ }
+
+ @Test
+ public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
+ Person p = new Person();
+ p.setAge(20);
+ p.setName("Joe");
+
+ FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
+ try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
+ objectOutputStream.writeObject(p);
+ }
+
+ FileInputStream fileInputStream = new FileInputStream("yofile.txt");
+ try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
+ Person p2 = (Person) objectInputStream.readObject();
+ assertEquals(p2.getAge(), p.getAge());
+ assertEquals(p2.getName(), p.getName());
+ }
+ }
+
+ @Test(expected = ClassCastException.class)
+ public void whenSerializingUsingApacheCommons_ThenThrowsError() {
+ Address address = new Address();
+ address.setHouseNumber(10);
+ SerializationUtils.serialize((Serializable) address);
+ }
+
+ @Test
+ public void whenSerializingAndDeserializingUsingApacheCommons_ThenObjectIsTheSame() {
+ Person p = new Person();
+ p.setAge(20);
+ p.setName("Joe");
+ byte[] serialize = SerializationUtils.serialize(p);
+ Person p2 = (Person) SerializationUtils.deserialize(serialize);
+ assertEquals(p2.getAge(), p.getAge());
+ assertEquals(p2.getName(), p.getName());
+ }
+
+ @Test(expected = ClassCastException.class)
+ public void whenSerializingUsingSpringSerializationUtils_ThenThrowsError() {
+ Address address = new Address();
+ address.setHouseNumber(10);
+ org.springframework.util.SerializationUtils.serialize((Serializable) address);
+ }
+
+ @Test
+ public void whenSerializingAndDeserializingUsingSpringSerializationUtils_ThenObjectIsTheSame() {
+ Person p = new Person();
+ p.setAge(20);
+ p.setName("Joe");
+ byte[] serialize = org.springframework.util.SerializationUtils.serialize(p);
+ Person p2 = (Person) org.springframework.util.SerializationUtils.deserialize(serialize);
+ assertEquals(p2.getAge(), p.getAge());
+ assertEquals(p2.getName(), p.getName());
+ }
+
+ @Test(expected = ClassCastException.class)
+ public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException {
+ Address address = new Address();
+ address.setHouseNumber(10);
+ MySerializationUtils.serialize((Serializable) address);
+ }
+
+ @Test
+ public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
+ Person p = new Person();
+ p.setAge(20);
+ p.setName("Joe");
+ byte[] serialize = MySerializationUtils.serialize(p);
+ Person p2 = MySerializationUtils.deserialize(serialize, Person.class);
+ assertEquals(p2.getAge(), p.getAge());
+ assertEquals(p2.getName(), p.getName());
+ }
+
+ @Test
+ public void whenSerializingUsingCustomSerializationUtils_ThanOk() {
+ assertFalse(MySerializationUtils.isSerializable(Address.class));
+ assertTrue(MySerializationUtils.isSerializable(Person.class));
+ assertTrue(MySerializationUtils.isSerializable(Integer.class));
+ }
+}