Skip to content

Commit

Permalink
Spring Framework 6.0 recipe with migration for Spring Assert (#522)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
pativa and timtebeek authored May 8, 2024
1 parent 82ec0a4 commit eddf42e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/spring-boot-30.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions src/main/resources/META-INF/rewrite/spring-framework-60.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Copyright 2024 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.
#

########################################################################################################################
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 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.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");
}
}
"""
)
);
}
}

0 comments on commit eddf42e

Please sign in to comment.