Skip to content

Commit d337473

Browse files
committed
build: moved IT tests to its own package
1 parent f2bb304 commit d337473

File tree

3 files changed

+222
-81
lines changed

3 files changed

+222
-81
lines changed

pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,21 @@
126126
<artifactId>maven-compiler-plugin</artifactId>
127127
<version>3.14.0</version>
128128
</plugin>
129+
<plugin>
130+
<groupId>org.codehaus.mojo</groupId>
131+
<artifactId>build-helper-maven-plugin</artifactId>
132+
<version>3.6.1</version>
133+
</plugin>
129134
<plugin>
130135
<groupId>org.apache.maven.plugins</groupId>
131136
<artifactId>maven-surefire-plugin</artifactId>
132137
<version>3.5.3</version>
133138
</plugin>
139+
<plugin>
140+
<groupId>org.apache.maven.plugins</groupId>
141+
<artifactId>maven-failsafe-plugin</artifactId>
142+
<version>3.5.3</version>
143+
</plugin>
134144
<plugin>
135145
<groupId>org.apache.maven.plugins</groupId>
136146
<artifactId>maven-jar-plugin</artifactId>
@@ -210,6 +220,38 @@
210220
</execution>
211221
</executions>
212222
</plugin>
223+
<plugin>
224+
<groupId>org.codehaus.mojo</groupId>
225+
<artifactId>build-helper-maven-plugin</artifactId>
226+
<executions>
227+
<execution>
228+
<id>add-test-source</id>
229+
<phase>process-resources</phase>
230+
<goals>
231+
<goal>add-test-source</goal>
232+
</goals>
233+
<configuration>
234+
<sources>
235+
<source>src/it/java</source>
236+
</sources>
237+
</configuration>
238+
</execution>
239+
</executions>
240+
</plugin>
241+
<plugin>
242+
<groupId>org.apache.maven.plugins</groupId>
243+
<artifactId>maven-failsafe-plugin</artifactId>
244+
<executions>
245+
<execution>
246+
<id>integration-test</id>
247+
<goals>
248+
<goal>integration-test</goal>
249+
<goal>verify</goal>
250+
</goals>
251+
<phase>integration-test</phase>
252+
</execution>
253+
</executions>
254+
</plugin>
213255
<plugin>
214256
<groupId>org.apache.maven.plugins</groupId>
215257
<artifactId>maven-compiler-plugin</artifactId>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package org.codejive.jpm;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.io.PrintStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.io.TempDir;
14+
import picocli.CommandLine;
15+
16+
/** Integration tests for the Main class, focusing on the new 'do' command and aliases. */
17+
class MainIT {
18+
19+
@TempDir Path tempDir;
20+
21+
private String originalDir;
22+
23+
@BeforeEach
24+
void setUp() {
25+
originalDir = System.getProperty("user.dir");
26+
System.setProperty("user.dir", tempDir.toString());
27+
System.setProperty("picocli.ansi", "false");
28+
}
29+
30+
@AfterEach
31+
void tearDown() {
32+
System.setProperty("user.dir", originalDir);
33+
}
34+
35+
/**
36+
* Helper method to capture stdout/stderr for tests that need to check command output. Only use
37+
* this for tests that check jpm's own output, not for tests that execute system commands.
38+
*/
39+
private TestOutputCapture captureOutput() {
40+
PrintStream originalOut = System.out;
41+
PrintStream originalErr = System.err;
42+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
43+
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
44+
System.setOut(new PrintStream(outContent));
45+
System.setErr(new PrintStream(errContent));
46+
47+
return new TestOutputCapture(originalOut, originalErr, outContent, errContent);
48+
}
49+
50+
/** Helper class to manage output capture and restoration */
51+
private static class TestOutputCapture implements AutoCloseable {
52+
private final PrintStream originalOut;
53+
private final PrintStream originalErr;
54+
private final ByteArrayOutputStream outContent;
55+
private final ByteArrayOutputStream errContent;
56+
57+
TestOutputCapture(
58+
PrintStream originalOut,
59+
PrintStream originalErr,
60+
ByteArrayOutputStream outContent,
61+
ByteArrayOutputStream errContent) {
62+
this.originalOut = originalOut;
63+
this.originalErr = originalErr;
64+
this.outContent = outContent;
65+
this.errContent = errContent;
66+
}
67+
68+
String getOut() {
69+
return outContent.toString();
70+
}
71+
72+
String getErr() {
73+
return errContent.toString();
74+
}
75+
76+
@Override
77+
public void close() {
78+
System.setOut(originalOut);
79+
System.setErr(originalErr);
80+
}
81+
}
82+
83+
@Test
84+
void testDoAliasWithArgs() throws IOException {
85+
createAppYmlWithRepositories();
86+
try (TestOutputCapture capture = captureOutput()) {
87+
CommandLine cmd = Main.getCommandLine();
88+
int exitCode = cmd.execute("build", "--foo", "bar");
89+
90+
assertThat(exitCode).isEqualTo(0);
91+
String output = capture.getOut();
92+
// The run action should execute and include the classpath in the output
93+
assertThat(output).contains("javac", "jfiglet", "--foo bar");
94+
}
95+
}
96+
97+
@Test
98+
void testCopyCommandWithRepositoryOptions() throws IOException {
99+
// Test copy command with --repo options
100+
CommandLine cmd = Main.getCommandLine();
101+
int exitCode =
102+
cmd.execute(
103+
"copy",
104+
"--repo",
105+
"central=https://repo1.maven.org/maven2",
106+
"--repo",
107+
"https://jcenter.bintray.com",
108+
"com.google.guava:guava:31.1-jre");
109+
110+
// The command should execute successfully (even if dependency resolution might fail)
111+
assertThat(exitCode >= 0).isTrue();
112+
}
113+
114+
@Test
115+
void testInstallCommandWithRepositoryOptions() throws IOException {
116+
CommandLine cmd = Main.getCommandLine();
117+
int exitCode =
118+
cmd.execute(
119+
"install",
120+
"--repo",
121+
"central=https://repo1.maven.org/maven2",
122+
"com.google.guava:guava:31.1-jre");
123+
124+
// The command should execute successfully (even if dependency resolution might fail)
125+
assertThat(exitCode >= 0).isTrue();
126+
}
127+
128+
@Test
129+
void testPathCommandWithRepositoryOptionsAndAppYml() throws IOException {
130+
// Create app.yml with repositories
131+
createAppYmlWithRepositories();
132+
133+
try (TestOutputCapture capture = captureOutput()) {
134+
CommandLine cmd = Main.getCommandLine();
135+
int exitCode =
136+
cmd.execute(
137+
"path",
138+
"--repo",
139+
"jcenter=https://jcenter.bintray.com",
140+
"com.google.guava:guava:31.1-jre");
141+
142+
// The command should execute (even if dependency resolution might fail)
143+
assertThat(exitCode >= 0).isTrue();
144+
}
145+
}
146+
147+
private void createAppYmlWithRepositories() throws IOException {
148+
String yamlContent =
149+
"dependencies:\n"
150+
+ " com.github.lalyos:jfiglet: \"0.0.9\"\n"
151+
+ "\n"
152+
+ "repositories:\n"
153+
+ " central: \"https://repo1.maven.org/maven2\"\n"
154+
+ " custom: \"https://my.custom.repo/maven2\"\n"
155+
+ "\n"
156+
+ "actions:\n"
157+
+ " build: \"echo javac -cp {{deps}} *.java\"\n"
158+
+ " test: \"echo java -cp {{deps}} TestRunner\"\n";
159+
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
160+
}
161+
}

src/test/java/org/codejive/jpm/MainIntegrationTest.java renamed to src/test/java/org/codejive/jpm/MainTest.java

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codejive.jpm;
22

3-
import static org.assertj.core.api.Assertions.*;
3+
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.io.ByteArrayOutputStream;
66
import java.io.IOException;
@@ -14,7 +14,7 @@
1414
import picocli.CommandLine;
1515

1616
/** Integration tests for the Main class, focusing on the new 'do' command and aliases. */
17-
class MainIntegrationTest {
17+
class MainTest {
1818

1919
@TempDir Path tempDir;
2020

@@ -24,6 +24,7 @@ class MainIntegrationTest {
2424
void setUp() {
2525
originalDir = System.getProperty("user.dir");
2626
System.setProperty("user.dir", tempDir.toString());
27+
System.setProperty("picocli.ansi", "false");
2728
}
2829

2930
@AfterEach
@@ -235,14 +236,16 @@ void testDoWithOutput() throws IOException {
235236

236237
@Test
237238
void testMainWithNoArgs() {
238-
// Test the default behavior using CommandLine
239-
CommandLine cmd = Main.getCommandLine();
240-
int exitCode = cmd.execute();
241-
242-
// Should show help when no args provided (CommandLine default behavior)
243-
// The Main.main() method redirects to interactive search, but CommandLine.execute()
244-
// with no args typically shows help
245-
assertThat(exitCode >= 0).isTrue(); // Should not be negative (internal error)
239+
try (TestOutputCapture capture = captureOutput()) {
240+
// Test the default behavior using CommandLine
241+
CommandLine cmd = Main.getCommandLine();
242+
int exitCode = cmd.execute();
243+
assertThat(exitCode >= 0).isTrue(); // Should not be negative (internal error)
244+
assertThat(capture.getErr()).contains("Missing required subcommand");
245+
assertThat(capture.getErr()).contains("Usage: jpm [-hV] [COMMAND]");
246+
assertThat(capture.getErr())
247+
.contains("Simple command line tool for managing Maven artifacts");
248+
}
246249
}
247250

248251
@Test
@@ -259,97 +262,32 @@ void testDoAliasWithArgs() throws IOException {
259262
}
260263
}
261264

262-
@Test
263-
void testCopyCommandWithRepositoryOptions() throws IOException {
264-
// Test copy command with --repo options
265-
CommandLine cmd = Main.getCommandLine();
266-
int exitCode =
267-
cmd.execute(
268-
"copy",
269-
"--repo",
270-
"central=https://repo1.maven.org/maven2",
271-
"--repo",
272-
"https://jcenter.bintray.com",
273-
"com.google.guava:guava:31.1-jre");
274-
275-
// The command should execute successfully (even if dependency resolution might fail)
276-
assertThat(exitCode >= 0).isTrue();
277-
}
278-
279-
@Test
280-
void testInstallCommandWithRepositoryOptions() throws IOException {
281-
CommandLine cmd = Main.getCommandLine();
282-
int exitCode =
283-
cmd.execute(
284-
"install",
285-
"--repo",
286-
"central=https://repo1.maven.org/maven2",
287-
"com.google.guava:guava:31.1-jre");
288-
289-
// The command should execute successfully (even if dependency resolution might fail)
290-
assertThat(exitCode >= 0).isTrue();
291-
}
292-
293-
@Test
294-
void testPathCommandWithRepositoryOptionsAndAppYml() throws IOException {
295-
// Create app.yml with repositories
296-
createAppYmlWithRepositories();
297-
298-
try (TestOutputCapture capture = captureOutput()) {
299-
CommandLine cmd = Main.getCommandLine();
300-
int exitCode =
301-
cmd.execute(
302-
"path",
303-
"--repo",
304-
"jcenter=https://jcenter.bintray.com",
305-
"com.google.guava:guava:31.1-jre");
306-
307-
// The command should execute (even if dependency resolution might fail)
308-
assertThat(exitCode >= 0).isTrue();
309-
}
310-
}
311-
312265
private void createAppYml() throws IOException {
313266
String yamlContent =
314267
"dependencies:\n"
315-
+ " com.github.lalyos:jfiglet: \"0.0.9\"\n"
268+
+ " fake:dummy: \"1.2.3\"\n"
316269
+ "\n"
317270
+ "actions:\n"
318-
+ " build: \"echo building... .{/}libs{:}{{deps}}\"\n"
319-
+ " test: \"echo testing... .{/}libs{:}{{deps}}\"\n"
320-
+ " run: \"echo running... .{/}libs{:}{{deps}}\"\n"
271+
+ " build: \"echo building... .{/}libs{:}ext\"\n"
272+
+ " test: \"echo testing... .{/}libs{:}ext\"\n"
273+
+ " run: \"echo running... .{/}libs{:}ext\"\n"
321274
+ " hello: \"echo Hello World\"\n";
322275
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
323276
}
324277

325278
private void createAppYmlWithoutActions() throws IOException {
326-
String yamlContent = "dependencies:\n" + " com.github.lalyos:jfiglet: \"0.0.9\"\n";
279+
String yamlContent = "dependencies:\n" + " fake:dummy: \"1.2.3\"\n";
327280
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
328281
}
329282

330283
private void createAppYmlWithoutBuildAction() throws IOException {
331284
String yamlContent =
332285
"dependencies:\n"
333-
+ " com.github.lalyos:jfiglet: \"0.0.9\"\n"
286+
+ " fake:dummy: \"1.2.3\"\n"
334287
+ "\n"
335288
+ "actions:\n"
336289
+ " test: \"java -cp {{deps}} TestRunner\"\n"
337290
+ " hello: \"echo Hello World\"\n";
338291
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
339292
}
340-
341-
private void createAppYmlWithRepositories() throws IOException {
342-
String yamlContent =
343-
"dependencies:\n"
344-
+ " com.github.lalyos:jfiglet: \"0.0.9\"\n"
345-
+ "\n"
346-
+ "repositories:\n"
347-
+ " central: \"https://repo1.maven.org/maven2\"\n"
348-
+ " custom: \"https://my.custom.repo/maven2\"\n"
349-
+ "\n"
350-
+ "actions:\n"
351-
+ " build: \"javac -cp {{deps}} *.java\"\n"
352-
+ " test: \"java -cp {{deps}} TestRunner\"\n";
353-
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
354-
}
355293
}

0 commit comments

Comments
 (0)