From a0781d1d8862aa81fc8138d48a062fbd38c19e47 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Wed, 1 Feb 2017 01:59:02 +0530 Subject: [PATCH] BAEL 533: Pull request for Java 9 Convenience Factory Methods for Collections (#1078) * remove verbose from compiler plugin, change project build encoding to UTF-8 * BAEL - 533 - convenience factory methods for collections test files --- core-java-9/pom.xml | 4 +- .../collections/ListFactoryMethodsTest.java | 61 ++++++++++++++++++ .../collections/MapFactoryMethodsTest.java | 62 +++++++++++++++++++ .../collections/SetFactoryMethodsTest.java | 60 ++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/collections/ListFactoryMethodsTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/collections/MapFactoryMethodsTest.java create mode 100644 core-java-9/src/test/java/com/baeldung/java9/language/collections/SetFactoryMethodsTest.java diff --git a/core-java-9/pom.xml b/core-java-9/pom.xml index decba19c53..9d1ff29ef7 100644 --- a/core-java-9/pom.xml +++ b/core-java-9/pom.xml @@ -57,7 +57,6 @@ 1.9 1.9 - true @@ -72,6 +71,9 @@ + + UTF-8 + 1.7.21 diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/collections/ListFactoryMethodsTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/collections/ListFactoryMethodsTest.java new file mode 100644 index 0000000000..8e19d00b6a --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/collections/ListFactoryMethodsTest.java @@ -0,0 +1,61 @@ +package com.baeldung.java9.language.collections; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class ListFactoryMethodsTest { + + @Test + public void whenListCreated_thenSuccess() { + List traditionlList = new ArrayList(); + traditionlList.add("foo"); + traditionlList.add("bar"); + traditionlList.add("baz"); + List factoryCreatedList = List.of("foo", "bar", "baz"); + assertEquals(traditionlList, factoryCreatedList); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() { + List list = List.of("foo", "bar"); + list.add("baz"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() { + List list = List.of("foo", "bar"); + list.set(0, "baz"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() { + List list = List.of("foo", "bar"); + list.remove("foo"); + } + + @Test(expected = NullPointerException.class) + public void onNullElem_ifNullPtrExpnThrown_thenSuccess() { + List.of("foo", "bar", null); + } + + @Test + public void ifNotArrayList_thenSuccess() { + List list = List.of("foo", "bar"); + assertFalse(list instanceof ArrayList); + } + + @Test + public void ifListSizeIsOne_thenSuccess() { + int[] arr = { 1, 2, 3, 4 }; + List list = List.of(arr); + assertEquals(1, list.size()); + assertArrayEquals(arr, list.get(0)); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/collections/MapFactoryMethodsTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/collections/MapFactoryMethodsTest.java new file mode 100644 index 0000000000..13469ff93d --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/collections/MapFactoryMethodsTest.java @@ -0,0 +1,62 @@ +package com.baeldung.java9.language.collections; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class MapFactoryMethodsTest { + + @Test + public void whenMapCreated_thenSuccess() { + Map traditionlMap = new HashMap(); + traditionlMap.put("foo", "a"); + traditionlMap.put("bar", "b"); + traditionlMap.put("baz", "c"); + Map factoryCreatedMap = Map.of("foo", "a", "bar", "b", "baz", "c"); + assertEquals(traditionlMap, factoryCreatedMap); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() { + Map map = Map.of("foo", "a", "bar", "b"); + map.put("baz", "c"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() { + Map map = Map.of("foo", "a", "bar", "b"); + map.put("foo", "c"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() { + Map map = Map.of("foo", "a", "bar", "b"); + map.remove("foo"); + } + + @Test(expected = IllegalArgumentException.class) + public void givenDuplicateKeys_ifIllegalArgExp_thenSuccess() { + Map.of("foo", "a", "foo", "b"); + } + + @Test(expected = NullPointerException.class) + public void onNullKey_ifNullPtrExp_thenSuccess() { + Map.of("foo", "a", null, "b"); + } + + @Test(expected = NullPointerException.class) + public void onNullValue_ifNullPtrExp_thenSuccess() { + Map.of("foo", "a", "bar", null); + } + + @Test + public void ifNotHashMap_thenSuccess() { + Map map = Map.of("foo", "a", "bar", "b"); + assertFalse(map instanceof HashMap); + } + +} diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/collections/SetFactoryMethodsTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/collections/SetFactoryMethodsTest.java new file mode 100644 index 0000000000..b8537d7c82 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/language/collections/SetFactoryMethodsTest.java @@ -0,0 +1,60 @@ +package com.baeldung.java9.language.collections; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class SetFactoryMethodsTest { + + @Test + public void whenSetCreated_thenSuccess() { + Set traditionlSet = new HashSet(); + traditionlSet.add("foo"); + traditionlSet.add("bar"); + traditionlSet.add("baz"); + Set factoryCreatedSet = Set.of("foo", "bar", "baz"); + assertEquals(traditionlSet, factoryCreatedSet); + } + + @Test(expected = IllegalArgumentException.class) + public void onDuplicateElem_IfIllegalArgExp_thenSuccess() { + Set.of("foo", "bar", "baz", "foo"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() { + Set set = Set.of("foo", "bar"); + set.add("baz"); + } + + @Test(expected = UnsupportedOperationException.class) + public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() { + Set set = Set.of("foo", "bar", "baz"); + set.remove("foo"); + } + + @Test(expected = NullPointerException.class) + public void onNullElem_ifNullPtrExpnThrown_thenSuccess() { + Set.of("foo", "bar", null); + } + + @Test + public void ifNotHashSet_thenSuccess() { + Set list = Set.of("foo", "bar"); + assertFalse(list instanceof HashSet); + } + + @Test + public void ifSetSizeIsOne_thenSuccess() { + int[] arr = { 1, 2, 3, 4 }; + Set set = Set.of(arr); + assertEquals(1, set.size()); + assertArrayEquals(arr, set.iterator().next()); + } + +}