Serenity build fox (#2236)
* Dependency clash resolved * Fix GuavaStreams tests * Fix GuavaStreams tests
This commit is contained in:
parent
fe2d2a6604
commit
62254d9beb
|
@ -1,23 +1,33 @@
|
||||||
package com.baeldung.guava.tutorial;
|
package com.baeldung.guava.tutorial;
|
||||||
|
|
||||||
import com.google.common.collect.Streams;
|
import com.google.common.collect.Streams;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.OptionalDouble;
|
||||||
|
import java.util.OptionalInt;
|
||||||
|
import java.util.OptionalLong;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.DoubleStream;
|
import java.util.stream.DoubleStream;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.LongStream;
|
import java.util.stream.LongStream;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static com.baeldung.guava.tutorial.StreamUtility.assertStreamEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
public class GuavaStreamsUnitTest {
|
public class GuavaStreamsUnitTest {
|
||||||
|
|
||||||
List<Integer> numbers;
|
private List<Integer> numbers;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
|
numbers = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -26,17 +36,17 @@ public class GuavaStreamsUnitTest {
|
||||||
Stream streamFromCollection = Streams.stream(numbers);
|
Stream streamFromCollection = Streams.stream(numbers);
|
||||||
|
|
||||||
//Assert.assertNotNull(streamFromCollection);
|
//Assert.assertNotNull(streamFromCollection);
|
||||||
StreamUtility.assertStreamEquals(streamFromCollection, numbers.stream());
|
assertStreamEquals(streamFromCollection, numbers.stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createStreamsWithIterable() {
|
public void createStreamsWithIterable() {
|
||||||
Iterable<Integer> numbersIterable = (Iterable<Integer>) numbers;
|
Iterable<Integer> numbersIterable = numbers;
|
||||||
|
|
||||||
Stream streamFromIterable = Streams.stream(numbersIterable);
|
Stream streamFromIterable = Streams.stream(numbersIterable);
|
||||||
|
|
||||||
Assert.assertNotNull(streamFromIterable);
|
assertNotNull(streamFromIterable);
|
||||||
StreamUtility.assertStreamEquals(streamFromIterable, numbers.stream());
|
assertStreamEquals(streamFromIterable, numbers.stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -45,8 +55,8 @@ public class GuavaStreamsUnitTest {
|
||||||
|
|
||||||
Stream streamFromIterator = Streams.stream(numbersIterator);
|
Stream streamFromIterator = Streams.stream(numbersIterator);
|
||||||
|
|
||||||
Assert.assertNotNull(streamFromIterator);
|
assertNotNull(streamFromIterator);
|
||||||
StreamUtility.assertStreamEquals(streamFromIterator, numbers.stream());
|
assertStreamEquals(streamFromIterator, numbers.stream());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -54,8 +64,8 @@ public class GuavaStreamsUnitTest {
|
||||||
|
|
||||||
Stream streamFromOptional = Streams.stream(Optional.of(1));
|
Stream streamFromOptional = Streams.stream(Optional.of(1));
|
||||||
|
|
||||||
Assert.assertNotNull(streamFromOptional);
|
assertNotNull(streamFromOptional);
|
||||||
Assert.assertEquals(streamFromOptional.count(), 1);
|
assertEquals(streamFromOptional.count(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -63,8 +73,8 @@ public class GuavaStreamsUnitTest {
|
||||||
|
|
||||||
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
LongStream streamFromOptionalLong = Streams.stream(OptionalLong.of(1));
|
||||||
|
|
||||||
Assert.assertNotNull(streamFromOptionalLong);
|
assertNotNull(streamFromOptionalLong);
|
||||||
Assert.assertEquals(streamFromOptionalLong.count(), 1);
|
assertEquals(streamFromOptionalLong.count(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -73,7 +83,7 @@ public class GuavaStreamsUnitTest {
|
||||||
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
IntStream streamFromOptionalInt = Streams.stream(OptionalInt.of(1));
|
||||||
|
|
||||||
//Assert.assertNotNull(streamFromOptionalInt);
|
//Assert.assertNotNull(streamFromOptionalInt);
|
||||||
Assert.assertEquals(streamFromOptionalInt.count(), 1);
|
assertEquals(streamFromOptionalInt.count(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -82,63 +92,54 @@ public class GuavaStreamsUnitTest {
|
||||||
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
DoubleStream streamFromOptionalDouble = Streams.stream(OptionalDouble.of(1.0));
|
||||||
|
|
||||||
//Assert.assertNotNull(streamFromOptionalDouble);
|
//Assert.assertNotNull(streamFromOptionalDouble);
|
||||||
Assert.assertEquals(streamFromOptionalDouble.count(), 1);
|
assertEquals(streamFromOptionalDouble.count(), 1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfSameType() {
|
public void concatStreamsOfSameType() {
|
||||||
Stream oddNumbers = Arrays
|
List<Integer> oddNumbers = Arrays
|
||||||
.asList(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
|
.asList(1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
|
||||||
.stream();
|
List<Integer> evenNumbers = Arrays
|
||||||
Stream evenNumbers = Arrays
|
.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20);
|
||||||
.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
|
|
||||||
.stream();
|
|
||||||
|
|
||||||
Stream combinedStreams = Streams.concat(oddNumbers, evenNumbers);
|
Stream<Integer> combinedStreams = Streams.concat(oddNumbers.stream(), evenNumbers.stream());
|
||||||
|
|
||||||
//Assert.assertNotNull(combinedStreams);
|
//Assert.assertNotNull(combinedStreams);
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, Stream.concat(oddNumbers, evenNumbers));
|
assertStreamEquals(combinedStreams, Stream.concat(oddNumbers.stream(), evenNumbers.stream()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfTypeLongStream() {
|
public void concatStreamsOfTypeLongStream() {
|
||||||
LongStream firstTwenty = LongStream.range(1, 20);
|
LongStream combinedStreams = Streams.concat(LongStream.range(1, 21), LongStream.range(21, 40));
|
||||||
LongStream nextTwenty = LongStream.range(21, 40);
|
|
||||||
|
|
||||||
LongStream combinedStreams = Streams.concat(firstTwenty, nextTwenty);
|
assertNotNull(combinedStreams);
|
||||||
|
assertStreamEquals(combinedStreams, LongStream.range(1, 40));
|
||||||
Assert.assertNotNull(combinedStreams);
|
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, LongStream.concat(firstTwenty, nextTwenty));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void concatStreamsOfTypeIntStream() {
|
public void concatStreamsOfTypeIntStream() {
|
||||||
IntStream firstTwenty = IntStream.range(1, 20);
|
IntStream combinedStreams = Streams.concat(IntStream.range(1, 20), IntStream.range(21, 40));
|
||||||
IntStream nextTwenty = IntStream.range(21, 40);
|
|
||||||
|
|
||||||
IntStream combinedStreams = Streams.concat(firstTwenty, nextTwenty);
|
assertNotNull(combinedStreams);
|
||||||
|
assertStreamEquals(combinedStreams, IntStream.concat(IntStream.range(1, 20), IntStream.range(21, 40)));
|
||||||
Assert.assertNotNull(combinedStreams);
|
|
||||||
StreamUtility.assertStreamEquals(combinedStreams, IntStream.concat(firstTwenty, nextTwenty));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findLastOfStream() {
|
public void findLastOfStream() {
|
||||||
Optional<Integer> lastElement = Streams.findLast(numbers.stream());
|
Optional<Integer> lastElement = Streams.findLast(numbers.stream());
|
||||||
|
|
||||||
Assert.assertNotNull(lastElement.get());
|
assertEquals(lastElement.get(), numbers.get(19));
|
||||||
Assert.assertEquals(lastElement.get(), numbers.get(20));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void mapWithIndexTest() {
|
public void mapWithIndexTest() {
|
||||||
Stream stringSream = Stream.of("a", "b", "c");
|
Stream<String> stringStream = Stream.of("a", "b", "c");
|
||||||
|
|
||||||
Stream<String> mappedStream = Streams.mapWithIndex(stringSream, (str, index) -> str + ":" + index);
|
Stream<String> mappedStream = Streams.mapWithIndex(stringStream, (str, index) -> str + ":" + index);
|
||||||
|
|
||||||
//Assert.assertNotNull(mappedStream);
|
//Assert.assertNotNull(mappedStream);
|
||||||
Assert.assertEquals(mappedStream
|
assertEquals(mappedStream
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.get(), "a:0");
|
.get(), "a:0");
|
||||||
|
|
||||||
|
@ -146,12 +147,12 @@ public class GuavaStreamsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void streamsZipTest() {
|
public void streamsZipTest() {
|
||||||
Stream stringSream = Stream.of("a", "b", "c");
|
Stream<String> stringSream = Stream.of("a", "b", "c");
|
||||||
Stream intStream = Stream.of(1, 2, 3);
|
Stream<Integer> intStream = Stream.of(1, 2, 3);
|
||||||
Stream<String> mappedStream = Streams.zip(stringSream, intStream, (str, index) -> str + ":" + index);
|
Stream<String> mappedStream = Streams.zip(stringSream, intStream, (str, index) -> str + ":" + index);
|
||||||
|
|
||||||
//Assert.assertNotNull(mappedStream);
|
//Assert.assertNotNull(mappedStream);
|
||||||
Assert.assertEquals(mappedStream
|
assertEquals(mappedStream
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.get(), "a:1");
|
.get(), "a:1");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue