Search in sources :

Example 81 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class TesterTldListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    servletContext = sc;
    // Try and use one of the Servlet 3.0 methods that should be blocked
    try {
        sc.getServletRegistrations();
        log.append("FAIL-01");
    } catch (UnsupportedOperationException uoe) {
        log.append("PASS-01");
    } catch (Exception e) {
        log.append("FAIL-02");
    }
}
Also used : ServletContext(jakarta.servlet.ServletContext)

Example 82 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class TestCorsFilter method testInitDefaultFilterConfig.

@Test
public void testInitDefaultFilterConfig() throws IOException, ServletException {
    TesterHttpServletRequest request = new TesterHttpServletRequest();
    request.setHeader(CorsFilter.REQUEST_HEADER_ORIGIN, TesterFilterConfigs.HTTPS_WWW_APACHE_ORG);
    request.setMethod("GET");
    TesterHttpServletResponse response = new TesterHttpServletResponse();
    CorsFilter corsFilter = new CorsFilter();
    corsFilter.init(new FilterConfig() {

        @Override
        public ServletContext getServletContext() {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public String getFilterName() {
            return null;
        }
    });
    corsFilter.doFilter(request, response, filterChain);
    Assert.assertNull(response.getHeader(CorsFilter.RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN));
    Assert.assertTrue(((Boolean) request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_IS_CORS_REQUEST)).booleanValue());
    Assert.assertTrue(request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_ORIGIN).equals(TesterFilterConfigs.HTTPS_WWW_APACHE_ORG));
    Assert.assertTrue(request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_REQUEST_TYPE).equals(CorsFilter.CORSRequestType.SIMPLE.name().toLowerCase(Locale.ENGLISH)));
}
Also used : Enumeration(java.util.Enumeration) ServletContext(jakarta.servlet.ServletContext) FilterConfig(jakarta.servlet.FilterConfig) Test(org.junit.Test)

Example 83 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class TestApplicationContext method testGetJspConfigDescriptor.

@Test
public void testGetJspConfigDescriptor() throws Exception {
    Tomcat tomcat = getTomcatInstanceTestWebapp(false, false);
    StandardContext standardContext = (StandardContext) tomcat.getHost().findChildren()[0];
    ServletContext servletContext = standardContext.getServletContext();
    Assert.assertNull(servletContext.getJspConfigDescriptor());
    tomcat.start();
    Assert.assertNotNull(servletContext.getJspConfigDescriptor());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ServletContext(jakarta.servlet.ServletContext) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 84 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class TestTagPluginManager method testBug54240.

@Test
public void testBug54240() throws Exception {
    Tomcat tomcat = getTomcatInstanceTestWebapp(false, true);
    ServletContext context = ((Context) tomcat.getHost().findChildren()[0]).getServletContext();
    TagPluginManager manager = new TagPluginManager(context);
    Node.Nodes nodes = new Node.Nodes();
    Node.CustomTag c = new Node.CustomTag("test:ATag", "test", "ATag", "http://tomcat.apache.org/jasper", null, null, null, null, null, new TagFileInfo("ATag", "http://tomcat.apache.org/jasper", tagInfo));
    c.setTagHandlerClass(TesterTag.class);
    nodes.add(c);
    manager.apply(nodes, null, null);
    Node n = nodes.getNode(0);
    Assert.assertNotNull(n);
    Node.CustomTag t = (Node.CustomTag) n;
    Assert.assertNotNull(t.getAtSTag());
    Node.Nodes sTag = c.getAtSTag();
    Node scriptlet = sTag.getNode(0);
    Assert.assertNotNull(scriptlet);
    Node.Scriptlet s = (Node.Scriptlet) scriptlet;
    Assert.assertEquals("//Just a comment", s.getText());
}
Also used : ServletContext(jakarta.servlet.ServletContext) Context(org.apache.catalina.Context) TagFileInfo(jakarta.servlet.jsp.tagext.TagFileInfo) Tomcat(org.apache.catalina.startup.Tomcat) ServletContext(jakarta.servlet.ServletContext) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 85 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class AsyncStockContextListener method contextDestroyed.

@Override
public void contextDestroyed(ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    Stockticker stockticker = (Stockticker) sc.getAttribute(STOCK_TICKER_KEY);
    stockticker.shutdown();
}
Also used : ServletContext(jakarta.servlet.ServletContext)

Aggregations

ServletContext (jakarta.servlet.ServletContext)116 Test (org.junit.jupiter.api.Test)45 ServletConfig (jakarta.servlet.ServletConfig)34 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)17 FilterRegistration (jakarta.servlet.FilterRegistration)15 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 ServletException (jakarta.servlet.ServletException)12 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 Filter (jakarta.servlet.Filter)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 Context (org.apache.catalina.Context)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 Test (org.junit.Test)6