|
1 | 1 | package com.enot.cmd; |
2 | 2 |
|
3 | 3 | import com.enot.cmd.core.Cmd; |
4 | | -import org.hamcrest.core.IsEqual; |
5 | | -import org.hamcrest.core.IsSame; |
6 | | -import org.junit.Assert; |
7 | 4 | import org.junit.Test; |
8 | 5 | import org.zeroturnaround.exec.stream.LogOutputStream; |
9 | 6 |
|
|
16 | 13 | import java.util.UUID; |
17 | 14 | import java.util.concurrent.TimeoutException; |
18 | 15 |
|
19 | | -import static org.junit.Assert.*; |
| 16 | +import static org.hamcrest.core.AllOf.allOf; |
| 17 | +import static org.hamcrest.core.Is.is; |
| 18 | +import static org.hamcrest.core.IsNot.not; |
| 19 | +import static org.junit.Assert.assertThat; |
20 | 20 |
|
21 | 21 | public class CmdTest { |
22 | 22 | @Test |
23 | 23 | public void interpreterCommandLine() { |
24 | | - Assert.assertThat( |
| 24 | + assertThat( |
25 | 25 | new Cmd() |
26 | 26 | .interpreter("sh") |
27 | 27 | .command("-c", "echo Hello;") |
28 | 28 | .commandLine(), |
29 | | - new IsEqual<>(Arrays.asList("sh", "-c", "echo Hello;"))); |
| 29 | + is(Arrays.asList("sh", "-c", "echo Hello;"))); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | @Test |
33 | 33 | public void execute() throws Exception { |
34 | | - Assert.assertEquals("Hello\n", |
| 34 | + assertThat( |
35 | 35 | new Cmd() |
36 | 36 | .configuring(e -> e.readOutput(true)) |
37 | 37 | .command("echo", "Hello") |
38 | 38 | .execute() |
39 | | - .outputUTF8()); |
| 39 | + .outputUTF8(), |
| 40 | + is("Hello\n")); |
40 | 41 | } |
41 | 42 |
|
42 | 43 | @Test |
43 | 44 | public void executeNoTimeout() throws Exception { |
44 | | - Assert.assertEquals("Hello\n", |
| 45 | + assertThat( |
45 | 46 | new Cmd() |
46 | 47 | .configuring(e -> e.readOutput(true)) |
47 | 48 | .command("echo", "Hello") |
48 | 49 | .executeNoTimeout() |
49 | | - .outputUTF8()); |
| 50 | + .outputUTF8(), |
| 51 | + is("Hello\n")); |
50 | 52 | } |
51 | 53 |
|
52 | 54 | @Test |
53 | 55 | public void start() throws Exception { |
54 | | - Assert.assertEquals("Hello\n", |
| 56 | + assertThat( |
55 | 57 | new Cmd() |
56 | 58 | .configuring(e -> e.readOutput(true)) |
57 | 59 | .command("echo", "Hello") |
58 | 60 | .start() |
59 | 61 | .getFuture() |
60 | 62 | .get() |
61 | | - .outputUTF8()); |
| 63 | + .outputUTF8(), |
| 64 | + is("Hello\n")); |
62 | 65 | } |
63 | 66 |
|
64 | 67 | @Test |
65 | 68 | public void executeScript() throws Exception { |
66 | | - Assert.assertEquals("Hello\n", |
| 69 | + assertThat( |
67 | 70 | new Cmd() |
68 | 71 | .configuring(e -> e.readOutput(true)) |
69 | 72 | .interpreter("sh") |
70 | 73 | .command("-c", "s='Hello'; echo $s;") |
71 | 74 | .execute() |
72 | | - .outputUTF8()); |
| 75 | + .outputUTF8(), |
| 76 | + is("Hello\n")); |
73 | 77 | } |
74 | 78 |
|
75 | 79 | @Test |
76 | 80 | public void createWorkDir() throws Exception { |
77 | | - File workDir = generateRandomPath().toFile(); |
78 | | - |
79 | | - assertFalse(workDir.exists()); |
80 | | - |
81 | | - new Cmd() |
82 | | - .configuring(e -> e.directory(workDir)) |
83 | | - .command("echo", "hello world") |
84 | | - .execute(); |
85 | | - |
86 | | - assertTrue(workDir.exists()); |
| 81 | + final File workDir = generateRandomPath().toFile(); |
| 82 | + assertThat(true, allOf( |
| 83 | + is(not(workDir.exists())), |
| 84 | + is(0 == new Cmd() |
| 85 | + .configuring(e -> e.directory(workDir)) |
| 86 | + .command("echo", "hello world") |
| 87 | + .execute() |
| 88 | + .getExitValue()), |
| 89 | + is(workDir.exists()) |
| 90 | + )); |
87 | 91 | } |
88 | 92 |
|
89 | 93 | @Test |
90 | 94 | public void cleanUp() throws Exception { |
91 | | - File workDir = generateRandomPath().toFile(); |
92 | | - |
93 | | - assertFalse(workDir.exists()); |
94 | | - |
95 | | - new Cmd() |
96 | | - .configuring(e -> e.directory(workDir)) |
97 | | - .cleanUp(true) |
98 | | - .command("echo", "hello world") |
99 | | - .execute(); |
100 | | - |
101 | | - assertFalse("Directory should be deleted", workDir.exists()); |
| 95 | + final File workDir = generateRandomPath().toFile(); |
| 96 | + assertThat(true, allOf( |
| 97 | + is(not(workDir.exists())), |
| 98 | + is(0 == new Cmd() |
| 99 | + .configuring(e -> e.directory(workDir)) |
| 100 | + .cleanUp(true) |
| 101 | + .command("echo", "hello world") |
| 102 | + .execute() |
| 103 | + .getExitValue()), |
| 104 | + is(not(workDir.exists())) |
| 105 | + )); |
102 | 106 | } |
103 | 107 |
|
104 | 108 | @Test |
105 | 109 | public void outputFile() throws Exception { |
106 | | - Path execDir = generateRandomPath(); |
107 | | - String outputFileName = "test.output"; |
108 | | - new Cmd() |
109 | | - .configuring(e -> e.directory(execDir.toFile())) |
110 | | - .outputFileName(outputFileName) |
111 | | - .command("echo", "hello world") |
112 | | - .execute(); |
113 | | - assertTrue("Output file should be exists", Paths.get(execDir.toString(), outputFileName).toFile().exists()); |
| 110 | + final String outputFileName = "test.output"; |
| 111 | + final File workDir = generateRandomPath().toFile(); |
| 112 | + assertThat(true, allOf( |
| 113 | + is(not(workDir.exists())), |
| 114 | + is(0 == new Cmd() |
| 115 | + .configuring(e -> e.directory(workDir)) |
| 116 | + .outputFileName(outputFileName) |
| 117 | + .command("echo", "hello world") |
| 118 | + .execute() |
| 119 | + .getExitValue()), |
| 120 | + is(new File(workDir, outputFileName).exists()) |
| 121 | + )); |
114 | 122 | } |
115 | 123 |
|
116 | 124 | @Test |
117 | 125 | public void beforeStartListener() throws IOException, InterruptedException, TimeoutException { |
118 | | - ArrayList<String> lines = new ArrayList<>(); |
119 | | - final String arg = "line1"; |
120 | | - new Cmd() |
121 | | - .listening() |
122 | | - .beforeStart(e -> e.redirectOutputAlsoTo(new LogOutputStream() { |
123 | | - @Override |
124 | | - protected void processLine(String line) { |
125 | | - lines.add(line); |
126 | | - } |
127 | | - })) |
128 | | - .back() |
129 | | - .command("echo", arg) |
130 | | - .execute(); |
131 | | - |
132 | | - assertEquals(1, lines.size()); |
133 | | - assertEquals(arg, lines.get(0)); |
| 126 | + final ArrayList<String> lines = new ArrayList<>(); |
| 127 | + final String message = "line1"; |
| 128 | + assertThat(true, allOf( |
| 129 | + is(0 == new Cmd() |
| 130 | + .listening() |
| 131 | + .beforeStart(e -> e.redirectOutputAlsoTo(new LogOutputStream() { |
| 132 | + @Override |
| 133 | + protected void processLine(String line) { |
| 134 | + lines.add(line); |
| 135 | + } |
| 136 | + })) |
| 137 | + .back() |
| 138 | + .command("echo", message) |
| 139 | + .execute() |
| 140 | + .getExitValue()), |
| 141 | + is(1 == lines.size()), |
| 142 | + is(message.equals(lines.get(0))) |
| 143 | + )); |
134 | 144 | } |
135 | 145 |
|
136 | 146 | private Path generateRandomPath() { |
|
0 commit comments