Refactor IterableStreamConversionTest

This commit is contained in:
pivovarit 2017-01-17 14:29:42 +01:00
parent 32ad05a43e
commit 2c522ca047
1 changed files with 13 additions and 29 deletions

View File

@ -1,48 +1,32 @@
package com.baeldung.java.conversion;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
public class IterableStreamConversionTest {
@Test
public void givenIterable_whenConvertedToStream_thenNotNull() {
String[] names = {"Testing", "Iterable", "conversion", "to", "Stream"};
StreamIterable<String> iterable = new StreamIterable<>(names);
Iterable<String> iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream");
Assert.assertNotNull(StreamSupport.stream(iterable.spliterator(), false));
}
@Test
public void whenConvertedToList_thenCorrect() {
String[] names = {"Testing", "Iterable", "conversion", "to", "Stream"};
StreamIterable<String> iterable = new StreamIterable<>(names);
Stream<String> convertedStream = StreamSupport.stream(iterable.spliterator(), false);
List<String> collected = convertedStream.map(String::toUpperCase).collect(Collectors.toList());
assertThat(collected, contains("TESTING", "ITERABLE", "CONVERSION", "TO", "STREAM"));
}
}
Iterable<String> iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream");
class StreamIterable<T> implements Iterable<T> {
private List<T> list;
List<String> result = StreamSupport.stream(iterable.spliterator(), false)
.map(String::toUpperCase).collect(Collectors.toList());
StreamIterable(T[] array) {
this.list = Arrays.asList(array);
assertThat(result, contains("TESTING", "ITERABLE", "CONVERSION", "TO", "STREAM"));
}
@Override
public Iterator<T> iterator() {
return list.iterator();
}
}