|
| 1 | +package io.github.netmikey.testprocesses.functional; |
| 2 | + |
| 3 | +import static io.github.netmikey.testprocesses.TestProcessDefinitionBy.*; |
| 4 | +import static io.github.netmikey.testprocesses.functional.testfixtures.TestHelper.*; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.TestMethodOrder; |
| 7 | +import org.assertj.core.api.Assertions; |
| 8 | +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
| 9 | +import org.junit.jupiter.api.Order; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | + |
| 13 | +import io.github.netmikey.testprocesses.StartStrategy; |
| 14 | +import io.github.netmikey.testprocesses.TestProcessDefinitionBy; |
| 15 | +import io.github.netmikey.testprocesses.TestProcessesRegistry; |
| 16 | +import io.github.netmikey.testprocesses.functional.testfixtures.EchoTestProcess; |
| 17 | +import io.github.netmikey.testprocesses.utils.StreamStart; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test the bahvior of the {@link StreamStart} setting when retrieving streams |
| 21 | + * from a test process that has been started imperatively using the API (instead |
| 22 | + * of the test-method-bound annotation). |
| 23 | + * <p> |
| 24 | + * To do this, the test method execution is ordered. Beware of the order when |
| 25 | + * editing this test. Also, since the order and the state between the running |
| 26 | + * test methods is important, running single test methods will fail of course. |
| 27 | + */ |
| 28 | +@TestProcessesSpringBootTest |
| 29 | +@TestMethodOrder(OrderAnnotation.class) |
| 30 | +public class StreamStartImperativeTest { |
| 31 | + |
| 32 | + private static final String MARKER_1 = "+++ MARKER_1"; |
| 33 | + |
| 34 | + private static final String MARKER_2 = "+++ MARKER_2"; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private TestProcessesRegistry registry; |
| 38 | + |
| 39 | + private TestProcessDefinitionBy<EchoTestProcess> echoTestProcess = clazz(EchoTestProcess.class); |
| 40 | + |
| 41 | + /** |
| 42 | + * Test starting a test process imperatively / "manually" / using the API |
| 43 | + * and then retrieving its stdOut stream. |
| 44 | + */ |
| 45 | + @Test |
| 46 | + @Order(1) |
| 47 | + public void testWithProcessStartedViaAPI() { |
| 48 | + // Start EchoTestProcess using API |
| 49 | + registry.start(echoTestProcess, StartStrategy.REQUIRE_RESTART); |
| 50 | + |
| 51 | + sendToEchoProcess(registry, MARKER_1); |
| 52 | + |
| 53 | + // JUnit Test was running before EchoTestProcess, should return the |
| 54 | + // whole stream |
| 55 | + String absoluteLog = registry.stdOutAsStringOf(echoTestProcess, StreamStart.ABSOLUTE); |
| 56 | + String currentTestLog = registry.stdOutAsStringOf(echoTestProcess, StreamStart.CURRENT_TEST); |
| 57 | + |
| 58 | + // Both should be equal |
| 59 | + Assertions.assertThat(absoluteLog).isEqualTo(currentTestLog); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test that a test process started imperatively / "manually" / using the |
| 64 | + * API in a previous test obtains the "current test" markers on its streams |
| 65 | + * when the present test starts and behaves as any regular already-running |
| 66 | + * process. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + @Order(2) |
| 70 | + public void testWithProcessFromPreviousTestStartedViaAPI() { |
| 71 | + // EchoTestProcess should still be running |
| 72 | + |
| 73 | + sendToEchoProcess(registry, MARKER_2); |
| 74 | + |
| 75 | + Assertions.assertThat(registry.stdOutAsStringOf(echoTestProcess, StreamStart.ABSOLUTE)) |
| 76 | + .contains(MARKER_1, MARKER_2); |
| 77 | + |
| 78 | + Assertions.assertThat(registry.stdOutAsStringOf(echoTestProcess, StreamStart.CURRENT_TEST)) |
| 79 | + .contains(MARKER_2) |
| 80 | + .doesNotContain(MARKER_1); |
| 81 | + } |
| 82 | +} |
0 commit comments