Skip to content

Commit a0781d1

Browse files
vivekkr12pivovarit
authored andcommitted
BAEL 533: Pull request for Java 9 Convenience Factory Methods for Collections (eugenp#1078)
* remove verbose from compiler plugin, change project build encoding to UTF-8 * BAEL - 533 - convenience factory methods for collections test files
1 parent d071360 commit a0781d1

4 files changed

Lines changed: 186 additions & 1 deletion

File tree

‎core-java-9/pom.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<configuration>
5858
<source>1.9</source>
5959
<target>1.9</target>
60-
<verbose>true</verbose>
6160
</configuration>
6261
</plugin>
6362

@@ -72,6 +71,9 @@
7271
</build>
7372

7473
<properties>
74+
<!-- project build encoding -->
75+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
76+
7577
<!-- logging -->
7678
<org.slf4j.version>1.7.21</org.slf4j.version>
7779

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.java9.language.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
12+
public class ListFactoryMethodsTest {
13+
14+
@Test
15+
public void whenListCreated_thenSuccess() {
16+
List<String> traditionlList = new ArrayList<String>();
17+
traditionlList.add("foo");
18+
traditionlList.add("bar");
19+
traditionlList.add("baz");
20+
List<String> factoryCreatedList = List.of("foo", "bar", "baz");
21+
assertEquals(traditionlList, factoryCreatedList);
22+
}
23+
24+
@Test(expected = UnsupportedOperationException.class)
25+
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
26+
List<String> list = List.of("foo", "bar");
27+
list.add("baz");
28+
}
29+
30+
@Test(expected = UnsupportedOperationException.class)
31+
public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() {
32+
List<String> list = List.of("foo", "bar");
33+
list.set(0, "baz");
34+
}
35+
36+
@Test(expected = UnsupportedOperationException.class)
37+
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
38+
List<String> list = List.of("foo", "bar");
39+
list.remove("foo");
40+
}
41+
42+
@Test(expected = NullPointerException.class)
43+
public void onNullElem_ifNullPtrExpnThrown_thenSuccess() {
44+
List.of("foo", "bar", null);
45+
}
46+
47+
@Test
48+
public void ifNotArrayList_thenSuccess() {
49+
List<String> list = List.of("foo", "bar");
50+
assertFalse(list instanceof ArrayList);
51+
}
52+
53+
@Test
54+
public void ifListSizeIsOne_thenSuccess() {
55+
int[] arr = { 1, 2, 3, 4 };
56+
List<int[]> list = List.of(arr);
57+
assertEquals(1, list.size());
58+
assertArrayEquals(arr, list.get(0));
59+
}
60+
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.java9.language.collections;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertFalse;
10+
11+
public class MapFactoryMethodsTest {
12+
13+
@Test
14+
public void whenMapCreated_thenSuccess() {
15+
Map<String, String> traditionlMap = new HashMap<String, String>();
16+
traditionlMap.put("foo", "a");
17+
traditionlMap.put("bar", "b");
18+
traditionlMap.put("baz", "c");
19+
Map<String, String> factoryCreatedMap = Map.of("foo", "a", "bar", "b", "baz", "c");
20+
assertEquals(traditionlMap, factoryCreatedMap);
21+
}
22+
23+
@Test(expected = UnsupportedOperationException.class)
24+
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
25+
Map<String, String> map = Map.of("foo", "a", "bar", "b");
26+
map.put("baz", "c");
27+
}
28+
29+
@Test(expected = UnsupportedOperationException.class)
30+
public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() {
31+
Map<String, String> map = Map.of("foo", "a", "bar", "b");
32+
map.put("foo", "c");
33+
}
34+
35+
@Test(expected = UnsupportedOperationException.class)
36+
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
37+
Map<String, String> map = Map.of("foo", "a", "bar", "b");
38+
map.remove("foo");
39+
}
40+
41+
@Test(expected = IllegalArgumentException.class)
42+
public void givenDuplicateKeys_ifIllegalArgExp_thenSuccess() {
43+
Map.of("foo", "a", "foo", "b");
44+
}
45+
46+
@Test(expected = NullPointerException.class)
47+
public void onNullKey_ifNullPtrExp_thenSuccess() {
48+
Map.of("foo", "a", null, "b");
49+
}
50+
51+
@Test(expected = NullPointerException.class)
52+
public void onNullValue_ifNullPtrExp_thenSuccess() {
53+
Map.of("foo", "a", "bar", null);
54+
}
55+
56+
@Test
57+
public void ifNotHashMap_thenSuccess() {
58+
Map<String, String> map = Map.of("foo", "a", "bar", "b");
59+
assertFalse(map instanceof HashMap);
60+
}
61+
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.java9.language.collections;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
12+
public class SetFactoryMethodsTest {
13+
14+
@Test
15+
public void whenSetCreated_thenSuccess() {
16+
Set<String> traditionlSet = new HashSet<String>();
17+
traditionlSet.add("foo");
18+
traditionlSet.add("bar");
19+
traditionlSet.add("baz");
20+
Set<String> factoryCreatedSet = Set.of("foo", "bar", "baz");
21+
assertEquals(traditionlSet, factoryCreatedSet);
22+
}
23+
24+
@Test(expected = IllegalArgumentException.class)
25+
public void onDuplicateElem_IfIllegalArgExp_thenSuccess() {
26+
Set.of("foo", "bar", "baz", "foo");
27+
}
28+
29+
@Test(expected = UnsupportedOperationException.class)
30+
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
31+
Set<String> set = Set.of("foo", "bar");
32+
set.add("baz");
33+
}
34+
35+
@Test(expected = UnsupportedOperationException.class)
36+
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
37+
Set<String> set = Set.of("foo", "bar", "baz");
38+
set.remove("foo");
39+
}
40+
41+
@Test(expected = NullPointerException.class)
42+
public void onNullElem_ifNullPtrExpnThrown_thenSuccess() {
43+
Set.of("foo", "bar", null);
44+
}
45+
46+
@Test
47+
public void ifNotHashSet_thenSuccess() {
48+
Set<String> list = Set.of("foo", "bar");
49+
assertFalse(list instanceof HashSet);
50+
}
51+
52+
@Test
53+
public void ifSetSizeIsOne_thenSuccess() {
54+
int[] arr = { 1, 2, 3, 4 };
55+
Set<int[]> set = Set.of(arr);
56+
assertEquals(1, set.size());
57+
assertArrayEquals(arr, set.iterator().next());
58+
}
59+
60+
}

0 commit comments

Comments
 (0)