Skip to content

Commit fe11e8f

Browse files
authored
Idiomatic refactor (eugenp#2063)
* StringToCharStream * Spring integration tests * Spring integration tests
1 parent 53f4ec5 commit fe11e8f

4 files changed

Lines changed: 29 additions & 42 deletions

File tree

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
11
package com.baeldung.string;
22

3-
import java.util.HashMap;
43
import java.util.Map;
4+
import java.util.stream.Collectors;
55
import java.util.stream.IntStream;
66
import java.util.stream.Stream;
77

8-
/**
9-
* Created by smatt on 26/05/2017.
10-
*/
118
public class StringToCharStream {
129

13-
public StringToCharStream() {
14-
15-
//let's use the Stream API to manipulate a string
16-
//this will count the occurrence of each character in the test string
17-
10+
private StringToCharStream() {
1811
String testString = "tests";
1912

20-
//first get an IntStream
2113
IntStream intStream = testString.chars();
2214
IntStream intStream1 = testString.codePoints();
2315

24-
//now let's map them
2516
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
2617
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
2718

2819
System.out.println("Counting Occurrence of Letter");
29-
testString = "Noww";
30-
31-
//we don't want to use foreach, so . . .
32-
33-
Map<Character, Integer> map = new HashMap<>();
3420

35-
testString.codePoints()
36-
.mapToObj(c -> (char) c)
37-
.filter(c -> Character.isLetter(c))
38-
.forEach(c -> {
39-
if(map.containsKey(c)) {
40-
map.put(c, map.get(c) + 1);
41-
} else {
42-
map.put(c, 1);
43-
}
44-
});
21+
Map<Character, Integer> map = "Noww".codePoints()
22+
.mapToObj(c -> (char) c)
23+
.filter(Character::isLetter)
24+
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
4525

4626
//printing out the result here
4727
System.out.println(map.toString());
4828

4929
}
5030

51-
52-
public static void main(String [] args) {
31+
public static void main(String[] args) {
5332
new StringToCharStream();
5433
}
55-
56-
5734
}

core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
import static org.junit.Assert.assertThat;
1111
import static org.junit.Assert.assertTrue;
1212

13-
/**
14-
* Created by smatt on 09/06/2017.
15-
*/
1613
public class StringToCharStreamUnitTest {
1714

18-
String testString = "Tests";
15+
private String testString = "Tests";
1916

2017
@Test
2118
public void givenTestString_whenChars_thenReturnIntStream() {
@@ -37,9 +34,9 @@ public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
3734
}
3835

3936
@Test
40-
public void givenIntStream_whenMapToObj_thenReturnStringStream(){
37+
public void givenIntStream_whenMapToObj_thenReturnStringStream() {
4138
Stream<String> stringStream
42-
= testString.codePoints().mapToObj(c -> String.valueOf((char) c));
39+
= testString.codePoints().mapToObj(c -> String.valueOf((char) c));
4340
assertNotNull(stringStream);
4441
}
4542

spring-vertx/pom.xml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
</exclusion>
4242
</exclusions>
4343
</dependency>
44-
<dependency>
45-
<groupId>io.vertx</groupId>
46-
<artifactId>vertx-core</artifactId>
47-
<version>${vertx.version}</version>
48-
</dependency>
4944
<dependency>
5045
<groupId>io.vertx</groupId>
5146
<artifactId>vertx-web</artifactId>
@@ -70,6 +65,23 @@
7065
<groupId>org.springframework.boot</groupId>
7166
<artifactId>spring-boot-maven-plugin</artifactId>
7267
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-surefire-plugin</artifactId>
71+
<version>${maven-surefire-plugin.version}</version>
72+
<configuration>
73+
<forkCount>3</forkCount>
74+
<reuseForks>true</reuseForks>
75+
<excludes>
76+
<exclude>**/*IntegrationTest.java</exclude>
77+
<exclude>**/*LongRunningUnitTest.java</exclude>
78+
<exclude>**/*ManualTest.java</exclude>
79+
<exclude>**/JdbcTest.java</exclude>
80+
<exclude>**/*LiveTest.java</exclude>
81+
</excludes>
82+
<testFailureIgnore>true</testFailureIgnore>
83+
</configuration>
84+
</plugin>
7385
</plugins>
7486
</build>
7587

spring-vertx/src/test/java/com/baeldung/vertxspring/VertxSpringApplicationIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public class VertxSpringApplicationIntegrationTest {
1616
private TestRestTemplate restTemplate = new TestRestTemplate();
1717

1818
@Test
19-
public void givenUrl_whenReceivedArticles_thenSuccess() {
19+
public void givenUrl_whenReceivedArticles_thenSuccess() throws InterruptedException {
2020
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class);
21+
2122
assertEquals(200, responseEntity.getStatusCodeValue());
2223
}
2324

0 commit comments

Comments
 (0)