From eddf42e7a46a04c4466379706d4fdc950ad89618 Mon Sep 17 00:00:00 2001 From: Patrik Ivarsson <83230533+pativa@users.noreply.github.com> Date: Wed, 8 May 2024 10:33:32 +0200 Subject: [PATCH] Spring Framework 6.0 recipe with migration for Spring `Assert` (#522) * Spring Framework 6.0 recipe * Only includes a migration for removed methods in `org.springframework.util.Assert` for now * Add a minimal unit test for MigrateSpringAssert * Add DocumentExample to MigrateSpringAssert --------- Co-authored-by: Tim te Beek --- .../META-INF/rewrite/spring-boot-30.yml | 1 + .../META-INF/rewrite/spring-framework-60.yml | 81 +++++++++++++++++++ .../framework/MigrateSpringAssertTest.java | 60 ++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 src/main/resources/META-INF/rewrite/spring-framework-60.yml create mode 100644 src/test/java/org/openrewrite/java/spring/framework/MigrateSpringAssertTest.java diff --git a/src/main/resources/META-INF/rewrite/spring-boot-30.yml b/src/main/resources/META-INF/rewrite/spring-boot-30.yml index 77f45f86c..b7d6c408c 100644 --- a/src/main/resources/META-INF/rewrite/spring-boot-30.yml +++ b/src/main/resources/META-INF/rewrite/spring-boot-30.yml @@ -66,6 +66,7 @@ recipeList: - org.openrewrite.java.spring.boot3.MigrateThymeleafDependencies - org.openrewrite.java.spring.boot3.UpgradeSpringDoc_2 - org.openrewrite.java.spring.boot3.MigrateDropWizardDependencies + - org.openrewrite.java.spring.framework.UpgradeSpringFramework_6_0 - org.openrewrite.java.spring.security6.UpgradeSpringSecurity_6_0 - org.openrewrite.java.spring.cloud2022.UpgradeSpringCloud_2022 - org.openrewrite.hibernate.MigrateToHibernate61 diff --git a/src/main/resources/META-INF/rewrite/spring-framework-60.yml b/src/main/resources/META-INF/rewrite/spring-framework-60.yml new file mode 100644 index 000000000..194ad9c9e --- /dev/null +++ b/src/main/resources/META-INF/rewrite/spring-framework-60.yml @@ -0,0 +1,81 @@ +# +# Copyright 2024 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. +# + +######################################################################################################################## +# Spring Framework 6.0 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.spring.framework.UpgradeSpringFramework_6_0 +displayName: Migrate to Spring Framework 6.0 +description: Migrate applications to the latest Spring Framework 6.0 release. +recipeList: + - org.openrewrite.java.spring.framework.UpgradeSpringFramework_5_3 + - org.openrewrite.java.dependencies.UpgradeDependencyVersion: + groupId: org.springframework + artifactId: "*" + newVersion: 6.0.x + - org.openrewrite.java.spring.framework.MigrateSpringAssert + +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.spring.framework.MigrateSpringAssert +displayName: Migrate removed Spring `Assert` methods +description: Assert methods without a message argument have been removed in Spring Framework 6.0. +recipeList: + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert state(boolean) + argumentIndex: 1 + literal: must be true + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert isTrue(boolean) + argumentIndex: 1 + literal: must be true + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert isNull(java.lang.Object) + argumentIndex: 1 + literal: must be null + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert notNull(java.lang.Object) + argumentIndex: 1 + literal: must not be null + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert hasLength(java.lang.String) + argumentIndex: 1 + literal: must have length; it must not be null or empty + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert hasText(java.lang.String) + argumentIndex: 1 + literal: must have text; it must not be null, empty, or blank + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert doesNotContain(java.lang.String, java.lang.String) + argumentIndex: 2 + literal: must not contain the substring + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert notEmpty(java.lang.Object[]) + argumentIndex: 1 + literal: must not be empty + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert noNullElements(java.lang.Object[]) + argumentIndex: 1 + literal: must not contain any null elements + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert noNullElements(java.util.Collection) + argumentIndex: 1 + literal: must not be empty + - org.openrewrite.java.AddLiteralMethodArgument: + methodPattern: org.springframework.util.Assert notEmpty(java.util.Map) + argumentIndex: 1 + literal: must not be empty diff --git a/src/test/java/org/openrewrite/java/spring/framework/MigrateSpringAssertTest.java b/src/test/java/org/openrewrite/java/spring/framework/MigrateSpringAssertTest.java new file mode 100644 index 000000000..51d5cfee8 --- /dev/null +++ b/src/test/java/org/openrewrite/java/spring/framework/MigrateSpringAssertTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2024 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.java.spring.framework; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class MigrateSpringAssertTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResources("org.openrewrite.java.spring.framework.MigrateSpringAssert") + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-core-5")); + } + + @Test + @DocumentExample + void migrateSpringAssert() { + rewriteRun( + //language=java + java( + """ + class A { + void test() { + org.springframework.util.Assert.state(true); + org.springframework.util.Assert.isTrue(true); + } + } + """, + """ + class A { + void test() { + org.springframework.util.Assert.state(true, "must be true"); + org.springframework.util.Assert.isTrue(true, "must be true"); + } + } + """ + ) + ); + } +}