Search in sources :

Example 1 with JavaClassCacheEntry

use of org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry in project tomcat by apache.

the class TestContextConfigAnnotation method testAnnotation.

@Test
public void testAnnotation() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    ServletDef servletDef = webxml.getServlets().get("param");
    Assert.assertNotNull(servletDef);
    Assert.assertEquals("Hello", servletDef.getParameterMap().get("foo"));
    Assert.assertEquals("World!", servletDef.getParameterMap().get("bar"));
    Assert.assertEquals("param", webxml.getServletMappings().get("/annotation/overwrite"));
    Assert.assertEquals("param", servletDef.getDescription());
    Assert.assertEquals("param", servletDef.getDisplayName());
    Assert.assertEquals("paramLarge.png", servletDef.getLargeIcon());
    Assert.assertEquals("paramSmall.png", servletDef.getSmallIcon());
    Assert.assertEquals(Boolean.FALSE, servletDef.getAsyncSupported());
    Assert.assertEquals(Integer.valueOf(0), servletDef.getLoadOnStartup());
    Assert.assertNull(servletDef.getEnabled());
    Assert.assertNull(servletDef.getJspFile());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 2 with JavaClassCacheEntry

use of org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry in project tomcat by apache.

the class TestContextConfigAnnotation method testCheckHandleTypes.

@Test
public void testCheckHandleTypes() throws Exception {
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    config.handlesTypesAnnotations = true;
    config.handlesTypesNonAnnotations = true;
    // Need a Context, Loader and ClassLoader for checkHandleTypes
    StandardContext context = new StandardContext();
    context.setLoader(new TesterLoader());
    config.context = context;
    // Add an SCI that has no interest in any type
    SCI sciNone = new SCI();
    config.initializerClassMap.put(sciNone, new HashSet<>());
    // Add an SCI with an interest in Servlets
    SCI sciServlet = new SCI();
    config.initializerClassMap.put(sciServlet, new HashSet<>());
    config.typeInitializerMap.put(Servlet.class, new HashSet<>());
    config.typeInitializerMap.get(Servlet.class).add(sciServlet);
    // Add an SCI with an interest in Objects - i.e. everything
    SCI sciObject = new SCI();
    config.initializerClassMap.put(sciObject, new HashSet<>());
    config.typeInitializerMap.put(Object.class, new HashSet<>());
    config.typeInitializerMap.get(Object.class).add(sciObject);
    // Scan Servlet, Filter, Servlet, Listener
    WebXml ignore = new WebXml();
    File file = paramClassResource("org/apache/catalina/startup/ParamServlet");
    config.processAnnotationsFile(file, ignore, false, javaClassCache);
    file = paramClassResource("org/apache/catalina/startup/ParamFilter");
    config.processAnnotationsFile(file, ignore, false, javaClassCache);
    file = paramClassResource("org/apache/catalina/startup/TesterServlet");
    config.processAnnotationsFile(file, ignore, false, javaClassCache);
    file = paramClassResource("org/apache/catalina/startup/TestListener");
    config.processAnnotationsFile(file, ignore, false, javaClassCache);
    // Check right number of classes were noted to be handled
    Assert.assertEquals(0, config.initializerClassMap.get(sciNone).size());
    Assert.assertEquals(2, config.initializerClassMap.get(sciServlet).size());
    Assert.assertEquals(4, config.initializerClassMap.get(sciObject).size());
}
Also used : HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) WebXml(org.apache.tomcat.util.descriptor.web.WebXml) StandardContext(org.apache.catalina.core.StandardContext) Servlet(jakarta.servlet.Servlet) File(java.io.File) Test(org.junit.Test)

Example 3 with JavaClassCacheEntry

use of org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry in project tomcat by apache.

the class TestContextConfigAnnotation method testDuplicateMapping.

@Test
public void testDuplicateMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/DuplicateMappingParamServlet");
    Assert.assertTrue(pFile.exists());
    try {
        config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
        Assert.fail();
    } catch (IllegalArgumentException ex) {
    // ignore
    }
    ServletDef servletDef = webxml.getServlets().get("param");
    Assert.assertNull(servletDef);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 4 with JavaClassCacheEntry

use of org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry in project tomcat by apache.

the class TestContextConfigAnnotation method testOverwriteFilterMapping.

@Test
public void testOverwriteFilterMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterName("paramFilter");
    filterDef.setFilterClass("org.apache.catalina.startup.ParamFilter");
    filterDef.addInitParameter("message", "tomcat");
    filterDef.setDescription("Description");
    filterDef.setDisplayName("DisplayName");
    filterDef.setLargeIcon("LargeIcon");
    filterDef.setSmallIcon("SmallIcon");
    filterDef.setAsyncSupported("true");
    webxml.addFilter(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.addURLPatternDecoded("/param1");
    filterMap.setFilterName("paramFilter");
    webxml.addFilterMapping(filterMap);
    ContextConfig config = new ContextConfig();
    File sFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
    config.processAnnotationsFile(sFile, webxml, false, javaClassCache);
    File fFile = paramClassResource("org/apache/catalina/startup/ParamFilter");
    config.processAnnotationsFile(fFile, webxml, false, javaClassCache);
    FilterDef fdef = webxml.getFilters().get("paramFilter");
    Assert.assertNotNull(fdef);
    Assert.assertEquals(filterDef, fdef);
    Assert.assertEquals("tomcat", fdef.getParameterMap().get("message"));
    Set<FilterMap> filterMappings = webxml.getFilterMappings();
    Assert.assertTrue(filterMappings.contains(filterMap));
    // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
    // 8.2.3.3.vi page 81
    String[] urlPatterns = filterMap.getURLPatterns();
    Assert.assertNotNull(urlPatterns);
    Assert.assertEquals(1, urlPatterns.length);
    Assert.assertEquals("/param1", urlPatterns[0]);
    // check simple Parameter
    Assert.assertEquals("Description", fdef.getDescription());
    Assert.assertEquals("DisplayName", fdef.getDisplayName());
    Assert.assertEquals("LargeIcon", fdef.getLargeIcon());
    Assert.assertEquals("SmallIcon", fdef.getSmallIcon());
    // FIXME: Strange why servletDef is Boolean and FilterDef is String?
    Assert.assertEquals("true", fdef.getAsyncSupported());
    String[] dis = filterMap.getDispatcherNames();
    Assert.assertEquals(2, dis.length);
    Assert.assertEquals(DispatcherType.ERROR.toString(), dis[0]);
    Assert.assertEquals(DispatcherType.ASYNC.toString(), dis[1]);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) File(java.io.File) Test(org.junit.Test)

Example 5 with JavaClassCacheEntry

use of org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry in project tomcat by apache.

the class TestContextConfigAnnotation method testSetupWebXMLNoMapping.

@Test
public void testSetupWebXMLNoMapping() throws Exception {
    WebXml webxml = new WebXml();
    Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
    ServletDef servletDef = new ServletDef();
    servletDef.setServletName("param1");
    servletDef.setServletClass("org.apache.catalina.startup.NoMappingParamServlet");
    servletDef.addInitParameter("foo", "tomcat");
    webxml.addServlet(servletDef);
    webxml.addServletMapping("/param", "param1");
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/NoMappingParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
    Assert.assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
    Assert.assertEquals("World!", servletDef.getParameterMap().get("bar"));
    ServletDef servletDef1 = webxml.getServlets().get("param1");
    Assert.assertNotNull(servletDef1);
    Assert.assertEquals(servletDef, servletDef1);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)9 HashMap (java.util.HashMap)9 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)9 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)9 Test (org.junit.Test)9 ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)5 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)3 Servlet (jakarta.servlet.Servlet)1 StandardContext (org.apache.catalina.core.StandardContext)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1