Skip to content

Commit e85d18e

Browse files
committed
added tests
1 parent ee409f1 commit e85d18e

File tree

3 files changed

+363
-0
lines changed

3 files changed

+363
-0
lines changed

src/test/java/org/codejive/jpm/MainIntegrationTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,56 @@ void testDoAliasWithArgs() throws IOException {
259259
}
260260
}
261261

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+
262312
private void createAppYml() throws IOException {
263313
String yamlContent =
264314
"dependencies:\n"
@@ -287,4 +337,19 @@ private void createAppYmlWithoutBuildAction() throws IOException {
287337
+ " hello: \"echo Hello World\"\n";
288338
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
289339
}
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+
}
290355
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.codejive.jpm;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
import org.junit.jupiter.api.Test;
9+
10+
/** Tests for Main class CLI repository option parsing. */
11+
class MainRepositoryOptionsTest {
12+
13+
@Test
14+
void testGetRepositoryMapWithNamedRepositories() {
15+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
16+
mixin.repositories =
17+
List.of(
18+
"central=https://repo1.maven.org/maven2",
19+
"jcenter=https://jcenter.bintray.com",
20+
"custom=https://my.custom.repo/maven2");
21+
22+
Map<String, String> result = mixin.getRepositoryMap();
23+
24+
assertThat(result).hasSize(3);
25+
assertThat(result)
26+
.containsEntry("central", "https://repo1.maven.org/maven2")
27+
.containsEntry("jcenter", "https://jcenter.bintray.com")
28+
.containsEntry("custom", "https://my.custom.repo/maven2");
29+
}
30+
31+
@Test
32+
void testGetRepositoryMapWithUnnamedRepositories() {
33+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
34+
mixin.repositories =
35+
List.of(
36+
"https://repo1.maven.org/maven2",
37+
"https://jcenter.bintray.com",
38+
"https://my.custom.repo/maven2");
39+
40+
Map<String, String> result = mixin.getRepositoryMap();
41+
42+
assertThat(result).hasSize(3);
43+
// For unnamed repos, the hostname should be used as the name
44+
assertThat(result)
45+
.containsEntry("repo1.maven.org", "https://repo1.maven.org/maven2")
46+
.containsEntry("jcenter.bintray.com", "https://jcenter.bintray.com")
47+
.containsEntry("my.custom.repo", "https://my.custom.repo/maven2");
48+
}
49+
50+
@Test
51+
void testGetRepositoryMapWithMixedRepositories() {
52+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
53+
mixin.repositories =
54+
List.of(
55+
"central=https://repo1.maven.org/maven2",
56+
"https://jcenter.bintray.com",
57+
"custom=https://my.custom.repo/maven2",
58+
"https://oss.sonatype.org/content/repositories/snapshots");
59+
60+
Map<String, String> result = mixin.getRepositoryMap();
61+
62+
assertThat(result).hasSize(4);
63+
assertThat(result)
64+
.containsEntry("central", "https://repo1.maven.org/maven2")
65+
.containsEntry("jcenter.bintray.com", "https://jcenter.bintray.com")
66+
.containsEntry("custom", "https://my.custom.repo/maven2")
67+
.containsEntry(
68+
"oss.sonatype.org",
69+
"https://oss.sonatype.org/content/repositories/snapshots");
70+
}
71+
72+
@Test
73+
void testGetRepositoryMapWithEmptyList() {
74+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
75+
mixin.repositories = new ArrayList<>();
76+
77+
Map<String, String> result = mixin.getRepositoryMap();
78+
79+
assertThat(result).isEmpty();
80+
}
81+
82+
@Test
83+
void testGetRepositoryMapWithInvalidUrls() {
84+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
85+
mixin.repositories =
86+
List.of(
87+
"invalid-url",
88+
"also-invalid",
89+
"file:///local/path"); // Valid URL but not HTTP
90+
91+
Map<String, String> result = mixin.getRepositoryMap();
92+
93+
assertThat(result).hasSize(3);
94+
// For invalid URLs, the entire string should be used as both name and URL
95+
assertThat(result)
96+
.containsEntry("invalid-url", "invalid-url")
97+
.containsEntry("also-invalid", "also-invalid");
98+
// For file:// URLs, getHost() returns null/empty, so name becomes empty
99+
assertThat(result).containsEntry("", "file:///local/path");
100+
}
101+
102+
@Test
103+
void testGetRepositoryMapWithEqualsInUrl() {
104+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
105+
mixin.repositories =
106+
List.of(
107+
"nexus=https://nexus.example.com/repository/maven-public/?foo=bar",
108+
"https://repo.example.com/path?param=value&other=test");
109+
110+
Map<String, String> result = mixin.getRepositoryMap();
111+
112+
assertThat(result).hasSize(2);
113+
assertThat(result)
114+
.containsEntry(
115+
"nexus", "https://nexus.example.com/repository/maven-public/?foo=bar");
116+
// The second URL has = in it, so it gets split at the first =
117+
assertThat(result).containsEntry("https://repo.example.com/path?param", "value&other=test");
118+
}
119+
120+
@Test
121+
void testGetRepositoryMapWithDuplicateNames() {
122+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
123+
mixin.repositories =
124+
List.of(
125+
"central=https://repo1.maven.org/maven2",
126+
"central=https://repo.maven.apache.org/maven2"); // Duplicate name
127+
128+
Map<String, String> result = mixin.getRepositoryMap();
129+
130+
assertThat(result).hasSize(1);
131+
// The last one should win
132+
assertThat(result).containsEntry("central", "https://repo.maven.apache.org/maven2");
133+
}
134+
135+
@Test
136+
void testGetRepositoryMapWithEmptyNameOrUrl() {
137+
Main.BaseArtifactsMixin mixin = new Main.BaseArtifactsMixin();
138+
mixin.repositories = List.of("=https://repo1.maven.org/maven2", "name=", "=");
139+
140+
Map<String, String> result = mixin.getRepositoryMap();
141+
142+
assertThat(result).hasSize(3);
143+
// When = is at the beginning, it's not treated as a name=value separator
144+
assertThat(result)
145+
.containsEntry("=https://repo1.maven.org/maven2", "=https://repo1.maven.org/maven2")
146+
.containsEntry("name", "")
147+
.containsEntry("=", "=");
148+
}
149+
}

src/test/java/org/codejive/jpm/config/AppInfoTest.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,153 @@ void testAppInfoWithComplexActions() throws IOException {
159159
System.setProperty("user.dir", originalDir);
160160
}
161161
}
162+
163+
@Test
164+
void testReadAppInfoWithRepositories() throws IOException {
165+
// Create a test app.yml file with repositories
166+
Path appYmlPath = tempDir.resolve("app.yml");
167+
String yamlContent =
168+
"dependencies:\n"
169+
+ " com.example:test-lib: \"1.0.0\"\n"
170+
+ "\n"
171+
+ "repositories:\n"
172+
+ " central: \"https://repo1.maven.org/maven2\"\n"
173+
+ " jcenter: \"https://jcenter.bintray.com\"\n"
174+
+ " custom: \"https://my.custom.repo/maven2\"\n";
175+
Files.writeString(appYmlPath, yamlContent);
176+
177+
String originalDir = System.getProperty("user.dir");
178+
System.setProperty("user.dir", tempDir.toString());
179+
180+
try {
181+
AppInfo appInfo = AppInfo.read();
182+
183+
// Test repository retrieval
184+
assertThat(appInfo.repositories).hasSize(3);
185+
assertThat(appInfo.repositories)
186+
.containsEntry("central", "https://repo1.maven.org/maven2")
187+
.containsEntry("jcenter", "https://jcenter.bintray.com")
188+
.containsEntry("custom", "https://my.custom.repo/maven2");
189+
190+
// Test dependencies are still parsed correctly
191+
assertThat(appInfo.dependencies).hasSize(1);
192+
assertThat(appInfo.dependencies).containsEntry("com.example:test-lib", "1.0.0");
193+
} finally {
194+
System.setProperty("user.dir", originalDir);
195+
}
196+
}
197+
198+
@Test
199+
void testReadAppInfoWithoutRepositories() throws IOException {
200+
// Create a test app.yml file without repositories
201+
Path appYmlPath = tempDir.resolve("app.yml");
202+
String yamlContent = "dependencies:\n" + " com.example:test-lib: \"1.0.0\"\n";
203+
Files.writeString(appYmlPath, yamlContent);
204+
205+
String originalDir = System.getProperty("user.dir");
206+
System.setProperty("user.dir", tempDir.toString());
207+
208+
try {
209+
AppInfo appInfo = AppInfo.read();
210+
211+
// Test no repositories
212+
assertThat(appInfo.repositories).isEmpty();
213+
214+
// Test dependencies are still parsed correctly
215+
assertThat(appInfo.dependencies).hasSize(1);
216+
} finally {
217+
System.setProperty("user.dir", originalDir);
218+
}
219+
}
220+
221+
@Test
222+
void testWriteAppInfoWithRepositories() throws IOException {
223+
AppInfo appInfo = new AppInfo();
224+
appInfo.dependencies.put("com.example:test-lib", "1.0.0");
225+
appInfo.repositories.put("central", "https://repo1.maven.org/maven2");
226+
appInfo.repositories.put("custom", "https://my.custom.repo/maven2");
227+
228+
String originalDir = System.getProperty("user.dir");
229+
System.setProperty("user.dir", tempDir.toString());
230+
231+
try {
232+
AppInfo.write(appInfo);
233+
234+
// Verify the file was written
235+
Path appYmlPath = tempDir.resolve("app.yml");
236+
assertThat(appYmlPath).exists();
237+
238+
// Read it back and verify
239+
AppInfo readBack = AppInfo.read();
240+
assertThat(readBack.repositories).hasSize(2);
241+
assertThat(readBack.repositories)
242+
.containsEntry("central", "https://repo1.maven.org/maven2")
243+
.containsEntry("custom", "https://my.custom.repo/maven2");
244+
assertThat(readBack.dependencies).hasSize(1);
245+
} finally {
246+
System.setProperty("user.dir", originalDir);
247+
}
248+
}
249+
250+
@Test
251+
void testWriteAppInfoWithoutRepositories() throws IOException {
252+
AppInfo appInfo = new AppInfo();
253+
appInfo.dependencies.put("com.example:test-lib", "1.0.0");
254+
// No repositories added
255+
256+
String originalDir = System.getProperty("user.dir");
257+
System.setProperty("user.dir", tempDir.toString());
258+
259+
try {
260+
AppInfo.write(appInfo);
261+
262+
// Read it back and verify repositories section is not present
263+
AppInfo readBack = AppInfo.read();
264+
assertThat(readBack.repositories).isEmpty();
265+
assertThat(readBack.dependencies).hasSize(1);
266+
267+
// Also verify the YAML content doesn't contain repositories section
268+
String content = Files.readString(tempDir.resolve("app.yml"));
269+
assertThat(content).doesNotContain("repositories:");
270+
} finally {
271+
System.setProperty("user.dir", originalDir);
272+
}
273+
}
274+
275+
@Test
276+
void testAppInfoWithComplexRepositoriesAndActions() throws IOException {
277+
Path appYmlPath = tempDir.resolve("app.yml");
278+
String yamlContent =
279+
"dependencies:\n"
280+
+ " com.example:test-lib: \"1.0.0\"\n"
281+
+ "\n"
282+
+ "repositories:\n"
283+
+ " central: \"https://repo1.maven.org/maven2\"\n"
284+
+ " sonatype-snapshots: \"https://oss.sonatype.org/content/repositories/snapshots\"\n"
285+
+ "\n"
286+
+ "actions:\n"
287+
+ " build: \"javac -cp {{deps}} *.java\"\n";
288+
Files.writeString(appYmlPath, yamlContent);
289+
290+
String originalDir = System.getProperty("user.dir");
291+
System.setProperty("user.dir", tempDir.toString());
292+
293+
try {
294+
AppInfo appInfo = AppInfo.read();
295+
296+
// Test all sections are parsed correctly
297+
assertThat(appInfo.dependencies).hasSize(1);
298+
assertThat(appInfo.repositories).hasSize(2);
299+
assertThat(appInfo.getActionNames()).hasSize(1);
300+
301+
assertThat(appInfo.repositories)
302+
.containsEntry("central", "https://repo1.maven.org/maven2")
303+
.containsEntry(
304+
"sonatype-snapshots",
305+
"https://oss.sonatype.org/content/repositories/snapshots");
306+
assertThat(appInfo.getAction("build")).isEqualTo("javac -cp {{deps}} *.java");
307+
} finally {
308+
System.setProperty("user.dir", originalDir);
309+
}
310+
}
162311
}

0 commit comments

Comments
 (0)