|
29 | 29 | import org.openrewrite.maven.tree.GroupArtifact; |
30 | 30 | import org.openrewrite.maven.tree.GroupArtifactVersion; |
31 | 31 | import org.openrewrite.maven.tree.ResolvedDependency; |
| 32 | +import org.openrewrite.properties.tree.Properties; |
32 | 33 | import org.openrewrite.test.RecipeSpec; |
33 | 34 | import org.openrewrite.test.RewriteTest; |
34 | 35 | import org.openrewrite.test.SourceSpec; |
|
43 | 44 | import static org.openrewrite.gradle.Assertions.settingsGradle; |
44 | 45 | import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; |
45 | 46 | import static org.openrewrite.java.Assertions.mavenProject; |
| 47 | +import static org.openrewrite.properties.Assertions.properties; |
46 | 48 |
|
47 | 49 | class GradleProjectTest implements RewriteTest { |
48 | 50 |
|
@@ -76,6 +78,184 @@ void noopUpgrade() { |
76 | 78 | ); |
77 | 79 | } |
78 | 80 |
|
| 81 | + @Test |
| 82 | + void repositoryWithCredentials() { |
| 83 | + rewriteRun( |
| 84 | + spec -> spec.recipe(new UpgradeDependencyInMarker( |
| 85 | + new GroupArtifactVersion("org.openrewrite", "rewrite-java", "8.56.0"), |
| 86 | + "implementation", |
| 87 | + (original, updated) -> assertThat(updated) |
| 88 | + .isSameAs(original) |
| 89 | + .satisfies(gp -> assertThat(gp.getMavenRepositories()) |
| 90 | + .singleElement() |
| 91 | + .satisfies(repo -> { |
| 92 | + assertThat(repo.getUri()).isEqualTo("https://example.com/maven2"); |
| 93 | + assertThat(repo.getUsername()).isEqualTo("dummyuser"); |
| 94 | + assertThat(repo.getPassword()).isEqualTo("dummypass"); |
| 95 | + })) |
| 96 | + )), |
| 97 | + properties( |
| 98 | + """ |
| 99 | + mavenUsername=dummyuser |
| 100 | + mavenPassword=dummypass |
| 101 | + """, |
| 102 | + spec -> spec.path("gradle.properties") |
| 103 | + ), |
| 104 | + buildGradle( |
| 105 | + """ |
| 106 | + plugins { |
| 107 | + id("java") |
| 108 | + } |
| 109 | + repositories { |
| 110 | + maven { |
| 111 | + url = "https://example.com/maven2" |
| 112 | + credentials { |
| 113 | + username = findProperty("mavenUsername") |
| 114 | + password = findProperty("mavenPassword") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + dependencies { |
| 119 | + implementation("org.openrewrite:rewrite-java:8.56.0") |
| 120 | + } |
| 121 | + """ |
| 122 | + ) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void repositoryWithPreemptiveCredentials() { |
| 128 | + rewriteRun( |
| 129 | + spec -> spec.recipe(new UpgradeDependencyInMarker( |
| 130 | + new GroupArtifactVersion("org.openrewrite", "rewrite-java", "8.56.0"), |
| 131 | + "implementation", |
| 132 | + (original, updated) -> assertThat(updated) |
| 133 | + .isSameAs(original) |
| 134 | + .satisfies(gp -> assertThat(gp.getMavenRepositories()) |
| 135 | + .singleElement() |
| 136 | + .satisfies(repo -> { |
| 137 | + assertThat(repo.getUri()).isEqualTo("https://example.com/maven2"); |
| 138 | + assertThat(repo.getUsername()).isEqualTo("dummyuser"); |
| 139 | + assertThat(repo.getPassword()).isEqualTo("dummypass"); |
| 140 | + })) |
| 141 | + )), |
| 142 | + properties( |
| 143 | + """ |
| 144 | + mavenUsername=dummyuser |
| 145 | + mavenPassword=dummypass |
| 146 | + """, |
| 147 | + spec -> spec.path("gradle.properties") |
| 148 | + ), |
| 149 | + buildGradle( |
| 150 | + """ |
| 151 | + plugins { |
| 152 | + id("java") |
| 153 | + } |
| 154 | + repositories { |
| 155 | + maven { |
| 156 | + url = "https://example.com/maven2" |
| 157 | + credentials { |
| 158 | + username = findProperty("mavenUsername") |
| 159 | + password = findProperty("mavenPassword") |
| 160 | + authentication { |
| 161 | + basic(BasicAuthentication) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + dependencies { |
| 167 | + implementation("org.openrewrite:rewrite-java:8.56.0") |
| 168 | + } |
| 169 | + """ |
| 170 | + ) |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void repositoryWithPasswordCredentials() { |
| 176 | + rewriteRun( |
| 177 | + spec -> spec.recipe(new UpgradeDependencyInMarker( |
| 178 | + new GroupArtifactVersion("org.openrewrite", "rewrite-java", "8.56.0"), |
| 179 | + "implementation", |
| 180 | + (original, updated) -> assertThat(updated) |
| 181 | + .isSameAs(original) |
| 182 | + .satisfies(gp -> assertThat(gp.getMavenRepositories()) |
| 183 | + .singleElement() |
| 184 | + .satisfies(repo -> { |
| 185 | + assertThat(repo.getUri()).isEqualTo("https://example.com/maven2"); |
| 186 | + assertThat(repo.getUsername()).isEqualTo("dummyuser"); |
| 187 | + assertThat(repo.getPassword()).isEqualTo("dummypass"); |
| 188 | + })) |
| 189 | + )), |
| 190 | + properties( |
| 191 | + """ |
| 192 | + mySecureRepositoryUsername=dummyuser |
| 193 | + mySecureRepositoryPassword=dummypass |
| 194 | + """, |
| 195 | + spec -> spec.path("gradle.properties") |
| 196 | + ), |
| 197 | + buildGradle( |
| 198 | + """ |
| 199 | + plugins { |
| 200 | + id("java") |
| 201 | + } |
| 202 | + repositories { |
| 203 | + maven { |
| 204 | + name = "mySecureRepository" |
| 205 | + url = "https://example.com/maven2" |
| 206 | + credentials(PasswordCredentials) |
| 207 | + } |
| 208 | + } |
| 209 | + dependencies { |
| 210 | + implementation("org.openrewrite:rewrite-java:8.56.0") |
| 211 | + } |
| 212 | + """ |
| 213 | + ) |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + void repositoryWithHttpHeaderCredentials() { |
| 219 | + rewriteRun( |
| 220 | + spec -> spec.recipe(new UpgradeDependencyInMarker( |
| 221 | + new GroupArtifactVersion("org.openrewrite", "rewrite-java", "8.56.0"), |
| 222 | + "implementation", |
| 223 | + (original, updated) -> assertThat(updated) |
| 224 | + .isSameAs(original) |
| 225 | + .satisfies(gp -> assertThat(gp.getMavenRepositories()) |
| 226 | + .singleElement() |
| 227 | + .satisfies(repo -> { |
| 228 | + assertThat(repo.getUri()).isEqualTo("https://example.com/maven2"); |
| 229 | + assertThat(repo.getUsername()).isNull(); |
| 230 | + assertThat(repo.getPassword()).isNull(); |
| 231 | + })) |
| 232 | + )), |
| 233 | + buildGradle( |
| 234 | + """ |
| 235 | + plugins { |
| 236 | + id("java") |
| 237 | + } |
| 238 | + repositories { |
| 239 | + maven { |
| 240 | + name = "mySecureRepository" |
| 241 | + url = "https://example.com/maven2" |
| 242 | + credentials(HttpHeaderCredentials) { |
| 243 | + name = "Private-Token" |
| 244 | + value = "TOKEN" |
| 245 | + } |
| 246 | + authentication { |
| 247 | + header(HttpHeaderAuthentication) |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + dependencies { |
| 252 | + implementation("org.openrewrite:rewrite-java:8.56.0") |
| 253 | + } |
| 254 | + """ |
| 255 | + ) |
| 256 | + ); |
| 257 | + } |
| 258 | + |
79 | 259 | @Test |
80 | 260 | void multiProject() { |
81 | 261 | rewriteRun( |
@@ -366,7 +546,7 @@ void changeConstraint() { |
366 | 546 |
|
367 | 547 | GradleDependencyConstraint jacksonConstraint = constraints.stream() |
368 | 548 | .filter(c -> "com.fasterxml.jackson.core".equals(c.getGroupId()) && |
369 | | - "jackson-databind".equals(c.getArtifactId())) |
| 549 | + "jackson-databind".equals(c.getArtifactId())) |
370 | 550 | .findFirst() |
371 | 551 | .orElse(null); |
372 | 552 |
|
@@ -429,6 +609,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() { |
429 | 609 | @Override |
430 | 610 | @SneakyThrows |
431 | 611 | public Tree visit(Tree tree, ExecutionContext ctx) { |
| 612 | + if (tree instanceof Properties.File) { |
| 613 | + // Skip gradle.properties files |
| 614 | + return tree; |
| 615 | + } |
432 | 616 | GradleProject original = tree.getMarkers().findFirst(GradleProject.class).orElseThrow(() -> fail("Missing GradleProject")); |
433 | 617 | GradleProject updated = original.upgradeDirectDependencyVersion(configuration, newGav, ctx); |
434 | 618 | testAssertion.accept(original, updated); |
|
0 commit comments