BAEL-376 - Adding more test code
This commit is contained in:
parent
287fbc3b10
commit
1a7f172587
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<? extends Building> buildings) {
|
||||
for (Building building : buildings) {
|
||||
building.paint();
|
||||
}
|
||||
buildings.stream().forEach(Building::paint);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
public class SubBuilding extends Building {
|
||||
|
||||
}
|
|
@ -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<Integer> list = Generics.fromArrayToList(intArray);
|
||||
// testing the generic method with Integer
|
||||
@Test
|
||||
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
||||
List<Integer> 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<String> list = Generics.fromArrayToList(stringArray);
|
||||
// testing the generic method with String
|
||||
@Test
|
||||
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
|
||||
List<String> 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<Integer> 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<Integer> 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<SubBuilding> subBuildingsList = new ArrayList<>();
|
||||
subBuildingsList.add(new SubBuilding());
|
||||
subBuildingsList.add(new SubBuilding());
|
||||
List<Building> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> 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));
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue