From f06a9f4106cff01c7de0b05b6d980dfc98533e7d Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Fri, 30 Nov 2018 01:30:19 -0300 Subject: [PATCH 1/3] Sample code for BAEL-2333 --- java-streams/pom.xml | 10 +- .../com/baeldung/stream/filter/Customer.java | 55 +++++++++ .../stream/filter/StreamFilterUnitTest.java | 115 ++++++++++++++++++ 3 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 java-streams/src/main/java/com/baeldung/stream/filter/Customer.java create mode 100644 java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java diff --git a/java-streams/pom.xml b/java-streams/pom.xml index e4670c268d..2b52ebb4b3 100644 --- a/java-streams/pom.xml +++ b/java-streams/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 java-streams 0.1.0-SNAPSHOT @@ -74,6 +74,11 @@ aspectjweaver ${asspectj.version} + + pl.touk + throwing-function + ${throwing-function.version} + @@ -108,8 +113,9 @@ 1.15 0.6.5 2.10 + 1.3 - 3.6.1 + 3.11.1 1.8.9 diff --git a/java-streams/src/main/java/com/baeldung/stream/filter/Customer.java b/java-streams/src/main/java/com/baeldung/stream/filter/Customer.java new file mode 100644 index 0000000000..49da6e7175 --- /dev/null +++ b/java-streams/src/main/java/com/baeldung/stream/filter/Customer.java @@ -0,0 +1,55 @@ +package com.baeldung.stream.filter; + +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Customer { + private String name; + private int points; + private String profilePhotoUrl; + + public Customer(String name, int points) { + this(name, points, ""); + } + + public Customer(String name, int points, String profilePhotoUrl) { + this.name = name; + this.points = points; + this.profilePhotoUrl = profilePhotoUrl; + } + + public String getName() { + return name; + } + + public int getPoints() { + return points; + } + + public boolean hasOver(int points) { + return this.points > points; + } + + public boolean hasOverThousandPoints() { + return this.points > 100; + } + + public boolean hasValidProfilePhoto() throws IOException { + URL url = new URL(this.profilePhotoUrl); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + return connection.getResponseCode() == HttpURLConnection.HTTP_OK; + } + + public boolean hasValidProfilePhotoWithoutCheckedException() { + try { + URL url = new URL(this.profilePhotoUrl); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + return connection.getResponseCode() == HttpURLConnection.HTTP_OK; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java b/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java new file mode 100644 index 0000000000..c89a27cdf1 --- /dev/null +++ b/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java @@ -0,0 +1,115 @@ +package com.baeldung.stream.filter; + +import org.junit.jupiter.api.Test; +import pl.touk.throwing.ThrowingPredicate; +import pl.touk.throwing.exception.WrappedException; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +public class StreamFilterUnitTest { + + @Test + public void givenListOfCustomers_whenFilterByLambda_thenGetTwo() { + List customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); + + long customersWithMoreThan100Points = customers + .stream() + .filter(c -> c.getPoints() > 100) + .count(); + + assertThat(customersWithMoreThan100Points).isEqualTo(2); + } + + @Test + public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() { + List customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); + + long customersWithMoreThan100Points = customers + .stream() + .filter(Customer::hasOverThousandPoints) + .count(); + + assertThat(customersWithMoreThan100Points).isEqualTo(2); + } + + @Test + public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() { + List> customers = Arrays.asList(Optional.of(new Customer("John P.", 15)), Optional.of(new Customer("Sarah M.", 200)), Optional.empty(), Optional.of(new Customer("Mary T.", 300)), Optional.empty()); + + long customersWithMoreThan100Points = customers + .stream() + .flatMap(c -> c + .map(Stream::of) + .orElseGet(Stream::empty)) + .filter(Customer::hasOverThousandPoints) + .count(); + + assertThat(customersWithMoreThan100Points).isEqualTo(2); + } + + @Test + public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() { + List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + + assertThatThrownBy(() -> customers + .stream() + .filter(Customer::hasValidProfilePhotoWithoutCheckedException) + .count()).isInstanceOf(RuntimeException.class); + } + + @Test + public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() { + List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + + assertThatThrownBy(() -> customers + .stream() + .filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto))) + .count()).isInstanceOf(WrappedException.class); + } + + @Test + public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() { + List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + + long customersWithValidProfilePhoto = customers + .stream() + .filter(c -> { + try { + return c.hasValidProfilePhoto(); + } catch (IOException e) { + //handle exception + } + return false; + }) + .count(); + + assertThat(customersWithValidProfilePhoto).isEqualTo(2); + } + + @Test + public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() { + List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), + new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + + assertThatThrownBy(() -> customers + .stream() + .filter(c -> { + try { + return c.hasValidProfilePhoto(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .count()).isInstanceOf(RuntimeException.class); + } +} From be16616bb82749212e259b4d0e44d3f28601a9f4 Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Sat, 8 Dec 2018 16:21:30 -0300 Subject: [PATCH 2/3] Added new samples --- java-streams/.attach_pid12113 | 0 .../stream/filter/StreamFilterUnitTest.java | 94 ++++++++++++++----- 2 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 java-streams/.attach_pid12113 diff --git a/java-streams/.attach_pid12113 b/java-streams/.attach_pid12113 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java b/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java index c89a27cdf1..cf82802940 100644 --- a/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java +++ b/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java @@ -8,56 +8,93 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; public class StreamFilterUnitTest { @Test - public void givenListOfCustomers_whenFilterByLambda_thenGetTwo() { - List customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); + public void givenListOfCustomers_whenFilterByPoints_thenGetTwo() { + Customer john = new Customer("John P.", 15); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1); + List customers = Arrays.asList(john, sarah, charles, mary); - long customersWithMoreThan100Points = customers + List customersWithMoreThan100Points = customers .stream() .filter(c -> c.getPoints() > 100) - .count(); + .collect(Collectors.toList()); - assertThat(customersWithMoreThan100Points).isEqualTo(2); + assertThat(customersWithMoreThan100Points).hasSize(2); + assertThat(customersWithMoreThan100Points).contains(sarah, charles); + } + + @Test + public void givenListOfCustomers_whenFilterByPointsAndName_thenGetOne() { + Customer john = new Customer("John P.", 15); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1); + List customers = Arrays.asList(john, sarah, charles, mary); + + List charlesWithMoreThan100Points = customers + .stream() + .filter(c -> c.getPoints() > 100 && c + .getName() + .startsWith("Charles")) + .collect(Collectors.toList()); + + assertThat(charlesWithMoreThan100Points).hasSize(1); + assertThat(charlesWithMoreThan100Points).contains(charles); } @Test public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() { - List customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1)); + Customer john = new Customer("John P.", 15); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1); + List customers = Arrays.asList(john, sarah, charles, mary); - long customersWithMoreThan100Points = customers + List customersWithMoreThan100Points = customers .stream() .filter(Customer::hasOverThousandPoints) - .count(); + .collect(Collectors.toList()); - assertThat(customersWithMoreThan100Points).isEqualTo(2); + assertThat(customersWithMoreThan100Points).hasSize(2); + assertThat(customersWithMoreThan100Points).contains(sarah, charles); } @Test public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() { - List> customers = Arrays.asList(Optional.of(new Customer("John P.", 15)), Optional.of(new Customer("Sarah M.", 200)), Optional.empty(), Optional.of(new Customer("Mary T.", 300)), Optional.empty()); + Optional john = Optional.of(new Customer("John P.", 15)); + Optional sarah = Optional.of(new Customer("Sarah M.", 200)); + Optional mary = Optional.of(new Customer("Mary T.", 300)); + List> customers = Arrays.asList(john, sarah, Optional.empty(), mary, Optional.empty()); - long customersWithMoreThan100Points = customers + List customersWithMoreThan100Points = customers .stream() .flatMap(c -> c .map(Stream::of) .orElseGet(Stream::empty)) .filter(Customer::hasOverThousandPoints) - .count(); + .collect(Collectors.toList()); - assertThat(customersWithMoreThan100Points).isEqualTo(2); + assertThat(customersWithMoreThan100Points).hasSize(2); + assertThat(customersWithMoreThan100Points).contains(sarah.get(), mary.get()); } @Test public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() { - List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), - new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); + List customers = Arrays.asList(john, sarah, charles, mary); assertThatThrownBy(() -> customers .stream() @@ -67,21 +104,27 @@ public class StreamFilterUnitTest { @Test public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() { - List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), - new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); + List customers = Arrays.asList(john, sarah, charles, mary); assertThatThrownBy(() -> customers .stream() .filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto))) - .count()).isInstanceOf(WrappedException.class); + .collect(Collectors.toList())).isInstanceOf(WrappedException.class); } @Test public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() { - List customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), - new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e")); + Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"); + Customer sarah = new Customer("Sarah M.", 200); + Customer charles = new Customer("Charles B.", 150); + Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"); + List customers = Arrays.asList(john, sarah, charles, mary); - long customersWithValidProfilePhoto = customers + List customersWithValidProfilePhoto = customers .stream() .filter(c -> { try { @@ -91,9 +134,10 @@ public class StreamFilterUnitTest { } return false; }) - .count(); + .collect(Collectors.toList()); - assertThat(customersWithValidProfilePhoto).isEqualTo(2); + assertThat(customersWithValidProfilePhoto).hasSize(2); + assertThat(customersWithValidProfilePhoto).contains(john, mary); } @Test @@ -110,6 +154,6 @@ public class StreamFilterUnitTest { throw new RuntimeException(e); } }) - .count()).isInstanceOf(RuntimeException.class); + .collect(Collectors.toList())).isInstanceOf(RuntimeException.class); } } From dfca36b9addbd24a123ca250d40a3c8dfda75258 Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Sat, 8 Dec 2018 16:31:20 -0300 Subject: [PATCH 3/3] Removed .attach_pid12113 file --- java-streams/.attach_pid12113 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 java-streams/.attach_pid12113 diff --git a/java-streams/.attach_pid12113 b/java-streams/.attach_pid12113 deleted file mode 100644 index e69de29bb2..0000000000