|
| 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("repo1", "invalid-url") |
| 97 | + .containsEntry("repo2", "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(1); |
| 143 | + // When = is at the beginning, it's not treated as a name=value separator |
| 144 | + assertThat(result).containsEntry("repo1", "https://repo1.maven.org/maven2"); |
| 145 | + } |
| 146 | +} |
0 commit comments