|
| 1 | +package org.codejive.jpm; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import org.codejive.jpm.config.AppInfo; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.io.TempDir; |
| 13 | + |
| 14 | +/** Tests for Jpm class, focusing on repository handling functionality. */ |
| 15 | +class JpmRepositoriesTest { |
| 16 | + |
| 17 | + @TempDir Path tempDir; |
| 18 | + |
| 19 | + @Test |
| 20 | + void testGetRepositoriesWithEmptyExtraRepos() throws IOException { |
| 21 | + // Create app.yml with repositories |
| 22 | + createAppYmlWithRepositories(); |
| 23 | + |
| 24 | + String originalDir = System.getProperty("user.dir"); |
| 25 | + System.setProperty("user.dir", tempDir.toString()); |
| 26 | + |
| 27 | + try { |
| 28 | + Jpm jpm = Jpm.builder().build(); |
| 29 | + AppInfo appInfo = AppInfo.read(); |
| 30 | + Map<String, String> extraRepos = new HashMap<>(); |
| 31 | + |
| 32 | + // Use reflection to access private method for testing |
| 33 | + Map<String, String> result = invokeGetRepositories(jpm, extraRepos, appInfo); |
| 34 | + |
| 35 | + // Should only contain app.yml repositories |
| 36 | + assertThat(result).hasSize(2); |
| 37 | + assertThat(result) |
| 38 | + .containsEntry("central", "https://repo1.maven.org/maven2") |
| 39 | + .containsEntry("custom", "https://my.custom.repo/maven2"); |
| 40 | + } finally { |
| 41 | + System.setProperty("user.dir", originalDir); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testGetRepositoriesWithOnlyExtraRepos() throws IOException { |
| 47 | + // Create app.yml without repositories |
| 48 | + createAppYmlWithoutRepositories(); |
| 49 | + |
| 50 | + String originalDir = System.getProperty("user.dir"); |
| 51 | + System.setProperty("user.dir", tempDir.toString()); |
| 52 | + |
| 53 | + try { |
| 54 | + Jpm jpm = Jpm.builder().build(); |
| 55 | + AppInfo appInfo = AppInfo.read(); |
| 56 | + Map<String, String> extraRepos = new HashMap<>(); |
| 57 | + extraRepos.put("jcenter", "https://jcenter.bintray.com"); |
| 58 | + extraRepos.put("sonatype", "https://oss.sonatype.org/content/repositories/snapshots"); |
| 59 | + |
| 60 | + Map<String, String> result = invokeGetRepositories(jpm, extraRepos, appInfo); |
| 61 | + |
| 62 | + // Should only contain extra repositories |
| 63 | + assertThat(result).hasSize(2); |
| 64 | + assertThat(result) |
| 65 | + .containsEntry("jcenter", "https://jcenter.bintray.com") |
| 66 | + .containsEntry( |
| 67 | + "sonatype", "https://oss.sonatype.org/content/repositories/snapshots"); |
| 68 | + } finally { |
| 69 | + System.setProperty("user.dir", originalDir); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testGetRepositoriesWithBothAppYmlAndExtraRepos() throws IOException { |
| 75 | + // Create app.yml with repositories |
| 76 | + createAppYmlWithRepositories(); |
| 77 | + |
| 78 | + String originalDir = System.getProperty("user.dir"); |
| 79 | + System.setProperty("user.dir", tempDir.toString()); |
| 80 | + |
| 81 | + try { |
| 82 | + Jpm jpm = Jpm.builder().build(); |
| 83 | + AppInfo appInfo = AppInfo.read(); |
| 84 | + Map<String, String> extraRepos = new HashMap<>(); |
| 85 | + extraRepos.put("jcenter", "https://jcenter.bintray.com"); |
| 86 | + extraRepos.put("sonatype", "https://oss.sonatype.org/content/repositories/snapshots"); |
| 87 | + |
| 88 | + Map<String, String> result = invokeGetRepositories(jpm, extraRepos, appInfo); |
| 89 | + |
| 90 | + // Should contain both app.yml and extra repositories |
| 91 | + assertThat(result).hasSize(4); |
| 92 | + assertThat(result) |
| 93 | + .containsEntry("central", "https://repo1.maven.org/maven2") |
| 94 | + .containsEntry("custom", "https://my.custom.repo/maven2") |
| 95 | + .containsEntry("jcenter", "https://jcenter.bintray.com") |
| 96 | + .containsEntry( |
| 97 | + "sonatype", "https://oss.sonatype.org/content/repositories/snapshots"); |
| 98 | + } finally { |
| 99 | + System.setProperty("user.dir", originalDir); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testGetRepositoriesWithOverlappingNames() throws IOException { |
| 105 | + // Create app.yml with repositories |
| 106 | + createAppYmlWithRepositories(); |
| 107 | + |
| 108 | + String originalDir = System.getProperty("user.dir"); |
| 109 | + System.setProperty("user.dir", tempDir.toString()); |
| 110 | + |
| 111 | + try { |
| 112 | + Jpm jpm = Jpm.builder().build(); |
| 113 | + AppInfo appInfo = AppInfo.read(); |
| 114 | + Map<String, String> extraRepos = new HashMap<>(); |
| 115 | + // Override central with a different URL and add a new one |
| 116 | + extraRepos.put("central", "https://repo.maven.apache.org/maven2"); // Different URL |
| 117 | + extraRepos.put("jcenter", "https://jcenter.bintray.com"); |
| 118 | + |
| 119 | + Map<String, String> result = invokeGetRepositories(jpm, extraRepos, appInfo); |
| 120 | + |
| 121 | + // Should contain 3 repositories, with extra repo overriding app.yml repo |
| 122 | + assertThat(result).hasSize(3); |
| 123 | + assertThat(result) |
| 124 | + .containsEntry( |
| 125 | + "central", |
| 126 | + "https://repo.maven.apache.org/maven2") // Should use extra repo value |
| 127 | + .containsEntry("custom", "https://my.custom.repo/maven2") // From app.yml |
| 128 | + .containsEntry("jcenter", "https://jcenter.bintray.com"); // From extra repos |
| 129 | + } finally { |
| 130 | + System.setProperty("user.dir", originalDir); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testGetRepositoriesWithEmptyAppYmlAndEmptyExtraRepos() throws IOException { |
| 136 | + // Create app.yml without repositories |
| 137 | + createAppYmlWithoutRepositories(); |
| 138 | + |
| 139 | + String originalDir = System.getProperty("user.dir"); |
| 140 | + System.setProperty("user.dir", tempDir.toString()); |
| 141 | + |
| 142 | + try { |
| 143 | + Jpm jpm = Jpm.builder().build(); |
| 144 | + AppInfo appInfo = AppInfo.read(); |
| 145 | + Map<String, String> extraRepos = new HashMap<>(); |
| 146 | + |
| 147 | + Map<String, String> result = invokeGetRepositories(jpm, extraRepos, appInfo); |
| 148 | + |
| 149 | + // Should be empty |
| 150 | + assertThat(result).isEmpty(); |
| 151 | + } finally { |
| 152 | + System.setProperty("user.dir", originalDir); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testGetRepositoriesWithNullExtraRepos() throws IOException { |
| 158 | + // Create app.yml with repositories |
| 159 | + createAppYmlWithRepositories(); |
| 160 | + |
| 161 | + String originalDir = System.getProperty("user.dir"); |
| 162 | + System.setProperty("user.dir", tempDir.toString()); |
| 163 | + |
| 164 | + try { |
| 165 | + Jpm jpm = Jpm.builder().build(); |
| 166 | + AppInfo appInfo = AppInfo.read(); |
| 167 | + |
| 168 | + // Test with null extraRepos (should handle gracefully) |
| 169 | + // The actual implementation passes an empty map instead of null, |
| 170 | + // but let's test the boundary case |
| 171 | + assertThatThrownBy(() -> invokeGetRepositories(jpm, null, appInfo)) |
| 172 | + .isInstanceOf(RuntimeException.class) |
| 173 | + .hasRootCauseInstanceOf(NullPointerException.class); |
| 174 | + } finally { |
| 175 | + System.setProperty("user.dir", originalDir); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void createAppYmlWithRepositories() throws IOException { |
| 180 | + Path appYmlPath = tempDir.resolve("app.yml"); |
| 181 | + String yamlContent = |
| 182 | + "dependencies:\n" |
| 183 | + + " com.example:test-lib: \"1.0.0\"\n" |
| 184 | + + "\n" |
| 185 | + + "repositories:\n" |
| 186 | + + " central: \"https://repo1.maven.org/maven2\"\n" |
| 187 | + + " custom: \"https://my.custom.repo/maven2\"\n"; |
| 188 | + Files.writeString(appYmlPath, yamlContent); |
| 189 | + } |
| 190 | + |
| 191 | + private void createAppYmlWithoutRepositories() throws IOException { |
| 192 | + Path appYmlPath = tempDir.resolve("app.yml"); |
| 193 | + String yamlContent = "dependencies:\n" + " com.example:test-lib: \"1.0.0\"\n"; |
| 194 | + Files.writeString(appYmlPath, yamlContent); |
| 195 | + } |
| 196 | + |
| 197 | + private Map<String, String> invokeGetRepositories( |
| 198 | + Jpm jpm, Map<String, String> extraRepos, AppInfo appInfo) { |
| 199 | + try { |
| 200 | + // Use reflection to access private method |
| 201 | + var method = Jpm.class.getDeclaredMethod("getRepositories", Map.class, AppInfo.class); |
| 202 | + method.setAccessible(true); |
| 203 | + return (Map<String, String>) method.invoke(jpm, extraRepos, appInfo); |
| 204 | + } catch (Exception e) { |
| 205 | + throw new RuntimeException("Failed to invoke getRepositories method", e); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments