diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/DeepCopyPerson.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/DeepCopyPerson.java deleted file mode 100644 index 4435a2720d..0000000000 --- a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/DeepCopyPerson.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.objectcopy; - -import java.io.*; - -class DeepCopyPerson implements Serializable { - - int age; - - public DeepCopyPerson(int age) { - this.age = age; - } - - public static DeepCopyPerson deepCopy(DeepCopyPerson person) throws IOException, ClassNotFoundException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(bos); - out.writeObject(person); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - ObjectInputStream in = new ObjectInputStream(bis); - - return (DeepCopyPerson) in.readObject(); - } -} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/ShallowCopyPerson.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/ShallowCopyPerson.java deleted file mode 100644 index df06bfa988..0000000000 --- a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectcopy/ShallowCopyPerson.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.objectcopy; - -class ShallowCopyPerson { - - int age; - - public ShallowCopyPerson(int age) { - this.age = age; - } -} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java new file mode 100644 index 0000000000..e67c802a2e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java @@ -0,0 +1,21 @@ +package com.baeldung.objectmutability; + +public final class ImmutablePerson { + + private final String name; + private final int age; + + public ImmutablePerson(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/DeepCopyPersonUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/DeepCopyPersonUnitTest.java deleted file mode 100644 index ae3a109c15..0000000000 --- a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/DeepCopyPersonUnitTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.objectcopy; - -import org.junit.jupiter.api.Test; - -import java.io.IOException; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class DeepCopyPersonUnitTest { - - @Test - public void givenPerson_whenDeepCopy_thenIndependentCopy() throws IOException, ClassNotFoundException { - DeepCopyPerson person1 = new DeepCopyPerson(30); - DeepCopyPerson person2 = DeepCopyPerson.deepCopy(person1); - person1.age = 25; - - assertEquals(30, person2.age); - } -} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/ShallowCopyPersonUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/ShallowCopyPersonUnitTest.java deleted file mode 100644 index 2aea938bde..0000000000 --- a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectcopy/ShallowCopyPersonUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.objectcopy; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class ShallowCopyPersonUnitTest { - - @Test - public void givenPerson_whenShallowCopy_thenSharedReference() { - ShallowCopyPerson person1 = new ShallowCopyPerson(30); - ShallowCopyPerson person2 = person1; - person1.age = 25; - - assertEquals(25, person2.age); - } -} diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java new file mode 100644 index 0000000000..f0f22df3ea --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.objectmutability; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +public class ImmutableObjectExamplesUnitTest { + + @Test + public void givenImmutableString_whenConcat_thenNotSameAndCorrectValues() { + String originalString = "Hello"; + String modifiedString = originalString.concat(" World"); + + assertNotSame(originalString, modifiedString); + + assertEquals("Hello", originalString); + assertEquals("Hello World", modifiedString); + } + + @Test + public void givenImmutableInteger_whenAdd_thenNotSameAndCorrectValue() { + Integer immutableInt = 42; + Integer modifiedInt = immutableInt + 8; + + assertNotSame(immutableInt, modifiedInt); + + assertEquals(42, (int) immutableInt); + assertEquals(50, (int) modifiedInt); + } +} + + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java new file mode 100644 index 0000000000..6aeebcb242 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.objectmutability; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ImmutablePersonUnitTest { + + @Test + public void givenImmutablePerson_whenModifyName_thenCompilationError() { + ImmutablePerson person = new ImmutablePerson("John", 30); + // person.setName("Jane"); + } + + @Test + public void givenImmutablePerson_whenAccessFields_thenCorrectValues() { + ImmutablePerson person = new ImmutablePerson("John", 30); + + assertEquals("John", person.getName()); + assertEquals(30, person.getAge()); + } +} + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java new file mode 100644 index 0000000000..57e36efbf4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.objectmutability; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MutableObjectExamplesUnitTest { + + @Test + public void givenMutableString_whenModify_thenCorrectValue() { + StringBuilder mutableString = new StringBuilder("Hello"); + mutableString.append(" World"); + + assertEquals("Hello World", mutableString.toString()); + } + + @Test + public void givenMutableList_whenAddElement_thenCorrectSize() { + List mutableList = new ArrayList<>(); + mutableList.add("Java"); + + assertEquals(1, mutableList.size()); + } +} +