From 1a7f172587fd7d528210b6af404d8226efba2180 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sat, 3 Dec 2016 10:53:17 +0100 Subject: [PATCH] BAEL-376 - Adding more test code --- .../java/com/baeldung/generics/Building.java | 7 ++ .../java/com/baeldung/generics/Generics.java | 6 +- .../java/com/baeldung/generics/House.java | 12 +++ .../com/baeldung/generics/SubBuilding.java | 4 - .../com/baeldung/generics/GenericsTest.java | 77 ++++++++++--------- .../java/nio2/async/AsyncEchoClient.java | 4 +- .../java/nio2/async/AsyncEchoTest.java | 3 +- 7 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/generics/House.java delete mode 100644 core-java/src/main/java/com/baeldung/generics/SubBuilding.java diff --git a/core-java/src/main/java/com/baeldung/generics/Building.java b/core-java/src/main/java/com/baeldung/generics/Building.java index cfdcc63170..2af6ee0711 100644 --- a/core-java/src/main/java/com/baeldung/generics/Building.java +++ b/core-java/src/main/java/com/baeldung/generics/Building.java @@ -1,5 +1,12 @@ +package com.baeldung.generics; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Building { + private static final Logger LOGGER = LoggerFactory.getLogger(Building.class); + public void paint() { + LOGGER.info("Building"); } } diff --git a/core-java/src/main/java/com/baeldung/generics/Generics.java b/core-java/src/main/java/com/baeldung/generics/Generics.java index 31a6ee6b60..e8b7dd1182 100644 --- a/core-java/src/main/java/com/baeldung/generics/Generics.java +++ b/core-java/src/main/java/com/baeldung/generics/Generics.java @@ -1,3 +1,5 @@ +package com.baeldung.generics; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -21,9 +23,7 @@ public class Generics { // example of a generic method with a wild card, this method can be used // with a list of any subtype of Building public static boolean paintAllBuildings(List buildings) { - for (Building building : buildings) { - building.paint(); - } + buildings.stream().forEach(Building::paint); return true; } diff --git a/core-java/src/main/java/com/baeldung/generics/House.java b/core-java/src/main/java/com/baeldung/generics/House.java new file mode 100644 index 0000000000..e10922120f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/generics/House.java @@ -0,0 +1,12 @@ +package com.baeldung.generics; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class House extends Building { + private static final Logger LOGGER = LoggerFactory.getLogger(House.class); + + public void paint() { + LOGGER.info("House"); + } +} diff --git a/core-java/src/main/java/com/baeldung/generics/SubBuilding.java b/core-java/src/main/java/com/baeldung/generics/SubBuilding.java deleted file mode 100644 index 938eb6caf5..0000000000 --- a/core-java/src/main/java/com/baeldung/generics/SubBuilding.java +++ /dev/null @@ -1,4 +0,0 @@ - -public class SubBuilding extends Building { - -} diff --git a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java index c04a9f21cb..0488ae3d22 100644 --- a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java +++ b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java @@ -1,55 +1,56 @@ -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; +package com.baeldung.generics; + +import org.junit.Test; import java.util.ArrayList; import java.util.List; -import org.junit.Test; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; public class GenericsTest { - // testing the generic method with Integer - @Test - public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() { - Integer[] intArray = { 1, 2, 3, 4, 5 }; - List list = Generics.fromArrayToList(intArray); + // testing the generic method with Integer + @Test + public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() { + Integer[] intArray = {1, 2, 3, 4, 5}; + List list = Generics.fromArrayToList(intArray); - assertThat(list, hasItems(intArray)); - } + assertThat(list, hasItems(intArray)); + } - // testing the generic method with String - @Test - public void givenArrayOfStrings_thanListOfStringsReturnedOK() { - String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" }; - List list = Generics.fromArrayToList(stringArray); + // testing the generic method with String + @Test + public void givenArrayOfStrings_thanListOfStringsReturnedOK() { + String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"}; + List list = Generics.fromArrayToList(stringArray); - assertThat(list, hasItems(stringArray)); - } + assertThat(list, hasItems(stringArray)); + } - // testing the generic method with Number as upper bound with Integer - // if we test fromArrayToListWithUpperBound with any type that doesn't - // extend Number it will fail to compile - @Test - public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() { - Integer[] intArray = { 1, 2, 3, 4, 5 }; - List list = Generics.fromArrayToListWithUpperBound(intArray); + // testing the generic method with Number as upper bound with Integer + // if we test fromArrayToListWithUpperBound with any type that doesn't + // extend Number it will fail to compile + @Test + public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() { + Integer[] intArray = {1, 2, 3, 4, 5}; + List list = Generics.fromArrayToListWithUpperBound(intArray); - assertThat(list, hasItems(intArray)); - } + assertThat(list, hasItems(intArray)); + } - // testing paintAllBuildings method with a subtype of Building, the method - // will work with all subtypes of Building - @Test - public void givenSubTypeOfAwildCardBoundedGenericMethod() { + // testing paintAllBuildings method with a subtype of Building, the method + // will work with all subtypes of Building + @Test + public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() { - List subBuildingsList = new ArrayList<>(); - subBuildingsList.add(new SubBuilding()); - subBuildingsList.add(new SubBuilding()); + List subBuildingsList = new ArrayList<>(); + subBuildingsList.add(new Building()); + subBuildingsList.add(new House()); - boolean result = Generics.paintAllBuildings(subBuildingsList); - assertTrue(result); - - } + boolean result = Generics.paintAllBuildings(subBuildingsList); + assertTrue(result); + } } \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java index d64fbc763e..2b25120dd4 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java @@ -40,7 +40,7 @@ public class AsyncEchoClient { } } - public String sendMessage(String message) { + public String sendMessage(String message) throws ExecutionException, InterruptedException { byte[] byteMsg = new String(message).getBytes(); ByteBuffer buffer = ByteBuffer.wrap(byteMsg); Future writeResult = client.write(buffer); @@ -65,7 +65,7 @@ public class AsyncEchoClient { } } - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { AsyncEchoClient client = AsyncEchoClient.getInstance(); client.start(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java index 7ac388fb09..475c91095e 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java @@ -3,6 +3,7 @@ package com.baeldung.java.nio2.async; import static org.junit.Assert.*; import java.io.IOException; +import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Before; @@ -20,7 +21,7 @@ public class AsyncEchoTest { } @Test - public void givenServerClient_whenServerEchosMessage_thenCorrect() { + public void givenServerClient_whenServerEchosMessage_thenCorrect() throws ExecutionException, InterruptedException { String resp1 = client.sendMessage("hello"); String resp2 = client.sendMessage("world"); assertEquals("hello", resp1);