Skip to content

Commit f06a9f4

Browse files
Sample code for BAEL-2333
1 parent ea6aa58 commit f06a9f4

3 files changed

Lines changed: 178 additions & 2 deletions

File tree

java-streams/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<artifactId>java-streams</artifactId>
55
<version>0.1.0-SNAPSHOT</version>
@@ -74,6 +74,11 @@
7474
<artifactId>aspectjweaver</artifactId>
7575
<version>${asspectj.version}</version>
7676
</dependency>
77+
<dependency>
78+
<groupId>pl.touk</groupId>
79+
<artifactId>throwing-function</artifactId>
80+
<version>${throwing-function.version}</version>
81+
</dependency>
7782
</dependencies>
7883

7984
<build>
@@ -108,8 +113,9 @@
108113
<protonpack.version>1.15</protonpack.version>
109114
<streamex.version>0.6.5</streamex.version>
110115
<joda.version>2.10</joda.version>
116+
<throwing-function.version>1.3</throwing-function.version>
111117
<!-- testing -->
112-
<assertj.version>3.6.1</assertj.version>
118+
<assertj.version>3.11.1</assertj.version>
113119
<asspectj.version>1.8.9</asspectj.version>
114120
</properties>
115121
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.stream.filter;
2+
3+
import javax.net.ssl.HttpsURLConnection;
4+
import java.io.IOException;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
public class Customer {
9+
private String name;
10+
private int points;
11+
private String profilePhotoUrl;
12+
13+
public Customer(String name, int points) {
14+
this(name, points, "");
15+
}
16+
17+
public Customer(String name, int points, String profilePhotoUrl) {
18+
this.name = name;
19+
this.points = points;
20+
this.profilePhotoUrl = profilePhotoUrl;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public int getPoints() {
28+
return points;
29+
}
30+
31+
public boolean hasOver(int points) {
32+
return this.points > points;
33+
}
34+
35+
public boolean hasOverThousandPoints() {
36+
return this.points > 100;
37+
}
38+
39+
public boolean hasValidProfilePhoto() throws IOException {
40+
URL url = new URL(this.profilePhotoUrl);
41+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
42+
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
43+
}
44+
45+
public boolean hasValidProfilePhotoWithoutCheckedException() {
46+
try {
47+
URL url = new URL(this.profilePhotoUrl);
48+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
49+
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
50+
} catch (IOException e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
55+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.baeldung.stream.filter;
2+
3+
import org.junit.jupiter.api.Test;
4+
import pl.touk.throwing.ThrowingPredicate;
5+
import pl.touk.throwing.exception.WrappedException;
6+
7+
import java.io.IOException;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.stream.Stream;
12+
13+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
14+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
15+
16+
public class StreamFilterUnitTest {
17+
18+
@Test
19+
public void givenListOfCustomers_whenFilterByLambda_thenGetTwo() {
20+
List<Customer> customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1));
21+
22+
long customersWithMoreThan100Points = customers
23+
.stream()
24+
.filter(c -> c.getPoints() > 100)
25+
.count();
26+
27+
assertThat(customersWithMoreThan100Points).isEqualTo(2);
28+
}
29+
30+
@Test
31+
public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() {
32+
List<Customer> customers = Arrays.asList(new Customer("John P.", 15), new Customer("Sarah M.", 200), new Customer("Charles B.", 150), new Customer("Mary T.", 1));
33+
34+
long customersWithMoreThan100Points = customers
35+
.stream()
36+
.filter(Customer::hasOverThousandPoints)
37+
.count();
38+
39+
assertThat(customersWithMoreThan100Points).isEqualTo(2);
40+
}
41+
42+
@Test
43+
public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() {
44+
List<Optional<Customer>> 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());
45+
46+
long customersWithMoreThan100Points = customers
47+
.stream()
48+
.flatMap(c -> c
49+
.map(Stream::of)
50+
.orElseGet(Stream::empty))
51+
.filter(Customer::hasOverThousandPoints)
52+
.count();
53+
54+
assertThat(customersWithMoreThan100Points).isEqualTo(2);
55+
}
56+
57+
@Test
58+
public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() {
59+
List<Customer> 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),
60+
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
61+
62+
assertThatThrownBy(() -> customers
63+
.stream()
64+
.filter(Customer::hasValidProfilePhotoWithoutCheckedException)
65+
.count()).isInstanceOf(RuntimeException.class);
66+
}
67+
68+
@Test
69+
public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() {
70+
List<Customer> 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),
71+
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
72+
73+
assertThatThrownBy(() -> customers
74+
.stream()
75+
.filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto)))
76+
.count()).isInstanceOf(WrappedException.class);
77+
}
78+
79+
@Test
80+
public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
81+
List<Customer> 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),
82+
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
83+
84+
long customersWithValidProfilePhoto = customers
85+
.stream()
86+
.filter(c -> {
87+
try {
88+
return c.hasValidProfilePhoto();
89+
} catch (IOException e) {
90+
//handle exception
91+
}
92+
return false;
93+
})
94+
.count();
95+
96+
assertThat(customersWithValidProfilePhoto).isEqualTo(2);
97+
}
98+
99+
@Test
100+
public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() {
101+
List<Customer> 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),
102+
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
103+
104+
assertThatThrownBy(() -> customers
105+
.stream()
106+
.filter(c -> {
107+
try {
108+
return c.hasValidProfilePhoto();
109+
} catch (IOException e) {
110+
throw new RuntimeException(e);
111+
}
112+
})
113+
.count()).isInstanceOf(RuntimeException.class);
114+
}
115+
}

0 commit comments

Comments
 (0)