Idiomatic refactor (#2063)

* StringToCharStream

* Spring integration tests

* Spring integration tests
This commit is contained in:
Grzegorz Piwowarek 2017-06-13 16:54:45 +02:00 committed by GitHub
parent 53f4ec5f87
commit fe11e8f7d4
4 changed files with 29 additions and 42 deletions

View File

@ -1,57 +1,34 @@
package com.baeldung.string; package com.baeldung.string;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
/**
* Created by smatt on 26/05/2017.
*/
public class StringToCharStream { public class StringToCharStream {
public StringToCharStream() { private StringToCharStream() {
//let's use the Stream API to manipulate a string
//this will count the occurrence of each character in the test string
String testString = "tests"; String testString = "tests";
//first get an IntStream
IntStream intStream = testString.chars(); IntStream intStream = testString.chars();
IntStream intStream1 = testString.codePoints(); IntStream intStream1 = testString.codePoints();
//now let's map them
Stream<Character> characterStream = intStream.mapToObj(c -> (char) c); Stream<Character> characterStream = intStream.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c); Stream<Character> characterStream1 = intStream1.mapToObj(c -> (char) c);
System.out.println("Counting Occurrence of Letter"); System.out.println("Counting Occurrence of Letter");
testString = "Noww";
//we don't want to use foreach, so . . . Map<Character, Integer> map = "Noww".codePoints()
Map<Character, Integer> map = new HashMap<>();
testString.codePoints()
.mapToObj(c -> (char) c) .mapToObj(c -> (char) c)
.filter(c -> Character.isLetter(c)) .filter(Character::isLetter)
.forEach(c -> { .collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
if(map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
});
//printing out the result here //printing out the result here
System.out.println(map.toString()); System.out.println(map.toString());
} }
public static void main(String[] args) { public static void main(String[] args) {
new StringToCharStream(); new StringToCharStream();
} }
} }

View File

@ -10,12 +10,9 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/**
* Created by smatt on 09/06/2017.
*/
public class StringToCharStreamUnitTest { public class StringToCharStreamUnitTest {
String testString = "Tests"; private String testString = "Tests";
@Test @Test
public void givenTestString_whenChars_thenReturnIntStream() { public void givenTestString_whenChars_thenReturnIntStream() {

View File

@ -41,11 +41,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency> <dependency>
<groupId>io.vertx</groupId> <groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId> <artifactId>vertx-web</artifactId>
@ -70,6 +65,23 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@ -16,8 +16,9 @@ public class VertxSpringApplicationIntegrationTest {
private TestRestTemplate restTemplate = new TestRestTemplate(); private TestRestTemplate restTemplate = new TestRestTemplate();
@Test @Test
public void givenUrl_whenReceivedArticles_thenSuccess() { public void givenUrl_whenReceivedArticles_thenSuccess() throws InterruptedException {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class); ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class);
assertEquals(200, responseEntity.getStatusCodeValue()); assertEquals(200, responseEntity.getStatusCodeValue());
} }