diff --git a/rewrite-core/src/main/java/org/openrewrite/remote/Remote.java b/rewrite-core/src/main/java/org/openrewrite/remote/Remote.java index f405714a134..52c708b40e8 100644 --- a/rewrite-core/src/main/java/org/openrewrite/remote/Remote.java +++ b/rewrite-core/src/main/java/org/openrewrite/remote/Remote.java @@ -105,6 +105,20 @@ static Builder builder(UUID id, Path sourcePath, Markers markers, URI uri) { return new Builder(id, sourcePath, markers, uri); } + @Override + default

TreeVisitor> printer(Cursor cursor) { + return new TreeVisitor>() { + @Override + public Tree visitSourceFile(SourceFile sourceFile, PrintOutputCapture

p) { + ExecutionContext ctx = p.getContext() instanceof ExecutionContext ? (ExecutionContext) p.getContext() : + new InMemoryExecutionContext(); + HttpSender sender = HttpSenderExecutionContextView.view(ctx).getHttpSender(); + p.out.append(StringUtils.readFully(getInputStream(sender), StandardCharsets.UTF_8)); + return sourceFile; + } + }; + } + class Builder { protected final UUID id; protected final Path sourcePath; diff --git a/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java b/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java index 3208734e82a..b72f6fc2e26 100644 --- a/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java +++ b/rewrite-core/src/main/java/org/openrewrite/remote/RemoteArchive.java @@ -18,9 +18,9 @@ import lombok.EqualsAndHashCode; import lombok.Value; import lombok.With; +import org.apache.tools.ant.taskdefs.Exec; import org.intellij.lang.annotations.Language; -import org.openrewrite.FileAttributes; -import org.openrewrite.PathUtils; +import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.ipc.http.HttpSender; import org.openrewrite.marker.Markers; diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddGradleWrapper.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddGradleWrapper.java index bdd4f77ce16..70ec1ccad5f 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddGradleWrapper.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/AddGradleWrapper.java @@ -90,7 +90,7 @@ public AddGradleWrapper(String version, String distribution) { @Override protected TreeVisitor getApplicableTest() { - return new HasSourcePath<>("regex", ".+\\.gradle(\\.kts)?$"); + return new IsBuildGradle<>(); } @Override diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java index 83a5d78026c..245d80d8cc5 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/Assertions.java @@ -40,7 +40,7 @@ public static SourceSpecs buildGradle(@Language("groovy") @Nullable String befor } public static SourceSpecs buildGradle(@Language("groovy") @Nullable String before, Consumer> spec) { - SourceSpec gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, null); + SourceSpec gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, (String) null); gradle.path(Paths.get("build.gradle")); spec.accept(gradle); return gradle; @@ -65,7 +65,7 @@ public static SourceSpecs settingsGradle(@Language("groovy") @Nullable String be } public static SourceSpecs settingsGradle(@Language("groovy") @Nullable String before, Consumer> spec) { - SourceSpec gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, null); + SourceSpec gradle = new SourceSpec<>(G.CompilationUnit.class, "gradle", gradleParser, before, (String) null); gradle.path(Paths.get("settings.gradle")); spec.accept(gradle); return gradle; diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsBuildGradle.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsBuildGradle.java index 56bae9598f3..363b245a1af 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsBuildGradle.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsBuildGradle.java @@ -22,8 +22,10 @@ public class IsBuildGradle

extends JavaIsoVisitor

{ @Override public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, P p) { - if (cu.getSourcePath().toString().endsWith(".gradle") && - !cu.getSourcePath().toString().endsWith("settings.gradle")) { + if ((cu.getSourcePath().toString().endsWith(".gradle") || + cu.getSourcePath().toString().endsWith(".gradle.kts")) && + !(cu.getSourcePath().toString().endsWith("settings.gradle") || + cu.getSourcePath().toString().endsWith("settings.gradle.kts"))) { return SearchResult.found(cu); } return super.visitJavaSourceFile(cu, p); diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsSettingsGradle.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsSettingsGradle.java index 671b93e6a6a..21c86d209f9 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsSettingsGradle.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/IsSettingsGradle.java @@ -19,12 +19,12 @@ import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.marker.SearchResult; -import java.nio.file.Paths; - public class IsSettingsGradle

extends JavaIsoVisitor

{ + @Override public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, P p) { - if (cu.getSourcePath().toString().endsWith("settings.gradle")) { + if (cu.getSourcePath().toString().endsWith("settings.gradle") || + cu.getSourcePath().toString().endsWith("settings.gradle.kts")) { return SearchResult.found(cu); } return super.visitJavaSourceFile(cu, p); diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java index 947732c542e..57b9adb1431 100755 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java @@ -33,9 +33,11 @@ import org.openrewrite.quark.Quark; import org.openrewrite.text.PlainText; +import java.nio.charset.StandardCharsets; import java.time.ZonedDateTime; import java.util.List; +import static java.util.Objects.requireNonNull; import static org.openrewrite.PathUtils.equalIgnoringSeparators; import static org.openrewrite.gradle.util.GradleWrapper.*; @@ -102,7 +104,7 @@ public Properties visitEntry(Properties.Entry entry, ExecutionContext context) { return entry; } - GradleWrapper gradleWrapper = validate(context).getValue(); + GradleWrapper gradleWrapper = requireNonNull(validate(context).getValue()); // Typical example: https://services.gradle.org/distributions/gradle-7.4-all.zip String currentDistributionUrl = entry.getValue().getText(); @@ -122,7 +124,8 @@ protected List visit(List before, ExecutionContext ctx) List sourceFileList = ListUtils.map(before, sourceFile -> { if (sourceFile instanceof PlainText && equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_SCRIPT_LOCATION)) { PlainText gradlew = (PlainText) setExecutable(sourceFile); - String gradlewText = StringUtils.readFully(UpdateGradleWrapper.class.getResourceAsStream("/gradlew"), sourceFile.getCharset()); + String gradlewText = StringUtils.readFully(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew")), + sourceFile.getCharset() == null ? StandardCharsets.UTF_8 : sourceFile.getCharset()); if (!gradlewText.equals(gradlew.getText())) { gradlew = gradlew.withText(gradlewText); } @@ -130,7 +133,8 @@ protected List visit(List before, ExecutionContext ctx) } if (sourceFile instanceof PlainText && equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_BATCH_LOCATION)) { PlainText gradlewBat = (PlainText) setExecutable(sourceFile); - String gradlewBatText = StringUtils.readFully(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat"), sourceFile.getCharset()); + String gradlewBatText = StringUtils.readFully(requireNonNull(UpdateGradleWrapper.class.getResourceAsStream("/gradlew.bat")), + sourceFile.getCharset() == null ? StandardCharsets.UTF_8 : sourceFile.getCharset()); if (!gradlewBatText.equals(gradlewBat.getText())) { gradlewBat = gradlewBat.withText(gradlewBatText); } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ActivateStyleTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ActivateStyleTest.java new file mode 100644 index 00000000000..bfbb3f5aabc --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ActivateStyleTest.java @@ -0,0 +1,178 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; + +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.properties.Assertions.properties; +import static org.openrewrite.test.SourceSpecs.other; + +class ActivateStyleTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", true)) + .typeValidationOptions(TypeValidation.none()); + } + + @Test + void addToRewriteDsl() { + rewriteRun( + buildGradle( + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + } + """, + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + activeStyle("org.openrewrite.java.IntelliJ") + } + """ + ) + ); + } + + @Test + void addToRewriteDslExistingStyle() { + rewriteRun( + spec -> spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", false)), + buildGradle( + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + activeStyle("otherStyle") + } + """, + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + activeStyle("otherStyle", "org.openrewrite.java.IntelliJ") + } + """ + ) + ); + } + + @Test + void addToRewriteDslOverwriteStyle() { + rewriteRun( + buildGradle( + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + activeStyle("com.your.Style") + } + """, + """ + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + rewrite { + activeRecipe("org.openrewrite.java.format.AutoFormat") + activeStyle("org.openrewrite.java.IntelliJ") + } + """ + ) + ); + } + + @Test + void addToProperties() { + rewriteRun( + other("", spec -> spec.path(".gradle.kts")), + properties( + """ + org.gradle.someProp=true + """, + """ + org.gradle.someProp=true + systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ + """, + spec -> spec.path("gradle.properties") + ) + ); + } + + @Test + void addToPropertiesStyles() { + rewriteRun( + spec -> spec.recipe(new ActivateStyle("org.openrewrite.java.IntelliJ", false)), + other("", spec -> spec.path(".gradle.kts")), + properties( + """ + org.gradle.someProp=true + systemProp.rewrite.activeStyles=org.openrewrite.java.Other + """, + """ + org.gradle.someProp=true + systemProp.rewrite.activeStyles=org.openrewrite.java.Other,org.openrewrite.java.IntelliJ + """, + spec -> spec.path("gradle.properties") + ) + ); + } + + @Test + void overwritePropertiesStyles() { + rewriteRun( + other("", spec -> spec.path(".gradle.kts")), + properties( + """ + org.gradle.someProp=true + systemProp.rewrite.activeStyles=org.openrewrite.java.Other + """, + """ + org.gradle.someProp=true + systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ + """, + spec -> spec.path("gradle.properties") + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDelegatesToGradleApiTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDelegatesToGradleApiTest.java new file mode 100644 index 00000000000..f18b7ec95bb --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDelegatesToGradleApiTest.java @@ -0,0 +1,269 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class AddDelegatesToGradleApiTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpath("groovy", "gradle-base-services")) + .recipe(new AddDelegatesToGradleApi()); + } + + @Test + void simpleMethod() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(Closure action) { } + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(@DelegatesTo(String.class) Closure action) { } + } + """ + ) + ); + } + + @Test + void methodWithBound() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(Closure action) { } + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(@DelegatesTo(String.class) Closure action) { } + } + """ + ) + ); + } + + @Test + void methodWithGenericBound() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(Closure action) { } + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + void someMethod(@DelegatesTo(String.class) Closure action) { } + } + """ + ) + ); + } + + @Test + void dontUnwrapTypesTooMuch() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + import java.util.List; + + class A { + void someMethod(Action> action) { } + void someMethod(Closure action) { } + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + import java.util.List; + + class A { + void someMethod(Action> action) { } + void someMethod(@DelegatesTo(List.class) Closure action) { } + } + """ + ) + ); + } + + @Test + void dontBeConfusedByOtherOverloads() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + + class A { + void someMethod(int i, Action action) {} + void someMethod(Action action) {} + void someMethod(String s, Action action) {} + void someMethod(Closure action) {} + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + + class A { + void someMethod(int i, Action action) {} + void someMethod(Action action) {} + void someMethod(String s, Action action) {} + void someMethod(@DelegatesTo(String.class) Closure action) {} + } + """ + ) + ); + } + + @Test + void leaveUnusableTypeInformationAlone() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + import java.util.List; + + class A { + void someMethod(Action action) { } + void someMethod(Closure action) { } + } + """ + ) + ); + } + + @Test + void commentSaysNoDelegate() { + rewriteRun( + java( + """ + package org.gradle.example; + + import groovy.lang.Closure; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + /** + * The {@link String} is passed to the closure as a parameter. + * @param action action + */ + void someMethod(Closure action) { } + + void anotherMethod(Action action) { } + + /** + * The {@link String} is the delegate and also passed to the closure as a parameter + * @param action action + */ + void anotherMethod(Closure action) { } + } + """, + """ + package org.gradle.example; + + import groovy.lang.Closure; + import groovy.lang.DelegatesTo; + import org.gradle.api.Action; + + class A { + void someMethod(Action action) { } + /** + * The {@link String} is passed to the closure as a parameter. + * @param action action + */ + void someMethod(Closure action) { } + + void anotherMethod(Action action) { } + + /** + * The {@link String} is the delegate and also passed to the closure as a parameter + * @param action action + */ + void anotherMethod(@DelegatesTo(String.class) Closure action) { } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddGradleWrapperTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddGradleWrapperTest.java new file mode 100644 index 00000000000..7d62dcfb1b5 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/AddGradleWrapperTest.java @@ -0,0 +1,149 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.intellij.lang.annotations.Language; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.openrewrite.PathUtils; +import org.openrewrite.RecipeRun; +import org.openrewrite.Result; +import org.openrewrite.SourceFile; +import org.openrewrite.remote.Remote; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.text.PlainText; + +import java.io.ByteArrayInputStream; +import java.net.URI; +import java.nio.file.Paths; +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION; +import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.properties.Assertions.properties; +import static org.openrewrite.test.SourceSpecs.dir; +import static org.openrewrite.test.SourceSpecs.other; + +@SuppressWarnings("UnusedProperty") +class AddGradleWrapperTest implements RewriteTest { + @Language("properties") + private static final String GRADLE_WRAPPER_PROPERTIES = """ + distributionBase=GRADLE_USER_HOME + distributionPath=wrapper/dists + distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4.2-bin.zip + zipStoreBase=GRADLE_USER_HOME + zipStorePath=wrapper/dists + """; + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe( + //language=yaml + new ByteArrayInputStream( + """ + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.test.AddGradleWrapper + displayName: Adds a Gradle wrapper + description: Add wrapper for gradle version 7.4.2 + recipeList: + - org.openrewrite.gradle.AddGradleWrapper: + version: "7.4.2" + """.getBytes() + ), + "org.openrewrite.test.AddGradleWrapper" + ); + } + + private S result(RecipeRun run, Class clazz, String endsWith) { + return run.getResults().stream() + .map(Result::getAfter) + .filter(Objects::nonNull) + .filter(r -> r.getSourcePath().endsWith(endsWith)) + .findFirst() + .map(clazz::cast) + .orElseThrow(); + } + + @Test + void addWrapper() { + rewriteRun( + spec -> spec.afterRecipe(run -> { + var gradleSh = result(run, PlainText.class, "gradlew"); + assertThat(gradleSh.getText()).isNotBlank(); + assertThat(gradleSh.getFileAttributes()).isNotNull(); + assertThat(gradleSh.getFileAttributes().isExecutable()).isTrue(); + + var gradleBat = result(run, PlainText.class, "gradlew.bat"); + assertThat(gradleBat.getText()).isNotBlank(); + assertThat(gradleBat.getFileAttributes()).isNotNull(); + assertThat(gradleBat.getFileAttributes().isExecutable()).isTrue(); + + var gradleWrapperJar = result(run, Remote.class, "gradle-wrapper.jar"); + assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.getSourcePath(), WRAPPER_JAR_LOCATION)).isTrue(); + assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")); + }), + buildGradle(""), + dir( + "gradle/wrapper", + properties(null, GRADLE_WRAPPER_PROPERTIES, spec -> spec.path(Paths.get("gradle-wrapper.properties"))) + ) + ); + } + + @Test + void addWrapperWhenIncomplete() { + rewriteRun( + spec -> spec.afterRecipe(run -> { + var gradleWrapperJar = result(run, Remote.class, "gradle-wrapper.jar"); + assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.getSourcePath(), WRAPPER_JAR_LOCATION)).isTrue(); + assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")); + }).expectedCyclesThatMakeChanges(1), + other("", spec -> spec.path("gradlew")), + other("", spec -> spec.path("gradlew.bat")), + buildGradle("") + ); + } + + @Disabled + @Test + void addWrapperToGradleKotlin() { + rewriteRun( + spec -> spec.afterRecipe(run -> assertThat(run.getResults()).isNotEmpty()) + .expectedCyclesThatMakeChanges(1), + other("", spec -> spec.path(".gradle.kts")) + ); + } + + @Test + void dontAddWrapperToMavenProject() { + rewriteRun( + spec -> spec.afterRecipe(run -> assertThat(run.getResults()).isEmpty()), + pomXml( + """ + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.java new file mode 100755 index 00000000000..73258afb204 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.java @@ -0,0 +1,193 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyArtifactIdTest implements RewriteTest { + + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId("org.springframework.boot", "spring-boot-starter", "new-starter", "")), + buildGradle( + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.springframework.boot:spring-boot-starter:2.5.4' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """, + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.springframework.boot:new-starter:2.5.4' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release' + api "org.openrewrite:rewrite-core:latest.release" + } + """, + """ + dependencies { + api 'org.openrewrite:dewrite-core:latest.release' + api "org.openrewrite:dewrite-core:latest.release" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" + } + """, + """ + dependencies { + api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release' + api group: "org.openrewrite", name: "dewrite-core", version: "latest.release" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value ={"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithoutVersion(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core' + api "org.openrewrite:rewrite-core" + api group: 'org.openrewrite', name: 'dewrite-core' + api group: "org.openrewrite", name: "dewrite-core" + } + """, + """ + dependencies { + api 'org.openrewrite:dewrite-core' + api "org.openrewrite:dewrite-core" + api group: 'org.openrewrite', name: 'dewrite-core' + api group: "org.openrewrite", name: "dewrite-core" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value ={"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier' + api "org.openrewrite:rewrite-core:latest.release:classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + api 'org.openrewrite:dewrite-core:latest.release:classifier' + api "org.openrewrite:dewrite-core:latest.release:classifier" + api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", classifier: "classifier" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value ={"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithExt(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core@ext' + api "org.openrewrite:rewrite-core@ext" + api 'org.openrewrite:rewrite-core:latest.release@ext' + api "org.openrewrite:rewrite-core:latest.release@ext" + api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + api "org.openrewrite:rewrite-core:latest.release:classifier@ext" + api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """, + """ + dependencies { + api 'org.openrewrite:dewrite-core@ext' + api "org.openrewrite:dewrite-core@ext" + api 'org.openrewrite:dewrite-core:latest.release@ext' + api "org.openrewrite:dewrite-core:latest.release@ext" + api 'org.openrewrite:dewrite-core:latest.release:classifier@ext' + api "org.openrewrite:dewrite-core:latest.release:classifier@ext" + api group: 'org.openrewrite', name: 'dewrite-core', extension: 'ext' + api group: "org.openrewrite", name: "dewrite-core", extension: "ext" + api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', extension: 'ext' + api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", extension: "ext" + api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyClassifierTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyClassifierTest.java new file mode 100644 index 00000000000..f329b9e5b4a --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyClassifierTest.java @@ -0,0 +1,170 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyClassifierTest implements RewriteTest { + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier("org.openrewrite", "*", "classified", "")), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-gradle:latest.release:classifier' + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-gradle:latest.release:classified' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier(group, artifact, "classified", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier' + api "org.openrewrite:rewrite-core:latest.release:classifier" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classified' + api "org.openrewrite:rewrite-core:latest.release:classified" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier(group, artifact, "classified", null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithoutVersion(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier(group, artifact, "classified", null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", classifier: "classifier" + } + """, + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classified' + api group: "org.openrewrite", name: "rewrite-core", classifier: "classified" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier(group, artifact, "classified", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier' + api "org.openrewrite:rewrite-core:latest.release:classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classified' + api "org.openrewrite:rewrite-core:latest.release:classified" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithExt(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyClassifier(group, artifact, "classified", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + api "org.openrewrite:rewrite-core:latest.release:classifier@ext" + api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", classifier: "classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classified@ext' + api "org.openrewrite:rewrite-core:latest.release:classified@ext" + api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classified' + api group: "org.openrewrite", name: "rewrite-core", classifier: "classified" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified", extension: "ext" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyConfigurationTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyConfigurationTest.java new file mode 100644 index 00000000000..523c1e405a7 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyConfigurationTest.java @@ -0,0 +1,184 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyConfigurationTest implements RewriteTest { + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration("org.openrewrite", "*", "implementation", "")), + buildGradle( + """ + dependencies { + compile 'org.openrewrite:rewrite-gradle:latest.release' + } + """, + """ + dependencies { + implementation 'org.openrewrite:rewrite-gradle:latest.release' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration(group, artifact, "implementation", null)), + buildGradle( + """ + dependencies { + compile 'org.openrewrite:rewrite-core:latest.release' + compile "org.openrewrite:rewrite-core:latest.release" + } + """, + """ + dependencies { + implementation 'org.openrewrite:rewrite-core:latest.release' + implementation "org.openrewrite:rewrite-core:latest.release" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration(group, artifact, "implementation", null)), + buildGradle( + """ + dependencies { + compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' + compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release" + } + """, + """ + dependencies { + implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' + implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release" + } + """ + ) + ); + } + + @Disabled + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void withoutVersionShouldNotChange(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration(group, artifact, "implementation", null)), + buildGradle( + """ + dependencies { + compile 'org.openrewrite:rewrite-core' + compile "org.openrewrite:rewrite-core" + compile group: 'org.openrewrite', name: 'rewrite-core' + compile group: "org.openrewrite", name: "rewrite-core" + } + """, + """ + dependencies { + implementation 'org.openrewrite:rewrite-core' + implementation "org.openrewrite:rewrite-core" + implementation group: 'org.openrewrite', name: 'rewrite-core' + implementation group: "org.openrewrite", name: "rewrite-core" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration(group, artifact, "implementation", null)), + buildGradle( + """ + dependencies { + compile 'org.openrewrite:rewrite-core:latest.release:classifier' + compile "org.openrewrite:rewrite-core:latest.release:classifier" + compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + implementation 'org.openrewrite:rewrite-core:latest.release:classifier' + implementation "org.openrewrite:rewrite-core:latest.release:classifier" + implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithExt(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyConfiguration(group, artifact, "implementation", null)), + buildGradle( + """ + dependencies { + compile 'org.openrewrite:rewrite-core@ext' + compile "org.openrewrite:rewrite-core@ext" + compile 'org.openrewrite:rewrite-core:latest.release@ext' + compile "org.openrewrite:rewrite-core:latest.release@ext" + compile 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + compile "org.openrewrite:rewrite-core:latest.release:classifier@ext" + compile group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' + compile group: "org.openrewrite", name: "rewrite-core", extension: "ext" + compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' + compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" + compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """, + """ + dependencies { + implementation 'org.openrewrite:rewrite-core@ext' + implementation "org.openrewrite:rewrite-core@ext" + implementation 'org.openrewrite:rewrite-core:latest.release@ext' + implementation "org.openrewrite:rewrite-core:latest.release@ext" + implementation 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + implementation "org.openrewrite:rewrite-core:latest.release:classifier@ext" + implementation group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' + implementation group: "org.openrewrite", name: "rewrite-core", extension: "ext" + implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' + implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" + implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyExtensionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyExtensionTest.java new file mode 100644 index 00000000000..a55f5896d5e --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyExtensionTest.java @@ -0,0 +1,140 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyExtensionTest implements RewriteTest { + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyExtension("org.openrewrite", "*", "jar", "")), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-gradle:latest.integration@war' + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-gradle:latest.integration@jar' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyExtension(group, artifact, "jar", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release@war' + api "org.openrewrite:rewrite-core:latest.release@war" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release@jar' + api "org.openrewrite:rewrite-core:latest.release@jar" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyExtension(group, artifact, "jar", null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'war' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "war" + } + """, + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'jar' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "jar" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithoutVersion(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyExtension(group, artifact, "jar", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core@war' + api "org.openrewrite:rewrite-core@war" + api group: 'org.openrewrite', name: 'rewrite-core', ext: 'war' + api group: "org.openrewrite", name: "rewrite-core", ext: "war" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core@jar' + api "org.openrewrite:rewrite-core@jar" + api group: 'org.openrewrite', name: 'rewrite-core', ext: 'jar' + api group: "org.openrewrite", name: "rewrite-core", ext: "jar" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyExtension(group, artifact, "jar", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier@war' + api "org.openrewrite:rewrite-core:latest.release:classifier@war" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'war' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "war" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier@jar' + api "org.openrewrite:rewrite-core:latest.release:classifier@jar" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'jar' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "jar" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyGroupIdTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyGroupIdTest.java new file mode 100755 index 00000000000..ceb9c71e1cc --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyGroupIdTest.java @@ -0,0 +1,193 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyGroupIdTest implements RewriteTest { + + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId("org.springframework.boot", "spring-boot-starter", "org.newboot", "")), + buildGradle( + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.springframework.boot:spring-boot-starter:2.5.4' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """, + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.newboot:spring-boot-starter:2.5.4' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId(group, artifact, "org.dewrite", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release' + api "org.openrewrite:rewrite-core:latest.release" + } + """, + """ + dependencies { + api 'org.dewrite:rewrite-core:latest.release' + api "org.dewrite:rewrite-core:latest.release" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId(group, artifact, "org.dewrite", null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" + } + """, + """ + dependencies { + api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release' + api group: "org.dewrite", name: "rewrite-core", version: "latest.release" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithoutVersion(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId(group, artifact, "org.dewrite", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core' + api "org.openrewrite:rewrite-core" + api group: 'org.openrewrite', name: 'rewrite-core' + api group: "org.openrewrite", name: "rewrite-core" + } + """, + """ + dependencies { + api 'org.dewrite:rewrite-core' + api "org.dewrite:rewrite-core" + api group: 'org.dewrite', name: 'rewrite-core' + api group: "org.dewrite", name: "rewrite-core" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId(group, artifact, "org.dewrite", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier' + api "org.openrewrite:rewrite-core:latest.release:classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + api 'org.dewrite:rewrite-core:latest.release:classifier' + api "org.dewrite:rewrite-core:latest.release:classifier" + api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.dewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithExt(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupId(group, artifact, "org.dewrite", null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core@ext' + api "org.openrewrite:rewrite-core@ext" + api 'org.openrewrite:rewrite-core:latest.release@ext' + api "org.openrewrite:rewrite-core:latest.release@ext" + api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + api "org.openrewrite:rewrite-core:latest.release:classifier@ext" + api group: 'org.openrewrite', name: 'rewrite-core', ext: 'ext' + api group: "org.openrewrite", name: "rewrite-core", ext: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "ext" + } + """, + """ + dependencies { + api 'org.dewrite:rewrite-core@ext' + api "org.dewrite:rewrite-core@ext" + api 'org.dewrite:rewrite-core:latest.release@ext' + api "org.dewrite:rewrite-core:latest.release@ext" + api 'org.dewrite:rewrite-core:latest.release:classifier@ext' + api "org.dewrite:rewrite-core:latest.release:classifier@ext" + api group: 'org.dewrite', name: 'rewrite-core', ext: 'ext' + api group: "org.dewrite", name: "rewrite-core", ext: "ext" + api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', ext: 'ext' + api group: "org.dewrite", name: "rewrite-core", version: "latest.release", ext: "ext" + api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'ext' + api group: "org.dewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "ext" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyVersionTest.java new file mode 100755 index 00000000000..be238891ab6 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyVersionTest.java @@ -0,0 +1,185 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeDependencyVersionTest implements RewriteTest { + + @Test + void worksWithEmptyStringConfig() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion("org.springframework.boot", "*", "2.5.5", null, "")), + buildGradle( + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.springframework.boot:spring-boot-starter:2.5.4' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """, + """ + dependencies { + rewrite 'org.openrewrite:rewrite-gradle:latest.integration' + implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' + implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' + implementation 'org.springframework.boot:spring-boot-starter:2.5.5' + implementation 'commons-lang:commons-lang:2.6' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion(group, artifact, "latest.integration", null, null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release' + api "org.openrewrite:rewrite-core:latest.release" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.integration' + api "org.openrewrite:rewrite-core:latest.integration" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void findMapStyleDependency(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion(group, artifact, "latest.integration", null, null)), + buildGradle( + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" + } + """, + """ + dependencies { + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void withoutVersionShouldNotChange(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion(group, artifact, "latest.integration", null, null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core' + api "org.openrewrite:rewrite-core" + api group: 'org.openrewrite', name: 'rewrite-core' + api group: "org.openrewrite", name: "rewrite-core" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithClassifier(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion(group, artifact, "latest.integration", null, null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release:classifier' + api "org.openrewrite:rewrite-core:latest.release:classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.integration:classifier' + api "org.openrewrite:rewrite-core:latest.integration:classifier" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', classifier: 'classifier' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", classifier: "classifier" + } + """ + ) + ); + } + + @ParameterizedTest + @CsvSource(value = {"org.openrewrite:rewrite-core", "*:*"}, delimiterString = ":") + void worksWithExt(String group, String artifact) { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersion(group, artifact, "latest.integration", null, null)), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core@ext' + api "org.openrewrite:rewrite-core@ext" + api 'org.openrewrite:rewrite-core:latest.release@ext' + api "org.openrewrite:rewrite-core:latest.release@ext" + api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' + api "org.openrewrite:rewrite-core:latest.release:classifier@ext" + api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" + } + """, + """ + dependencies { + api 'org.openrewrite:rewrite-core@ext' + api "org.openrewrite:rewrite-core@ext" + api 'org.openrewrite:rewrite-core:latest.integration@ext' + api "org.openrewrite:rewrite-core:latest.integration@ext" + api 'org.openrewrite:rewrite-core:latest.integration:classifier@ext' + api "org.openrewrite:rewrite-core:latest.integration:classifier@ext" + api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", extension: "ext" + api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', classifier: 'classifier', extension: 'ext' + api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", classifier: "classifier", extension: "ext" + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeJavaCompatibilityTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeJavaCompatibilityTest.java new file mode 100644 index 00000000000..58eff6a6890 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeJavaCompatibilityTest.java @@ -0,0 +1,441 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ChangeJavaCompatibilityTest implements RewriteTest { + @Nested + @DisplayName("When requested version is an integer") + class NewInteger extends BaseTest { + NewInteger() { + super("8"); + } + } + + @Nested + @DisplayName("When requested version is a double") + class NewDouble extends BaseTest { + NewDouble() { + super("1.8"); + } + } + + @Nested + @DisplayName("When requested version is a string (double quotes)") + class NewStringDoubleQuoted extends BaseTest { + NewStringDoubleQuoted() { + super("\"1.8\""); + } + } + + @Nested + @DisplayName("When requested version is a string (single quotes -tmp)") + class NewStringSingleQuoted extends BaseTest { + NewStringSingleQuoted() { + super("'1.8'"); + } + } + + @Nested + @DisplayName("When requested version is an enum (shorthand)") + class NewEnumShorthand extends BaseTest { + NewEnumShorthand() { + super("VERSION_1_8"); + } + } + + @Nested + @DisplayName("When requested version is an enum") + class NewEnum extends BaseTest { + NewEnum() { + super("JavaVersion.VERSION_1_8"); + } + } +} + +@SuppressWarnings("GroovyUnusedAssignment") +abstract class BaseTest implements RewriteTest { + private final String newVersion; + + BaseTest(String newVersion) { + this.newVersion = newVersion; + } + + @Test + void changeSourceCompatibility() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + sourceCompatibility = 7 + """, + """ + plugins { + java + } + + sourceCompatibility = %s + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + targetCompatibility = '7' + """, + """ + plugins { + java + } + + targetCompatibility = %s + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSetSourceCompatibility() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + setSourceCompatibility "7" + """, + """ + plugins { + java + } + + setSourceCompatibility %s + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSetTargetCompatibility() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + setTargetCompatibility 1.7 + """, + """ + plugins { + java + } + + setTargetCompatibility %s + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSourceCompatibility_JavaPluginExtension() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + java { + sourceCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + java { + sourceCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility_JavaPluginExtension() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + java { + targetCompatibility = '1.7' + } + """, + """ + plugins { + java + } + + java { + targetCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSourceCompatibility_CompileJava() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + compileJava { + sourceCompatibility = JavaVersion.VERSION_1_7 + } + """, + """ + plugins { + java + } + + compileJava { + sourceCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility_CompileJava() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + compileJava { + targetCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + compileJava { + targetCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSourceCompatibility_TasksNamedCompileJava() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + tasks.named("compileJava") { + sourceCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + tasks.named("compileJava") { + sourceCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility_TasksNamedCompileJava() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + tasks.named("compileJava") { + targetCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + tasks.named("compileJava") { + targetCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSourceCompatibility_TasksWithTypeJavaCompile() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + tasks.withType(JavaCompile).configureEach { + sourceCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + tasks.withType(JavaCompile).configureEach { + sourceCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility_TasksWithTypeJavaCompile() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + tasks.withType(JavaCompile).configureEach { + targetCompatibility = "1.7" + } + """, + """ + plugins { + java + } + + tasks.withType(JavaCompile).configureEach { + targetCompatibility = %s + } + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeSourceCompatibility_JavaPluginExtensionFieldAccess() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "source")), + buildGradle( + """ + plugins { + java + } + + java.sourceCompatibility = "1.7" + """, + """ + plugins { + java + } + + java.sourceCompatibility = %s + """.formatted(coerce(newVersion)) + ) + ); + } + + @Test + void changeTargetCompatibility_JavaPluginExtensionFieldAccess() { + rewriteRun( + spec -> spec.recipe(new ChangeJavaCompatibility(newVersion, "target")), + buildGradle( + """ + plugins { + java + } + + java.targetCompatibility = "1.7" + """, + """ + plugins { + java + } + + java.targetCompatibility = %s + """.formatted(coerce(newVersion)) + ) + ); + } + + private String coerce(String version) { + return version.startsWith("VERSION_") ? + "JavaVersion." + version : version; + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseMapNotationTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseMapNotationTest.java new file mode 100755 index 00000000000..e16f56d28e6 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseMapNotationTest.java @@ -0,0 +1,128 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class DependencyUseMapNotationTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new DependencyUseMapNotation()); + } + + @Test + void basicString() { + rewriteRun( + buildGradle( + """ + dependencies { + api('org.openrewrite:rewrite-core:latest.release') + implementation "group:artifact:version" + } + """, + """ + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') + implementation group: 'group', name: 'artifact', version: 'version' + } + """ + ) + ); + } + + @Test + void withGString() { + rewriteRun( + buildGradle( + """ + def version = "latest.release" + dependencies { + api("org.openrewrite:rewrite-core:$version") + implementation "group:artifact:$version" + } + """, + """ + def version = "latest.release" + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: version) + implementation group: 'group', name: 'artifact', version: version + } + """ + ) + ); + } + + @Test + void withExclusion() { + rewriteRun( + buildGradle( + """ + dependencies { + api("org.openrewrite:rewrite-core:latest.release") { + exclude group: "group", module: "artifact" + } + implementation "group:artifact:version", { + exclude group: "group2", module: "artifact2" + } + } + """, + """ + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') { + exclude group: "group", module: "artifact" + } + implementation group: 'group', name: 'artifact', version: 'version', { + exclude group: "group2", module: "artifact2" + } + } + """ + ) + ); + } + + @Test + void withGStringAndExclusion() { + rewriteRun( + buildGradle( + """ + dependencies { + api("org.openrewrite:rewrite-core:$version") { + exclude group: "group", module: "artifact" + } + implementation "group:artifact:$version", { + exclude group: "group2", module: "artifact2" + } + } + """, + """ + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: version) { + exclude group: "group", module: "artifact" + } + implementation group: 'group', name: 'artifact', version: version, { + exclude group: "group2", module: "artifact2" + } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseStringNotationTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseStringNotationTest.java new file mode 100644 index 00000000000..d0c32baa2b0 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/DependencyUseStringNotationTest.java @@ -0,0 +1,156 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class DependencyUseStringNotationTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new DependencyUseStringNotation()); + } + + @Test + void basicMap() { + rewriteRun( + buildGradle( + """ + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') + implementation group: 'group', name: 'artifact', version: 'version' + } + """, + """ + dependencies { + api("org.openrewrite:rewrite-core:latest.release") + implementation "group:artifact:version" + } + """ + ) + ); + } + + @Test + void basicMapLiteral() { + rewriteRun( + buildGradle( + """ + dependencies { + api([group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release']) + } + """, + """ + dependencies { + api("org.openrewrite:rewrite-core:latest.release") + } + """ + ) + ); + } + + @Test + void withGString() { + rewriteRun( + buildGradle( + """ + def version = "latest.release" + dependencies { + api(group: 'org.openrewrite', name: 'rewrite-core', version: version) + implementation group: 'group', name: 'artifact', version: version + } + """, + """ + def version = "latest.release" + dependencies { + api("org.openrewrite:rewrite-core:$version") + implementation "group:artifact:$version" + } + """ + ) + ); + } + + @Test + void withoutVersion() { + rewriteRun( + buildGradle( + """ + dependencies { + api(group: "org.openrewrite", name: "rewrite-core") + implementation group: "group", name: "artifact" + } + """, + """ + dependencies { + api("org.openrewrite:rewrite-core") + implementation "group:artifact" + } + """ + ) + ); + } + + @Test + void withExclusion() { + rewriteRun( + buildGradle( + """ + dependencies { + api(group: "org.openrewrite", name: "rewrite-core", version: "latest.release") { + exclude group: "group", module: "artifact" + } + } + """, + """ + dependencies { + api("org.openrewrite:rewrite-core:latest.release") { + exclude group: "group", module: "artifact" + } + } + """ + ) + ); + } + + @Test + void withGStringExclusion() { + rewriteRun( + buildGradle( + """ + def version = "latest.release" + dependencies { + api(group: "org.openrewrite", name: "rewrite-core", version: version) { + exclude group: "group", module: "artifact" + } + } + """, + """ + def version = "latest.release" + dependencies { + api("org.openrewrite:rewrite-core:$version") { + exclude group: "group", module: "artifact" + } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/GradleParserTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/GradleParserTest.java index e490cc073ce..95bc6fadfdc 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/GradleParserTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/GradleParserTest.java @@ -18,23 +18,22 @@ import org.junit.jupiter.api.Test; import org.openrewrite.test.RewriteTest; -import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.Assertions.settingsGradle; public class GradleParserTest implements RewriteTest { @Test - void buildAndSettingsGradle() { + void buildGradleAndSettingsGradle() { rewriteRun( settingsGradle( """ plugins { id 'com.gradle.enterprise' version '3.11.3' } - + gradleEnterprise { server = 'https://enterprise-samples.gradle.com' - + buildScan { publishAlways() } diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyTest.kt b/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveRepositoryTest.java similarity index 51% rename from rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyTest.kt rename to rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveRepositoryTest.java index 4398f712c29..1d98557cfc6 100755 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyTest.kt +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveRepositoryTest.java @@ -13,25 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.gradle.search +package org.openrewrite.gradle; -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.GradleRecipeTest +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; -class FindDependencyTest : GradleRecipeTest { +import static org.openrewrite.gradle.Assertions.buildGradle; + +class RemoveRepositoryTest implements RewriteTest { @Test - fun findDependency() = assertChanged( - recipe = FindDependency("org.openrewrite", "rewrite-core", "api"), - before = """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release' - } - """, - after = """ - dependencies { - /*~~>*/api 'org.openrewrite:rewrite-core:latest.release' - } - """ - ) + void removeJcenter() { + rewriteRun( + spec -> spec.recipe(new RemoveRepository("jcenter")), + buildGradle( + """ + repositories { + jcenter() + } + """, + """ + repositories { + \s + } + """ + ) + ); + } } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java new file mode 100755 index 00000000000..26ed3ee4bf9 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.openrewrite.PathUtils; +import org.openrewrite.RecipeRun; +import org.openrewrite.Result; +import org.openrewrite.SourceFile; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.remote.Remote; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.SourceSpecs; +import org.openrewrite.text.PlainText; + +import java.net.URI; +import java.util.Objects; +import java.util.function.UnaryOperator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.gradle.util.GradleWrapper.*; +import static org.openrewrite.properties.Assertions.properties; +import static org.openrewrite.test.SourceSpecs.other; +import static org.openrewrite.test.SourceSpecs.text; + +@SuppressWarnings("UnusedProperty") +class UpdateGradleWrapperTest implements RewriteTest { + private final UnaryOperator<@Nullable String> notEmpty = actual -> { + assertThat(actual).isNotNull(); + return actual + "\n"; + }; + + private final SourceSpecs gradlew = text("", notEmpty, spec -> spec.path(WRAPPER_SCRIPT_LOCATION)); + private final SourceSpecs gradlewBat = text("", notEmpty, spec -> spec.path(WRAPPER_BATCH_LOCATION)); + private final SourceSpecs gradleWrapperJarQuark = other("", spec -> spec.path(WRAPPER_JAR_LOCATION)); + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new UpdateGradleWrapper("7.4.2", null)); + } + + @ParameterizedTest + @ValueSource(strings = {"gradle\\wrapper\\gradle-wrapper.properties", "gradle/wrapper/gradle-wrapper.properties"}) + void updateVersionAndDistribution(String osPath) { + rewriteRun( + spec -> spec.afterRecipe(run -> { + var gradleSh = result(run, PlainText.class, "gradlew"); + assertThat(gradleSh.getText()).isNotBlank(); + + var gradleBat = result(run, PlainText.class, "gradlew.bat"); + assertThat(gradleBat.getText()).isNotBlank(); + + var gradleWrapperJar = result(run, Remote.class, "gradle-wrapper.jar"); + assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.getSourcePath(), WRAPPER_JAR_LOCATION)).isTrue(); + assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")); + }), + properties( + """ + distributionBase=GRADLE_USER_HOME + distributionPath=wrapper/dists + distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4-all.zip + zipStoreBase=GRADLE_USER_HOME + zipStorePath=wrapper/dists + """, + """ + distributionBase=GRADLE_USER_HOME + distributionPath=wrapper/dists + distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4.2-bin.zip + zipStoreBase=GRADLE_USER_HOME + zipStorePath=wrapper/dists + """, + spec -> spec.path(osPath) + ), + gradlew, + gradlewBat, + gradleWrapperJarQuark + ); + } + + private S result(RecipeRun run, Class clazz, String endsWith) { + return run.getResults().stream() + .map(Result::getAfter) + .filter(Objects::nonNull) + .filter(r -> r.getSourcePath().endsWith(endsWith)) + .findFirst() + .map(clazz::cast) + .orElseThrow(); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradePluginVersionTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradePluginVersionTest.java new file mode 100644 index 00000000000..504d641e133 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpgradePluginVersionTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle; + +import org.junit.jupiter.api.Test; +import org.openrewrite.gradle.plugins.UpgradePluginVersion; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class UpgradePluginVersionTest implements RewriteTest { + + @Test + void upgradePlugin() { + rewriteRun( + spec -> spec.recipe(new UpgradePluginVersion("com.jfrog.bintray", "latest.patch", null)), + buildGradle( + """ + plugins { + id 'com.jfrog.bintray' version '1.7.0' + id 'com.github.johnrengelman.shadow' version '6.1.0' + } + """, + """ + plugins { + id 'com.jfrog.bintray' version '1.7.3' + id 'com.github.johnrengelman.shadow' version '6.1.0' + } + """ + ) + ); + } + + @Test + void upgradePluginGlob() { + rewriteRun( + spec -> spec.recipe(new UpgradePluginVersion("com.jfrog.*", "1.8.X", null)), + buildGradle( + """ + plugins { + id 'com.jfrog.bintray' version '1.8.2' + } + """, + """ + plugins { + id 'com.jfrog.bintray' version '1.8.5' + } + """ + ) + ); + } + + @Test + void exactVersionDoesNotHaveToBeResolvable() { + rewriteRun( + spec -> spec.recipe(new UpgradePluginVersion("com.madeup.doesnotexist", "2.0", null)), + buildGradle( + """ + plugins { + id 'com.madeup.doesnotexist' version '1.0' + } + """, + """ + plugins { + id 'com.madeup.doesnotexist' version '2.0' + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/package-info.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/package-info.java similarity index 100% rename from rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/package-info.java rename to rewrite-gradle/src/test/java/org/openrewrite/gradle/package-info.java diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyHandlerTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyHandlerTest.java new file mode 100644 index 00000000000..1ae10ec24ba --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyHandlerTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.search; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.test.RewriteTest.fromRuntimeClasspath; + +class FindDependencyHandlerTest implements RewriteTest { + @Test + void findDependenciesBlock() { + rewriteRun( + spec -> spec.recipe(fromRuntimeClasspath("org.openrewrite.gradle.search.FindDependencyHandler")), + buildGradle( + """ + dependencies { + api 'com.google.guava:guava:23.0' + } + """, + """ + /*~~>*/dependencies { + api 'com.google.guava:guava:23.0' + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyTest.java new file mode 100755 index 00000000000..4f107b62987 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindDependencyTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.search; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class FindDependencyTest implements RewriteTest { + + @Test + void findDependency() { + rewriteRun( + spec -> spec.recipe(new FindDependency("org.openrewrite", "rewrite-core", "api")), + buildGradle( + """ + dependencies { + api 'org.openrewrite:rewrite-core:latest.release' + } + """, + """ + dependencies { + /*~~>*/api 'org.openrewrite:rewrite-core:latest.release' + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java index f5ecaf19dce..521ab0738e5 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindGradleProjectTest.java @@ -51,8 +51,8 @@ void isGradleGroovyProject() { @Test void isGradleKotlinProject() { rewriteRun( - //language=kotlin text( + //language=kotlin """ plugins { id("java") diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java index edec8ef3be8..81551764af0 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java @@ -31,7 +31,8 @@ public void defaults(RecipeSpec spec) { @Test void findPlugin() { rewriteRun( - buildGradle(""" + buildGradle( + """ plugins { id 'com.jfrog.bintray' version '1.8.5' } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.java new file mode 100644 index 00000000000..49b063abb31 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2022 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.security; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +@SuppressWarnings("HttpUrlsUsage") +class UseHttpsForRepositoriesTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new UseHttpsForRepositories()); + } + + @Test + void unchangedUseOfHttps() { + rewriteRun( + buildGradle( + """ + repositories { + maven { url 'https://repo.spring.example.com/libs-release-local' } + } + """ + ) + ); + } + + @Test + void updateUnwrappedInvocationToUseHttpsSingleQuote() { + rewriteRun( + buildGradle( + """ + repositories { + maven { url 'http://repo.spring.example.com/libs-release-local' } + } + """, + """ + repositories { + maven { url 'https://repo.spring.example.com/libs-release-local' } + } + """ + ) + ); + } + + @Test + void updateUnwrappedInvocationToUseHttpsDoubleQuote() { + rewriteRun( + buildGradle( + """ + repositories { + maven { url "http://repo.spring.example.com/libs-release-local" } + } + """, + """ + repositories { + maven { url "https://repo.spring.example.com/libs-release-local" } + } + """ + ) + ); + } + + @Test + void updateUnwrappedInvocationToUseHttpsGString() { + rewriteRun( + buildGradle( + """ + repositories { + maven { + def subRepo = properties.snapshot ? 'snapshot' : 'release' + url "http://repo.spring.example.com/libs-release-local/$subRepo" + } + } + """, + """ + repositories { + maven { + def subRepo = properties.snapshot ? 'snapshot' : 'release' + url "https://repo.spring.example.com/libs-release-local/$subRepo" + } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/BuildscriptTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/BuildscriptTest.java new file mode 100755 index 00000000000..2ae98a462d3 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/BuildscriptTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.tree; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class BuildscriptTest implements RewriteTest { + + @Test + void buildscriptWithDependencies() { + rewriteRun( + buildGradle( + """ + buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath 'com.netflix.nebula:nebula-dependency-recommender:9.1.1' + classpath 'com.netflix.nebula:gradle-netflixoss-project-plugin:9.1.0' + } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/ExtTest.kt b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/ExtTest.java similarity index 63% rename from rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/ExtTest.kt rename to rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/ExtTest.java index cde9a16f883..b2c6501960d 100755 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/ExtTest.kt +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/ExtTest.java @@ -13,24 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.gradle.tree +package org.openrewrite.gradle.tree; -import org.junit.jupiter.api.Test -import org.openrewrite.Issue -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest +import org.junit.jupiter.api.Test; +import org.openrewrite.Issue; +import org.openrewrite.test.RewriteTest; -class ExtTest : RewriteTest { +import static org.openrewrite.gradle.Assertions.buildGradle; + +class ExtTest implements RewriteTest { @Issue("https://github.com/openrewrite/rewrite/issues/1236") @Test - fun basicExt() = rewriteRun( - buildGradle( - """ - ext { - foo = "bar" - } + void basicExt() { + rewriteRun( + buildGradle( """ - ) - ) + ext { + foo = "bar" + } + """ + ) + ); + } } diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/PluginsTest.kt b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/PluginsTest.java similarity index 56% rename from rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/PluginsTest.kt rename to rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/PluginsTest.java index 5296bea1f00..60e6d922fbc 100755 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/PluginsTest.kt +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/PluginsTest.java @@ -13,22 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openrewrite.gradle.tree +package org.openrewrite.gradle.tree; -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class PluginsTest implements RewriteTest { -class PluginsTest : RewriteTest { @Test - fun applySomePlugins() = rewriteRun( - buildGradle( - """ - plugins { - id 'me.champeau.gradle.jmh' version '0.5.2' - id "com.github.spotbugs" version "4.6.0" apply false - } + void applySomePlugins() { + rewriteRun( + buildGradle( """ - ) - ) + plugins { + id 'me.champeau.gradle.jmh' version '0.5.2' + id "com.github.spotbugs" version "4.6.0" apply false + } + """ + ) + ); + } } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/RepositoryTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/RepositoryTest.java new file mode 100755 index 00000000000..7d340703343 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/RepositoryTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.tree; + +import org.junit.jupiter.api.Test; +import org.openrewrite.Issue; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class RepositoryTest implements RewriteTest { + + @Test + void builtInFunctions() { + rewriteRun( + buildGradle( + """ + repositories { + mavenCentral() + mavenLocal() + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite/issues/1253") + @Test + void customMavenRepo() { + rewriteRun( + buildGradle( + """ + repositories { + maven { + url "https://repo.spring.io/release" + } + maven { + setUrl("https://repo.spring.io/release") + } + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java new file mode 100755 index 00000000000..bd3ada9fb02 --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TaskTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.tree; + +import org.junit.jupiter.api.Test; +import org.openrewrite.Issue; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.TypeValidation; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class TaskTest implements RewriteTest { + + @Issue("https://github.com/openrewrite/rewrite/issues/1236") + @Test + void declareTaskOldStyle() { + rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), + buildGradle( + """ + task(testWithCloud, type: Test) { + useTestNG() + options.excludeGroups = [] as Set + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite/issues/1929") + @Test + void testDsl() { + rewriteRun( + spec -> spec.typeValidationOptions(TypeValidation.none()), + buildGradle( + """ + test { + // Ex: -PexcludeTests=com/google/cloud/healthcare/etl/runner/hl7v2tofhir/integ/* + if (project.hasProperty('excludeTests')) { + exclude project.property('excludeTests') as String + } + dependsOn('buildDeps') + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TestTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TestTest.java new file mode 100755 index 00000000000..eed8bf6428e --- /dev/null +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/tree/TestTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.gradle.tree; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.gradle.Assertions.buildGradle; + +class TestTest implements RewriteTest { + + @Test + void testDsl() { + rewriteRun( + buildGradle( + """ + test { + useJUnit() + useTestNG() + useJUnitPlatform() + systemProperty 'some.prop', 'value' + include 'org/foo/**' + exclude 'org/boo/**' + testLogging.showStandardStreams = true + minHeapSize = "128m" + maxHeapSize = "512m" + jvmArgs '-XX:MaxPermSize=256m' + failFast = true + } + """ + ) + ); + } +} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ActivateStyleTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ActivateStyleTest.kt deleted file mode 100644 index fb3b4b15e3e..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ActivateStyleTest.kt +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -@file:Suppress("UnusedProperty") - -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.properties.Assertions.properties -import org.openrewrite.test.RecipeSpec -import org.openrewrite.test.RewriteTest -import org.openrewrite.test.SourceSpecs.other -import org.openrewrite.test.TypeValidation - -class ActivateStyleTest : RewriteTest { - - override fun defaults(spec: RecipeSpec) { - spec.recipe(ActivateStyle("org.openrewrite.java.IntelliJ", true)) - .typeValidationOptions(TypeValidation.none()) - } - - @Test - fun addToRewriteDsl() = rewriteRun( - buildGradle(""" - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - } - """, - """ - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - activeStyle("org.openrewrite.java.IntelliJ") - } - """) - ) - - @Test - fun addToRewriteDslExistingStyle() = rewriteRun( - { spec -> spec.recipe(ActivateStyle("org.openrewrite.java.IntelliJ", false)) }, - buildGradle(""" - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - activeStyle("otherStyle") - } - """, - """ - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - activeStyle("otherStyle", "org.openrewrite.java.IntelliJ") - } - """) - ) - - @Test - fun addToRewriteDslOverwriteStyle() = rewriteRun( - buildGradle(""" - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - activeStyle("com.your.Style") - } - """, - """ - plugins { - id("java") - id("org.openrewrite.rewrite") - } - - rewrite { - activeRecipe("org.openrewrite.java.format.AutoFormat") - activeStyle("org.openrewrite.java.IntelliJ") - } - """) - ) - - @Test - fun addToProperties() = rewriteRun( - other("") { spec -> spec.path("build.gradle.kts") }, - properties(""" - org.gradle.someProp=true - """, - """ - org.gradle.someProp=true - systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ - """) { spec -> spec.path("gradle.properties") } - ) - - @Test - fun addToPropertiesStyles() = rewriteRun( - { spec -> spec.recipe(ActivateStyle("org.openrewrite.java.IntelliJ", false)) }, - other("") { spec -> spec.path("build.gradle.kts") }, - properties( - """ - org.gradle.someProp=true - systemProp.rewrite.activeStyles=org.openrewrite.java.Other - """, - """ - org.gradle.someProp=true - systemProp.rewrite.activeStyles=org.openrewrite.java.Other,org.openrewrite.java.IntelliJ - """ - ) { spec -> spec.path("gradle.properties") } - ) - - @Test - fun overwritePropertiesStyles() = rewriteRun( - other("") { spec -> spec.path("build.gradle.kts") }, - properties( - """ - org.gradle.someProp=true - systemProp.rewrite.activeStyles=org.openrewrite.java.Other - """, - """ - org.gradle.someProp=true - systemProp.rewrite.activeStyles=org.openrewrite.java.IntelliJ - """ - ) { spec -> spec.path("gradle.properties") } - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddDelegatesToGradleApiTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddDelegatesToGradleApiTest.kt deleted file mode 100644 index 544a03cbc98..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddDelegatesToGradleApiTest.kt +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.openrewrite.java.Assertions.java -import org.openrewrite.java.JavaParser -import org.openrewrite.test.RecipeSpec -import org.openrewrite.test.RewriteTest - -class AddDelegatesToGradleApiTest : RewriteTest { - - override fun defaults(spec: RecipeSpec) { - spec.parser( JavaParser.fromJavaVersion().classpath("groovy", "gradle-base-services") ) - .recipe(AddDelegatesToGradleApi()) - } - - @Test - fun simpleMethod() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(Closure action) { } - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(@DelegatesTo(String.class) Closure action) { } - } - """) - ) - - @Test - fun methodWithBound() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(Closure action) { } - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(@DelegatesTo(String.class) Closure action) { } - } - """) - ) - - @Test - fun methodWithGenericBound() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(Closure action) { } - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - void someMethod(@DelegatesTo(String.class) Closure action) { } - } - """) - ) - - @Test - fun dontUnwrapTypesTooMuch() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - import java.util.List; - - class A { - void someMethod(Action> action) { } - void someMethod(Closure action) { } - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - import java.util.List; - - class A { - void someMethod(Action> action) { } - void someMethod(@DelegatesTo(List.class) Closure action) { } - } - """) - ) - - @Test - fun dontBeConfusedByOtherOverloads() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - - class A { - void someMethod(int i, Action action) {} - void someMethod(Action action) {} - void someMethod(String s, Action action) {} - void someMethod(Closure action) {} - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - - class A { - void someMethod(int i, Action action) {} - void someMethod(Action action) {} - void someMethod(String s, Action action) {} - void someMethod(@DelegatesTo(String.class) Closure action) {} - } - """) - ) - - @Test - fun leaveUnusableTypeInformationAlone() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - import java.util.List; - - class A { - void someMethod(Action action) { } - void someMethod(Closure action) { } - } - """) - ) - - @Test - fun commentSaysNoDelegate() = rewriteRun( - java(""" - package org.gradle.example; - - import groovy.lang.Closure; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - /** - * The {@link String} is passed to the closure as a parameter. - * @param action - */ - void someMethod(Closure action) { } - - void anotherMethod(Action action) { } - - /** - * The {@link String} is the delegate and also passed to the closure as a parameter - * @param action - */ - void anotherMethod(Closure action) { } - } - """, - """ - package org.gradle.example; - - import groovy.lang.Closure; - import groovy.lang.DelegatesTo; - import org.gradle.api.Action; - - class A { - void someMethod(Action action) { } - /** - * The {@link String} is passed to the closure as a parameter. - * @param action - */ - void someMethod(Closure action) { } - - void anotherMethod(Action action) { } - - /** - * The {@link String} is the delegate and also passed to the closure as a parameter - * @param action - */ - void anotherMethod(@DelegatesTo(String.class) Closure action) { } - } - """) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddGradleWrapperTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddGradleWrapperTest.kt deleted file mode 100644 index 9fff8b25403..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/AddGradleWrapperTest.kt +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.openrewrite.PathUtils -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION -import org.openrewrite.maven.Assertions.pomXml -import org.openrewrite.properties.Assertions.properties -import org.openrewrite.remote.Remote -import org.openrewrite.test.RecipeSpec -import org.openrewrite.test.RewriteTest -import org.openrewrite.test.SourceSpecs.dir -import org.openrewrite.test.SourceSpecs.other -import org.openrewrite.text.PlainText -import java.net.URI -import java.nio.file.Paths - -@Suppress("UnusedProperty") -class AddGradleWrapperTest : RewriteTest { - companion object { - const val GRADLE_WRAPPER_PROPERTIES = """ - distributionBase=GRADLE_USER_HOME - distributionPath=wrapper/dists - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip - zipStoreBase=GRADLE_USER_HOME - zipStorePath=wrapper/dists - """ - } - - override fun defaults(spec: RecipeSpec) { - spec.recipe( - //language=yaml - """ - --- - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.test.AddGradleWrapper - displayName: Adds a Gradle wrapper - description: Add wrapper for gradle version 7.4.2 - recipeList: - - org.openrewrite.gradle.AddGradleWrapper: - version: "7.4.2" - """.trimIndent().byteInputStream(), "org.openrewrite.test.AddGradleWrapper" - ) - } - - @Test - fun addWrapper() = rewriteRun( - { spec -> - spec.afterRecipe { run -> - val gradleSh = - run.results.map { it.after }.filterIsInstance

().first { it.sourcePath.endsWith("gradlew") } - assertThat(gradleSh.text).isNotBlank - assertThat(gradleSh.fileAttributes!!.isExecutable).isTrue - - val gradleBat = run.results.map { it.after }.filterIsInstance<PlainText>() - .first { it.sourcePath.endsWith("gradlew.bat") } - assertThat(gradleBat.text).isNotBlank - assertThat(gradleBat.fileAttributes!!.isExecutable).isTrue - - val gradleWrapperJar = run.results.map { it.after }.filterIsInstance<Remote>() - .first { it.sourcePath.endsWith("gradle-wrapper.jar") } - assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.sourcePath, WRAPPER_JAR_LOCATION)) - assertThat(gradleWrapperJar.uri).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")) - } - }, - buildGradle(""), - dir( - "gradle/wrapper", - properties(null, GRADLE_WRAPPER_PROPERTIES) { spec -> - spec.path(Paths.get("gradle-wrapper.properties")) - } - ) - ) - - @Test - fun addWrapperWhenIncomplete() = rewriteRun( - { spec -> - spec.afterRecipe { run -> - val gradleWrapperJar = run.results.map { it.after }.filterIsInstance<Remote>() - .first { it.sourcePath.endsWith("gradle-wrapper.jar") } - assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.sourcePath, WRAPPER_JAR_LOCATION)) - assertThat(gradleWrapperJar.uri).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")) - }.expectedCyclesThatMakeChanges(1) - }, - other("") { spec -> spec.path("gradlew") }, - other("") { spec -> spec.path("gradlew.bat") }, - buildGradle(""), - ) - - @Test - fun addWrapperToGradleKotlin() = rewriteRun( - { spec -> - spec.afterRecipe { run -> run.results.isNotEmpty() }.expectedCyclesThatMakeChanges(1) - }, - other("") { spec -> spec.path("build.gradle.kts") } - ) - - @Test - fun dontAddWrapperToMavenProject() = rewriteRun( - { spec -> - spec.afterRecipe { run -> run.results.isEmpty() } - }, - pomXml( - """ - <project> - <groupId>com.mycompany.app</groupId> - <artifactId>my-app</artifactId> - <version>1</version> - </project> - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.kt deleted file mode 100755 index d6237c927bc..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyArtifactIdTest.kt +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyArtifactIdTest : RewriteTest { - - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId("org.springframework.boot", "spring-boot-starter", "new-starter", "")) - }, - buildGradle( - """ - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.springframework.boot:spring-boot-starter:2.5.4' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """, - """ - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.springframework.boot:new-starter:2.5.4' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release' - api "org.openrewrite:rewrite-core:latest.release" - } - """, - """ - dependencies { - api 'org.openrewrite:dewrite-core:latest.release' - api "org.openrewrite:dewrite-core:latest.release" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" - } - """, - """ - dependencies { - api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release' - api group: "org.openrewrite", name: "dewrite-core", version: "latest.release" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithoutVersion(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core' - api "org.openrewrite:rewrite-core" - api group: 'org.openrewrite', name: 'dewrite-core' - api group: "org.openrewrite", name: "dewrite-core" - } - """, - """ - dependencies { - api 'org.openrewrite:dewrite-core' - api "org.openrewrite:dewrite-core" - api group: 'org.openrewrite', name: 'dewrite-core' - api group: "org.openrewrite", name: "dewrite-core" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier' - api "org.openrewrite:rewrite-core:latest.release:classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - api 'org.openrewrite:dewrite-core:latest.release:classifier' - api "org.openrewrite:dewrite-core:latest.release:classifier" - api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", classifier: "classifier" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithExt(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyArtifactId(group, artifact, "dewrite-core", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core@ext' - api "org.openrewrite:rewrite-core@ext" - api 'org.openrewrite:rewrite-core:latest.release@ext' - api "org.openrewrite:rewrite-core:latest.release@ext" - api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - api "org.openrewrite:rewrite-core:latest.release:classifier@ext" - api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """, - """ - dependencies { - api 'org.openrewrite:dewrite-core@ext' - api "org.openrewrite:dewrite-core@ext" - api 'org.openrewrite:dewrite-core:latest.release@ext' - api "org.openrewrite:dewrite-core:latest.release@ext" - api 'org.openrewrite:dewrite-core:latest.release:classifier@ext' - api "org.openrewrite:dewrite-core:latest.release:classifier@ext" - api group: 'org.openrewrite', name: 'dewrite-core', extension: 'ext' - api group: "org.openrewrite", name: "dewrite-core", extension: "ext" - api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', extension: 'ext' - api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", extension: "ext" - api group: 'org.openrewrite', name: 'dewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - api group: "org.openrewrite", name: "dewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyClassifierTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyClassifierTest.kt deleted file mode 100644 index 513c3ce4914..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyClassifierTest.kt +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyClassifierTest : RewriteTest { - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier("org.openrewrite", "*", "classified", "")) - }, - buildGradle(""" - dependencies { - api 'org.openrewrite:rewrite-gradle:latest.release:classifier' - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-gradle:latest.release:classified' - } - """) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier(group, artifact, "classified", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier' - api "org.openrewrite:rewrite-core:latest.release:classifier" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classified' - api "org.openrewrite:rewrite-core:latest.release:classified" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier(group, artifact, "classified", null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithoutVersion(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier(group, artifact, "classified", null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", classifier: "classifier" - } - """, - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classified' - api group: "org.openrewrite", name: "rewrite-core", classifier: "classified" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier(group, artifact, "classified", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier' - api "org.openrewrite:rewrite-core:latest.release:classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classified' - api "org.openrewrite:rewrite-core:latest.release:classified" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithExt(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyClassifier(group, artifact, "classified", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - api "org.openrewrite:rewrite-core:latest.release:classifier@ext" - api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", classifier: "classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classified@ext' - api "org.openrewrite:rewrite-core:latest.release:classified@ext" - api group: 'org.openrewrite', name: 'rewrite-core', classifier: 'classified' - api group: "org.openrewrite", name: "rewrite-core", classifier: "classified" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classified', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classified", extension: "ext" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyConfigurationTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyConfigurationTest.kt deleted file mode 100644 index 391ded6ce7d..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyConfigurationTest.kt +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Disabled -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyConfigurationTest : RewriteTest { - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration("org.openrewrite", "*", "implementation", "")) - }, - buildGradle(""" - dependencies { - compile 'org.openrewrite:rewrite-gradle:latest.release' - } - """, - """ - dependencies { - implementation 'org.openrewrite:rewrite-gradle:latest.release' - } - """) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration(group, artifact, "implementation", null)) - }, - buildGradle( - """ - dependencies { - compile 'org.openrewrite:rewrite-core:latest.release' - compile "org.openrewrite:rewrite-core:latest.release" - } - """, - """ - dependencies { - implementation 'org.openrewrite:rewrite-core:latest.release' - implementation "org.openrewrite:rewrite-core:latest.release" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration(group, artifact, "implementation", null)) - }, - buildGradle( - """ - dependencies { - compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' - compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release" - } - """, - """ - dependencies { - implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' - implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release" - } - """ - ) - ) - - @Disabled - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun withoutVersionShouldNotChange(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration(group, artifact, "implementation", null)) - }, - buildGradle( - """ - dependencies { - compile 'org.openrewrite:rewrite-core' - compile "org.openrewrite:rewrite-core" - compile group: 'org.openrewrite', name: 'rewrite-core' - compile group: "org.openrewrite", name: "rewrite-core" - } - """, - """ - dependencies { - implementation 'org.openrewrite:rewrite-core' - implementation "org.openrewrite:rewrite-core" - implementation group: 'org.openrewrite', name: 'rewrite-core' - implementation group: "org.openrewrite", name: "rewrite-core" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration(group, artifact, "implementation", null)) - }, - buildGradle( - """ - dependencies { - compile 'org.openrewrite:rewrite-core:latest.release:classifier' - compile "org.openrewrite:rewrite-core:latest.release:classifier" - compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - implementation 'org.openrewrite:rewrite-core:latest.release:classifier' - implementation "org.openrewrite:rewrite-core:latest.release:classifier" - implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithExt(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyConfiguration(group, artifact, "implementation", null)) - }, - buildGradle( - """ - dependencies { - compile 'org.openrewrite:rewrite-core@ext' - compile "org.openrewrite:rewrite-core@ext" - compile 'org.openrewrite:rewrite-core:latest.release@ext' - compile "org.openrewrite:rewrite-core:latest.release@ext" - compile 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - compile "org.openrewrite:rewrite-core:latest.release:classifier@ext" - compile group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' - compile group: "org.openrewrite", name: "rewrite-core", extension: "ext" - compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' - compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" - compile group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - compile group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """, - """ - dependencies { - implementation 'org.openrewrite:rewrite-core@ext' - implementation "org.openrewrite:rewrite-core@ext" - implementation 'org.openrewrite:rewrite-core:latest.release@ext' - implementation "org.openrewrite:rewrite-core:latest.release@ext" - implementation 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - implementation "org.openrewrite:rewrite-core:latest.release:classifier@ext" - implementation group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' - implementation group: "org.openrewrite", name: "rewrite-core", extension: "ext" - implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' - implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" - implementation group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - implementation group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyExtensionTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyExtensionTest.kt deleted file mode 100644 index 5574e25aa67..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyExtensionTest.kt +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyExtensionTest : RewriteTest { - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyExtension("org.openrewrite", "*", "jar", "")) - }, - buildGradle(""" - dependencies { - api 'org.openrewrite:rewrite-gradle:latest.integration@war' - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-gradle:latest.integration@jar' - } - """) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyExtension(group, artifact, "jar", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release@war' - api "org.openrewrite:rewrite-core:latest.release@war" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release@jar' - api "org.openrewrite:rewrite-core:latest.release@jar" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyExtension(group, artifact, "jar", null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'war' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "war" - } - """, - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'jar' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "jar" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithoutVersion(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyExtension(group, artifact, "jar", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core@war' - api "org.openrewrite:rewrite-core@war" - api group: 'org.openrewrite', name: 'rewrite-core', ext: 'war' - api group: "org.openrewrite", name: "rewrite-core", ext: "war" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core@jar' - api "org.openrewrite:rewrite-core@jar" - api group: 'org.openrewrite', name: 'rewrite-core', ext: 'jar' - api group: "org.openrewrite", name: "rewrite-core", ext: "jar" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyExtension(group, artifact, "jar", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier@war' - api "org.openrewrite:rewrite-core:latest.release:classifier@war" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'war' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "war" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier@jar' - api "org.openrewrite:rewrite-core:latest.release:classifier@jar" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'jar' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "jar" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyGroupIdTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyGroupIdTest.kt deleted file mode 100755 index dff50300613..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyGroupIdTest.kt +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyGroupIdTest : RewriteTest { - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId("org.springframework.boot", "spring-boot-starter", "org.newboot", "")) - }, - buildGradle( - """ - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.springframework.boot:spring-boot-starter:2.5.4' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """, - """ - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.newboot:spring-boot-starter:2.5.4' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId(group, artifact, "org.dewrite", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release' - api "org.openrewrite:rewrite-core:latest.release" - } - """, - """ - dependencies { - api 'org.dewrite:rewrite-core:latest.release' - api "org.dewrite:rewrite-core:latest.release" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId(group, artifact, "org.dewrite", null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" - } - """, - """ - dependencies { - api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release' - api group: "org.dewrite", name: "rewrite-core", version: "latest.release" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithoutVersion(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId(group, artifact, "org.dewrite", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core' - api "org.openrewrite:rewrite-core" - api group: 'org.openrewrite', name: 'rewrite-core' - api group: "org.openrewrite", name: "rewrite-core" - } - """, - """ - dependencies { - api 'org.dewrite:rewrite-core' - api "org.dewrite:rewrite-core" - api group: 'org.dewrite', name: 'rewrite-core' - api group: "org.dewrite", name: "rewrite-core" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId(group, artifact, "org.dewrite", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier' - api "org.openrewrite:rewrite-core:latest.release:classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - api 'org.dewrite:rewrite-core:latest.release:classifier' - api "org.dewrite:rewrite-core:latest.release:classifier" - api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.dewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithExt(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyGroupId(group, artifact, "org.dewrite", null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core@ext' - api "org.openrewrite:rewrite-core@ext" - api 'org.openrewrite:rewrite-core:latest.release@ext' - api "org.openrewrite:rewrite-core:latest.release@ext" - api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - api "org.openrewrite:rewrite-core:latest.release:classifier@ext" - api group: 'org.openrewrite', name: 'rewrite-core', ext: 'ext' - api group: "org.openrewrite", name: "rewrite-core", ext: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', ext: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", ext: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "ext" - } - """, - """ - dependencies { - api 'org.dewrite:rewrite-core@ext' - api "org.dewrite:rewrite-core@ext" - api 'org.dewrite:rewrite-core:latest.release@ext' - api "org.dewrite:rewrite-core:latest.release@ext" - api 'org.dewrite:rewrite-core:latest.release:classifier@ext' - api "org.dewrite:rewrite-core:latest.release:classifier@ext" - api group: 'org.dewrite', name: 'rewrite-core', ext: 'ext' - api group: "org.dewrite", name: "rewrite-core", ext: "ext" - api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', ext: 'ext' - api group: "org.dewrite", name: "rewrite-core", version: "latest.release", ext: "ext" - api group: 'org.dewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', ext: 'ext' - api group: "org.dewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", ext: "ext" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyVersionTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyVersionTest.kt deleted file mode 100755 index 319d2c086c6..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeDependencyVersionTest.kt +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.CsvSource -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class ChangeDependencyVersionTest : RewriteTest { - @Test - fun worksWithEmptyStringConfig() = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion("org.springframework.boot", "*", "2.5.5", null, "")) - }, - buildGradle(""" - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.springframework.boot:spring-boot-starter:2.5.4' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """, - """ - dependencies { - rewrite 'org.openrewrite:rewrite-gradle:latest.integration' - implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.3' - implementation 'org.springframework.integration:spring-integration-ftp:5.5.1' - implementation 'org.springframework.boot:spring-boot-starter:2.5.5' - implementation 'commons-lang:commons-lang:2.6' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - } - """) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion(group, artifact, "latest.integration", null, null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release' - api "org.openrewrite:rewrite-core:latest.release" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.integration' - api "org.openrewrite:rewrite-core:latest.integration" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun findMapStyleDependency(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion(group, artifact, "latest.integration", null, null)) - }, - buildGradle( - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release" - } - """, - """ - dependencies { - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun withoutVersionShouldNotChange(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion(group, artifact, "latest.integration", null, null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core' - api "org.openrewrite:rewrite-core" - api group: 'org.openrewrite', name: 'rewrite-core' - api group: "org.openrewrite", name: "rewrite-core" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithClassifier(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion(group, artifact, "latest.integration", null, null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.release:classifier' - api "org.openrewrite:rewrite-core:latest.release:classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core:latest.integration:classifier' - api "org.openrewrite:rewrite-core:latest.integration:classifier" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', classifier: 'classifier' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", classifier: "classifier" - } - """ - ) - ) - - @ParameterizedTest - @CsvSource(value = ["org.openrewrite:rewrite-core", "*:*"], delimiterString = ":") - fun worksWithExt(group: String, artifact: String) = rewriteRun( - { spec -> - spec.recipe(ChangeDependencyVersion(group, artifact, "latest.integration", null, null)) - }, - buildGradle( - """ - dependencies { - api 'org.openrewrite:rewrite-core@ext' - api "org.openrewrite:rewrite-core@ext" - api 'org.openrewrite:rewrite-core:latest.release@ext' - api "org.openrewrite:rewrite-core:latest.release@ext" - api 'org.openrewrite:rewrite-core:latest.release:classifier@ext' - api "org.openrewrite:rewrite-core:latest.release:classifier@ext" - api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release', classifier: 'classifier', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.release", classifier: "classifier", extension: "ext" - } - """, - """ - dependencies { - api 'org.openrewrite:rewrite-core@ext' - api "org.openrewrite:rewrite-core@ext" - api 'org.openrewrite:rewrite-core:latest.integration@ext' - api "org.openrewrite:rewrite-core:latest.integration@ext" - api 'org.openrewrite:rewrite-core:latest.integration:classifier@ext' - api "org.openrewrite:rewrite-core:latest.integration:classifier@ext" - api group: 'org.openrewrite', name: 'rewrite-core', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", extension: "ext" - api group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.integration', classifier: 'classifier', extension: 'ext' - api group: "org.openrewrite", name: "rewrite-core", version: "latest.integration", classifier: "classifier", extension: "ext" - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeJavaCompatibilityTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeJavaCompatibilityTest.kt deleted file mode 100644 index 07f85de3b10..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/ChangeJavaCompatibilityTest.kt +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.DisplayName -import org.junit.jupiter.api.Nested -import org.junit.jupiter.api.Test - -class ChangeJavaCompatibilityTest : GradleRecipeTest { - @Nested - @DisplayName("When requested version is an integer") - class NewInteger : BaseTest("8") - - @Nested - @DisplayName("When requested version is a double") - class NewDouble : BaseTest("1.8") - - @Nested - @DisplayName("When requested version is a string (double quotes)") - class NewStringDoubleQuoted : BaseTest("\"1.8\"") - - @Nested - @DisplayName("When requested version is a string (single quotes -tmp)") - class NewStringSingleQuoted : BaseTest("'1.8'") - - @Nested - @DisplayName("When requested version is an enum (shorthand)") - class NewEnumShorthand : BaseTest("VERSION_1_8") - - @Nested - @DisplayName("When requested version is an enum") - class NewEnum : BaseTest("JavaVersion.VERSION_1_8") - - abstract class BaseTest(val newVersion: String) : GradleRecipeTest { - @Test - fun changeSourceCompatibility() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - sourceCompatibility = 7 - """, - after = """ - plugins { - java - } - - sourceCompatibility = ${coerce(newVersion)} - """ - ) - - @Test - fun changeTargetCompatibility() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - targetCompatibility = '7' - """, - after = """ - plugins { - java - } - - targetCompatibility = ${coerce(newVersion)} - """ - ) - - @Test - fun changeSetSourceCompatibility() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - setSourceCompatibility "7" - """, - after = """ - plugins { - java - } - - setSourceCompatibility ${coerce(newVersion)} - """ - ) - - @Test - fun changeSetTargetCompatibility() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - setTargetCompatibility 1.7 - """, - after = """ - plugins { - java - } - - setTargetCompatibility ${coerce(newVersion)} - """ - ) - - @Test - fun changeSourceCompatibility_JavaPluginExtension() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - java { - sourceCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - java { - sourceCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeTargetCompatibility_JavaPluginExtension() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - java { - targetCompatibility = '1.7' - } - """, - after = """ - plugins { - java - } - - java { - targetCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeSourceCompatibility_CompileJava() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - compileJava { - sourceCompatibility = JavaVersion.VERSION_1_7 - } - """, - after = """ - plugins { - java - } - - compileJava { - sourceCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeTargetCompatibility_CompileJava() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - compileJava { - targetCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - compileJava { - targetCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeSourceCompatibility_TasksNamedCompileJava() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - tasks.named("compileJava") { - sourceCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - tasks.named("compileJava") { - sourceCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeTargetCompatibility_TasksNamedCompileJava() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - tasks.named("compileJava") { - targetCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - tasks.named("compileJava") { - targetCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeSourceCompatibility_TasksWithTypeJavaCompile() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - tasks.withType(JavaCompile).configureEach { - sourceCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - tasks.withType(JavaCompile).configureEach { - sourceCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeTargetCompatibility_TasksWithTypeJavaCompile() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - tasks.withType(JavaCompile).configureEach { - targetCompatibility = "1.7" - } - """, - after = """ - plugins { - java - } - - tasks.withType(JavaCompile).configureEach { - targetCompatibility = ${coerce(newVersion)} - } - """ - ) - - @Test - fun changeSourceCompatibility_JavaPluginExtensionFieldAccess() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "source"), - before = """ - plugins { - java - } - - java.sourceCompatibility = "1.7" - """, - after = """ - plugins { - java - } - - java.sourceCompatibility = ${coerce(newVersion)} - """ - ) - - @Test - fun changeTargetCompatibility_JavaPluginExtensionFieldAccess() = assertChanged( - recipe = ChangeJavaCompatibility(newVersion, "target"), - before = """ - plugins { - java - } - - java.targetCompatibility = "1.7" - """, - after = """ - plugins { - java - } - - java.targetCompatibility = ${coerce(newVersion)} - """ - ) - - private fun coerce(version: String): String { - return if (version.startsWith("VERSION_")) "JavaVersion.$version" else version - } - } -} \ No newline at end of file diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseMapNotationTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseMapNotationTest.kt deleted file mode 100755 index 38819e9f38a..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseMapNotationTest.kt +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.openrewrite.Recipe - -class DependencyUseMapNotationTest: GradleRecipeTest { - override val recipe: Recipe - get() = DependencyUseMapNotation() - - @Test - fun basicString() = assertChanged( - before = """ - dependencies { - api('org.openrewrite:rewrite-core:latest.release') - implementation "group:artifact:version" - } - """, - after = """ - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') - implementation group: 'group', name: 'artifact', version: 'version' - } - """ - ) - - @Test - fun withGString() = assertChanged( - before = """ - def version = "latest.release" - dependencies { - api("org.openrewrite:rewrite-core:${"$"}version") - implementation "group:artifact:${"$"}version" - } - """, - after = """ - def version = "latest.release" - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: version) - implementation group: 'group', name: 'artifact', version: version - } - """ - ) - - @Test - fun withExclusion() = assertChanged( - before = """ - dependencies { - api("org.openrewrite:rewrite-core:latest.release") { - exclude group: "group", module: "artifact" - } - implementation "group:artifact:version", { - exclude group: "group2", module: "artifact2" - } - } - """, - after = """ - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') { - exclude group: "group", module: "artifact" - } - implementation group: 'group', name: 'artifact', version: 'version', { - exclude group: "group2", module: "artifact2" - } - } - """ - ) - - @Test - fun withGStringAndExclusion() = assertChanged( - before = """ - dependencies { - api("org.openrewrite:rewrite-core:${"$"}version") { - exclude group: "group", module: "artifact" - } - implementation "group:artifact:${"$"}version", { - exclude group: "group2", module: "artifact2" - } - } - """, - after = """ - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: version) { - exclude group: "group", module: "artifact" - } - implementation group: 'group', name: 'artifact', version: version, { - exclude group: "group2", module: "artifact2" - } - } - """ - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseStringNotationTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseStringNotationTest.kt deleted file mode 100644 index dedd0f714cf..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/DependencyUseStringNotationTest.kt +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.openrewrite.Recipe - -class DependencyUseStringNotationTest: GradleRecipeTest { - override val recipe: Recipe - get() = DependencyUseStringNotation() - - @Test - fun basicMap() = assertChanged( - before = """ - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release') - implementation group: 'group', name: 'artifact', version: 'version' - } - """, - after = """ - dependencies { - api("org.openrewrite:rewrite-core:latest.release") - implementation "group:artifact:version" - } - """ - ) - - @Test - fun basicMapLiteral() = assertChanged( - before = """ - dependencies { - api([group: 'org.openrewrite', name: 'rewrite-core', version: 'latest.release']) - } - """, - after = """ - dependencies { - api("org.openrewrite:rewrite-core:latest.release") - } - """ - ) - - @Test - fun withGString() = assertChanged( - before = """ - def version = "latest.release" - dependencies { - api(group: 'org.openrewrite', name: 'rewrite-core', version: version) - implementation group: 'group', name: 'artifact', version: version - } - """, - after = """ - def version = "latest.release" - dependencies { - api("org.openrewrite:rewrite-core:${"$"}version") - implementation "group:artifact:${"$"}version" - } - """ - ) - - @Test - fun withoutVersion() = assertChanged( - before = """ - dependencies { - api(group: "org.openrewrite", name: "rewrite-core") - implementation group: "group", name: "artifact" - } - """, - after = """ - dependencies { - api("org.openrewrite:rewrite-core") - implementation "group:artifact" - } - """ - ) - - @Test - fun withExclusion() = assertChanged( - before = """ - dependencies { - api(group: "org.openrewrite", name: "rewrite-core", version: "latest.release") { - exclude group: "group", module: "artifact" - } - } - """, - after = """ - dependencies { - api("org.openrewrite:rewrite-core:latest.release") { - exclude group: "group", module: "artifact" - } - } - """ - ) - - @Test - fun withGStringExclusion() = assertChanged( - before = """ - def version = "latest.release" - dependencies { - api(group: "org.openrewrite", name: "rewrite-core", version: version) { - exclude group: "group", module: "artifact" - } - } - """, - after = """ - def version = "latest.release" - dependencies { - api("org.openrewrite:rewrite-core:${"$"}version") { - exclude group: "group", module: "artifact" - } - } - """ - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/RemoveRepositoryTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/RemoveRepositoryTest.kt deleted file mode 100755 index 8636c3c83ee..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/RemoveRepositoryTest.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test - -class RemoveRepositoryTest : GradleRecipeTest { - @Test - fun removeJcenter() = assertChanged( - recipe = RemoveRepository("jcenter"), - before = """ - repositories { - jcenter() - } - """, - after = """ - repositories { - - } - """ - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpdateGradleWrapperTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpdateGradleWrapperTest.kt deleted file mode 100755 index 523fd07c39b..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpdateGradleWrapperTest.kt +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Test -import org.openrewrite.PathUtils -import org.openrewrite.Tree.randomId -import org.openrewrite.gradle.util.GradleWrapper.* -import org.openrewrite.marker.Markers -import org.openrewrite.properties.PropertiesParser -import org.openrewrite.properties.tree.Properties -import org.openrewrite.quark.Quark -import org.openrewrite.remote.Remote -import org.openrewrite.text.PlainText -import java.net.URI -import java.nio.file.Paths - -@Suppress("UnusedProperty") -class UpdateGradleWrapperTest { - - private val gradlew = PlainText(randomId(), WRAPPER_SCRIPT_LOCATION, Markers.EMPTY,null, false, null, null,"") - private val gradlewBat = PlainText(randomId(), WRAPPER_BATCH_LOCATION, Markers.EMPTY, null, false, null, null,"") - private val gradleWrapperJarQuark = Quark(randomId(), WRAPPER_JAR_LOCATION, Markers.EMPTY, null, null) - - @Test - fun updateVersionAndDistribution() { - val gradleWrapperProps = PropertiesParser().parse( - """ - distributionBase=GRADLE_USER_HOME - distributionPath=wrapper/dists - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip - zipStoreBase=GRADLE_USER_HOME - zipStorePath=wrapper/dists - """.trimIndent())[0]!! - .withSourcePath(Paths.get("gradle", "wrapper", "gradle-wrapper.properties")) - - val result = UpdateGradleWrapper("7.4.2", null) - .run(listOf(gradleWrapperProps, gradlew, gradlewBat, gradleWrapperJarQuark)) - .results - .map { it.after } - assertThat(result.size).isEqualTo(4) - - val props = result.filterIsInstance<Properties.File>()[0] - assertThat(props.printAll()).isEqualTo( - //language=properties - """ - distributionBase=GRADLE_USER_HOME - distributionPath=wrapper/dists - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip - zipStoreBase=GRADLE_USER_HOME - zipStorePath=wrapper/dists - """.trimIndent() - ) - - val gradleSh = result.filterIsInstance<PlainText>().first { it.sourcePath.endsWith("gradlew") } - assertThat(gradleSh.text).isNotBlank - - val gradleBat = result.filterIsInstance<PlainText>().first { it.sourcePath.endsWith("gradlew.bat") } - assertThat(gradleBat.text).isNotBlank - - val gradleWrapperJar = result.filterIsInstance<Remote>().first { it.sourcePath.endsWith("gradle-wrapper.jar") } - assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.sourcePath, WRAPPER_JAR_LOCATION)) - assertThat(gradleWrapperJar.uri).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")) - } - - @Test - fun updateVersionAndDistributionWindows() { - val gradleWrapperProps = PropertiesParser().parse( - """ - distributionBase=GRADLE_USER_HOME - distributionPath=wrapper/dists - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip - zipStoreBase=GRADLE_USER_HOME - zipStorePath=wrapper/dists - """.trimIndent() - )[0]!! - .withSourcePath( - Paths.get("gradle\\wrapper\\gradle-wrapper.properties") - ) - - val result = UpdateGradleWrapper("7.4.2", null) - .run(listOf(gradleWrapperProps, gradlew, gradlewBat, gradleWrapperJarQuark)) - .results - .map { it.after } - assertThat(result.size).isEqualTo(4) - - val props = result.filterIsInstance<Properties.File>()[0] - assertThat(props.printAll()).isEqualTo( - //language=properties - """ - distributionBase=GRADLE_USER_HOME - distributionPath=wrapper/dists - distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip - zipStoreBase=GRADLE_USER_HOME - zipStorePath=wrapper/dists - """.trimIndent() - ) - - val gradleSh = result.filterIsInstance<PlainText>().first { it.sourcePath.endsWith("gradlew") } - assertThat(gradleSh.text).isNotBlank - - val gradleBat = result.filterIsInstance<PlainText>().first { it.sourcePath.endsWith("gradlew.bat") } - assertThat(gradleBat.text).isNotBlank - - val gradleWrapperJar = result.filterIsInstance<Remote>() - .first { it.sourcePath.toString().endsWith("gradle-wrapper.jar") } - assertThat(PathUtils.equalIgnoringSeparators(gradleWrapperJar.sourcePath, WRAPPER_JAR_LOCATION)) - assertThat(gradleWrapperJar.uri).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")) - } -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpgradePluginVersionTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpgradePluginVersionTest.kt deleted file mode 100644 index 539267286dd..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/UpgradePluginVersionTest.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.gradle.plugins.UpgradePluginVersion -import org.openrewrite.test.RewriteTest - -class UpgradePluginVersionTest : RewriteTest { - - @Test - fun upgradePlugin() = rewriteRun( - { spec -> spec.recipe(UpgradePluginVersion("com.jfrog.bintray", "latest.patch", null)) }, - buildGradle(""" - plugins { - id 'com.jfrog.bintray' version '1.7.0' - id 'com.github.johnrengelman.shadow' version '6.1.0' - } - """, - """ - plugins { - id 'com.jfrog.bintray' version '1.7.3' - id 'com.github.johnrengelman.shadow' version '6.1.0' - } - """) - ) - - @Test - fun upgradePluginGlob() = rewriteRun( - { spec -> spec.recipe(UpgradePluginVersion("com.jfrog.*", "1.8.X", null)) }, - buildGradle(""" - plugins { - id 'com.jfrog.bintray' version '1.8.2' - } - """, - """ - plugins { - id 'com.jfrog.bintray' version '1.8.5' - } - """) - ) - - @Test - fun exactVersionDoesNotHaveToBeResolvable() = rewriteRun( - { spec -> spec.recipe(UpgradePluginVersion("com.madeup.doesnotexist", "2.0", null)) }, - buildGradle(""" - plugins { - id 'com.madeup.doesnotexist' version '1.0' - } - """, - """ - plugins { - id 'com.madeup.doesnotexist' version '2.0' - } - """) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyHandlerTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyHandlerTest.kt deleted file mode 100644 index 972833a96e1..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/FindDependencyHandlerTest.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.search - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.GradleRecipeTest - -class FindDependencyHandlerTest : GradleRecipeTest { - @Test - fun findDependenciesBlock() = assertChanged( - recipe = fromRuntimeClasspath("org.openrewrite.gradle.search.FindDependencyHandler"), - before = """ - dependencies { - api 'com.google.guava:guava:23.0' - } - """, - after = """ - /*~~>*/dependencies { - api 'com.google.guava:guava:23.0' - } - """ - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/package-info.java b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/package-info.java deleted file mode 100755 index 390fa215651..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/search/package-info.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -@NonNullApi -package org.openrewrite.gradle.search; - -import org.openrewrite.internal.lang.NonNullApi; diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.kt deleted file mode 100644 index 7afa257ee59..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/security/UseHttpsForRepositoriesTest.kt +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2022 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.security - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RecipeSpec -import org.openrewrite.test.RewriteTest - -@Suppress("HttpUrlsUsage") -class UseHttpsForRepositoriesTest : RewriteTest { - override fun defaults(spec: RecipeSpec) { - spec.recipe(UseHttpsForRepositories()) - } - - @Test - fun unchangedUseOfHttps() = rewriteRun( - buildGradle( - """ - repositories { - maven { url 'https://repo.spring.example.com/libs-release-local' } - } - """ - ) - ) - - @Test - fun updateUnwrappedInvocationToUseHttpsSingleQuote() = rewriteRun( - buildGradle( - """ - repositories { - maven { url 'http://repo.spring.example.com/libs-release-local' } - } - """, - """ - repositories { - maven { url 'https://repo.spring.example.com/libs-release-local' } - } - """ - ) - ) - - @Test - fun updateUnwrappedInvocationToUseHttpsDoubleQuote() = rewriteRun( - buildGradle( - """ - repositories { - maven { url "http://repo.spring.example.com/libs-release-local" } - } - """, - """ - repositories { - maven { url "https://repo.spring.example.com/libs-release-local" } - } - """ - ) - ) - - @Test - fun updateUnwrappedInvocationToUseHttpsGString() = rewriteRun( - buildGradle( - """ - repositories { - maven { - def subRepo = properties.snapshot ? 'snapshot' : 'release' - url "http://repo.spring.example.com/libs-release-local/${'$'}subRepo" - } - } - """, - """ - repositories { - maven { - def subRepo = properties.snapshot ? 'snapshot' : 'release' - url "https://repo.spring.example.com/libs-release-local/${'$'}subRepo" - } - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/BuildscriptTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/BuildscriptTest.kt deleted file mode 100755 index 5a6b577b720..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/BuildscriptTest.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.tree - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class BuildscriptTest : RewriteTest { - - @Test - fun buildscriptWithDependencies() = rewriteRun( - buildGradle( - """ - buildscript { - repositories { - mavenCentral() - } - dependencies { - classpath 'com.netflix.nebula:nebula-dependency-recommender:9.1.1' - classpath 'com.netflix.nebula:gradle-netflixoss-project-plugin:9.1.0' - } - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/RepositoryTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/RepositoryTest.kt deleted file mode 100755 index 83370647ce1..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/RepositoryTest.kt +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.tree - -import org.junit.jupiter.api.Test -import org.openrewrite.Issue -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class RepositoryTest : RewriteTest { - - @Test - fun builtInFunctions() = rewriteRun( - buildGradle( - """ - repositories { - mavenCentral() - mavenLocal() - } - """ - ) - ) - - @Issue("https://github.com/openrewrite/rewrite/issues/1253") - @Test - fun customMavenRepo() = rewriteRun( - buildGradle( - """ - repositories { - maven { - url "https://repo.spring.io/release" - } - maven { - setUrl("https://repo.spring.io/release") - } - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TaskTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TaskTest.kt deleted file mode 100755 index f85426bdf5c..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TaskTest.kt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.tree - -import org.junit.jupiter.api.Test -import org.openrewrite.Issue -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.TypeValidation -import org.openrewrite.test.RewriteTest - -class TaskTest : RewriteTest { - - @Issue("https://github.com/openrewrite/rewrite/issues/1236") - @Test - fun declareTaskOldStyle() = rewriteRun( - { spec -> spec.typeValidationOptions(TypeValidation.none()) }, - buildGradle( - """ - task(testWithCloud, type: Test) { - useTestNG() - options.excludeGroups = [] as Set - } - """ - ) - ) - - @Issue("https://github.com/openrewrite/rewrite/issues/1929") - @Test - fun testDsl() = rewriteRun( - { spec -> spec.typeValidationOptions(TypeValidation.none()) }, - buildGradle( - """ - test { - // Ex: -PexcludeTests=com/google/cloud/healthcare/etl/runner/hl7v2tofhir/integ/* - if (project.hasProperty('excludeTests')) { - exclude project.property('excludeTests') as String - } - dependsOn('buildDeps') - } - """ - ) - ) -} diff --git a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TestTest.kt b/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TestTest.kt deleted file mode 100755 index 96f11c02420..00000000000 --- a/rewrite-gradle/src/test/kotlin/org/openrewrite/gradle/tree/TestTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * <p> - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * <p> - * https://www.apache.org/licenses/LICENSE-2.0 - * <p> - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle.tree - -import org.junit.jupiter.api.Test -import org.openrewrite.gradle.Assertions.buildGradle -import org.openrewrite.test.RewriteTest - -class TestTest : RewriteTest { - - @Test - fun testDsl() = rewriteRun( - buildGradle( - """ - test { - useJUnit() - useTestNG() - useJUnitPlatform() - systemProperty 'some.prop', 'value' - include 'org/foo/**' - exclude 'org/boo/**' - testLogging.showStandardStreams = true - minHeapSize = "128m" - maxHeapSize = "512m" - jvmArgs '-XX:MaxPermSize=256m' - failFast = true - } - """ - ) - ) -} diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/Assertions.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/Assertions.java index 334577a4299..443fc775ea4 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/Assertions.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/Assertions.java @@ -36,7 +36,7 @@ public static SourceSpecs groovy(@Language("groovy") @Nullable String before) { } public static SourceSpecs groovy(@Language("groovy") @Nullable String before, Consumer<SourceSpec<G.CompilationUnit>> spec) { - SourceSpec<G.CompilationUnit> groovy = new SourceSpec<>(G.CompilationUnit.class, null, GroovyParser.builder(), before, null); + SourceSpec<G.CompilationUnit> groovy = new SourceSpec<>(G.CompilationUnit.class, null, GroovyParser.builder(), before, (String) null); spec.accept(groovy); return groovy; } diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/Assertions.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/Assertions.java index a9c0c3ae4da..3acf1c82d0a 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/Assertions.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/Assertions.java @@ -31,7 +31,7 @@ public static SourceSpecs hcl(@Nullable String before) { } public static SourceSpecs hcl(@Nullable String before, Consumer<SourceSpec<Hcl.ConfigFile>> spec) { - SourceSpec<Hcl.ConfigFile> hcl = new SourceSpec<>(Hcl.ConfigFile.class, null, HclParser.builder(), before, null); + SourceSpec<Hcl.ConfigFile> hcl = new SourceSpec<>(Hcl.ConfigFile.class, null, HclParser.builder(), before, (String) null); spec.accept(hcl); return hcl; } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java b/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java index acf331644a3..0d6f3752c87 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/Assertions.java @@ -110,7 +110,8 @@ public static SourceSpecs java(@Language("java") @Nullable String before, @Langu public static SourceSpecs java(@Language("java") @Nullable String before, @Language("java") @Nullable String after, Consumer<SourceSpec<J.CompilationUnit>> spec) { - SourceSpec<J.CompilationUnit> java = new SourceSpec<>(J.CompilationUnit.class, null, javaParser, before, after, + SourceSpec<J.CompilationUnit> java = new SourceSpec<>(J.CompilationUnit.class, null, javaParser, before, + s -> after, Assertions::validateTypes, Assertions::customizeExecutionContext); acceptSpec(spec, java); diff --git a/rewrite-json/src/main/java/org/openrewrite/json/Assertions.java b/rewrite-json/src/main/java/org/openrewrite/json/Assertions.java index 60ef0b0d05f..b394f3c6fc3 100644 --- a/rewrite-json/src/main/java/org/openrewrite/json/Assertions.java +++ b/rewrite-json/src/main/java/org/openrewrite/json/Assertions.java @@ -35,7 +35,7 @@ public static SourceSpecs json(@Language("json") @Nullable String before) { } public static SourceSpecs json(@Language("json") @Nullable String before, Consumer<SourceSpec<Json.Document>> spec) { - SourceSpec<Json.Document> json = new SourceSpec<>(Json.Document.class, null, JsonParser.builder(), before, null); + SourceSpec<Json.Document> json = new SourceSpec<>(Json.Document.class, null, JsonParser.builder(), before, (String) null); spec.accept(json); return json; } diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java index a2d4d115a42..8ae9cc779d0 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/Assertions.java @@ -55,7 +55,7 @@ public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Lang public static SourceSpecs pomXml(@Language("xml") @Nullable String before, @Language("xml") @Nullable String after, Consumer<SourceSpec<Xml.Document>> spec) { SourceSpec<Xml.Document> maven = new SourceSpec<>(Xml.Document.class, "maven", MavenParser.builder(), before, - after, SourceSpec.EachResult.noop, Assertions::customizeExecutionContext); + s -> after, SourceSpec.EachResult.noop, Assertions::customizeExecutionContext); maven.path("pom.xml"); spec.accept(maven); return maven; diff --git a/rewrite-properties/src/main/java/org/openrewrite/properties/Assertions.java b/rewrite-properties/src/main/java/org/openrewrite/properties/Assertions.java index 5e980b22206..5e3be29d4cb 100644 --- a/rewrite-properties/src/main/java/org/openrewrite/properties/Assertions.java +++ b/rewrite-properties/src/main/java/org/openrewrite/properties/Assertions.java @@ -32,7 +32,7 @@ public static SourceSpecs properties(@Language("properties") @Nullable String be } public static SourceSpecs properties(@Language("properties") @Nullable String before, Consumer<SourceSpec<Properties.File>> spec) { - SourceSpec<Properties.File> properties = new SourceSpec<>(Properties.File.class, null, PropertiesParser.builder(), before, null); + SourceSpec<Properties.File> properties = new SourceSpec<>(Properties.File.class, null, PropertiesParser.builder(), before, (String) null); spec.accept(properties); return properties; } diff --git a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/Assertions.java b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/Assertions.java index a02ac111d18..65ca1869d30 100644 --- a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/Assertions.java +++ b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/Assertions.java @@ -34,7 +34,7 @@ public static SourceSpecs proto(@Language("protobuf") @Nullable String before) { } public static SourceSpecs proto(@Language("protobuf") @Nullable String before, Consumer<SourceSpec<Proto.Document>> spec) { - SourceSpec<Proto.Document> proto = new SourceSpec<>(Proto.Document.class, null, ProtoParser.builder(), before, null); + SourceSpec<Proto.Document> proto = new SourceSpec<>(Proto.Document.class, null, ProtoParser.builder(), before, (String) null); spec.accept(proto); return proto; } diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java b/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java index a2fd8eeda30..7b1cb9a7a12 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RecipeSpec.java @@ -84,7 +84,7 @@ public static RecipeSpec defaults() { }; // The before and after here don't mean anything - SourceSpec<SourceFile> allSources = new SourceSpec<>(SourceFile.class, null, QuarkParser.builder(), "", null); + SourceSpec<SourceFile> allSources = new SourceSpec<>(SourceFile.class, null, QuarkParser.builder(), "", (String) null); /** * Configuration that applies to all source file inputs. diff --git a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java index 705814000ea..5ed4772ee83 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java @@ -288,7 +288,7 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) sourceSpec.getSourcePath()) .isNull(); String actual = result.getAfter().printAll(out.clone()).trim(); - String expected = trimIndentPreserveCRLF(sourceSpec.after); + String expected = trimIndentPreserveCRLF(sourceSpec.after.apply(actual)); assertThat(actual).isEqualTo(expected); continue nextSourceSpec; } @@ -302,7 +302,7 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) if (result.getAfter() != null && !(result.getAfter() instanceof Remote)) { assertThat(sourceSpec.after).as("Either before or after must be specified in a SourceSpec").isNotNull(); String actual = result.getAfter().printAll(out.clone()).trim(); - String expected = trimIndentPreserveCRLF(sourceSpec.after); + String expected = trimIndentPreserveCRLF(sourceSpec.after.apply(actual)); if (actual.equals(expected)) { expectedNewSources.remove(sourceSpec); //noinspection unchecked @@ -321,7 +321,7 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) if (result.getAfter() instanceof Remote) { assertThat(sourceSpec.after).as("Either before or after must be specified in a SourceSpec").isNotNull(); String actual = result.getAfter().printAll(out.clone()); - String expected = trimIndentPreserveCRLF(sourceSpec.after); + String expected = trimIndentPreserveCRLF(sourceSpec.after.apply(actual)); if (actual.equals(expected)) { expectedNewSources.remove(sourceSpec); //noinspection unchecked @@ -338,24 +338,27 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) nextSourceFile: for (Map.Entry<SourceFile, SourceSpec<?>> specForSourceFile : specBySourceFile.entrySet()) { - String expectedAfter = specForSourceFile.getValue().after; for (Result result : recipeRun.getResults()) { if (result.getBefore() == specForSourceFile.getKey()) { - if (expectedAfter != null && result.getAfter() != null) { - String actual = result.getAfter().printAll(out.clone()); - String expected = trimIndentPreserveCRLF(expectedAfter); - assertThat(actual).isEqualTo(expected); - specForSourceFile.getValue().eachResult.accept(result.getAfter(), testMethodSpec, testClassSpec); - } else if (expectedAfter == null && result.getAfter() != null) { - if (result.diff().isEmpty()) { - fail("An empty diff was generated. The recipe incorrectly changed a reference without changing its contents."); + if(result.getAfter() != null) { + String expectedAfter = specForSourceFile.getValue().after == null ? null : + specForSourceFile.getValue().after.apply(result.getAfter().printAll(out.clone())); + if (expectedAfter != null) { + String actual = result.getAfter().printAll(out.clone()); + String expected = trimIndentPreserveCRLF(expectedAfter); + assertThat(actual).isEqualTo(expected); + specForSourceFile.getValue().eachResult.accept(result.getAfter(), testMethodSpec, testClassSpec); + } else { + if (result.diff().isEmpty() && !(result.getAfter() instanceof Remote)) { + fail("An empty diff was generated. The recipe incorrectly changed a reference without changing its contents."); + } + + assert result.getBefore() != null; + assertThat(result.getAfter().printAll(out.clone())) + .as("The recipe must not make changes") + .isEqualTo(result.getBefore().printAll(out.clone())); } - - assert result.getBefore() != null; - assertThat(result.getAfter().printAll(out.clone())) - .as("The recipe must not make changes") - .isEqualTo(result.getBefore().printAll(out.clone())); - } else if (expectedAfter != null && result.getAfter() == null) { + } else if (result.getAfter() == null) { assert result.getBefore() != null; fail("The recipe deleted a source file [" + result.getBefore().getSourcePath() + "] that was not expected to change"); } @@ -368,9 +371,9 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) } // if we get here, there was no result. - if (expectedAfter != null) { + if (specForSourceFile.getValue().after != null) { String before = trimIndentPreserveCRLF(specForSourceFile.getKey().printAll(out.clone())); - String expected = trimIndentPreserveCRLF(expectedAfter); + String expected = trimIndentPreserveCRLF(specForSourceFile.getValue().after.apply(null)); assertThat(before) .as("The recipe should have made the following change.") @@ -382,7 +385,9 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs) } SoftAssertions newFilesGenerated = new SoftAssertions(); for (SourceSpec<?> expectedNewSource : expectedNewSources) { - newFilesGenerated.assertThat(expectedNewSource.after).as("No new source file was generated that matched.").isEmpty(); + newFilesGenerated.assertThat(expectedNewSource.after == null ? null : expectedNewSource.after.apply(null)) + .as("No new source file was generated that matched.") + .isEmpty(); } newFilesGenerated.assertAll(); diff --git a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java index aefab6e53fc..26a09f81fe7 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpec.java @@ -29,6 +29,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.function.UnaryOperator; @RequiredArgsConstructor @EqualsAndHashCode(onlyExplicitlyIncluded = true) @@ -48,7 +49,7 @@ public class SourceSpec<T extends SourceFile> implements SourceSpecs { final String before; @Nullable - final String after; + final UnaryOperator<String> after; /** * Apply a function to each SourceFile after recipe execution. @@ -65,6 +66,11 @@ public interface EachResult { public SourceSpec(Class<T> sourceFileType, @Nullable String dsl, Parser.Builder parser, @Nullable String before, @Nullable String after) { + this(sourceFileType, dsl, parser, before, after == null ? null : s -> after); + } + + public SourceSpec(Class<T> sourceFileType, @Nullable String dsl, + Parser.Builder parser, @Nullable String before, @Nullable UnaryOperator<@Nullable String> after) { this.sourceFileType = sourceFileType; this.dsl = dsl; this.parser = parser; diff --git a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpecs.java b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpecs.java index 004ece4095e..4170aeaca46 100644 --- a/rewrite-test/src/main/java/org/openrewrite/test/SourceSpecs.java +++ b/rewrite-test/src/main/java/org/openrewrite/test/SourceSpecs.java @@ -15,7 +15,7 @@ */ package org.openrewrite.test; -import org.openrewrite.Parser; +import org.openrewrite.Incubating; import org.openrewrite.SourceFile; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.quark.Quark; @@ -24,6 +24,7 @@ import org.openrewrite.text.PlainTextParser; import java.util.function.Consumer; +import java.util.function.UnaryOperator; @SuppressWarnings("unused") public interface SourceSpecs extends Iterable<SourceSpec<?>> { @@ -42,7 +43,13 @@ static SourceSpecs other(@Nullable String before) { } static SourceSpecs other(@Nullable String before, Consumer<SourceSpec<Quark>> spec) { - SourceSpec<Quark> quark = new SourceSpec<>(Quark.class, null, QuarkParser.builder(), before, null); + SourceSpec<Quark> quark = new SourceSpec<>(Quark.class, null, QuarkParser.builder(), before, (String) null); + spec.accept(quark); + return quark; + } + + static SourceSpecs other(@Nullable String before, @Nullable UnaryOperator<@Nullable String> after, Consumer<SourceSpec<Quark>> spec) { + SourceSpec<Quark> quark = new SourceSpec<>(Quark.class, null, QuarkParser.builder(), before, after); spec.accept(quark); return quark; } @@ -65,7 +72,7 @@ static SourceSpecs text(@Nullable String before) { } static SourceSpecs text(@Nullable String before, Consumer<SourceSpec<PlainText>> spec) { - SourceSpec<PlainText> text = new SourceSpec<>(PlainText.class, null, PlainTextParser.builder(), before, null); + SourceSpec<PlainText> text = new SourceSpec<>(PlainText.class, null, PlainTextParser.builder(), before, (String) null); spec.accept(text); return text; } @@ -81,4 +88,12 @@ static SourceSpecs text(@Nullable String before, String after, spec.accept(text); return text; } + + @Incubating(since = "7.33.0") + static SourceSpecs text(@Nullable String before, @Nullable UnaryOperator<@Nullable String> after, + Consumer<SourceSpec<PlainText>> spec) { + SourceSpec<PlainText> text = new SourceSpec<>(PlainText.class, null, PlainTextParser.builder(), before, after); + spec.accept(text); + return text; + } } diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/Assertions.java b/rewrite-xml/src/main/java/org/openrewrite/xml/Assertions.java index 5331f87526d..b3687b19a9a 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/Assertions.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/Assertions.java @@ -33,7 +33,7 @@ public static SourceSpecs xml(@Language("xml") @Nullable String before) { } public static SourceSpecs xml(@Language("xml") @Nullable String before, Consumer<SourceSpec<Xml.Document>> spec) { - SourceSpec<Xml.Document> xml = new SourceSpec<>(Xml.Document.class, null, XmlParser.builder(), before, null); + SourceSpec<Xml.Document> xml = new SourceSpec<>(Xml.Document.class, null, XmlParser.builder(), before, (String) null); spec.accept(xml); return xml; } diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/Assertions.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/Assertions.java index f63bad23660..1c082aaaaf1 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/Assertions.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/Assertions.java @@ -34,7 +34,7 @@ public static SourceSpecs yaml(@Language("yml") @Nullable String before) { } public static SourceSpecs yaml(@Language("yml") @Nullable String before, Consumer<SourceSpec<Yaml.Documents>> spec) { - SourceSpec<Yaml.Documents> yaml = new SourceSpec<>(Yaml.Documents.class, null, YamlParser.builder(), before, null); + SourceSpec<Yaml.Documents> yaml = new SourceSpec<>(Yaml.Documents.class, null, YamlParser.builder(), before, (String) null); spec.accept(yaml); return yaml; }