1515 */
1616package org .openrewrite .java .spring .boot2 ;
1717
18+ import org .junit .jupiter .api .Nested ;
1819import org .junit .jupiter .api .Test ;
20+ import org .openrewrite .DocumentExample ;
1921import org .openrewrite .Issue ;
2022import org .openrewrite .config .Environment ;
21- import org .openrewrite .DocumentExample ;
23+ import org .openrewrite .gradle . Assertions ;
2224import org .openrewrite .test .RecipeSpec ;
2325import org .openrewrite .test .RewriteTest ;
2426
2527import java .util .regex .Matcher ;
2628import java .util .regex .Pattern ;
2729
2830import static org .assertj .core .api .Assertions .assertThat ;
31+ import static org .openrewrite .gradle .Assertions .withToolingApi ;
2932import static org .openrewrite .maven .Assertions .pomXml ;
3033
3134@ Issue ("https://github.com/openrewrite/rewrite-spring/issues/274" )
@@ -39,79 +42,54 @@ public void defaults(RecipeSpec spec) {
3942 .activateRecipes ("org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7" ));
4043 }
4144
42- @ DocumentExample
43- @ Test
44- void switchArtifactIdAndUpdateVersionNumber () {
45- rewriteRun (
46- //language=xml
47- pomXml (
48- """
49- <project>
50- <modelVersion>4.0.0</modelVersion>
51- <groupId>com.example</groupId >
52- <artifactId>demo</artifactId >
53- <version>0.0.1-SNAPSHOT</version >
54- <dependencies >
55- <dependency >
56- <groupId>mysql</groupId >
57- <artifactId>mysql-connector-java</artifactId >
58- <version>8.0.30</version >
59- <scope>runtime</scope >
60- </dependency >
61- </dependencies >
62- </project >
63- """ ,
64- """
65- <project>
66- <modelVersion>4.0.0</modelVersion>
67- <groupId>com.example</groupId >
68- <artifactId>demo</artifactId >
69- <version>0.0.1-SNAPSHOT</version >
70- <dependencies >
71- <dependency >
72- <groupId>com.mysql</groupId >
73- <artifactId>mysql-connector-j</artifactId >
74- <version>8.0.33</version >
75- <scope>runtime</scope >
76- </dependency >
77- </dependencies >
78- </project >
79- """
80- )
81- );
82- }
45+ @ Nested
46+ class Maven {
47+ @ DocumentExample
48+ @ Test
49+ void switchArtifactIdAndUpdateVersionNumber () {
50+ rewriteRun (
51+ //language=xml
52+ pomXml (
53+ """
54+ <project >
55+ <modelVersion>4.0.0</modelVersion >
56+ <groupId>com.example</groupId >
57+ <artifactId>demo</artifactId >
58+ <version>0.0.1-SNAPSHOT</version >
59+ <dependencies >
60+ <dependency >
61+ <groupId>mysql</groupId >
62+ <artifactId>mysql-connector-java</artifactId >
63+ <version>8.0.30</version >
64+ <scope>runtime</scope >
65+ </dependency >
66+ </dependencies>
67+ </project>
68+ """ ,
69+ """
70+ <project >
71+ <modelVersion>4.0.0</modelVersion >
72+ <groupId>com.example</groupId >
73+ <artifactId>demo</artifactId >
74+ <version>0.0.1-SNAPSHOT</version >
75+ <dependencies >
76+ <dependency >
77+ <groupId>com.mysql</groupId >
78+ <artifactId>mysql-connector-j</artifactId >
79+ <version>8.0.33</version >
80+ <scope>runtime</scope >
81+ </dependency >
82+ </dependencies>
83+ </project>
84+ """ ) );
85+ }
8386
84- @ Test
85- void doNotPinWhenNotVersioned () {
86- rewriteRun (
87- pomXml (
88- //language=xml
89- """
90- <project>
91- <modelVersion>4.0.0</modelVersion>
92- <groupId>com.example</groupId>
93- <artifactId>demo</artifactId>
94- <version>0.0.1-SNAPSHOT</version>
95- <parent>
96- <groupId>org.springframework.boot</groupId>
97- <artifactId>spring-boot-starter-parent</artifactId>
98- <version>2.7.7</version>
99- <relativePath/> <!-- lookup parent from repository -->
100- </parent>
101- <dependencies>
102- <dependency>
103- <groupId>mysql</groupId>
104- <artifactId>mysql-connector-java</artifactId>
105- <scope>runtime</scope>
106- </dependency>
107- </dependencies>
108- </project>
109- """ ,
110- spec -> spec .after (pom -> {
111- Matcher version = Pattern .compile ("2.7.\\ d+" ).matcher (pom );
112- assertThat (version .find ()).describedAs ("Expected 2.7.x in %s" , pom ).isTrue ();
87+ @ Test
88+ void doNotPinWhenNotVersioned () {
89+ rewriteRun (
90+ pomXml (
11391 //language=xml
114- return String . format ( """
92+ """
11593 <project>
11694 <modelVersion>4.0.0</modelVersion>
11795 <groupId>com.example</groupId>
@@ -120,20 +98,125 @@ void doNotPinWhenNotVersioned() {
12098 <parent>
12199 <groupId>org.springframework.boot</groupId>
122100 <artifactId>spring-boot-starter-parent</artifactId>
123- <version>%s </version>
101+ <version>2.7.7 </version>
124102 <relativePath/> <!-- lookup parent from repository -->
125103 </parent>
126104 <dependencies>
127105 <dependency>
128- <groupId>com. mysql</groupId>
129- <artifactId>mysql-connector-j </artifactId>
106+ <groupId>mysql</groupId>
107+ <artifactId>mysql-connector-java </artifactId>
130108 <scope>runtime</scope>
131109 </dependency>
132110 </dependencies>
133111 </project>
134- """ , version .group (0 ));
135- })
136- )
137- );
112+ """ ,
113+ spec -> spec .after (pom -> {
114+ Matcher version = Pattern .compile ("2.7.\\ d+" ).matcher (pom );
115+ assertThat (version .find ()).describedAs ("Expected 2.7.x in %s" , pom ).isTrue ();
116+ //language=xml
117+ return String .format (
118+ """
119+ <project>
120+ <modelVersion>4.0.0</modelVersion>
121+ <groupId>com.example</groupId>
122+ <artifactId>demo</artifactId>
123+ <version>0.0.1-SNAPSHOT</version>
124+ <parent>
125+ <groupId>org.springframework.boot</groupId>
126+ <artifactId>spring-boot-starter-parent</artifactId>
127+ <version>%s</version>
128+ <relativePath/> <!-- lookup parent from repository -->
129+ </parent>
130+ <dependencies>
131+ <dependency>
132+ <groupId>com.mysql</groupId>
133+ <artifactId>mysql-connector-j</artifactId>
134+ <scope>runtime</scope>
135+ </dependency>
136+ </dependencies>
137+ </project>
138+ """ , version .group (0 ));
139+ })
140+ )
141+ );
142+ }
143+ }
144+
145+ @ Nested
146+ @ Issue ("https://github.com/openrewrite/rewrite-spring/issues/375" )
147+ class Gradle {
148+
149+ @ DocumentExample
150+ @ Test
151+ void switchArtifactIdAndUpdateVersionNumber () {
152+ rewriteRun (spec -> spec .beforeRecipe (withToolingApi ()),
153+ //language=groovy
154+ Assertions .buildGradle (
155+ """
156+ plugins {
157+ id 'java'
158+ }
159+
160+ repositories {
161+ mavenCentral()
162+ }
163+
164+ dependencies {
165+ runtimeOnly 'mysql:mysql-connector-java:8.0.30'
166+ }
167+ """ ,
168+ """
169+ plugins {
170+ id 'java'
171+ }
172+
173+ repositories {
174+ mavenCentral()
175+ }
176+
177+ dependencies {
178+ runtimeOnly 'com.mysql:mysql-connector-j:8.0.33'
179+ }
180+ """ )
181+ );
182+ }
183+
184+ @ Test
185+ void doNotPinWhenNotVersioned () {
186+ rewriteRun (spec -> spec .beforeRecipe (withToolingApi ()),
187+ //language=groovy
188+ Assertions .buildGradle (
189+ """
190+ plugins {
191+ id 'java'
192+ id 'org.springframework.boot' version '2.6.1'
193+ id 'io.spring.dependency-management' version '1.0.11.RELEASE'
194+ }
195+
196+ repositories {
197+ mavenCentral()
198+ }
199+
200+ dependencies {
201+ runtimeOnly 'mysql:mysql-connector-java'
202+ }
203+ """ ,
204+ """
205+ plugins {
206+ id 'java'
207+ id 'org.springframework.boot' version '2.6.1'
208+ id 'io.spring.dependency-management' version '1.0.11.RELEASE'
209+ }
210+
211+ repositories {
212+ mavenCentral()
213+ }
214+
215+ dependencies {
216+ runtimeOnly 'com.mysql:mysql-connector-j'
217+ }
218+ """ )
219+ );
220+ }
138221 }
139222}
0 commit comments