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 {
|
public class Building {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
|
||||||
|
|
||||||
public void paint() {
|
public void paint() {
|
||||||
|
LOGGER.info("Building");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
package com.baeldung.generics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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
|
// example of a generic method with a wild card, this method can be used
|
||||||
// with a list of any subtype of Building
|
// with a list of any subtype of Building
|
||||||
public static boolean paintAllBuildings(List<? extends Building> buildings) {
|
public static boolean paintAllBuildings(List<? extends Building> buildings) {
|
||||||
for (Building building : buildings) {
|
buildings.stream().forEach(Building::paint);
|
||||||
building.paint();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
core-java/src/main/java/com/baeldung/generics/House.java
Normal file
12
core-java/src/main/java/com/baeldung/generics/House.java
Normal file
@ -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;
|
package com.baeldung.generics;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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 {
|
public class GenericsTest {
|
||||||
|
|
||||||
// testing the generic method with Integer
|
// testing the generic method with Integer
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
Integer[] intArray = {1, 2, 3, 4, 5};
|
||||||
List<Integer> list = Generics.fromArrayToList(intArray);
|
List<Integer> list = Generics.fromArrayToList(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing the generic method with String
|
// testing the generic method with String
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||||
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
|
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
|
||||||
List<String> list = Generics.fromArrayToList(stringArray);
|
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
|
// testing the generic method with Number as upper bound with Integer
|
||||||
// if we test fromArrayToListWithUpperBound with any type that doesn't
|
// if we test fromArrayToListWithUpperBound with any type that doesn't
|
||||||
// extend Number it will fail to compile
|
// extend Number it will fail to compile
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
Integer[] intArray = {1, 2, 3, 4, 5};
|
||||||
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing paintAllBuildings method with a subtype of Building, the method
|
// testing paintAllBuildings method with a subtype of Building, the method
|
||||||
// will work with all subtypes of Building
|
// will work with all subtypes of Building
|
||||||
@Test
|
@Test
|
||||||
public void givenSubTypeOfAwildCardBoundedGenericMethod() {
|
public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() {
|
||||||
|
|
||||||
List<SubBuilding> subBuildingsList = new ArrayList<>();
|
List<Building> subBuildingsList = new ArrayList<>();
|
||||||
subBuildingsList.add(new SubBuilding());
|
subBuildingsList.add(new Building());
|
||||||
subBuildingsList.add(new SubBuilding());
|
subBuildingsList.add(new House());
|
||||||
|
|
||||||
boolean result = Generics.paintAllBuildings(subBuildingsList);
|
boolean result = Generics.paintAllBuildings(subBuildingsList);
|
||||||
assertTrue(result);
|
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();
|
byte[] byteMsg = new String(message).getBytes();
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
|
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
|
||||||
Future<Integer> writeResult = client.write(buffer);
|
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();
|
AsyncEchoClient client = AsyncEchoClient.getInstance();
|
||||||
client.start();
|
client.start();
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
@ -3,6 +3,7 @@ package com.baeldung.java.nio2.async;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -20,7 +21,7 @@ public class AsyncEchoTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
|
public void givenServerClient_whenServerEchosMessage_thenCorrect() throws ExecutionException, InterruptedException {
|
||||||
String resp1 = client.sendMessage("hello");
|
String resp1 = client.sendMessage("hello");
|
||||||
String resp2 = client.sendMessage("world");
|
String resp2 = client.sendMessage("world");
|
||||||
assertEquals("hello", resp1);
|
assertEquals("hello", resp1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user