|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.jakarta; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.openrewrite.java.JavaParser; |
| 20 | +import org.openrewrite.test.RecipeSpec; |
| 21 | +import org.openrewrite.test.RewriteTest; |
| 22 | + |
| 23 | +import static org.openrewrite.java.Assertions.*; |
| 24 | +import static org.openrewrite.maven.Assertions.pomXml; |
| 25 | + |
| 26 | +class JavaxWsToJakartaWsTest implements RewriteTest { |
| 27 | + @Override |
| 28 | + public void defaults(RecipeSpec spec) { |
| 29 | + spec.parser(JavaParser.fromJavaVersion() |
| 30 | + .classpath("javax.ws.rs-api")) |
| 31 | + .recipeFromResource( |
| 32 | + "/META-INF/rewrite/jakarta-ee-9.yml", |
| 33 | + "org.openrewrite.java.migrate.jakarta.JavaxWsToJakartaWs"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void switchesJavaxWsApiDependencyToJakartaWsApiDependency() { |
| 38 | + rewriteRun( |
| 39 | + mavenProject( |
| 40 | + "Sample", |
| 41 | + srcMainJava( |
| 42 | + //language=java |
| 43 | + java( |
| 44 | + """ |
| 45 | + import javax.ws.rs.core.MediaType; |
| 46 | + public class TestApplication { |
| 47 | + MediaType getType() { |
| 48 | + return MediaType.APPLICATION_JSON_TYPE; |
| 49 | + } |
| 50 | + } |
| 51 | + """, |
| 52 | + """ |
| 53 | + import jakarta.ws.rs.core.MediaType; |
| 54 | + public class TestApplication { |
| 55 | + MediaType getType() { |
| 56 | + return MediaType.APPLICATION_JSON_TYPE; |
| 57 | + } |
| 58 | + } |
| 59 | + """ |
| 60 | + ) |
| 61 | + ), |
| 62 | + //language=xml |
| 63 | + pomXml( |
| 64 | + """ |
| 65 | + <?xml version="1.0" encoding="UTF-8"?> |
| 66 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 67 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 68 | + <modelVersion>4.0.0</modelVersion> |
| 69 | + <groupId>com.example</groupId> |
| 70 | + <artifactId>demo</artifactId> |
| 71 | + <version>0.0.1-SNAPSHOT</version> |
| 72 | + <dependencies> |
| 73 | + <dependency> |
| 74 | + <groupId>javax.ws.rs</groupId> |
| 75 | + <artifactId>javax.ws.rs-api</artifactId> |
| 76 | + <version>2.1.1</version> |
| 77 | + </dependency> |
| 78 | + </dependencies> |
| 79 | + </project> |
| 80 | + """, |
| 81 | + """ |
| 82 | + <?xml version="1.0" encoding="UTF-8"?> |
| 83 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 84 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 85 | + <modelVersion>4.0.0</modelVersion> |
| 86 | + <groupId>com.example</groupId> |
| 87 | + <artifactId>demo</artifactId> |
| 88 | + <version>0.0.1-SNAPSHOT</version> |
| 89 | + <dependencies> |
| 90 | + <dependency> |
| 91 | + <groupId>jakarta.ws.rs</groupId> |
| 92 | + <artifactId>jakarta.ws.rs-api</artifactId> |
| 93 | + <version>3.0.0</version> |
| 94 | + </dependency> |
| 95 | + </dependencies> |
| 96 | + </project> |
| 97 | + """ |
| 98 | + ) |
| 99 | + ) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void addsJakartaWsApiDependencyIfExistingInTransitive() { |
| 105 | + rewriteRun( |
| 106 | + mavenProject( |
| 107 | + "Sample", |
| 108 | + srcMainJava( |
| 109 | + //language=java |
| 110 | + java( |
| 111 | + """ |
| 112 | + import javax.ws.rs.core.MediaType; |
| 113 | + public class TestApplication { |
| 114 | + MediaType getType() { |
| 115 | + return MediaType.APPLICATION_JSON_TYPE; |
| 116 | + } |
| 117 | + } |
| 118 | + """, |
| 119 | + """ |
| 120 | + import jakarta.ws.rs.core.MediaType; |
| 121 | + public class TestApplication { |
| 122 | + MediaType getType() { |
| 123 | + return MediaType.APPLICATION_JSON_TYPE; |
| 124 | + } |
| 125 | + } |
| 126 | + """ |
| 127 | + ) |
| 128 | + ), |
| 129 | + //language=xml |
| 130 | + pomXml( |
| 131 | + """ |
| 132 | + <?xml version="1.0" encoding="UTF-8"?> |
| 133 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 134 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 135 | + <modelVersion>4.0.0</modelVersion> |
| 136 | + <groupId>com.example</groupId> |
| 137 | + <artifactId>demo</artifactId> |
| 138 | + <version>0.0.1-SNAPSHOT</version> |
| 139 | + </project> |
| 140 | + """, |
| 141 | + """ |
| 142 | + <?xml version="1.0" encoding="UTF-8"?> |
| 143 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 144 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 145 | + <modelVersion>4.0.0</modelVersion> |
| 146 | + <groupId>com.example</groupId> |
| 147 | + <artifactId>demo</artifactId> |
| 148 | + <version>0.0.1-SNAPSHOT</version> |
| 149 | + <dependencies> |
| 150 | + <dependency> |
| 151 | + <groupId>jakarta.ws.rs</groupId> |
| 152 | + <artifactId>jakarta.ws.rs-api</artifactId> |
| 153 | + <version>3.0.0</version> |
| 154 | + </dependency> |
| 155 | + </dependencies> |
| 156 | + </project> |
| 157 | + """ |
| 158 | + ) |
| 159 | + ) |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void ignoresJakartaWsApiDependencyIfAlreadyExisting() { |
| 165 | + rewriteRun( |
| 166 | + mavenProject( |
| 167 | + "Sample", |
| 168 | + //language=xml |
| 169 | + pomXml( |
| 170 | + """ |
| 171 | + <?xml version="1.0" encoding="UTF-8"?> |
| 172 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 173 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 174 | + <modelVersion>4.0.0</modelVersion> |
| 175 | + <groupId>com.example</groupId> |
| 176 | + <artifactId>demo</artifactId> |
| 177 | + <version>0.0.1-SNAPSHOT</version> |
| 178 | + <dependencies> |
| 179 | + <dependency> |
| 180 | + <groupId>jakarta.ws.rs</groupId> |
| 181 | + <artifactId>jakarta.ws.rs-api</artifactId> |
| 182 | + <version>3.0.0</version> |
| 183 | + </dependency> |
| 184 | + </dependencies> |
| 185 | + </project> |
| 186 | + """ |
| 187 | + ) |
| 188 | + ) |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments