|
| 1 | +package com.ibm.cldk.javaee; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import com.ibm.cldk.schema.JCallable; |
| 6 | +import com.ibm.cldk.schema.JEntrypointReport; |
| 7 | +import com.ibm.cldk.schema.JModule; |
| 8 | +import com.ibm.cldk.schema.JType; |
| 9 | +import com.ibm.cldk.syntactic_analysis.L1Extractor; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.TreeMap; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.io.TempDir; |
| 19 | + |
| 20 | +/** |
| 21 | + * Which methods the Jakarta finder marks (#263): the servlet / filter / listener lifecycle methods |
| 22 | + * of an entrypoint type, not any method that happens to take a servlet-typed parameter. A helper |
| 23 | + * such as DayTrader's {@code requestDispatch(ctx, req, resp, page)} is ordinary code whose |
| 24 | + * parameters are bound by its callers — marking it an entrypoint made every interprocedural tier |
| 25 | + * refuse to bind them. |
| 26 | + */ |
| 27 | +class JakartaEntrypointMethodsTest { |
| 28 | + |
| 29 | + @TempDir |
| 30 | + Path root; |
| 31 | + |
| 32 | + private Map<String, List<String>> marks(String source) throws Exception { |
| 33 | + Path src = root.resolve("src/main/java/demo"); |
| 34 | + Files.createDirectories(src); |
| 35 | + Files.writeString(src.resolve("Web.java"), source, StandardCharsets.UTF_8); |
| 36 | + Map<String, JModule> modules = L1Extractor.extractAll( |
| 37 | + root, "app", null, new LinkedHashMap<>(), 1, 3, "ast", new JEntrypointReport()); |
| 38 | + Map<String, List<String>> out = new TreeMap<>(); |
| 39 | + for (JModule m : modules.values()) { |
| 40 | + for (Map.Entry<String, JType> e : m.getTypes().entrySet()) { |
| 41 | + for (JCallable c : e.getValue().getCallables().values()) { |
| 42 | + out.put(e.getKey() + "." + c.getSignature().substring(0, c.getSignature().indexOf('(')), |
| 43 | + c.getEntrypointFrameworks()); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return out; |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void lifecycleMethodsOfAServletAreMarkedAndItsHelpersAreNot() throws Exception { |
| 52 | + Map<String, List<String>> m = marks( |
| 53 | + "package demo;\n" |
| 54 | + + "import javax.servlet.http.*;\n" |
| 55 | + + "public class Front extends HttpServlet {\n" |
| 56 | + + " protected void doGet(HttpServletRequest req, HttpServletResponse res) { show(req, res, \"/x.jsp\"); }\n" |
| 57 | + + " public void init() {}\n" |
| 58 | + + " void show(HttpServletRequest req, HttpServletResponse res, String page) {}\n" |
| 59 | + + " static String render(HttpServletRequest req) { return \"\"; }\n" |
| 60 | + + "}\n"); |
| 61 | + assertEquals(List.of("jakarta"), m.get("Front.doGet")); |
| 62 | + assertEquals(List.of("jakarta"), m.get("Front.init")); |
| 63 | + assertEquals(List.of(), m.get("Front.show"), "a helper taking req/res is not an entrypoint"); |
| 64 | + assertEquals(List.of(), m.get("Front.render")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void aLifecycleNameOutsideAnEntrypointTypeIsNotMarked() throws Exception { |
| 69 | + Map<String, List<String>> m = marks( |
| 70 | + "package demo;\n" |
| 71 | + + "import javax.servlet.http.*;\n" |
| 72 | + + "public class Plain {\n" |
| 73 | + + " void doGet(HttpServletRequest req, HttpServletResponse res) {}\n" |
| 74 | + + " public void init() {}\n" |
| 75 | + + "}\n"); |
| 76 | + assertEquals(List.of(), m.get("Plain.doGet")); |
| 77 | + assertEquals(List.of(), m.get("Plain.init")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void filterAndListenerAndEndpointCallbacksAreMarked() throws Exception { |
| 82 | + Map<String, List<String>> m = marks( |
| 83 | + "package demo;\n" |
| 84 | + + "import javax.servlet.*;\n" |
| 85 | + + "@WebFilter(\"/*\")\n" |
| 86 | + + "class Gate implements Filter {\n" |
| 87 | + + " public void doFilter(ServletRequest q, ServletResponse s, FilterChain c) {}\n" |
| 88 | + + " void log(ServletRequest q) {}\n" |
| 89 | + + "}\n" |
| 90 | + + "class Boot implements ServletContextListener {\n" |
| 91 | + + " public void contextInitialized(ServletContextEvent e) {}\n" |
| 92 | + + "}\n" |
| 93 | + + "@ServerEndpoint(\"/ws\")\n" |
| 94 | + + "class Sock {\n" |
| 95 | + + " @OnMessage public void onText(String msg) {}\n" |
| 96 | + + " void send(String msg) {}\n" |
| 97 | + + "}\n"); |
| 98 | + assertEquals(List.of("jakarta"), m.get("Gate.doFilter")); |
| 99 | + assertEquals(List.of(), m.get("Gate.log")); |
| 100 | + assertEquals(List.of("jakarta"), m.get("Boot.contextInitialized")); |
| 101 | + assertEquals(List.of("jakarta"), m.get("Sock.onText")); |
| 102 | + assertEquals(List.of(), m.get("Sock.send")); |
| 103 | + } |
| 104 | +} |
0 commit comments