diff --git a/core-java-modules/core-java/pom.xml b/core-java-modules/core-java/pom.xml index 00f26b0e0a..42262be29a 100644 --- a/core-java-modules/core-java/pom.xml +++ b/core-java-modules/core-java/pom.xml @@ -62,10 +62,10 @@ ${javamoney.moneta.version} - org.springframework - spring-core - ${spring.core.version} - + org.springframework + spring-core + ${spring.core.version} + 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/main/java/com/baeldung/util/SerializationUtils.java b/core-java-modules/core-java/src/main/java/com/baeldung/util/SerializationUtils.java deleted file mode 100644 index 2a9391965f..0000000000 --- a/core-java-modules/core-java/src/main/java/com/baeldung/util/SerializationUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -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 SerializationUtils { - - 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 index e0ee2bb654..bc9cf04b86 100644 --- 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 @@ -1,7 +1,8 @@ package com.baeldung.serialization; -import static org.junit.Assert.assertTrue; +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; @@ -16,94 +17,93 @@ import org.junit.Test; 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 + @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); - } + 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(); - assertTrue(p2.getAge() == p.getAge()); - assertTrue(p2.getName().equals(p.getName())); - } - } + 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(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); - assertTrue(p2.getAge() == p.getAge()); - assertTrue(p2.getName().equals(p.getName())); - } + @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(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); - assertTrue(p2.getAge() == p.getAge()); - assertTrue(p2.getName().equals(p.getName())); - } + @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); - com.baeldung.util.SerializationUtils.serialize((Serializable) address); - } + @Test(expected = ClassCastException.class) + public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException { + Address address = new Address(); + address.setHouseNumber(10); + com.baeldung.util.MySerializationUtils.serialize((Serializable) address); + } - @Test - public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() - throws IOException, ClassNotFoundException { - Person p = new Person(); - p.setAge(20); - p.setName("Joe"); - byte[] serialize = com.baeldung.util.SerializationUtils.serialize(p); - Person p2 = com.baeldung.util.SerializationUtils.deserialize(serialize, Person.class); - assertTrue(p2.getAge() == p.getAge()); - assertTrue(p2.getName().equals(p.getName())); - } + @Test + public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() throws IOException, ClassNotFoundException { + Person p = new Person(); + p.setAge(20); + p.setName("Joe"); + byte[] serialize = com.baeldung.util.MySerializationUtils.serialize(p); + Person p2 = com.baeldung.util.MySerializationUtils.deserialize(serialize, Person.class); + assertEquals(p2.getAge(), p.getAge()); + assertEquals(p2.getName(), p.getName()); + } - @Test - public void whenSerializingUsingCustomSerializationUtils_ThanOk() { - assertFalse(com.baeldung.util.SerializationUtils.isSerializable(Address.class)); - assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Person.class)); - assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Integer.class)); - } + @Test + public void whenSerializingUsingCustomSerializationUtils_ThanOk() { + assertFalse(com.baeldung.util.MySerializationUtils.isSerializable(Address.class)); + assertTrue(com.baeldung.util.MySerializationUtils.isSerializable(Person.class)); + assertTrue(com.baeldung.util.MySerializationUtils.isSerializable(Integer.class)); + } }