Skip to content

Commit 1a7f172

Browse files
BAEL-376 - Adding more test code
1 parent 287fbc3 commit 1a7f172

7 files changed

Lines changed: 73 additions & 56 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
package com.baeldung.generics;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
15

26
public class Building {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
8+
39
public void paint() {
10+
LOGGER.info("Building");
411
}
512
}

‎core-java/src/main/java/com/baeldung/generics/Generics.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.baeldung.generics;
2+
13
import java.util.ArrayList;
24
import java.util.Arrays;
35
import java.util.List;
@@ -21,9 +23,7 @@ public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
2123
// example of a generic method with a wild card, this method can be used
2224
// with a list of any subtype of Building
2325
public static boolean paintAllBuildings(List<? extends Building> buildings) {
24-
for (Building building : buildings) {
25-
building.paint();
26-
}
26+
buildings.stream().forEach(Building::paint);
2727
return true;
2828
}
2929

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.generics;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class House extends Building {
7+
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
8+
9+
public void paint() {
10+
LOGGER.info("House");
11+
}
12+
}

‎core-java/src/main/java/com/baeldung/generics/SubBuilding.java‎

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
import static org.hamcrest.CoreMatchers.hasItems;
2-
import static org.hamcrest.MatcherAssert.assertThat;
3-
import static org.junit.Assert.assertTrue;
1+
package com.baeldung.generics;
2+
3+
import org.junit.Test;
44

55
import java.util.ArrayList;
66
import java.util.List;
77

8-
import org.junit.Test;
8+
import static org.hamcrest.CoreMatchers.hasItems;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.junit.Assert.assertTrue;
911

1012
public class GenericsTest {
1113

12-
// testing the generic method with Integer
13-
@Test
14-
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
15-
Integer[] intArray = { 1, 2, 3, 4, 5 };
16-
List<Integer> list = Generics.fromArrayToList(intArray);
17-
18-
assertThat(list, hasItems(intArray));
19-
}
20-
21-
// testing the generic method with String
22-
@Test
23-
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
24-
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
25-
List<String> list = Generics.fromArrayToList(stringArray);
26-
27-
assertThat(list, hasItems(stringArray));
28-
}
29-
30-
// testing the generic method with Number as upper bound with Integer
31-
// if we test fromArrayToListWithUpperBound with any type that doesn't
32-
// extend Number it will fail to compile
33-
@Test
34-
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
35-
Integer[] intArray = { 1, 2, 3, 4, 5 };
36-
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
37-
38-
assertThat(list, hasItems(intArray));
39-
}
40-
41-
// testing paintAllBuildings method with a subtype of Building, the method
42-
// will work with all subtypes of Building
43-
@Test
44-
public void givenSubTypeOfAwildCardBoundedGenericMethod() {
45-
46-
List<SubBuilding> subBuildingsList = new ArrayList<>();
47-
subBuildingsList.add(new SubBuilding());
48-
subBuildingsList.add(new SubBuilding());
49-
50-
boolean result = Generics.paintAllBuildings(subBuildingsList);
51-
assertTrue(result);
52-
53-
}
14+
// testing the generic method with Integer
15+
@Test
16+
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
17+
Integer[] intArray = {1, 2, 3, 4, 5};
18+
List<Integer> list = Generics.fromArrayToList(intArray);
19+
20+
assertThat(list, hasItems(intArray));
21+
}
22+
23+
// testing the generic method with String
24+
@Test
25+
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
26+
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
27+
List<String> list = Generics.fromArrayToList(stringArray);
28+
29+
assertThat(list, hasItems(stringArray));
30+
}
31+
32+
// testing the generic method with Number as upper bound with Integer
33+
// if we test fromArrayToListWithUpperBound with any type that doesn't
34+
// extend Number it will fail to compile
35+
@Test
36+
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
37+
Integer[] intArray = {1, 2, 3, 4, 5};
38+
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
39+
40+
assertThat(list, hasItems(intArray));
41+
}
42+
43+
// testing paintAllBuildings method with a subtype of Building, the method
44+
// will work with all subtypes of Building
45+
@Test
46+
public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() {
47+
48+
List<Building> subBuildingsList = new ArrayList<>();
49+
subBuildingsList.add(new Building());
50+
subBuildingsList.add(new House());
51+
52+
boolean result = Generics.paintAllBuildings(subBuildingsList);
53+
assertTrue(result);
54+
}
5455

5556
}

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private void start() {
4040
}
4141
}
4242

43-
public String sendMessage(String message) {
43+
public String sendMessage(String message) throws ExecutionException, InterruptedException {
4444
byte[] byteMsg = new String(message).getBytes();
4545
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
4646
Future<Integer> writeResult = client.write(buffer);
@@ -65,7 +65,7 @@ public void stop() {
6565
}
6666
}
6767

68-
public static void main(String[] args) throws IOException {
68+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
6969
AsyncEchoClient client = AsyncEchoClient.getInstance();
7070
client.start();
7171
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.*;
44

55
import java.io.IOException;
6+
import java.util.concurrent.ExecutionException;
67

78
import org.junit.After;
89
import org.junit.Before;
@@ -20,7 +21,7 @@ public void setup() throws IOException, InterruptedException {
2021
}
2122

2223
@Test
23-
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
24+
public void givenServerClient_whenServerEchosMessage_thenCorrect() throws ExecutionException, InterruptedException {
2425
String resp1 = client.sendMessage("hello");
2526
String resp2 = client.sendMessage("world");
2627
assertEquals("hello", resp1);

0 commit comments

Comments
 (0)