Skip to content

Commit 3bec6f7

Browse files
authored
Merge pull request eugenp#5813 from earth001/BAEL-2333a
BAEL-2333 Sample code
2 parents 7ddcb76 + dfca36b commit 3bec6f7

3 files changed

Lines changed: 222 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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.Collectors;
12+
import java.util.stream.Stream;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
16+
17+
public class StreamFilterUnitTest {
18+
19+
@Test
20+
public void givenListOfCustomers_whenFilterByPoints_thenGetTwo() {
21+
Customer john = new Customer("John P.", 15);
22+
Customer sarah = new Customer("Sarah M.", 200);
23+
Customer charles = new Customer("Charles B.", 150);
24+
Customer mary = new Customer("Mary T.", 1);
25+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
26+
27+
List<Customer> customersWithMoreThan100Points = customers
28+
.stream()
29+
.filter(c -> c.getPoints() > 100)
30+
.collect(Collectors.toList());
31+
32+
assertThat(customersWithMoreThan100Points).hasSize(2);
33+
assertThat(customersWithMoreThan100Points).contains(sarah, charles);
34+
}
35+
36+
@Test
37+
public void givenListOfCustomers_whenFilterByPointsAndName_thenGetOne() {
38+
Customer john = new Customer("John P.", 15);
39+
Customer sarah = new Customer("Sarah M.", 200);
40+
Customer charles = new Customer("Charles B.", 150);
41+
Customer mary = new Customer("Mary T.", 1);
42+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
43+
44+
List<Customer> charlesWithMoreThan100Points = customers
45+
.stream()
46+
.filter(c -> c.getPoints() > 100 && c
47+
.getName()
48+
.startsWith("Charles"))
49+
.collect(Collectors.toList());
50+
51+
assertThat(charlesWithMoreThan100Points).hasSize(1);
52+
assertThat(charlesWithMoreThan100Points).contains(charles);
53+
}
54+
55+
@Test
56+
public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() {
57+
Customer john = new Customer("John P.", 15);
58+
Customer sarah = new Customer("Sarah M.", 200);
59+
Customer charles = new Customer("Charles B.", 150);
60+
Customer mary = new Customer("Mary T.", 1);
61+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
62+
63+
List<Customer> customersWithMoreThan100Points = customers
64+
.stream()
65+
.filter(Customer::hasOverThousandPoints)
66+
.collect(Collectors.toList());
67+
68+
assertThat(customersWithMoreThan100Points).hasSize(2);
69+
assertThat(customersWithMoreThan100Points).contains(sarah, charles);
70+
}
71+
72+
@Test
73+
public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() {
74+
Optional<Customer> john = Optional.of(new Customer("John P.", 15));
75+
Optional<Customer> sarah = Optional.of(new Customer("Sarah M.", 200));
76+
Optional<Customer> mary = Optional.of(new Customer("Mary T.", 300));
77+
List<Optional<Customer>> customers = Arrays.asList(john, sarah, Optional.empty(), mary, Optional.empty());
78+
79+
List<Customer> customersWithMoreThan100Points = customers
80+
.stream()
81+
.flatMap(c -> c
82+
.map(Stream::of)
83+
.orElseGet(Stream::empty))
84+
.filter(Customer::hasOverThousandPoints)
85+
.collect(Collectors.toList());
86+
87+
assertThat(customersWithMoreThan100Points).hasSize(2);
88+
assertThat(customersWithMoreThan100Points).contains(sarah.get(), mary.get());
89+
}
90+
91+
@Test
92+
public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() {
93+
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
94+
Customer sarah = new Customer("Sarah M.", 200);
95+
Customer charles = new Customer("Charles B.", 150);
96+
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
97+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
98+
99+
assertThatThrownBy(() -> customers
100+
.stream()
101+
.filter(Customer::hasValidProfilePhotoWithoutCheckedException)
102+
.count()).isInstanceOf(RuntimeException.class);
103+
}
104+
105+
@Test
106+
public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() {
107+
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
108+
Customer sarah = new Customer("Sarah M.", 200);
109+
Customer charles = new Customer("Charles B.", 150);
110+
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
111+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
112+
113+
assertThatThrownBy(() -> customers
114+
.stream()
115+
.filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto)))
116+
.collect(Collectors.toList())).isInstanceOf(WrappedException.class);
117+
}
118+
119+
@Test
120+
public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
121+
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
122+
Customer sarah = new Customer("Sarah M.", 200);
123+
Customer charles = new Customer("Charles B.", 150);
124+
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
125+
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
126+
127+
List<Customer> customersWithValidProfilePhoto = customers
128+
.stream()
129+
.filter(c -> {
130+
try {
131+
return c.hasValidProfilePhoto();
132+
} catch (IOException e) {
133+
//handle exception
134+
}
135+
return false;
136+
})
137+
.collect(Collectors.toList());
138+
139+
assertThat(customersWithValidProfilePhoto).hasSize(2);
140+
assertThat(customersWithValidProfilePhoto).contains(john, mary);
141+
}
142+
143+
@Test
144+
public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() {
145+
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),
146+
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
147+
148+
assertThatThrownBy(() -> customers
149+
.stream()
150+
.filter(c -> {
151+
try {
152+
return c.hasValidProfilePhoto();
153+
} catch (IOException e) {
154+
throw new RuntimeException(e);
155+
}
156+
})
157+
.collect(Collectors.toList())).isInstanceOf(RuntimeException.class);
158+
}
159+
}

0 commit comments

Comments
 (0)