From d1a9ff7480f8f21543c02d61992d0dd20fa4e56d Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Tue, 30 Jun 2020 15:52:17 +0200 Subject: [PATCH 01/12] [BAEL-4281] Examples of array comparisons --- java-collections/README.md | 8 +++ java-collections/pom.xml | 49 +++++++++++++++++++ .../com/baeldung/comparingarrays/Plane.java | 39 +++++++++++++++ .../src/main/resources/logback.xml | 13 +++++ .../baeldung/DeepEqualsCompareUnitTest.java | 34 +++++++++++++ .../com/baeldung/EqualsCompareUnitTest.java | 29 +++++++++++ .../com/baeldung/LengthsCompareUnitTest.java | 22 +++++++++ .../com/baeldung/OrderCompareUnitTest.java | 34 +++++++++++++ .../baeldung/ReferenceCompareUnitTest.java | 34 +++++++++++++ 9 files changed, 262 insertions(+) create mode 100644 java-collections/README.md create mode 100644 java-collections/pom.xml create mode 100644 java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java create mode 100644 java-collections/src/main/resources/logback.xml create mode 100644 java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java create mode 100644 java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java create mode 100644 java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java create mode 100644 java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java create mode 100644 java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java diff --git a/java-collections/README.md b/java-collections/README.md new file mode 100644 index 0000000000..480b7f257a --- /dev/null +++ b/java-collections/README.md @@ -0,0 +1,8 @@ +## Java Collections Examples + +This module support code for articles about handling java collections and arrays. + +### Relevant Articles: +- [Comparing two integer arrays in Java](https://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java) +- [Compare two arrays with not the same order](https://stackoverflow.com/questions/54711228/compare-two-arrays-with-not-the-same-order) +- More articles: [[next -->]](../java-collections-conversions-2) \ No newline at end of file diff --git a/java-collections/pom.xml b/java-collections/pom.xml new file mode 100644 index 0000000000..5e0ee42244 --- /dev/null +++ b/java-collections/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + java-collections + 0.1.0-SNAPSHOT + java-collections + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.hamcrest + hamcrest-all + ${hamcrest-all.version} + test + + + + + java-collections + + + src/main/resources + true + + + + + + 4.1 + + diff --git a/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java b/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java new file mode 100644 index 0000000000..db61807878 --- /dev/null +++ b/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java @@ -0,0 +1,39 @@ +package com.baeldung.comparingarrays; + +import java.util.Objects; + +public class Plane { + + private final String name; + + private final String model; + + public Plane(String name, String model) { + + this.name = name; + this.model = model; + } + + public String getName() { + return name; + } + + public String getModel() { + return model; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Plane plane = (Plane) o; + return Objects.equals(name, plane.name) && Objects.equals(model, plane.model); + } + + @Override + public int hashCode() { + return Objects.hash(name, model); + } +} diff --git a/java-collections/src/main/resources/logback.xml b/java-collections/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/java-collections/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java new file mode 100644 index 0000000000..f311d0e5c9 --- /dev/null +++ b/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung; + +import com.baeldung.comparingarrays.Plane; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DeepEqualsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameContent_thenDeepEquals() { + final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + + boolean result = Arrays.deepEquals(planes1, planes2); + assertTrue("Result is not true", result); + } + + @Test + public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") }, + new Plane[] { new Plane("Plane 1", "A320") } }; + + boolean result = Arrays.deepEquals(planes1, planes2); + assertFalse("Result is true", result); + } +} diff --git a/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java new file mode 100644 index 0000000000..b56395a848 --- /dev/null +++ b/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class EqualsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameContent_thenEquals() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + boolean result = Arrays.equals(planes1, planes2); + assertTrue("Result is not true", result); + } + + @Test + public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + boolean result = Arrays.equals(planes1, planes2); + assertFalse("Result is true", result); + } +} diff --git a/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java new file mode 100644 index 0000000000..8b3c90c427 --- /dev/null +++ b/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; +import static org.junit.Assert.assertThat; + +public class LengthsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 }; + + assertThat(planes1, arrayWithSize(8)); + assertThat(quantities, arrayWithSize(8)); + assertThat(planes1.length, is(8)); + assertThat(quantities.length, is(8)); + } +} + diff --git a/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java new file mode 100644 index 0000000000..8be7251c05 --- /dev/null +++ b/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung; + +import com.baeldung.comparingarrays.Plane; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.assertTrue; + +public class OrderCompareUnitTest { + @Test + public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + final Plane[][] planes1 = new Plane[][] { + new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { + new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } }; + + Comparator planeComparator = (o1, o2) -> { + if (o1.getName() + .equals(o2.getName())) { + return o2.getModel() + .compareTo(o1.getModel()); + } + return o2.getName() + .compareTo(o1.getName()); + }; + Arrays.sort(planes1[0], planeComparator); + Arrays.sort(planes2[0], planeComparator); + + boolean result = Arrays.deepEquals(planes1, planes2); + assertTrue("Result is false", result); + } +} diff --git a/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java new file mode 100644 index 0000000000..9716a34607 --- /dev/null +++ b/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; + +public class ReferenceCompareUnitTest { + + @Test + public void givenArray1andArray2_whenEquals_thenEqual() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = planes1; + + assertSame("Objects are not equal!", planes1, planes2); + + planes2[0] = "747"; + + assertSame("Objects are not same!", planes1, planes2); + assertEquals("Objects are not equal!", "747", planes2[0]); + assertEquals("Objects are not equal!", "747", planes1[0]); + } + + @Test + public void givenArray1andArray2_whenDifferentValues_thenNotEqual() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + assertNotSame("Objects are the same!", planes1, planes2); + assertNotEquals("Objects are equal!", planes1, planes2); + } +} From 289276f8ff9ca11d30036d8673fbaddb9f36409c Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Tue, 21 Jul 2020 06:36:14 +0200 Subject: [PATCH 02/12] Revert "[BAEL-4281] Examples of array comparisons" This reverts commit d1a9ff7480f8f21543c02d61992d0dd20fa4e56d. --- java-collections/README.md | 8 --- java-collections/pom.xml | 49 ------------------- .../com/baeldung/comparingarrays/Plane.java | 39 --------------- .../src/main/resources/logback.xml | 13 ----- .../baeldung/DeepEqualsCompareUnitTest.java | 34 ------------- .../com/baeldung/EqualsCompareUnitTest.java | 29 ----------- .../com/baeldung/LengthsCompareUnitTest.java | 22 --------- .../com/baeldung/OrderCompareUnitTest.java | 34 ------------- .../baeldung/ReferenceCompareUnitTest.java | 34 ------------- 9 files changed, 262 deletions(-) delete mode 100644 java-collections/README.md delete mode 100644 java-collections/pom.xml delete mode 100644 java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java delete mode 100644 java-collections/src/main/resources/logback.xml delete mode 100644 java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java delete mode 100644 java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java delete mode 100644 java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java delete mode 100644 java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java delete mode 100644 java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java diff --git a/java-collections/README.md b/java-collections/README.md deleted file mode 100644 index 480b7f257a..0000000000 --- a/java-collections/README.md +++ /dev/null @@ -1,8 +0,0 @@ -## Java Collections Examples - -This module support code for articles about handling java collections and arrays. - -### Relevant Articles: -- [Comparing two integer arrays in Java](https://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java) -- [Compare two arrays with not the same order](https://stackoverflow.com/questions/54711228/compare-two-arrays-with-not-the-same-order) -- More articles: [[next -->]](../java-collections-conversions-2) \ No newline at end of file diff --git a/java-collections/pom.xml b/java-collections/pom.xml deleted file mode 100644 index 5e0ee42244..0000000000 --- a/java-collections/pom.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - 4.0.0 - java-collections - 0.1.0-SNAPSHOT - java-collections - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../parent-java - - - - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - org.hamcrest - hamcrest-all - ${hamcrest-all.version} - test - - - - - java-collections - - - src/main/resources - true - - - - - - 4.1 - - diff --git a/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java b/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java deleted file mode 100644 index db61807878..0000000000 --- a/java-collections/src/main/java/com/baeldung/comparingarrays/Plane.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.comparingarrays; - -import java.util.Objects; - -public class Plane { - - private final String name; - - private final String model; - - public Plane(String name, String model) { - - this.name = name; - this.model = model; - } - - public String getName() { - return name; - } - - public String getModel() { - return model; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - Plane plane = (Plane) o; - return Objects.equals(name, plane.name) && Objects.equals(model, plane.model); - } - - @Override - public int hashCode() { - return Objects.hash(name, model); - } -} diff --git a/java-collections/src/main/resources/logback.xml b/java-collections/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/java-collections/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java deleted file mode 100644 index f311d0e5c9..0000000000 --- a/java-collections/src/test/java/com/baeldung/DeepEqualsCompareUnitTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung; - -import com.baeldung.comparingarrays.Plane; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class DeepEqualsCompareUnitTest { - - @Test - public void givenArray1andArray2_whenSameContent_thenDeepEquals() { - final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, - new Plane[] { new Plane("Plane 2", "B738") } }; - final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, - new Plane[] { new Plane("Plane 2", "B738") } }; - - boolean result = Arrays.deepEquals(planes1, planes2); - assertTrue("Result is not true", result); - } - - @Test - public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { - final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, - new Plane[] { new Plane("Plane 2", "B738") } }; - final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") }, - new Plane[] { new Plane("Plane 1", "A320") } }; - - boolean result = Arrays.deepEquals(planes1, planes2); - assertFalse("Result is true", result); - } -} diff --git a/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java deleted file mode 100644 index b56395a848..0000000000 --- a/java-collections/src/test/java/com/baeldung/EqualsCompareUnitTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class EqualsCompareUnitTest { - - @Test - public void givenArray1andArray2_whenSameContent_thenEquals() { - final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - - boolean result = Arrays.equals(planes1, planes2); - assertTrue("Result is not true", result); - } - - @Test - public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() { - final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" }; - - boolean result = Arrays.equals(planes1, planes2); - assertFalse("Result is true", result); - } -} diff --git a/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java deleted file mode 100644 index 8b3c90c427..0000000000 --- a/java-collections/src/test/java/com/baeldung/LengthsCompareUnitTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung; - -import org.junit.jupiter.api.Test; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; -import static org.junit.Assert.assertThat; - -public class LengthsCompareUnitTest { - - @Test - public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() { - final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 }; - - assertThat(planes1, arrayWithSize(8)); - assertThat(quantities, arrayWithSize(8)); - assertThat(planes1.length, is(8)); - assertThat(quantities.length, is(8)); - } -} - diff --git a/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java deleted file mode 100644 index 8be7251c05..0000000000 --- a/java-collections/src/test/java/com/baeldung/OrderCompareUnitTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung; - -import com.baeldung.comparingarrays.Plane; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Comparator; - -import static org.junit.Assert.assertTrue; - -public class OrderCompareUnitTest { - @Test - public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { - final Plane[][] planes1 = new Plane[][] { - new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } }; - final Plane[][] planes2 = new Plane[][] { - new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } }; - - Comparator planeComparator = (o1, o2) -> { - if (o1.getName() - .equals(o2.getName())) { - return o2.getModel() - .compareTo(o1.getModel()); - } - return o2.getName() - .compareTo(o1.getName()); - }; - Arrays.sort(planes1[0], planeComparator); - Arrays.sort(planes2[0], planeComparator); - - boolean result = Arrays.deepEquals(planes1, planes2); - assertTrue("Result is false", result); - } -} diff --git a/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java b/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java deleted file mode 100644 index 9716a34607..0000000000 --- a/java-collections/src/test/java/com/baeldung/ReferenceCompareUnitTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung; - -import org.junit.jupiter.api.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; - -public class ReferenceCompareUnitTest { - - @Test - public void givenArray1andArray2_whenEquals_thenEqual() { - final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - final String[] planes2 = planes1; - - assertSame("Objects are not equal!", planes1, planes2); - - planes2[0] = "747"; - - assertSame("Objects are not same!", planes1, planes2); - assertEquals("Objects are not equal!", "747", planes2[0]); - assertEquals("Objects are not equal!", "747", planes1[0]); - } - - @Test - public void givenArray1andArray2_whenDifferentValues_thenNotEqual() { - final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - - assertNotSame("Objects are the same!", planes1, planes2); - assertNotEquals("Objects are equal!", planes1, planes2); - } -} From ad1d9b4b847270082314659ad2736c6ee0f2fe14 Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Tue, 21 Jul 2020 06:37:09 +0200 Subject: [PATCH 03/12] [BAEL-4281] moves example module --- .../java/com/baeldung/arraycompare/Plane.java | 39 +++++++++++++++++++ .../DeepEqualsCompareUnitTest.java | 33 ++++++++++++++++ .../arraycompare/EqualsCompareUnitTest.java | 29 ++++++++++++++ .../arraycompare/LengthsCompareUnitTest.java | 22 +++++++++++ .../arraycompare/OrderCompareUnitTest.java | 33 ++++++++++++++++ .../ReferenceCompareUnitTest.java | 34 ++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java new file mode 100644 index 0000000000..1731578b76 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arraycompare/Plane.java @@ -0,0 +1,39 @@ +package com.baeldung.arraycompare; + +import java.util.Objects; + +public class Plane { + + private final String name; + + private final String model; + + public Plane(String name, String model) { + + this.name = name; + this.model = model; + } + + public String getName() { + return name; + } + + public String getModel() { + return model; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Plane plane = (Plane) o; + return Objects.equals(name, plane.name) && Objects.equals(model, plane.model); + } + + @Override + public int hashCode() { + return Objects.hash(name, model); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java new file mode 100644 index 0000000000..ab7eaec127 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.arraycompare; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DeepEqualsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameContent_thenDeepEquals() { + final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + + boolean result = Arrays.deepEquals(planes1, planes2); + assertTrue("Result is not true", result); + } + + @Test + public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, + new Plane[] { new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") }, + new Plane[] { new Plane("Plane 1", "A320") } }; + + boolean result = Arrays.deepEquals(planes1, planes2); + assertFalse("Result is true", result); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java new file mode 100644 index 0000000000..dd74eb8f0a --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.arraycompare; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class EqualsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameContent_thenEquals() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + boolean result = Arrays.equals(planes1, planes2); + assertTrue("Result is not true", result); + } + + @Test + public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + boolean result = Arrays.equals(planes1, planes2); + assertFalse("Result is true", result); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java new file mode 100644 index 0000000000..ef22d69d90 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.arraycompare; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; +import static org.junit.Assert.assertThat; + +public class LengthsCompareUnitTest { + + @Test + public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 }; + + assertThat(planes1, arrayWithSize(8)); + assertThat(quantities, arrayWithSize(8)); + assertThat(planes1.length, is(8)); + assertThat(quantities.length, is(8)); + } +} + diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java new file mode 100644 index 0000000000..77cc91092a --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.arraycompare; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.assertTrue; + +public class OrderCompareUnitTest { + @Test + public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + final Plane[][] planes1 = new Plane[][] { + new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } }; + final Plane[][] planes2 = new Plane[][] { + new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } }; + + Comparator planeComparator = (o1, o2) -> { + if (o1.getName() + .equals(o2.getName())) { + return o2.getModel() + .compareTo(o1.getModel()); + } + return o2.getName() + .compareTo(o1.getName()); + }; + Arrays.sort(planes1[0], planeComparator); + Arrays.sort(planes2[0], planeComparator); + + boolean result = Arrays.deepEquals(planes1, planes2); + assertTrue("Result is false", result); + } +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java new file mode 100644 index 0000000000..8a26fc8e74 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.arraycompare; + +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; + +public class ReferenceCompareUnitTest { + + @Test + public void givenArray1andArray2_whenEquals_thenEqual() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = planes1; + + assertSame("Objects are not equal!", planes1, planes2); + + planes2[0] = "747"; + + assertSame("Objects are not same!", planes1, planes2); + assertEquals("Objects are not equal!", "747", planes2[0]); + assertEquals("Objects are not equal!", "747", planes1[0]); + } + + @Test + public void givenArray1andArray2_whenDifferentValues_thenNotEqual() { + final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; + + assertNotSame("Objects are the same!", planes1, planes2); + assertNotEquals("Objects are equal!", planes1, planes2); + } +} From cffc591b5cd84ef056bc90d5e3a3d087d8882850 Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Tue, 21 Jul 2020 06:55:28 +0200 Subject: [PATCH 04/12] [BAEL-4281] Naming convention and conversion to assertJ --- .../DeepEqualsCompareUnitTest.java | 13 +++++------- .../arraycompare/EqualsCompareUnitTest.java | 13 +++++------- .../arraycompare/LengthsCompareUnitTest.java | 12 ++++------- .../arraycompare/OrderCompareUnitTest.java | 6 +++--- .../ReferenceCompareUnitTest.java | 20 ++++++++----------- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java index ab7eaec127..c8ebafb26b 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/DeepEqualsCompareUnitTest.java @@ -4,30 +4,27 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; public class DeepEqualsCompareUnitTest { @Test - public void givenArray1andArray2_whenSameContent_thenDeepEquals() { + public void givenSameContents_whenDeepEquals_thenTrue() { final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, new Plane[] { new Plane("Plane 2", "B738") } }; final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, new Plane[] { new Plane("Plane 2", "B738") } }; - boolean result = Arrays.deepEquals(planes1, planes2); - assertTrue("Result is not true", result); + assertThat(Arrays.deepEquals(planes1, planes2)).isTrue(); } @Test - public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + public void givenSameContentsWithDifferentOrder_whenDeepEquals_thenFalse() { final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") }, new Plane[] { new Plane("Plane 2", "B738") } }; final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") }, new Plane[] { new Plane("Plane 1", "A320") } }; - boolean result = Arrays.deepEquals(planes1, planes2); - assertFalse("Result is true", result); + assertThat(Arrays.deepEquals(planes1, planes2)).isFalse(); } } diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java index dd74eb8f0a..a022bf7082 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/EqualsCompareUnitTest.java @@ -4,26 +4,23 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; public class EqualsCompareUnitTest { @Test - public void givenArray1andArray2_whenSameContent_thenEquals() { + public void givenSameContents_whenEquals_thenTrue() { final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - boolean result = Arrays.equals(planes1, planes2); - assertTrue("Result is not true", result); + assertThat(Arrays.equals(planes1, planes2)).isTrue(); } @Test - public void givenArray1andArray2_whenSameContentOtherSort_thenNotEquals() { + public void givenSameContentsDifferentOrder_whenEquals_thenFalse() { final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" }; - boolean result = Arrays.equals(planes1, planes2); - assertFalse("Result is true", result); + assertThat(Arrays.equals(planes1, planes2)).isFalse(); } } diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java index ef22d69d90..23187b827c 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/LengthsCompareUnitTest.java @@ -2,21 +2,17 @@ package com.baeldung.arraycompare; import org.junit.jupiter.api.Test; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class LengthsCompareUnitTest { @Test - public void givenArray1andArray2_whenSameSizes_thenSizeEqualsOk() { + public void givenSameContent_whenSizeCompare_thenTrue() { final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 }; - assertThat(planes1, arrayWithSize(8)); - assertThat(quantities, arrayWithSize(8)); - assertThat(planes1.length, is(8)); - assertThat(quantities.length, is(8)); + assertThat(planes1).hasSize(8); + assertThat(quantities).hasSize(8); } } diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java index 77cc91092a..4dd7964020 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/OrderCompareUnitTest.java @@ -5,11 +5,12 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Comparator; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertTrue; public class OrderCompareUnitTest { @Test - public void givenArray1andArray2_whenNotSameContent_thenNotDeepEquals() { + public void givenSameContentDifferentOrder_whenSortedAndDeepEquals_thenTrue() { final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } }; final Plane[][] planes2 = new Plane[][] { @@ -27,7 +28,6 @@ public class OrderCompareUnitTest { Arrays.sort(planes1[0], planeComparator); Arrays.sort(planes2[0], planeComparator); - boolean result = Arrays.deepEquals(planes1, planes2); - assertTrue("Result is false", result); + assertThat(Arrays.deepEquals(planes1, planes2)).isTrue(); } } diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java index 8a26fc8e74..d8072a98e3 100644 --- a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ReferenceCompareUnitTest.java @@ -2,33 +2,29 @@ package com.baeldung.arraycompare; import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; +import static org.assertj.core.api.Assertions.assertThat; public class ReferenceCompareUnitTest { @Test - public void givenArray1andArray2_whenEquals_thenEqual() { + public void givenSameReferences_whenSame_thenTrue() { final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; final String[] planes2 = planes1; - assertSame("Objects are not equal!", planes1, planes2); + assertThat(planes1).isSameAs(planes2); planes2[0] = "747"; - assertSame("Objects are not same!", planes1, planes2); - assertEquals("Objects are not equal!", "747", planes2[0]); - assertEquals("Objects are not equal!", "747", planes1[0]); + assertThat(planes1).isSameAs(planes2); + assertThat(planes2[0]).isEqualTo("747"); + assertThat(planes1[0]).isEqualTo("747"); } @Test - public void givenArray1andArray2_whenDifferentValues_thenNotEqual() { + public void givenSameContentDifferentReferences_whenSame_thenFalse() { final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" }; - assertNotSame("Objects are the same!", planes1, planes2); - assertNotEquals("Objects are equal!", planes1, planes2); + assertThat(planes1).isNotSameAs(planes2); } } From 2127c7935452ca7556b65f913c598bcf36513f24 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 23 Jul 2020 19:00:08 +0200 Subject: [PATCH 05/12] BAEL-4289 --- .../baeldung/screenshot/ScreenshotTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java new file mode 100644 index 0000000000..3df31897e1 --- /dev/null +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java @@ -0,0 +1,54 @@ +import javax.imageio.ImageIO; +import java.awt.Component; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; +import java.io.File; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ScreenshotTest { + + @Test + public void takeScreenshotOfMainScreen() throws Exception { + Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); + BufferedImage capture = new Robot().createScreenCapture(screenRect); + File imageFile = new File("single-screen.bmp"); + ImageIO.write(capture, "bmp", imageFile); + assertTrue(imageFile.exists()); + imageFile.delete(); + } + + @Test + public void takeScreenshotOfAllScreens() throws Exception { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice[] screens = ge.getScreenDevices(); + Rectangle allScreenBounds = new Rectangle(); + for (GraphicsDevice screen : screens) { + Rectangle screenBounds = screen.getDefaultConfiguration().getBounds(); + allScreenBounds.width += screenBounds.width; + allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height); + } + BufferedImage capture = new Robot().createScreenCapture(allScreenBounds); + File imageFile = new File("all-screens.bmp"); + ImageIO.write(capture, "bmp", imageFile); + assertTrue(imageFile.exists()); + imageFile.delete(); + } + + @Test + public void makeScreenshot(Component component) throws Exception { + Rectangle componentRect = component.getBounds(); + BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB); + component.paint(bufferedImage.getGraphics()); + File imageFile = new File("component-screenshot.bmp"); + ImageIO.write(bufferedImage, "bmp", imageFile); + assertTrue(imageFile.exists()); + imageFile.delete(); + } + +} \ No newline at end of file From 12322ff46b265ccf3b58aac62c5d197bc495bda4 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Wed, 29 Jul 2020 08:59:06 +0200 Subject: [PATCH 06/12] update test --- .../java/com/baeldung/screenshot/ScreenshotTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java index 3df31897e1..20d2d333a3 100644 --- a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java @@ -17,10 +17,9 @@ public class ScreenshotTest { public void takeScreenshotOfMainScreen() throws Exception { Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenRect); - File imageFile = new File("single-screen.bmp"); + File imageFile = File.createTempFile("single-screen", "bmp"); ImageIO.write(capture, "bmp", imageFile); assertTrue(imageFile.exists()); - imageFile.delete(); } @Test @@ -34,10 +33,9 @@ public class ScreenshotTest { allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height); } BufferedImage capture = new Robot().createScreenCapture(allScreenBounds); - File imageFile = new File("all-screens.bmp"); + File imageFile = File.createTempFile("all-screens", "bmp"); ImageIO.write(capture, "bmp", imageFile); assertTrue(imageFile.exists()); - imageFile.delete(); } @Test @@ -45,10 +43,9 @@ public class ScreenshotTest { Rectangle componentRect = component.getBounds(); BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB); component.paint(bufferedImage.getGraphics()); - File imageFile = new File("component-screenshot.bmp"); + File imageFile = File.createTempFile("component-screenshot", "bmp"); ImageIO.write(bufferedImage, "bmp", imageFile); assertTrue(imageFile.exists()); - imageFile.delete(); } } \ No newline at end of file From baa46f6f4fb4ede1cb89893dacde69cbbf9baef1 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 30 Jul 2020 11:26:57 +0200 Subject: [PATCH 07/12] rename --- .../{ScreenshotTest.java => ScreenshotUnitTest.java} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/{ScreenshotTest.java => ScreenshotUnitTest.java} (85%) diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotUnitTest.java similarity index 85% rename from core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java rename to core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotUnitTest.java index 20d2d333a3..6bd0e7dff7 100644 --- a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotUnitTest.java @@ -11,10 +11,10 @@ import org.junit.Test; import static org.junit.Assert.assertTrue; -public class ScreenshotTest { +public class ScreenshotUnitTest { @Test - public void takeScreenshotOfMainScreen() throws Exception { + public void givenMainScreen_whenTakeScreenshot_thenSaveToFile() throws Exception { Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenRect); File imageFile = File.createTempFile("single-screen", "bmp"); @@ -23,7 +23,7 @@ public class ScreenshotTest { } @Test - public void takeScreenshotOfAllScreens() throws Exception { + public void givenMultipleScreens_whenTakeScreenshot_thenSaveToFile() throws Exception { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] screens = ge.getScreenDevices(); Rectangle allScreenBounds = new Rectangle(); @@ -39,7 +39,7 @@ public class ScreenshotTest { } @Test - public void makeScreenshot(Component component) throws Exception { + public void givenComponent_whenTakeScreenshot_thenSaveToFile(Component component) throws Exception { Rectangle componentRect = component.getBounds(); BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB); component.paint(bufferedImage.getGraphics()); From 06ac0b5589d6ff198b10c5ff411e1f09d64a18bd Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 30 Jul 2020 18:06:42 +0530 Subject: [PATCH 08/12] JAVA-1525: Moved 1 article to security module --- spring-5-reactive-security/README.md | 1 + .../cors/annotated/CorsOnAnnotatedElementsApplication.java | 0 .../cors/annotated/controllers/CorsOnClassController.java | 0 .../cors/annotated/controllers/CorsOnMethodsController.java | 0 .../reactive/cors/global/CorsGlobalConfigApplication.java | 0 .../reactive/cors/global/config/CorsGlobalConfiguration.java | 0 .../cors/global/controllers/FurtherCorsConfigsController.java | 0 .../cors/global/controllers/RegularRestController.java | 0 .../functional/handlers/CorsGlobalFunctionalHandler.java | 0 .../cors/global/functional/routers/CorsRouterFunctions.java | 0 .../reactive/cors/webfilter/CorsWebFilterApplication.java | 0 .../reactive/cors/webfilter/config/CorsWebFilterConfig.java | 0 .../webfilter/controllers/FurtherCorsConfigsController.java | 0 .../cors/webfilter/controllers/RegularRestController.java | 0 .../functional/handlers/CorsWithWebFilterHandler.java | 0 .../functional/routers/CorsWithWebFilterRouterFunctions.java | 0 .../reactive/cors/CorsOnAnnotatedElementsLiveTest.java | 4 +++- .../baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java | 4 +++- .../com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java | 4 +++- 19 files changed, 10 insertions(+), 3 deletions(-) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/annotated/CorsOnAnnotatedElementsApplication.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnClassController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnMethodsController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/CorsGlobalConfigApplication.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/config/CorsGlobalConfiguration.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/functional/handlers/CorsGlobalFunctionalHandler.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/global/functional/routers/CorsRouterFunctions.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/CorsWebFilterApplication.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/config/CorsWebFilterConfig.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/FurtherCorsConfigsController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/RegularRestController.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/functional/handlers/CorsWithWebFilterHandler.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/main/java/com/baeldung/reactive/cors/webfilter/functional/routers/CorsWithWebFilterRouterFunctions.java (100%) rename {spring-5-reactive => spring-5-reactive-security}/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java (96%) rename {spring-5-reactive => spring-5-reactive-security}/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java (94%) rename {spring-5-reactive => spring-5-reactive-security}/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java (94%) diff --git a/spring-5-reactive-security/README.md b/spring-5-reactive-security/README.md index a0f47a503d..915f74cd78 100644 --- a/spring-5-reactive-security/README.md +++ b/spring-5-reactive-security/README.md @@ -12,3 +12,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Guide to Spring 5 WebFlux](https://www.baeldung.com/spring-webflux) - [Introduction to the Functional Web Framework in Spring 5](https://www.baeldung.com/spring-5-functional-web) - [Guide to the AuthenticationManagerResolver in Spring Security](https://www.baeldung.com/spring-security-authenticationmanagerresolver) +- [Spring Webflux and CORS](https://www.baeldung.com/spring-webflux-cors) diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/CorsOnAnnotatedElementsApplication.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/CorsOnAnnotatedElementsApplication.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/CorsOnAnnotatedElementsApplication.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/CorsOnAnnotatedElementsApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnClassController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnClassController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnClassController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnClassController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnMethodsController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnMethodsController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnMethodsController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/annotated/controllers/CorsOnMethodsController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/CorsGlobalConfigApplication.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/CorsGlobalConfigApplication.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/CorsGlobalConfigApplication.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/CorsGlobalConfigApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/config/CorsGlobalConfiguration.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/config/CorsGlobalConfiguration.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/config/CorsGlobalConfiguration.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/config/CorsGlobalConfiguration.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/controllers/FurtherCorsConfigsController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/controllers/RegularRestController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/functional/handlers/CorsGlobalFunctionalHandler.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/functional/handlers/CorsGlobalFunctionalHandler.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/functional/handlers/CorsGlobalFunctionalHandler.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/functional/handlers/CorsGlobalFunctionalHandler.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/functional/routers/CorsRouterFunctions.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/functional/routers/CorsRouterFunctions.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/global/functional/routers/CorsRouterFunctions.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/global/functional/routers/CorsRouterFunctions.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/CorsWebFilterApplication.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/CorsWebFilterApplication.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/CorsWebFilterApplication.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/CorsWebFilterApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/config/CorsWebFilterConfig.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/config/CorsWebFilterConfig.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/config/CorsWebFilterConfig.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/config/CorsWebFilterConfig.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/FurtherCorsConfigsController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/FurtherCorsConfigsController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/FurtherCorsConfigsController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/FurtherCorsConfigsController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/RegularRestController.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/RegularRestController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/RegularRestController.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/controllers/RegularRestController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/functional/handlers/CorsWithWebFilterHandler.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/functional/handlers/CorsWithWebFilterHandler.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/functional/handlers/CorsWithWebFilterHandler.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/functional/handlers/CorsWithWebFilterHandler.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/functional/routers/CorsWithWebFilterRouterFunctions.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/functional/routers/CorsWithWebFilterRouterFunctions.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/cors/webfilter/functional/routers/CorsWithWebFilterRouterFunctions.java rename to spring-5-reactive-security/src/main/java/com/baeldung/reactive/cors/webfilter/functional/routers/CorsWithWebFilterRouterFunctions.java diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java similarity index 96% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java rename to spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java index e6847e63da..5fd20eedfd 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnAnnotatedElementsLiveTest.java @@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +import com.baeldung.reactive.cors.annotated.CorsOnAnnotatedElementsApplication; + +@SpringBootTest(classes = CorsOnAnnotatedElementsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CorsOnAnnotatedElementsLiveTest { private static final String BASE_URL = "http://localhost:8081"; diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java similarity index 94% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java rename to spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java index 008f1a16f2..5c6582c127 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnGlobalConfigLiveTest.java @@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +import com.baeldung.reactive.cors.global.CorsGlobalConfigApplication; + +@SpringBootTest(classes = CorsGlobalConfigApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CorsOnGlobalConfigLiveTest { private static final String BASE_URL = "http://localhost:8082"; diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java similarity index 94% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java rename to spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java index f8a4f34e29..7cc44454ec 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/cors/CorsOnWebFilterLiveTest.java @@ -6,7 +6,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +import com.baeldung.reactive.cors.webfilter.CorsWebFilterApplication; + +@SpringBootTest(classes = CorsWebFilterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class CorsOnWebFilterLiveTest { private static final String BASE_URL = "http://localhost:8083"; From 4dc2535bc5361493c305a1834b7c6ad90210ef3c Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 30 Jul 2020 18:07:49 +0530 Subject: [PATCH 09/12] JAVA-1525: Moved 1 article to reactive-2 module --- .../serversentevents/consumer/ConsumerSSEApplication.java | 0 .../consumer/controller/ClientController.java | 0 .../serversentevents/server/ServerSSEApplication.java | 0 .../serversentevents/server/controllers/ServerController.java | 0 .../reactive/serversentsevents/ServiceSentEventLiveTest.java | 4 +++- 5 files changed, 3 insertions(+), 1 deletion(-) rename {spring-5-reactive => spring-5-reactive-2}/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java (100%) rename {spring-5-reactive => spring-5-reactive-2}/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java (100%) rename {spring-5-reactive => spring-5-reactive-2}/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java (100%) rename {spring-5-reactive => spring-5-reactive-2}/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java (100%) rename {spring-5-reactive => spring-5-reactive-2}/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java (92%) diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java b/spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java rename to spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/consumer/ConsumerSSEApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java b/spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java rename to spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/consumer/controller/ClientController.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java b/spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java rename to spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/server/ServerSSEApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java b/spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java similarity index 100% rename from spring-5-reactive/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java rename to spring-5-reactive-2/src/main/java/com/baeldung/reactive/serversentevents/server/controllers/ServerController.java diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java b/spring-5-reactive-2/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java similarity index 92% rename from spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java rename to spring-5-reactive-2/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java index 547cd99034..946a038763 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java +++ b/spring-5-reactive-2/src/test/java/com/baeldung/reactive/serversentsevents/ServiceSentEventLiveTest.java @@ -8,7 +8,9 @@ import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.reactive.server.WebTestClient; -@SpringBootTest +import com.baeldung.reactive.serversentevents.server.ServerSSEApplication; + +@SpringBootTest(classes = ServerSSEApplication.class) @WithMockUser public class ServiceSentEventLiveTest { From efbd00fdde4246cd6ccf9b955f4cb59c511a6f37 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 30 Jul 2020 18:09:10 +0530 Subject: [PATCH 10/12] JAVA-1525: Moved 1 article to -client module to make space for 1 from reactive module --- spring-5-reactive-2/pom.xml | 5 +++++ spring-5-reactive-client/pom.xml | 4 ++++ .../java/com/baeldung/webclient/filter/WebClientFilters.java | 0 .../baeldung/webclient/filter/FilteredWebClientUnitTest.java | 0 4 files changed, 9 insertions(+) rename {spring-5-reactive-2 => spring-5-reactive-client}/src/main/java/com/baeldung/webclient/filter/WebClientFilters.java (100%) rename {spring-5-reactive-2 => spring-5-reactive-client}/src/test/java/com/baeldung/webclient/filter/FilteredWebClientUnitTest.java (100%) diff --git a/spring-5-reactive-2/pom.xml b/spring-5-reactive-2/pom.xml index fdeebd1dfd..4cb85d879e 100644 --- a/spring-5-reactive-2/pom.xml +++ b/spring-5-reactive-2/pom.xml @@ -53,6 +53,11 @@ reactor-test test + + org.springframework.security + spring-security-test + test + diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 0b5efd1a47..7ae7ba6edd 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -33,6 +33,10 @@ org.springframework.boot spring-boot-starter-webflux + + org.springframework.boot + spring-boot-starter-security + org.projectreactor reactor-spring diff --git a/spring-5-reactive-2/src/main/java/com/baeldung/webclient/filter/WebClientFilters.java b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/filter/WebClientFilters.java similarity index 100% rename from spring-5-reactive-2/src/main/java/com/baeldung/webclient/filter/WebClientFilters.java rename to spring-5-reactive-client/src/main/java/com/baeldung/webclient/filter/WebClientFilters.java diff --git a/spring-5-reactive-2/src/test/java/com/baeldung/webclient/filter/FilteredWebClientUnitTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/webclient/filter/FilteredWebClientUnitTest.java similarity index 100% rename from spring-5-reactive-2/src/test/java/com/baeldung/webclient/filter/FilteredWebClientUnitTest.java rename to spring-5-reactive-client/src/test/java/com/baeldung/webclient/filter/FilteredWebClientUnitTest.java From a788fa2751dbfbbdd93eca96551e494726174806 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 30 Jul 2020 18:09:51 +0530 Subject: [PATCH 11/12] JAVA-1525: README updates --- spring-5-reactive-2/README.md | 2 +- spring-5-reactive-client/README.md | 1 + spring-5-reactive/README.md | 3 --- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/spring-5-reactive-2/README.md b/spring-5-reactive-2/README.md index 061c15b148..54f7ad35b1 100644 --- a/spring-5-reactive-2/README.md +++ b/spring-5-reactive-2/README.md @@ -8,5 +8,5 @@ This module contains articles about reactive Spring 5 - [Testing Reactive Streams Using StepVerifier and TestPublisher](https://www.baeldung.com/reactive-streams-step-verifier-test-publisher) - [Debugging Reactive Streams in Spring 5](https://www.baeldung.com/spring-debugging-reactive-streams) - [Static Content in Spring WebFlux](https://www.baeldung.com/spring-webflux-static-content) -- [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters) +- [Server-Sent Events in Spring](https://www.baeldung.com/spring-server-sent-events) - More articles: [[<-- prev]](/spring-5-reactive) diff --git a/spring-5-reactive-client/README.md b/spring-5-reactive-client/README.md index bb308ae330..eebdc23aed 100644 --- a/spring-5-reactive-client/README.md +++ b/spring-5-reactive-client/README.md @@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Simultaneous Spring WebClient Calls](https://www.baeldung.com/spring-webclient-simultaneous-calls) - [Logging Spring WebClient Calls](https://www.baeldung.com/spring-log-webclient-calls) - [Mocking a WebClient in Spring](https://www.baeldung.com/spring-mocking-webclient) +- [Spring WebClient Filters](https://www.baeldung.com/spring-webclient-filters) diff --git a/spring-5-reactive/README.md b/spring-5-reactive/README.md index 41d831632a..1945b7ea33 100644 --- a/spring-5-reactive/README.md +++ b/spring-5-reactive/README.md @@ -7,14 +7,11 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring ### Relevant Articles -- [Introduction to the Functional Web Framework in Spring 5](https://www.baeldung.com/spring-5-functional-web) - [Spring 5 WebClient](https://www.baeldung.com/spring-5-webclient) - [Exploring the Spring 5 WebFlux URL Matching](https://www.baeldung.com/spring-5-mvc-url-matching) - [Reactive WebSockets with Spring 5](https://www.baeldung.com/spring-5-reactive-websockets) - [Spring Webflux Filters](https://www.baeldung.com/spring-webflux-filters) - [How to Set a Header on a Response with Spring 5](https://www.baeldung.com/spring-response-header) -- [Spring Webflux and CORS](https://www.baeldung.com/spring-webflux-cors) - [Handling Errors in Spring WebFlux](https://www.baeldung.com/spring-webflux-errors) -- [Server-Sent Events in Spring](https://www.baeldung.com/spring-server-sent-events) - [A Guide to Spring Session Reactive Support: WebSession](https://www.baeldung.com/spring-session-reactive) - More articles: [[next -->]](/spring-5-reactive-2) From ed51104e26ff61c9c245ccfbe53998cb41a2c768 Mon Sep 17 00:00:00 2001 From: vatsalgosar Date: Fri, 31 Jul 2020 01:26:27 +0530 Subject: [PATCH 12/12] BAEL-3941 (#9499) * BAEL-3941 - Code snippets for preserving line breaks using Jsoup while parsing HTML strings * BAEL-3941 - swapped the order of arguments in assertEquals --- .../jsoup/PreservingLineBreaksUnitTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 jsoup/src/test/java/com/baeldung/jsoup/PreservingLineBreaksUnitTest.java diff --git a/jsoup/src/test/java/com/baeldung/jsoup/PreservingLineBreaksUnitTest.java b/jsoup/src/test/java/com/baeldung/jsoup/PreservingLineBreaksUnitTest.java new file mode 100644 index 0000000000..0958fa96e2 --- /dev/null +++ b/jsoup/src/test/java/com/baeldung/jsoup/PreservingLineBreaksUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.jsoup; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.safety.Whitelist; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class PreservingLineBreaksUnitTest { + + @Test + public void whenBackSlashNNewLineCharacter_thenPreserveLineBreak() { + String strHTML = "Hello\nworld"; + Document.OutputSettings outputSettings = new Document.OutputSettings(); + outputSettings.prettyPrint(false); + String strWithNewLines = Jsoup.clean(strHTML, "", Whitelist.none(), outputSettings); + assertEquals("Hello\nworld", strWithNewLines); + } + + @Test + public void whenHTMLNewLineCharacters_thenPreserveLineBreak() { + String strHTML = "" + + "Hello" + + "
" + + "World" + + "

Paragraph

" + + ""; + Document jsoupDoc = Jsoup.parse(strHTML); + Document.OutputSettings outputSettings = new Document.OutputSettings(); + outputSettings.prettyPrint(false); + jsoupDoc.outputSettings(outputSettings); + jsoupDoc.select("br").before("\\n"); + jsoupDoc.select("p").before("\\n"); + String str = jsoupDoc.html().replaceAll("\\\\n", "\n"); + String strWithNewLines = + Jsoup.clean(str, "", Whitelist.none(), outputSettings); + assertEquals("Hello\nWorld\nParagraph", strWithNewLines); + } +}