From 4ecc3f224e54723132720da0d2b2484717c82576 Mon Sep 17 00:00:00 2001 From: Lucas Han Date: Sun, 19 May 2024 08:08:38 -0700 Subject: [PATCH] Create spring 2.7 data YML file with getReferenceById changes (#518) * Create spring 2.7 data YML file with getReferenceById changes * Include Spring Data migration with Spring Boot 2.7 migration * Add a quick unit test, that fails for now * Add DocumentExample * ChangeMethodName for Spring Data 2.5 should match overrides * Add missing spring-data-commons reference to test * Drop unused test dependency --------- Co-authored-by: Lucas Han Co-authored-by: Tim te Beek --- .../META-INF/rewrite/spring-boot-27.yml | 1 + .../META-INF/rewrite/spring-data-25.yml | 2 + .../META-INF/rewrite/spring-data-27.yml | 40 ++++++++++ .../UseJpaRepositoryGetReferenceByIdTest.java | 79 +++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/main/resources/META-INF/rewrite/spring-data-27.yml create mode 100644 src/testWithSpringBoot_2_7/java/org/openrewrite/java/spring/data/UseJpaRepositoryGetReferenceByIdTest.java diff --git a/src/main/resources/META-INF/rewrite/spring-boot-27.yml b/src/main/resources/META-INF/rewrite/spring-boot-27.yml index 067c6bbe9..d13a32af0 100644 --- a/src/main/resources/META-INF/rewrite/spring-boot-27.yml +++ b/src/main/resources/META-INF/rewrite/spring-boot-27.yml @@ -39,6 +39,7 @@ recipeList: groupId: org.springframework artifactId: "*" newVersion: 5.3.x + - org.openrewrite.java.spring.data.UpgradeSpringData_2_7 - org.openrewrite.java.spring.security5.UpgradeSpringSecurity_5_7 # Use recommended replacements for deprecated APIs - org.openrewrite.java.ChangeType: diff --git a/src/main/resources/META-INF/rewrite/spring-data-25.yml b/src/main/resources/META-INF/rewrite/spring-data-25.yml index 52223b492..6408ea49a 100644 --- a/src/main/resources/META-INF/rewrite/spring-data-25.yml +++ b/src/main/resources/META-INF/rewrite/spring-data-25.yml @@ -34,6 +34,7 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: org.springframework.data.jpa.repository.JpaRepository getOne(..) newMethodName: getById + matchOverrides: true --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.spring.data.UseJpaRepositoryDeleteAllInBatch @@ -43,3 +44,4 @@ recipeList: - org.openrewrite.java.ChangeMethodName: methodPattern: org.springframework.data.jpa.repository.JpaRepository deleteInBatch(..) newMethodName: deleteAllInBatch + matchOverrides: true diff --git a/src/main/resources/META-INF/rewrite/spring-data-27.yml b/src/main/resources/META-INF/rewrite/spring-data-27.yml new file mode 100644 index 000000000..b14a05ea4 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/spring-data-27.yml @@ -0,0 +1,40 @@ +# +# 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 Data 2.7 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.spring.data.UpgradeSpringData_2_7 +displayName: Migrate to Spring Data 2.7 +description: Migrate applications to the latest Spring Data 2.7 release. +recipeList: + - org.openrewrite.java.spring.data.UpgradeSpringData_2_5 + - org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById +displayName: Use `JpaRepository#getReferenceById(ID id)` +description: '`JpaRepository#getOne(ID)` was deprecated in 2.5 and `JpaRepository#getById(ID)` was deprecated in 2.7.' +recipeList: + - org.openrewrite.java.ChangeMethodName: + methodPattern: org.springframework.data.jpa.repository.JpaRepository getById(..) + newMethodName: getReferenceById + matchOverrides: true + - org.openrewrite.java.ChangeMethodName: + methodPattern: org.springframework.data.jpa.repository.JpaRepository getOne(..) + newMethodName: getReferenceById + matchOverrides: true diff --git a/src/testWithSpringBoot_2_7/java/org/openrewrite/java/spring/data/UseJpaRepositoryGetReferenceByIdTest.java b/src/testWithSpringBoot_2_7/java/org/openrewrite/java/spring/data/UseJpaRepositoryGetReferenceByIdTest.java new file mode 100644 index 000000000..91d1fddb0 --- /dev/null +++ b/src/testWithSpringBoot_2_7/java/org/openrewrite/java/spring/data/UseJpaRepositoryGetReferenceByIdTest.java @@ -0,0 +1,79 @@ +/* + * 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.data; + +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 UseJpaRepositoryGetReferenceByIdTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-data-commons-2.7", "spring-data-jpa-2.7")) + .recipeFromResource("/META-INF/rewrite/spring-data-27.yml", "org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById"); + } + + @Test + @DocumentExample + void matchAndUpdateReferences() { + //language=java + rewriteRun( + java( + """ + package foo; + public class Book {} + """ + ), + java( + """ + package foo; + import org.springframework.data.jpa.repository.JpaRepository; + public interface BookRepository extends JpaRepository { + } + """ + ), + java( + """ + import foo.*; + class A { + BookRepository repo; + void method(Long id) { + repo.getById(id); + repo.getOne(id); + } + } + """, + """ + import foo.*; + class A { + BookRepository repo; + void method(Long id) { + repo.getReferenceById(id); + repo.getReferenceById(id); + } + } + """ + ) + ); + } +}