bael-2508 - Stream.peek() examples (#6132)

* bael-2508 - Stream.peek() examples

* beal-2508 additional example

* bael-2508 moving examples
This commit is contained in:
fanatixan 2019-01-13 22:27:07 +01:00 committed by maibin
parent 4d90ca6c58
commit e7412182a9
1 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,118 @@
package com.baeldung.stream;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.StringWriter;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class PeekUnitTest {
private StringWriter out;
@BeforeEach
void setup() {
out = new StringWriter();
}
@Test
void givenStringStream_whenCallingPeekOnly_thenNoElementProcessed() {
// given
Stream<String> nameStream = Stream.of("Alice", "Bob", "Chuck");
// when
nameStream.peek(out::append);
// then
assertThat(out.toString()).isEmpty();
}
@Test
void givenStringStream_whenCallingForEachOnly_thenElementsProcessed() {
// given
Stream<String> nameStream = Stream.of("Alice", "Bob", "Chuck");
// when
nameStream.forEach(out::append);
// then
assertThat(out.toString()).isEqualTo("AliceBobChuck");
}
@Test
void givenStringStream_whenCallingPeekAndNoopForEach_thenElementsProcessed() {
// given
Stream<String> nameStream = Stream.of("Alice", "Bob", "Chuck");
// when
nameStream.peek(out::append)
.forEach(this::noop);
// then
assertThat(out.toString()).isEqualTo("AliceBobChuck");
}
@Test
void givenStringStream_whenCallingPeekAndCollect_thenElementsProcessed() {
// given
Stream<String> nameStream = Stream.of("Alice", "Bob", "Chuck");
// when
nameStream.peek(out::append)
.collect(Collectors.toList());
// then
assertThat(out.toString()).isEqualTo("AliceBobChuck");
}
@Test
void givenStringStream_whenCallingPeekAndForEach_thenElementsProcessedTwice() {
// given
Stream<String> nameStream = Stream.of("Alice", "Bob", "Chuck");
// when
nameStream.peek(out::append)
.forEach(out::append);
// then
assertThat(out.toString()).isEqualTo("AliceAliceBobBobChuckChuck");
}
@Test
void givenStringStream_whenCallingPeek_thenElementsProcessedTwice() {
// given
Stream<User> userStream = Stream.of(new User("Alice"), new User("Bob"), new User("Chuck"));
// when
userStream.peek(u -> u.setName(u.getName().toLowerCase()))
.map(User::getName)
.forEach(out::append);
// then
assertThat(out.toString()).isEqualTo("alicebobchuck");
}
private static class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private void noop(String s) {
}
}