|
1 | 1 | /*
|
2 |
| - * Copyright 2018-2019 the original author or authors. |
| 2 | + * Copyright 2018-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.function.Consumer;
|
22 | 22 |
|
23 |
| -import org.junit.Before; |
24 |
| -import org.junit.Ignore; |
25 |
| -import org.junit.Test; |
26 |
| -import org.junit.runner.RunWith; |
27 |
| -import org.mockito.Mock; |
28 |
| -import org.mockito.junit.MockitoJUnitRunner; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Disabled; |
| 25 | +import org.junit.jupiter.api.Test; |
29 | 26 |
|
30 | 27 | import static org.apache.commons.io.IOUtils.toInputStream;
|
31 | 28 | import static org.hamcrest.MatcherAssert.assertThat;
|
32 | 29 | import static org.hamcrest.Matchers.contains;
|
33 | 30 | import static org.hamcrest.core.Is.is;
|
34 | 31 | import static org.mockito.ArgumentMatchers.any;
|
| 32 | +import static org.mockito.Mockito.mock; |
35 | 33 | import static org.mockito.Mockito.verify;
|
36 | 34 | import static org.mockito.Mockito.verifyNoMoreInteractions;
|
37 | 35 | import static org.mockito.Mockito.when;
|
38 | 36 |
|
39 |
| -@RunWith(MockitoJUnitRunner.class) |
40 |
| -public class CommandTests { |
41 |
| - @Mock private Process executedProcess; |
42 |
| - @Mock private DockerComposeExecutable dockerComposeExecutable; |
43 |
| - @Mock private ErrorHandler errorHandler; |
| 37 | +class CommandTests { |
| 38 | + |
| 39 | + private Process executedProcess = mock(Process.class); |
| 40 | + private DockerComposeExecutable dockerComposeExecutable = mock(DockerComposeExecutable.class); |
| 41 | + private ErrorHandler errorHandler = mock(ErrorHandler.class); |
44 | 42 | private Command dockerComposeCommand;
|
45 | 43 | private final List<String> consumedLogLines = new ArrayList<>();
|
46 | 44 | private final Consumer<String> logConsumer = s -> consumedLogLines.add(s);
|
47 | 45 |
|
48 |
| - @Before |
49 |
| - public void before() throws IOException { |
| 46 | + @BeforeEach |
| 47 | + void prepareForTest() throws IOException { |
| 48 | + when(dockerComposeExecutable.commandName()).thenReturn("docker-compose"); |
50 | 49 | when(dockerComposeExecutable.execute(any())).thenReturn(executedProcess);
|
| 50 | + when(dockerComposeExecutable.execute(any(String[].class))).thenReturn(executedProcess); |
51 | 51 | dockerComposeCommand = new Command(dockerComposeExecutable, logConsumer);
|
52 |
| - |
53 | 52 | givenTheUnderlyingProcessHasOutput("");
|
54 | 53 | givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(0);
|
55 | 54 | }
|
56 | 55 |
|
57 |
| - @Test public void |
58 |
| - invoke_error_handler_when_exit_code_of_the_executed_process_is_non_0() throws IOException, InterruptedException { |
| 56 | + @Test |
| 57 | + void invokeErrorHandlerWhenExitCodeOfTheExecutedProcessIsNonZero() throws Exception { |
59 | 58 | int expectedExitCode = 1;
|
60 | 59 | givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(expectedExitCode);
|
61 | 60 | dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
62 |
| - |
63 | 61 | verify(errorHandler).handle(expectedExitCode, "", "docker-compose", "rm", "-f");
|
64 | 62 | }
|
65 | 63 |
|
66 |
| - @Test public void |
67 |
| - not_invoke_error_handler_when_exit_code_of_the_executed_process_is_0() throws IOException, InterruptedException { |
| 64 | + @Test |
| 65 | + void notInvokeErrorHandlerWhenExitCodeOfTheExecutedProcessIsZero() throws Exception { |
68 | 66 | dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
69 |
| - |
70 | 67 | verifyNoMoreInteractions(errorHandler);
|
71 | 68 | }
|
72 | 69 |
|
73 |
| - @Test public void |
74 |
| - return_output_when_exit_code_of_the_executed_process_is_non_0() throws IOException, InterruptedException { |
| 70 | + @Test |
| 71 | + void returnOutputWhenExitCodeOfTheExecutedProcessIsNonZero() throws Exception { |
75 | 72 | String expectedOutput = "test output";
|
76 | 73 | givenTheUnderlyingProcessTerminatesWithAnExitCodeOf(1);
|
77 | 74 | givenTheUnderlyingProcessHasOutput(expectedOutput);
|
78 | 75 | String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
79 |
| - |
80 | 76 | assertThat(commandOutput, is(expectedOutput));
|
81 | 77 | }
|
82 | 78 |
|
83 |
| - @Test public void |
84 |
| - return_output_when_exit_code_of_the_executed_process_is_0() throws IOException, InterruptedException { |
| 79 | + @Test |
| 80 | + void returnOutputWhenExitCodeOfTheExecutedProcessIsZero() throws Exception { |
85 | 81 | String expectedOutput = "test output";
|
86 | 82 | givenTheUnderlyingProcessHasOutput(expectedOutput);
|
87 | 83 | String commandOutput = dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
88 |
| - |
89 | 84 | assertThat(commandOutput, is(expectedOutput));
|
90 | 85 | }
|
91 | 86 |
|
92 |
| - @Test public void |
93 |
| - give_the_output_to_the_specified_consumer_as_it_is_available() throws IOException, InterruptedException { |
| 87 | + @Test |
| 88 | + void giveTheOutputToTheSpecifiedConsumerAsItIsAvailable() throws Exception { |
94 | 89 | givenTheUnderlyingProcessHasOutput("line 1\nline 2");
|
95 |
| - |
96 | 90 | dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
97 |
| - |
98 | 91 | assertThat(consumedLogLines, contains("line 1", "line 2"));
|
99 | 92 | }
|
100 | 93 |
|
101 |
| - // flaky test: https://circleci.com/gh/palantir/docker-compose-rule/378, 370, 367, 366 |
102 |
| - @Ignore |
103 |
| - @Test public void |
104 |
| - not_create_long_lived_threads_after_execution() throws IOException, InterruptedException { |
| 94 | + @Disabled("flaky test: https://circleci.com/gh/palantir/docker-compose-rule/378, 370, 367, 366") |
| 95 | + @Test |
| 96 | + void notCreateLongLivedThreadsAfterExecution() throws Exception { |
105 | 97 | int preThreadCount = Thread.getAllStackTraces().entrySet().size();
|
106 | 98 | dockerComposeCommand.execute(errorHandler, "rm", "-f");
|
107 | 99 | int postThreadCount = Thread.getAllStackTraces().entrySet().size();
|
|
0 commit comments