Changed module name. (#13098)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user