|
| 1 | +package io.reflectoring.functional; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.function.Executable; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.CsvSource; |
| 14 | +import org.opentest4j.AssertionFailedError; |
| 15 | + |
| 16 | +public class ExecutableTest { |
| 17 | + private final List<Long> numbers = Arrays.asList(100L, 200L, 50L, 300L); |
| 18 | + final Executable sorter = |
| 19 | + () -> { |
| 20 | + TimeUnit.SECONDS.sleep(2); |
| 21 | + numbers.sort(Long::compareTo); |
| 22 | + }; |
| 23 | + private final Executable checkSorting = |
| 24 | + () -> assertEquals(List.of(50L, 100L, 200L, 300L), numbers); |
| 25 | + private final Executable noChanges = () -> assertEquals(List.of(100L, 200L, 50L, 300L), numbers); |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @CsvSource({"1,1,2,Hello,H,bye,2,byebye", "4,5,9,Good,Go,Go,-10,", "10,21,31,Team,Tea,Stop,-2,"}) |
| 29 | + void testAssertAllWithExecutable( |
| 30 | + int num1, |
| 31 | + int num2, |
| 32 | + int sum, |
| 33 | + String input, |
| 34 | + String prefix, |
| 35 | + String arg, |
| 36 | + int count, |
| 37 | + String result) { |
| 38 | + assertAll( |
| 39 | + () -> assertEquals(sum, num1 + num2), |
| 40 | + () -> assertTrue(input.startsWith(prefix)), |
| 41 | + () -> { |
| 42 | + if (count < 0) { |
| 43 | + assertThrows( |
| 44 | + IllegalArgumentException.class, |
| 45 | + () -> { |
| 46 | + new ArrayList<>(count); |
| 47 | + }); |
| 48 | + } else { |
| 49 | + assertEquals(result, arg.repeat(count)); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @CsvSource({"one,0,o", "one,1,n"}) |
| 56 | + void testAssertDoesNotThrowWithExecutable(String input, int index, char result) { |
| 57 | + assertDoesNotThrow(() -> assertEquals(input.charAt(index), result)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testAssertThrowsWithExecutable() { |
| 62 | + List<String> input = Arrays.asList("one", "", "three", null, "five"); |
| 63 | + final IllegalArgumentException exception = |
| 64 | + assertThrows( |
| 65 | + IllegalArgumentException.class, |
| 66 | + () -> { |
| 67 | + for (String value : input) { |
| 68 | + if (value == null || value.isBlank()) { |
| 69 | + throw new IllegalArgumentException("Got invalid value"); |
| 70 | + } |
| 71 | + // process values |
| 72 | + } |
| 73 | + }); |
| 74 | + assertEquals("Got invalid value", exception.getMessage()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testAssertTimeoutWithExecutable() { |
| 79 | + assertAll( |
| 80 | + () -> |
| 81 | + assertThrows( |
| 82 | + AssertionFailedError.class, () -> assertTimeout(Duration.ofSeconds(1), sorter)), |
| 83 | + checkSorting); |
| 84 | + |
| 85 | + assertAll( |
| 86 | + () -> assertDoesNotThrow(() -> assertTimeout(Duration.ofSeconds(5), sorter)), checkSorting); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testAssertTimeoutPreemptivelyWithExecutable() { |
| 91 | + assertAll( |
| 92 | + () -> |
| 93 | + assertThrows( |
| 94 | + AssertionFailedError.class, |
| 95 | + () -> assertTimeoutPreemptively(Duration.ofSeconds(1), sorter)), |
| 96 | + noChanges); |
| 97 | + |
| 98 | + assertAll( |
| 99 | + () -> assertDoesNotThrow(() -> assertTimeoutPreemptively(Duration.ofSeconds(5), sorter)), |
| 100 | + checkSorting); |
| 101 | + } |
| 102 | +} |
0 commit comments