Skip to content

Commit b9ed487

Browse files
authored
Changed condition to use HasTypeAnnotation checking for annotation WebServlet (#770)
- The migrate-annotated-servlets-recipe is only applicable when classes annotated with @WebServlet exist
1 parent 9eca3a8 commit b9ed487

File tree

2 files changed

+105
-56
lines changed

2 files changed

+105
-56
lines changed

applications/spring-shell/src/test/java/org/springframework/sbm/MigrateAnnotatedServletsIntegrationTest.java

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,58 +35,61 @@ protected String getTestSubDir() {
3535
@Test
3636
void happyPath() {
3737

38-
String pomXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
39-
"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"\n" +
40-
" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
41-
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
42-
" <modelVersion>4.0.0</modelVersion>\n" +
43-
" <groupId>com.vmware.example</groupId>\n" +
44-
" <artifactId>jboss-sample</artifactId>\n" +
45-
" <version>1.0.0</version>\n" +
46-
" <properties>\n" +
47-
" <maven.compiler.source>1.8</maven.compiler.source>\n" +
48-
" <maven.compiler.target>11</maven.compiler.target>\n" +
49-
" </properties>\n" +
50-
" <dependencies>\n" +
51-
" <dependency>\n" +
52-
" <groupId>javax.ejb</groupId>\n" +
53-
" <artifactId>javax.ejb-api</artifactId>\n" +
54-
" <version>3.2</version>\n" +
55-
" <scope>provided</scope>\n" +
56-
" </dependency>\n" +
57-
" <dependency>\n" +
58-
" <groupId>javax.enterprise</groupId>\n" +
59-
" <artifactId>cdi-api</artifactId>\n" +
60-
" <version>1.2</version>\n" +
61-
" <scope>provided</scope>\n" +
62-
" </dependency>\n" +
63-
" <dependency>\n" +
64-
" <groupId>javax.servlet</groupId>\n" +
65-
" <artifactId>javax.servlet-api</artifactId>\n" +
66-
" <version>4.0.1</version>\n" +
67-
" </dependency>\n" +
68-
" </dependencies>\n" +
69-
"</project>\n";
70-
71-
String servletClass =
72-
"package org.jboss.as.quickstarts.helloworld;\n" +
73-
"\n" +
74-
"import java.io.IOException;\n" +
75-
"import java.io.PrintWriter;\n" +
76-
"\n" +
77-
"import javax.servlet.ServletException;\n" +
78-
"import javax.servlet.annotation.WebServlet;\n" +
79-
"import javax.servlet.http.HttpServlet;\n" +
80-
"import javax.servlet.http.HttpServletRequest;\n" +
81-
"import javax.servlet.http.HttpServletResponse;\n" +
82-
"\n" +
83-
"@WebServlet(\"/HelloWorld\")\n" +
84-
"public class HelloWorldServlet extends HttpServlet {\n" +
85-
" @Override\n" +
86-
" protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n" +
87-
" resp.getWriter().append(\"Hello World!\");\n" +
88-
" }\n" +
89-
"}\n";
38+
String pomXml = """
39+
<?xml version="1.0" encoding="UTF-8"?>
40+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
41+
xmlns="http://maven.apache.org/POM/4.0.0"
42+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
43+
<modelVersion>4.0.0</modelVersion>
44+
<groupId>com.vmware.example</groupId>
45+
<artifactId>jboss-sample</artifactId>
46+
<version>1.0.0</version>
47+
<properties>
48+
<maven.compiler.source>1.8</maven.compiler.source>
49+
<maven.compiler.target>11</maven.compiler.target>
50+
</properties>
51+
<dependencies>
52+
<dependency>
53+
<groupId>javax.ejb</groupId>
54+
<artifactId>javax.ejb-api</artifactId>
55+
<version>3.2</version>
56+
<scope>provided</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>javax.enterprise</groupId>
60+
<artifactId>cdi-api</artifactId>
61+
<version>1.2</version>
62+
<scope>provided</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>javax.servlet</groupId>
66+
<artifactId>javax.servlet-api</artifactId>
67+
<version>4.0.1</version>
68+
</dependency>
69+
</dependencies>
70+
</project>
71+
""";
72+
73+
String servletClass = """
74+
package org.jboss.as.quickstarts.helloworld;
75+
76+
import java.io.IOException;
77+
import java.io.PrintWriter;
78+
79+
import javax.servlet.ServletException;
80+
import javax.servlet.annotation.WebServlet;
81+
import javax.servlet.http.HttpServlet;
82+
import javax.servlet.http.HttpServletRequest;
83+
import javax.servlet.http.HttpServletResponse;
84+
85+
@WebServlet("/HelloWorld")
86+
public class HelloWorldServlet extends HttpServlet {
87+
@Override
88+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
89+
resp.getWriter().append("Hello World!");
90+
}
91+
}
92+
""";
9093

9194
writeFile(pomXml, "pom.xml");
9295
writeJavaFile(servletClass);
@@ -107,9 +110,10 @@ void happyPath() {
107110
assertThat(content).contains("@SpringBootApplication").withFailMessage(() -> "@SpringBootApplication annotation not found");
108111
assertThat(content).contains("@ServletComponentScan").withFailMessage(() -> "@ServletComponentScan annotation not found");
109112

110-
executeMavenGoals(getTestDir(), "package");
113+
String servlet = loadJavaFile("org.jboss.as.quickstarts.helloworld", "HelloWorldServlet");
114+
assertThat(content).contains("@SpringBootApplication").withFailMessage(() -> "@SpringBootApplication annotation not found");
111115

112-
executeMavenGoals(getTestDir(), "package", "spring-boot:build-image");
116+
executeMavenGoals(getTestDir(), "spring-boot:build-image");
113117

114118
Integer port = startDockerContainer("jboss-sample:1.0.0", "/HelloWorld", 8080);
115119

@@ -120,4 +124,49 @@ void happyPath() {
120124

121125
}
122126

127+
@Test
128+
void recipeNBotApplicableWhenOnlyFilterExists() {
129+
String pom = """
130+
<?xml version="1.0" encoding="UTF-8"?>
131+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
132+
xmlns="http://maven.apache.org/POM/4.0.0"
133+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
134+
<modelVersion>4.0.0</modelVersion>
135+
<groupId>com.vmware.example</groupId>
136+
<artifactId>jboss-sample</artifactId>
137+
<version>1.0.0</version>
138+
<properties>
139+
<maven.compiler.source>1.8</maven.compiler.source>
140+
<maven.compiler.target>11</maven.compiler.target>
141+
</properties>
142+
<dependencies>
143+
<dependency>
144+
<groupId>javax.servlet</groupId>
145+
<artifactId>javax.servlet-api</artifactId>
146+
<version>4.0.1</version>
147+
</dependency>
148+
</dependencies>
149+
</project>
150+
""";
151+
152+
String servletFilterClass = """
153+
package org.jboss.as.quickstarts.helloworld;
154+
import javax.servlet.annotation.WebFilter;
155+
156+
@WebFilter("/")
157+
public class MyFilter {
158+
}
159+
""";
160+
161+
writeFile(pom, "pom.xml");
162+
writeJavaFile(servletFilterClass);
163+
164+
executeMavenGoals(getTestDir(), "compile");
165+
166+
scanProject();
167+
168+
assertRecipeNotApplicable(
169+
"migrate-annotated-servlets"
170+
);
171+
}
123172
}

components/sbm-recipes-jee-to-boot/src/main/resources/recipes/migrate-annotated-servlets.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
order: 70
44
condition:
55
description: Any class has import starting with javax.servlet
6-
type: org.springframework.sbm.java.migration.conditions.HasImportStartingWith
7-
value: javax.servlet
6+
type: org.springframework.sbm.java.migration.conditions.HasTypeAnnotation
7+
annotation: javax.servlet.annotation.WebServlet
88
actions:
99
- type: org.springframework.sbm.build.migration.actions.AddDependencies
1010
condition:

0 commit comments

Comments
 (0)