From 13448aa5513890351319bc9db861eb101888d802 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Thu, 22 Feb 2024 11:49:07 +0200 Subject: [PATCH 01/13] Andrei Branza - test article shallowCopy-vs-deepCopy --- .../shallowCopy-vs-deepCopy/.gitignore | 38 +++++++++++++++++++ .../shallowCopy-vs-deepCopy/pom.xml | 33 ++++++++++++++++ .../com/baeldung/cloning/deep/Address.java | 25 ++++++++++++ .../com/baeldung/cloning/deep/Person.java | 28 ++++++++++++++ .../com/baeldung/cloning/shallow/Address.java | 25 ++++++++++++ .../com/baeldung/cloning/shallow/Person.java | 24 ++++++++++++ .../cloning/deep/DeepCloningTest.java | 23 +++++++++++ .../cloning/shallow/ShallowCloningTest.java | 21 ++++++++++ 8 files changed, 217 insertions(+) create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java create mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore b/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore new file mode 100644 index 0000000000..5ff6309b71 --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml b/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml new file mode 100644 index 0000000000..e6848cfacc --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + com.baeldung + shallowCopy-vs-deepCopy + 1.0-SNAPSHOT + + + 21 + 4.13.2 + test + ${java.version} + ${java.version} + UTF-8 + + + + junit + junit + ${junit.version} + ${test.scope + + + org.projectlombok + lombok + 1.18.30 + provided + + + \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java new file mode 100644 index 0000000000..0439c74f3a --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java @@ -0,0 +1,25 @@ +package com.baeldung.cloning.deep; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@EqualsAndHashCode +@ToString +public class Address implements Cloneable +{ + private String streetName; + private String cityName; + + @Override + public Address clone() { + try + { + return (Address) super.clone(); + } catch (CloneNotSupportedException cloneException) + { + throw new RuntimeException(cloneException); + } + } +} \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java new file mode 100644 index 0000000000..2088d538ea --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java @@ -0,0 +1,28 @@ +package com.baeldung.cloning.deep; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@EqualsAndHashCode +@ToString +public class Person implements Cloneable +{ + private String firstName; + private String lastName; + private Address address; + + public Person(Person personToBeCloned) { + Address addressToBeCloned = personToBeCloned.getAddress(); + + this.firstName = personToBeCloned.getFirstName(); + this.lastName = personToBeCloned.getLastName(); + this.address = new Address(addressToBeCloned.getStreetName(), addressToBeCloned.getCityName()); + } + + @Override + public Person clone() { + return new Person(this); + } +} diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java new file mode 100644 index 0000000000..9c79ed3383 --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java @@ -0,0 +1,25 @@ +package com.baeldung.cloning.shallow; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@EqualsAndHashCode +@ToString +public class Address implements Cloneable +{ + private String streetName; + private String cityName; + + @Override + public Address clone() { + try + { + return (Address) super.clone(); + } catch (CloneNotSupportedException cloneException) + { + throw new RuntimeException(cloneException); + } + } +} \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java new file mode 100644 index 0000000000..6b2165d72c --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java @@ -0,0 +1,24 @@ +package com.baeldung.cloning.shallow; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@EqualsAndHashCode +@ToString +public class Person implements Cloneable +{ + private String firstName; + private String lastName; + private Address address; + + @Override + public Person clone() { + try { + return (Person) super.clone(); + } catch (CloneNotSupportedException cloneException) { + throw new RuntimeException(cloneException); + } + } +} diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java new file mode 100644 index 0000000000..4b9d14fa9c --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java @@ -0,0 +1,23 @@ +package com.baeldung.cloning.deep; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class DeepCloningTest { + + Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); + + @Test + public void whenUsingDeepCopy_thenReferencesAreNotTheSame() + { + Person constructorCopyOfAlex = new Person(alex); + + assertEquals(alex, constructorCopyOfAlex); + + alex.setAddress(new Address("Unknown Street", "Unknown City")); + + assertNotEquals(alex.getAddress(), constructorCopyOfAlex.getAddress()); + } +} diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java new file mode 100644 index 0000000000..b513919911 --- /dev/null +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java @@ -0,0 +1,21 @@ +package com.baeldung.cloning.shallow; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class ShallowCloningTest { + + Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); + + @Test + public void whenUsingShallowCopy_ThenReferencesAreTheSame() + { + Person shallowCopyOfAlex = alex.clone(); + + assertEquals(alex, shallowCopyOfAlex); + + alex.getAddress().setCityName("Unknown City"); + + assertEquals(alex.getAddress(), shallowCopyOfAlex.getAddress()); + } +} From 9a421159eaf506d540388c487abd4336ef639408 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sat, 24 Feb 2024 20:59:30 +0200 Subject: [PATCH 02/13] Andrei Branza - removed gitignore file --- .../shallowCopy-vs-deepCopy/.gitignore | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore b/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore deleted file mode 100644 index 5ff6309b71..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore +++ /dev/null @@ -1,38 +0,0 @@ -target/ -!.mvn/wrapper/maven-wrapper.jar -!**/src/main/**/target/ -!**/src/test/**/target/ - -### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ -*.iws -*.iml -*.ipr - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -build/ -!**/src/main/**/build/ -!**/src/test/**/build/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file From 65437b41ad0217e4ce913c5ec633464948070dda Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sat, 24 Feb 2024 21:09:58 +0200 Subject: [PATCH 03/13] Andrei Branza - replaced tabs si 4 spaces. eliminated not needed clone method in Address class from shallow and deep copy --- .../com/baeldung/cloning/deep/Address.java | 18 +++---------- .../com/baeldung/cloning/deep/Person.java | 26 +++++++++---------- .../com/baeldung/cloning/shallow/Address.java | 18 +++---------- .../com/baeldung/cloning/shallow/Person.java | 16 ++++++------ 4 files changed, 29 insertions(+), 49 deletions(-) diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java index 0439c74f3a..5468533f19 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java @@ -7,19 +7,9 @@ import lombok.*; @AllArgsConstructor @EqualsAndHashCode @ToString -public class Address implements Cloneable -{ - private String streetName; - private String cityName; +public class Address implements Cloneable { + + private String streetName; + private String cityName; - @Override - public Address clone() { - try - { - return (Address) super.clone(); - } catch (CloneNotSupportedException cloneException) - { - throw new RuntimeException(cloneException); - } - } } \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java index 2088d538ea..27d2557525 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java @@ -7,22 +7,22 @@ import lombok.*; @AllArgsConstructor @EqualsAndHashCode @ToString -public class Person implements Cloneable -{ - private String firstName; - private String lastName; - private Address address; +public class Person implements Cloneable { - public Person(Person personToBeCloned) { - Address addressToBeCloned = personToBeCloned.getAddress(); + private String firstName; + private String lastName; + private Address address; - this.firstName = personToBeCloned.getFirstName(); - this.lastName = personToBeCloned.getLastName(); - this.address = new Address(addressToBeCloned.getStreetName(), addressToBeCloned.getCityName()); - } + public Person(Person personToBeCloned) { + Address addressToBeCloned = personToBeCloned.getAddress(); - @Override - public Person clone() { + this.firstName = personToBeCloned.getFirstName(); + this.lastName = personToBeCloned.getLastName(); + this.address = new Address(addressToBeCloned.getStreetName(), addressToBeCloned.getCityName()); + } + + @Override + public Person clone() { return new Person(this); } } diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java index 9c79ed3383..ca97ce8520 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java @@ -7,19 +7,9 @@ import lombok.*; @AllArgsConstructor @EqualsAndHashCode @ToString -public class Address implements Cloneable -{ - private String streetName; - private String cityName; +public class Address implements Cloneable { + + private String streetName; + private String cityName; - @Override - public Address clone() { - try - { - return (Address) super.clone(); - } catch (CloneNotSupportedException cloneException) - { - throw new RuntimeException(cloneException); - } - } } \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java index 6b2165d72c..fa54c9ad11 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java @@ -7,18 +7,18 @@ import lombok.*; @AllArgsConstructor @EqualsAndHashCode @ToString -public class Person implements Cloneable -{ - private String firstName; - private String lastName; - private Address address; +public class Person implements Cloneable { - @Override - public Person clone() { + private String firstName; + private String lastName; + private Address address; + + @Override + public Person clone() { try { return (Person) super.clone(); } catch (CloneNotSupportedException cloneException) { throw new RuntimeException(cloneException); } } -} +} \ No newline at end of file From 6efb061da7f2f2c934f9984ffd0dfd2ab11d3e5b Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sat, 24 Feb 2024 21:14:44 +0200 Subject: [PATCH 04/13] Andrei Branza - some missed tabs --- .../src/main/java/com/baeldung/cloning/deep/Address.java | 2 +- .../src/main/java/com/baeldung/cloning/deep/Person.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java index 5468533f19..2f3070b89b 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java @@ -9,7 +9,7 @@ import lombok.*; @ToString public class Address implements Cloneable { - private String streetName; + private String streetName; private String cityName; } \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java index 27d2557525..08ce8f79e0 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java @@ -23,6 +23,6 @@ public class Person implements Cloneable { @Override public Person clone() { - return new Person(this); - } + return new Person(this); + } } From 54851a8042a0388225a0abab84b047274048089e Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sat, 24 Feb 2024 21:17:24 +0200 Subject: [PATCH 05/13] Andrei Branza - added some spaces --- .../src/main/java/com/baeldung/cloning/deep/Address.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java index 2f3070b89b..5468533f19 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java @@ -9,7 +9,7 @@ import lombok.*; @ToString public class Address implements Cloneable { - private String streetName; + private String streetName; private String cityName; } \ No newline at end of file From 93020dc5444dd281c5675995d7a9472c5bf08f99 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sat, 24 Feb 2024 21:18:38 +0200 Subject: [PATCH 06/13] Andrei Branza - converted indents to spaces test classes --- .../baeldung/cloning/deep/DeepCloningTest.java | 18 +++++++++--------- .../cloning/shallow/ShallowCloningTest.java | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java index 4b9d14fa9c..56bf900f3a 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java @@ -7,17 +7,17 @@ import static org.junit.Assert.assertNotEquals; public class DeepCloningTest { - Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); + Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); - @Test - public void whenUsingDeepCopy_thenReferencesAreNotTheSame() - { - Person constructorCopyOfAlex = new Person(alex); + @Test + public void whenUsingDeepCopy_thenReferencesAreNotTheSame() + { + Person constructorCopyOfAlex = new Person(alex); - assertEquals(alex, constructorCopyOfAlex); + assertEquals(alex, constructorCopyOfAlex); - alex.setAddress(new Address("Unknown Street", "Unknown City")); + alex.setAddress(new Address("Unknown Street", "Unknown City")); - assertNotEquals(alex.getAddress(), constructorCopyOfAlex.getAddress()); - } + assertNotEquals(alex.getAddress(), constructorCopyOfAlex.getAddress()); + } } diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java index b513919911..a52357c5d5 100644 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java +++ b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java @@ -5,17 +5,17 @@ import static org.junit.Assert.*; public class ShallowCloningTest { - Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); + Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); - @Test - public void whenUsingShallowCopy_ThenReferencesAreTheSame() - { + @Test + public void whenUsingShallowCopy_ThenReferencesAreTheSame() + { Person shallowCopyOfAlex = alex.clone(); - assertEquals(alex, shallowCopyOfAlex); + assertEquals(alex, shallowCopyOfAlex); - alex.getAddress().setCityName("Unknown City"); + alex.getAddress().setCityName("Unknown City"); - assertEquals(alex.getAddress(), shallowCopyOfAlex.getAddress()); - } + assertEquals(alex.getAddress(), shallowCopyOfAlex.getAddress()); + } } From 33834c45c49cc951458fe2853ed089eaaa8dee21 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sun, 24 Mar 2024 13:04:18 +0200 Subject: [PATCH 07/13] Andrei Branza - deleted test article --- .../shallowCopy-vs-deepCopy/pom.xml | 33 ------------------- .../com/baeldung/cloning/deep/Address.java | 15 --------- .../com/baeldung/cloning/deep/Person.java | 28 ---------------- .../com/baeldung/cloning/shallow/Address.java | 15 --------- .../com/baeldung/cloning/shallow/Person.java | 24 -------------- .../cloning/deep/DeepCloningTest.java | 23 ------------- .../cloning/shallow/ShallowCloningTest.java | 21 ------------ 7 files changed, 159 deletions(-) delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java delete mode 100644 xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml b/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml deleted file mode 100644 index e6848cfacc..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - 4.0.0 - - com.baeldung - shallowCopy-vs-deepCopy - 1.0-SNAPSHOT - - - 21 - 4.13.2 - test - ${java.version} - ${java.version} - UTF-8 - - - - junit - junit - ${junit.version} - ${test.scope - - - org.projectlombok - lombok - 1.18.30 - provided - - - \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java deleted file mode 100644 index 5468533f19..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Address.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.cloning.deep; - -import lombok.*; - -@Getter -@Setter -@AllArgsConstructor -@EqualsAndHashCode -@ToString -public class Address implements Cloneable { - - private String streetName; - private String cityName; - -} \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java deleted file mode 100644 index 08ce8f79e0..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/deep/Person.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.cloning.deep; - -import lombok.*; - -@Getter -@Setter -@AllArgsConstructor -@EqualsAndHashCode -@ToString -public class Person implements Cloneable { - - private String firstName; - private String lastName; - private Address address; - - public Person(Person personToBeCloned) { - Address addressToBeCloned = personToBeCloned.getAddress(); - - this.firstName = personToBeCloned.getFirstName(); - this.lastName = personToBeCloned.getLastName(); - this.address = new Address(addressToBeCloned.getStreetName(), addressToBeCloned.getCityName()); - } - - @Override - public Person clone() { - return new Person(this); - } -} diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java deleted file mode 100644 index ca97ce8520..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Address.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.cloning.shallow; - -import lombok.*; - -@Getter -@Setter -@AllArgsConstructor -@EqualsAndHashCode -@ToString -public class Address implements Cloneable { - - private String streetName; - private String cityName; - -} \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java deleted file mode 100644 index fa54c9ad11..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/main/java/com/baeldung/cloning/shallow/Person.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.cloning.shallow; - -import lombok.*; - -@Getter -@Setter -@AllArgsConstructor -@EqualsAndHashCode -@ToString -public class Person implements Cloneable { - - private String firstName; - private String lastName; - private Address address; - - @Override - public Person clone() { - try { - return (Person) super.clone(); - } catch (CloneNotSupportedException cloneException) { - throw new RuntimeException(cloneException); - } - } -} \ No newline at end of file diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java deleted file mode 100644 index 56bf900f3a..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/deep/DeepCloningTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.cloning.deep; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -public class DeepCloningTest { - - Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); - - @Test - public void whenUsingDeepCopy_thenReferencesAreNotTheSame() - { - Person constructorCopyOfAlex = new Person(alex); - - assertEquals(alex, constructorCopyOfAlex); - - alex.setAddress(new Address("Unknown Street", "Unknown City")); - - assertNotEquals(alex.getAddress(), constructorCopyOfAlex.getAddress()); - } -} diff --git a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java b/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java deleted file mode 100644 index a52357c5d5..0000000000 --- a/xxAndreiBranza/shallowCopy-vs-deepCopy/src/test/java/com/baeldung/cloning/shallow/ShallowCloningTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.cloning.shallow; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class ShallowCloningTest { - - Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City")); - - @Test - public void whenUsingShallowCopy_ThenReferencesAreTheSame() - { - Person shallowCopyOfAlex = alex.clone(); - - assertEquals(alex, shallowCopyOfAlex); - - alex.getAddress().setCityName("Unknown City"); - - assertEquals(alex.getAddress(), shallowCopyOfAlex.getAddress()); - } -} From 992716d7ff2bca8076c7b890716ecb4d0d7ba42b Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sun, 24 Mar 2024 15:59:14 +0200 Subject: [PATCH 08/13] BAEL-6421 | Added necessary tests for the PrintWriter write vs print method article --- core-java-modules/core-java-io-apis-2/pom.xml | 10 ++ .../WriteVsPrintUnitTest.java | 113 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml index 89ab6d163a..a7362582ff 100644 --- a/core-java-modules/core-java-io-apis-2/pom.xml +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -85,6 +85,16 @@ core-java-io-apis-2 + + + org.apache.maven.plugins + maven-compiler-plugin + + 9 + 9 + + + src/main/resources diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java new file mode 100644 index 0000000000..8995e65318 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java @@ -0,0 +1,113 @@ +package com.baeldung.printwriterwritevsprint; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; + +public class WriteVsPrintUnitTest { + + Object outputFromPrintWriter; + public Object outputFromPrintWriter() { + try (BufferedReader br = new BufferedReader(new FileReader("output.txt"))){ + outputFromPrintWriter = br.readLine(); + } catch (IOException e){ + e.printStackTrace(); + Assertions.fail(); + } + return outputFromPrintWriter; + } + + @Test + public void whenUsingWriteInt_thenASCIICharacterIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write(48); + printWriter.close(); + + assertEquals("0", outputFromPrintWriter()); + } + + @Test + public void whenUsingWriteCharArrayFromOffset_thenCharArrayIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write(new char[]{'A','/','&','4','E'}, 1, 4 ); + printWriter.close(); + + assertEquals("/&4E", outputFromPrintWriter()); + } + + @Test + public void whenUsingWriteStringFromOffset_thenLengthOfStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write("StringExample", 6, 7 ); + printWriter.close(); + + assertEquals("Example", outputFromPrintWriter()); + } + + @Test + public void whenUsingPrintBoolean_thenStringValueIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print(true); + printWriter.close(); + + assertEquals("true", outputFromPrintWriter()); + } + + @Test + public void whenUsingPrintChar_thenCharIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print('A'); + printWriter.close(); + + assertEquals("A", outputFromPrintWriter()); + } + + @Test + public void whenUsingPrintInt_thenValueOfIntIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print(420); + printWriter.close(); + + assertEquals("420", outputFromPrintWriter()); + } + + @Test + public void whenUsingPrintString_thenStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print("RandomString"); + printWriter.close(); + + assertEquals("RandomString", outputFromPrintWriter()); + } + + @Test + public void whenUsingPrintObject_thenObjectToStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + Map example = new HashMap(); + + printWriter.print(example); + printWriter.close(); + + assertEquals(example.toString(), outputFromPrintWriter()); + } +} \ No newline at end of file From 2b1f0b280e87902401d6eb0f9d3144c1f937bdb6 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sun, 24 Mar 2024 16:03:10 +0200 Subject: [PATCH 09/13] BAEL-6421 | Cleaned the module pom from duplicate dependencies. --- core-java-modules/core-java-io-apis-2/pom.xml | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml index a7362582ff..650b454bb4 100644 --- a/core-java-modules/core-java-io-apis-2/pom.xml +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -20,28 +20,6 @@ ${lombok.version} provided - - org.junit.jupiter - junit-jupiter-engine - 5.7.2 - test - - - org.junit.jupiter - junit-jupiter - - - org.junit.jupiter - junit-jupiter - - - org.junit.jupiter - junit-jupiter - - - org.junit.jupiter - junit-jupiter - org.junit.jupiter @@ -63,18 +41,6 @@ ${junit-jupiter-version} test - - org.junit.jupiter - junit-jupiter - - - org.junit.jupiter - junit-jupiter - - - org.junit.jupiter - junit-jupiter - org.testng testng From 293574e3999d4896ad44c28f7e97ff1c84458703 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Sun, 7 Apr 2024 15:37:29 +0300 Subject: [PATCH 10/13] BAEL-6421 | Eliminated unnecessary plugin --- core-java-modules/core-java-io-apis-2/pom.xml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml index f5b0d3140b..dd71f2b53b 100644 --- a/core-java-modules/core-java-io-apis-2/pom.xml +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -51,16 +51,6 @@ core-java-io-apis-2 - - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - - src/main/resources From 3999bcb3bd5d12c0f09744293754bec580ff3805 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Mon, 8 Apr 2024 19:09:19 +0300 Subject: [PATCH 11/13] BAEL-6421 | Removed public keyword from methods --- .../WriteVsPrintUnitTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java index 8995e65318..ad9c0f4619 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java @@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.*; public class WriteVsPrintUnitTest { Object outputFromPrintWriter; - public Object outputFromPrintWriter() { + Object outputFromPrintWriter() { try (BufferedReader br = new BufferedReader(new FileReader("output.txt"))){ outputFromPrintWriter = br.readLine(); } catch (IOException e){ @@ -22,7 +22,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingWriteInt_thenASCIICharacterIsPrinted() throws FileNotFoundException { + void whenUsingWriteInt_thenASCIICharacterIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -33,7 +33,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingWriteCharArrayFromOffset_thenCharArrayIsPrinted() throws FileNotFoundException { + void whenUsingWriteCharArrayFromOffset_thenCharArrayIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -44,7 +44,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingWriteStringFromOffset_thenLengthOfStringIsPrinted() throws FileNotFoundException { + void whenUsingWriteStringFromOffset_thenLengthOfStringIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -55,7 +55,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingPrintBoolean_thenStringValueIsPrinted() throws FileNotFoundException { + void whenUsingPrintBoolean_thenStringValueIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -66,7 +66,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingPrintChar_thenCharIsPrinted() throws FileNotFoundException { + void whenUsingPrintChar_thenCharIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -77,7 +77,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingPrintInt_thenValueOfIntIsPrinted() throws FileNotFoundException { + void whenUsingPrintInt_thenValueOfIntIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -88,7 +88,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingPrintString_thenStringIsPrinted() throws FileNotFoundException { + void whenUsingPrintString_thenStringIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); @@ -99,7 +99,7 @@ public class WriteVsPrintUnitTest { } @Test - public void whenUsingPrintObject_thenObjectToStringIsPrinted() throws FileNotFoundException { + void whenUsingPrintObject_thenObjectToStringIsPrinted() throws FileNotFoundException { PrintWriter printWriter = new PrintWriter("output.txt"); From f3906abe87d9519dc21b90c977b8006bef2ef41a Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Thu, 11 Apr 2024 16:59:20 +0300 Subject: [PATCH 12/13] Andrei Branza - moved code for article to new module --- .../WriteVsPrintUnitTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 core-java-modules/core-java-io-5/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java diff --git a/core-java-modules/core-java-io-5/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java b/core-java-modules/core-java-io-5/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java new file mode 100644 index 0000000000..ad9c0f4619 --- /dev/null +++ b/core-java-modules/core-java-io-5/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java @@ -0,0 +1,113 @@ +package com.baeldung.printwriterwritevsprint; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; + +public class WriteVsPrintUnitTest { + + Object outputFromPrintWriter; + Object outputFromPrintWriter() { + try (BufferedReader br = new BufferedReader(new FileReader("output.txt"))){ + outputFromPrintWriter = br.readLine(); + } catch (IOException e){ + e.printStackTrace(); + Assertions.fail(); + } + return outputFromPrintWriter; + } + + @Test + void whenUsingWriteInt_thenASCIICharacterIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write(48); + printWriter.close(); + + assertEquals("0", outputFromPrintWriter()); + } + + @Test + void whenUsingWriteCharArrayFromOffset_thenCharArrayIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write(new char[]{'A','/','&','4','E'}, 1, 4 ); + printWriter.close(); + + assertEquals("/&4E", outputFromPrintWriter()); + } + + @Test + void whenUsingWriteStringFromOffset_thenLengthOfStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.write("StringExample", 6, 7 ); + printWriter.close(); + + assertEquals("Example", outputFromPrintWriter()); + } + + @Test + void whenUsingPrintBoolean_thenStringValueIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print(true); + printWriter.close(); + + assertEquals("true", outputFromPrintWriter()); + } + + @Test + void whenUsingPrintChar_thenCharIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print('A'); + printWriter.close(); + + assertEquals("A", outputFromPrintWriter()); + } + + @Test + void whenUsingPrintInt_thenValueOfIntIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print(420); + printWriter.close(); + + assertEquals("420", outputFromPrintWriter()); + } + + @Test + void whenUsingPrintString_thenStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + printWriter.print("RandomString"); + printWriter.close(); + + assertEquals("RandomString", outputFromPrintWriter()); + } + + @Test + void whenUsingPrintObject_thenObjectToStringIsPrinted() throws FileNotFoundException { + + PrintWriter printWriter = new PrintWriter("output.txt"); + + Map example = new HashMap(); + + printWriter.print(example); + printWriter.close(); + + assertEquals(example.toString(), outputFromPrintWriter()); + } +} \ No newline at end of file From 3826c5b8e699b7d1ed95a4ae1cbe9997a5455626 Mon Sep 17 00:00:00 2001 From: Andrei Branza Date: Thu, 11 Apr 2024 17:04:01 +0300 Subject: [PATCH 13/13] Andrei Branza - removed old code from old module --- .../WriteVsPrintUnitTest.java | 113 ------------------ 1 file changed, 113 deletions(-) delete mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java deleted file mode 100644 index ad9c0f4619..0000000000 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwriterwritevsprint/WriteVsPrintUnitTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.baeldung.printwriterwritevsprint; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.*; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.*; - -public class WriteVsPrintUnitTest { - - Object outputFromPrintWriter; - Object outputFromPrintWriter() { - try (BufferedReader br = new BufferedReader(new FileReader("output.txt"))){ - outputFromPrintWriter = br.readLine(); - } catch (IOException e){ - e.printStackTrace(); - Assertions.fail(); - } - return outputFromPrintWriter; - } - - @Test - void whenUsingWriteInt_thenASCIICharacterIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.write(48); - printWriter.close(); - - assertEquals("0", outputFromPrintWriter()); - } - - @Test - void whenUsingWriteCharArrayFromOffset_thenCharArrayIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.write(new char[]{'A','/','&','4','E'}, 1, 4 ); - printWriter.close(); - - assertEquals("/&4E", outputFromPrintWriter()); - } - - @Test - void whenUsingWriteStringFromOffset_thenLengthOfStringIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.write("StringExample", 6, 7 ); - printWriter.close(); - - assertEquals("Example", outputFromPrintWriter()); - } - - @Test - void whenUsingPrintBoolean_thenStringValueIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.print(true); - printWriter.close(); - - assertEquals("true", outputFromPrintWriter()); - } - - @Test - void whenUsingPrintChar_thenCharIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.print('A'); - printWriter.close(); - - assertEquals("A", outputFromPrintWriter()); - } - - @Test - void whenUsingPrintInt_thenValueOfIntIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.print(420); - printWriter.close(); - - assertEquals("420", outputFromPrintWriter()); - } - - @Test - void whenUsingPrintString_thenStringIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - printWriter.print("RandomString"); - printWriter.close(); - - assertEquals("RandomString", outputFromPrintWriter()); - } - - @Test - void whenUsingPrintObject_thenObjectToStringIsPrinted() throws FileNotFoundException { - - PrintWriter printWriter = new PrintWriter("output.txt"); - - Map example = new HashMap(); - - printWriter.print(example); - printWriter.close(); - - assertEquals(example.toString(), outputFromPrintWriter()); - } -} \ No newline at end of file