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