From b7f097bb922f20264ccdf306589e33720e40a947 Mon Sep 17 00:00:00 2001 From: Arya <108480101+drafii@users.noreply.github.com> Date: Fri, 25 Nov 2022 21:03:26 +0330 Subject: [PATCH] Serialize a Singleton in Java (#13074) * Added a new module. Also added Serializable Singleton article source code. * Modified the unit tests. And deleted the main classes. --- .../design-patterns-creational-2/README.md | 1 + .../design-patterns-creational-2/pom.xml | 17 +++++ .../serializable_singleton/EnumSingleton.java | 20 ++++++ .../serializable_singleton/Singleton.java | 28 ++++++++ .../EnumSingletonUnitTest.java | 69 +++++++++++++++++++ .../SingletonUnitTest.java | 68 ++++++++++++++++++ 6 files changed, 203 insertions(+) create mode 100644 patterns-modules/design-patterns-creational-2/README.md create mode 100644 patterns-modules/design-patterns-creational-2/pom.xml create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/EnumSingleton.java create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/Singleton.java create mode 100644 patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/EnumSingletonUnitTest.java create mode 100644 patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/SingletonUnitTest.java diff --git a/patterns-modules/design-patterns-creational-2/README.md b/patterns-modules/design-patterns-creational-2/README.md new file mode 100644 index 0000000000..7d843af9ea --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/README.md @@ -0,0 +1 @@ +### Relevant Articles: diff --git a/patterns-modules/design-patterns-creational-2/pom.xml b/patterns-modules/design-patterns-creational-2/pom.xml new file mode 100644 index 0000000000..2e67b346f2 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + design-patterns-creational-2 + 1.0 + design-patterns-creational-2 + jar + + + com.baeldung + patterns-modules + 1.0.0-SNAPSHOT + + + \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/EnumSingleton.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/EnumSingleton.java new file mode 100644 index 0000000000..4259211934 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/EnumSingleton.java @@ -0,0 +1,20 @@ +package com.baeldung.serializable_singleton; + +public enum EnumSingleton { + + INSTANCE("State Zero"); + + private String state; + + private EnumSingleton(String state) { + this.state = state; + } + + public static EnumSingleton getInstance() { + return INSTANCE; + } + + public String getState() { return this.state; } + + public void setState(String state) { this.state = state; } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/Singleton.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/Singleton.java new file mode 100644 index 0000000000..6605d5dcf7 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/serializable_singleton/Singleton.java @@ -0,0 +1,28 @@ +package com.baeldung.serializable_singleton; + +import java.io.Serializable; + +public class Singleton implements Serializable { + + private static Singleton INSTANCE; + private String state = "State Zero"; + + private Singleton() { + } + + public static Singleton getInstance() { + if(INSTANCE == null) { + INSTANCE = new Singleton(); + } + + return INSTANCE; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/EnumSingletonUnitTest.java b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/EnumSingletonUnitTest.java new file mode 100644 index 0000000000..e0a098056a --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/EnumSingletonUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.serializable_singleton; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +// Unit test for the EnumSingleton class. +public class EnumSingletonUnitTest { + + // Checks that when an EnumSingleton instance is serialized + // and then deserialized, its state is preserved. + @Test + public void givenEnumSingleton_whenSerializedAndDeserialized_thenStatePreserved() { + EnumSingleton es1 = EnumSingleton.getInstance(); + + es1.setState("State One"); + + try ( + FileOutputStream fos = new FileOutputStream("enum_singleton_test.txt"); + ObjectOutputStream oos = new ObjectOutputStream(fos); + FileInputStream fis = new FileInputStream("enum_singleton_test.txt"); + ObjectInputStream ois = new ObjectInputStream(fis)) { + + // Serializing. + oos.writeObject(es1); + + // Deserializing. + EnumSingleton es2 = (EnumSingleton) ois.readObject(); + + // Checking if the state is preserved. + assertEquals(es1.getState(), es2.getState()); + + } catch (Exception e) { + System.out.println(e); + } + } + + // Checking that when an EnumSingleton instance is serialized + // and then deserialized, then there is still one instance + // of the EnumSingleton class in memory. + @Test + public void givenEnumSingleton_whenSerializedAndDeserialized_thenOneInstance() { + EnumSingleton es1 = EnumSingleton.getInstance(); + + try ( + FileOutputStream fos = new FileOutputStream("enum_singleton_test.txt"); + ObjectOutputStream oos = new ObjectOutputStream(fos); + FileInputStream fis = new FileInputStream("enum_singleton_test.txt"); + ObjectInputStream ois = new ObjectInputStream(fis)) { + + // Serializing. + oos.writeObject(es1); + + // Deserializing. + EnumSingleton es2 = (EnumSingleton) ois.readObject(); + + // Checking if es1 and es2 are pointing to + // the same instance in memory. + assertEquals(es1, es2); + + } catch (Exception e) { + System.out.println(e); + } + } +} diff --git a/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/SingletonUnitTest.java b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/SingletonUnitTest.java new file mode 100644 index 0000000000..cc26eb6995 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/serializable_singleton/SingletonUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.serializable_singleton; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +// Unit test for the Singleton class. +public class SingletonUnitTest { + + // Checks that when a Singleton instance is serialized + // and then deserialized, its state is preserved. + @Test + public void givenSingleton_whenSerializedAndDeserialized_thenStatePreserved() { + Singleton s1 = Singleton.getInstance(); + + s1.setState("State One"); + + try ( + FileOutputStream fos = new FileOutputStream("singleton_test.txt"); + ObjectOutputStream oos = new ObjectOutputStream(fos); + FileInputStream fis = new FileInputStream("singleton_test.txt"); + ObjectInputStream ois = new ObjectInputStream(fis)) { + + // Serializing. + oos.writeObject(s1); + + // Deserializing. + Singleton s2 = (Singleton) ois.readObject(); + + // Checking if the state is preserved. + assertEquals(s1.getState(), s2.getState()); + + } catch (Exception e) { + System.out.println(e); + } + } + + // Checking that when a Singleton instance is serialized + // and then deserialized, then there are two instances of + // the Singleton class. + @Test + public void givenSingleton_whenSerializedAndDeserialized_thenTwoInstances() { + Singleton s1 = Singleton.getInstance(); + + try ( + FileOutputStream fos = new FileOutputStream("singleton_test.txt"); + ObjectOutputStream oos = new ObjectOutputStream(fos); + FileInputStream fis = new FileInputStream("singleton_test.txt"); + ObjectInputStream ois = new ObjectInputStream(fis)) { + + // Serializing. + oos.writeObject(s1); + + // Deserializing. + Singleton s2 = (Singleton) ois.readObject(); + + // Checking if s1 and s2 are not the same instance. + assertNotEquals(s1, s2); + + } catch (Exception e) { + System.out.println(e); + } + } +}