Skip to content

Commit 2cfbd8d

Browse files
authored
Formatting Changes for Java 5074 (eugenp#11206)
* Formatting Changes for Java 5074 Changed line change indentation to 2 spaces * Rename StreamToListComparisonWithCollectorsToListUnitTest to StreamToListComparisonWithCollectorsToListUnitTest.java
1 parent 92efe1b commit 2cfbd8d

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.baeldung.streams;
2+
3+
import java.util.List;
4+
import java.util.Locale;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class StreamToListComparisonWithCollectorsToListUnitTest {
12+
13+
@Test
14+
public void whenAddingtoCollectToList_thenSuccess() {
15+
16+
List<String> result = Stream.of(Locale.getISOCountries())
17+
.collect(Collectors.toList());
18+
19+
Assertions.assertDoesNotThrow(() -> {
20+
result.add("test");
21+
});
22+
}
23+
24+
@Test
25+
public void whenSortingtoCollectToList_thenSuccess() {
26+
27+
List<String> result = Stream.of(Locale.getISOCountries())
28+
.collect(Collectors.toList());
29+
30+
Assertions.assertDoesNotThrow(() -> {
31+
result.sort(String::compareToIgnoreCase);
32+
});
33+
}
34+
35+
@Test
36+
public void whenAddingCollectUnmodifiableToList_thenException() {
37+
38+
List<String> result = Stream.of(Locale.getISOCountries())
39+
.collect(Collectors.toUnmodifiableList());
40+
41+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
42+
result.add("test");
43+
});
44+
}
45+
46+
@Test
47+
public void whenSortingCollectUnmodifiableToList_thenSortException() {
48+
49+
List<String> result = Stream.of(Locale.getISOCountries())
50+
.collect(Collectors.toUnmodifiableList());
51+
52+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
53+
result.sort(String::compareToIgnoreCase);
54+
});
55+
}
56+
57+
@Test
58+
public void whenAddingStreamToList_thenException() {
59+
60+
List<String> result = Stream.of(Locale.getISOCountries())
61+
.toList();
62+
63+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
64+
result.add("test");
65+
});
66+
}
67+
68+
@Test
69+
public void whenSortingStreamToList_thenException() {
70+
71+
List<String> result = Stream.of(Locale.getISOCountries())
72+
.toList();
73+
74+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
75+
result.sort(String::compareToIgnoreCase);
76+
});
77+
}
78+
79+
@Test
80+
public void whenComparingStreamToList_withCollectToList_thenEqual() {
81+
82+
List<String> streamToList = Stream.of(Locale.getISOCountries())
83+
.toList();
84+
List<String> collectToList = Stream.of(Locale.getISOCountries())
85+
.collect(Collectors.toList());
86+
87+
Assertions.assertEquals(streamToList, collectToList);
88+
}
89+
90+
@Test
91+
public void whenComparingStreamToList_withUnmodifiedCollectToList_thenEqual() {
92+
93+
List<String> streamToList = Stream.of(Locale.getISOCountries())
94+
.toList();
95+
List<String> collectToUnmodifiableList = Stream.of(Locale.getISOCountries())
96+
.collect(Collectors.toUnmodifiableList());
97+
98+
Assertions.assertEquals(streamToList, collectToUnmodifiableList);
99+
}
100+
101+
@Test
102+
public void whenNullCollectorsToList_thenSuccess() {
103+
104+
Assertions.assertDoesNotThrow(() -> {
105+
Stream.of(null, null)
106+
.collect(Collectors.toList());
107+
});
108+
}
109+
110+
@Test
111+
public void whenNullCollectorsUnmodifiableToList_thenException() {
112+
113+
Assertions.assertThrows(NullPointerException.class, () -> {
114+
Stream.of(null, null)
115+
.collect(Collectors.toUnmodifiableList());
116+
});
117+
}
118+
119+
@Test
120+
public void whenNullStreamToList_thenSuccess() {
121+
122+
Assertions.assertDoesNotThrow(() -> {
123+
Stream.of(null, null)
124+
.toList();
125+
});
126+
}
127+
128+
}

0 commit comments

Comments
 (0)