StreamsTest junit-jupiter
This commit is contained in:
parent
3d4ed4a8ac
commit
115a6c64bc
|
@ -16,27 +16,37 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.core.Is.is;
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.hamcrest.core.IsNull.nullValue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
||||||
|
|
||||||
import java.lang.reflect.UndeclaredThrowableException;
|
import java.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.apache.commons.lang3.Functions.FailableConsumer;
|
import org.apache.commons.lang3.Functions.FailableConsumer;
|
||||||
import org.apache.commons.lang3.Functions.FailablePredicate;
|
import org.apache.commons.lang3.Functions.FailablePredicate;
|
||||||
|
import org.junit.jupiter.api.DynamicTest;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestFactory;
|
||||||
|
import org.junit.jupiter.api.function.Executable;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
class StreamsTest {
|
class StreamsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleStreamMap() {
|
void testSimpleStreamMap() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
|
final List<Integer> output = Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList());
|
||||||
assertEquals(6, output.size());
|
assertEquals(6, output.size());
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
assertEquals(i+1, output.get(i).intValue());
|
assertEquals(i+1, output.get(i).intValue());
|
||||||
|
@ -46,19 +56,16 @@ class StreamsTest {
|
||||||
@Test
|
@Test
|
||||||
void testSimpleStreamMapFailing() {
|
void testSimpleStreamMapFailing() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
|
||||||
try {
|
final Executable testMethod = () -> Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList());
|
||||||
Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
|
final NumberFormatException thrown = assertThrows(NumberFormatException.class, testMethod);
|
||||||
fail("Expected Exception");
|
assertEquals("For input string: \"4 \"", thrown.getMessage());
|
||||||
} catch (final NumberFormatException nfe) {
|
|
||||||
assertEquals("For input string: \"4 \"", nfe.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleStreamForEach() {
|
void testSimpleStreamForEach() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = new ArrayList<>();
|
final List<Integer> output = new ArrayList<>();
|
||||||
Functions.stream(input).forEach(s -> output.add(Integer.valueOf(s)));
|
Functions.stream(input).forEach((s) -> output.add(Integer.valueOf(s)));
|
||||||
assertEquals(6, output.size());
|
assertEquals(6, output.size());
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
assertEquals(i+1, output.get(i).intValue());
|
assertEquals(i+1, output.get(i).intValue());
|
||||||
|
@ -76,7 +83,7 @@ class StreamsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
|
protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
|
||||||
return s -> {
|
return (s) -> {
|
||||||
final Integer i = Integer.valueOf(s);
|
final Integer i = Integer.valueOf(s);
|
||||||
if (i.intValue() == 4) {
|
if (i.intValue() == 4) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
|
@ -84,41 +91,47 @@ class StreamsTest {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@TestFactory
|
||||||
void testSimpleStreamForEachFailing() {
|
Stream<DynamicTest> simpleStreamForEachFailing() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = new ArrayList<>();
|
|
||||||
final IllegalArgumentException ise = new IllegalArgumentException("Invalid argument: 4");
|
return Stream.of(
|
||||||
try {
|
|
||||||
Functions.stream(input).forEach(asIntConsumer(ise));
|
dynamicTest("IllegalArgumentException", () -> {
|
||||||
fail("Expected Exception");
|
final IllegalArgumentException ise = new IllegalArgumentException();
|
||||||
} catch (final IllegalArgumentException e) {
|
final Executable testMethod = () -> Functions.stream(input)
|
||||||
assertSame(ise, e);
|
.forEach(asIntConsumer(ise));
|
||||||
}
|
final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
|
||||||
output.clear();
|
assertThat(thrown.getMessage(), is(nullValue()));
|
||||||
final OutOfMemoryError oome = new OutOfMemoryError();
|
}),
|
||||||
try {
|
|
||||||
Functions.stream(input).forEach(asIntConsumer(oome));
|
dynamicTest("OutOfMemoryError", () -> {
|
||||||
fail("Expected Exception");
|
final OutOfMemoryError oome = new OutOfMemoryError();
|
||||||
} catch (final Throwable t) {
|
final Executable oomeTestMethod = () -> Functions.stream(input)
|
||||||
assertSame(oome, t);
|
.forEach(asIntConsumer(oome));
|
||||||
}
|
final OutOfMemoryError oomeThrown = assertThrows(OutOfMemoryError.class, oomeTestMethod);
|
||||||
output.clear();
|
assertThat(oomeThrown.getMessage(), is(nullValue()));
|
||||||
final SAXException se = new SAXException();
|
}),
|
||||||
try {
|
|
||||||
Functions.stream(input).forEach(asIntConsumer(se));
|
dynamicTest("SAXException", () -> {
|
||||||
fail("Expected Exception");
|
final SAXException se = new SAXException();
|
||||||
} catch (final UndeclaredThrowableException ute) {
|
final Executable seTestMethod = () -> Functions.stream(input)
|
||||||
assertSame(se, ute.getCause());
|
.forEach(asIntConsumer(se));
|
||||||
}
|
final UndeclaredThrowableException seThrown = assertThrows(UndeclaredThrowableException.class, seTestMethod);
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(seThrown.getMessage(), is(nullValue())),
|
||||||
|
() -> assertThat(seThrown.getCause(), is(equalTo(se)))
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleStreamFilter() {
|
void testSimpleStreamFilter() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input)
|
final List<Integer> output = Functions.stream(input)
|
||||||
.map(s -> Integer.valueOf(s))
|
.map((s) -> Integer.valueOf(s))
|
||||||
.filter(i -> {
|
.filter((i) -> {
|
||||||
return i.intValue() %2 == 0;
|
return i.intValue() %2 == 0;
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
@ -133,7 +146,7 @@ class StreamsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(final T pThrowable) {
|
protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(final T pThrowable) {
|
||||||
return i -> {
|
return (i) -> {
|
||||||
if (i.intValue() == 5) {
|
if (i.intValue() == 5) {
|
||||||
if (pThrowable != null) {
|
if (pThrowable != null) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
|
@ -143,49 +156,50 @@ class StreamsTest {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@TestFactory
|
||||||
void testSimpleStreamFilterFailing() {
|
Stream<DynamicTest> simpleStreamFilterFailing() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input)
|
final List<Integer> output = Functions.stream(input)
|
||||||
.map(s -> Integer.valueOf(s))
|
.map((s) -> Integer.valueOf(s))
|
||||||
.filter(asIntPredicate(null))
|
.filter(asIntPredicate(null))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertEvenNumbers(output);
|
assertEvenNumbers(output);
|
||||||
|
|
||||||
output.clear();
|
return Stream.of(
|
||||||
final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
|
|
||||||
try {
|
|
||||||
Functions.stream(input)
|
|
||||||
.map(s -> Integer.valueOf(s))
|
|
||||||
.filter(asIntPredicate(iae))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
fail("Expected Exception");
|
|
||||||
} catch (final IllegalArgumentException e) {
|
|
||||||
assertSame(iae, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
output.clear();
|
dynamicTest("IllegalArgumentException", () -> {
|
||||||
final OutOfMemoryError oome = new OutOfMemoryError();
|
final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
|
||||||
try {
|
final Executable testMethod = () -> Functions.stream(input)
|
||||||
Functions.stream(input)
|
.map((s) -> Integer.valueOf(s))
|
||||||
.map(s -> Integer.valueOf(s))
|
.filter(asIntPredicate(iae))
|
||||||
.filter(asIntPredicate(oome))
|
.collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, testMethod);
|
||||||
fail("Expected Exception");
|
assertThat(thrown.getMessage(), is(equalTo("Invalid argument: " + 5)));
|
||||||
} catch (final Throwable t) {
|
}),
|
||||||
assertSame(oome, t);
|
|
||||||
}
|
|
||||||
|
|
||||||
output.clear();
|
dynamicTest("OutOfMemoryError", () -> {
|
||||||
final SAXException se = new SAXException();
|
final OutOfMemoryError oome = new OutOfMemoryError();
|
||||||
try {
|
final Executable testMethod = () -> Functions.stream(input)
|
||||||
Functions.stream(input)
|
.map((s) -> Integer.valueOf(s))
|
||||||
.map(s -> Integer.valueOf(s))
|
.filter(asIntPredicate(oome))
|
||||||
.filter(asIntPredicate(se))
|
.collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, testMethod);
|
||||||
fail("Expected Exception");
|
assertThat(thrown.getMessage(), is(nullValue()));
|
||||||
} catch (final UndeclaredThrowableException t) {
|
}),
|
||||||
assertSame(se, t.getCause());
|
|
||||||
}
|
dynamicTest("SAXException", () -> {
|
||||||
|
final SAXException se = new SAXException();
|
||||||
|
final Executable testMethod = () -> Functions.stream(input)
|
||||||
|
.map((s) -> Integer.valueOf(s))
|
||||||
|
.filter(asIntPredicate(se))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
final UndeclaredThrowableException thrown = assertThrows(UndeclaredThrowableException.class, testMethod);
|
||||||
|
assertAll(
|
||||||
|
() -> assertThat(thrown.getMessage(), is(nullValue())),
|
||||||
|
() -> assertThat(thrown.getCause(), is(equalTo(se)))
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue