Changed module name. (#13098)

This commit is contained in:
Arya
2022-11-28 01:59:34 +03:30
committed by GitHub
parent 6e3bb64b1f
commit 71a02e94d9
6 changed files with 2 additions and 2 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}