@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void basicWacFeatures() throws Exception { assertNotNull("ServletContext should be set in the WAC.", wac.getServletContext()); assertNotNull("ServletContext should have been set via ServletContextAware.", servletContext); assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext); assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request); assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response); assertNotNull("MockHttpSession should have been autowired from the WAC.", session); assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest); Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertNotNull("Root WAC must be stored in the ServletContext as: " + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); assertSame("test WAC and Root WAC in ServletContext must be the same object.", wac, rootWac); assertSame("ServletContext instances must be the same object.", mockServletContext, wac.getServletContext()); assertSame("ServletContext in the WAC and in the mock request", mockServletContext, request.getServletContext()); assertEquals("Getting real path for ServletContext resource.", new File("src/main/webapp/index.jsp").getCanonicalPath(), mockServletContext.getRealPath("index.jsp")); }
@Test public void shouldDelegateToTheGivenServlet() throws IOException, ServletException { MockServletContext ctx = new MockServletContext(); ctx.addInitParameter(DelegatingListener.DELEGATE_SERVLET, DummyServlet.class.getCanonicalName()); ServletContextEvent evt = new ServletContextEvent(ctx); DelegatingListener listener = new DelegatingListener(); listener.contextInitialized(evt); assertThat((DummyServlet) ctx.getAttribute(DelegatingListener.DELEGATE_SERVLET), isA(DummyServlet.class)); DelegatingServlet servlet = new DelegatingServlet(); servlet.init(new MockServletConfig(ctx)); servlet.service(httpServletRequest, new MockHttpServletResponse()); verify(servletRequestWrapper).setRequestURI("/go/stuff/action"); } }
@Test public void testListener() throws Exception { Map<String,Object> overlayConfig = new HashMap<>(); overlayConfig.put("oryx.serving.model-manager-class", MockServingModelManager.class.getName()); Config config = ConfigUtils.overlayOn(overlayConfig, getConfig()); String serializedConfig = ConfigUtils.serialize(config); MockServletContext mockContext = new MockServletContext(); mockContext.addInitParameter(ConfigUtils.class.getName() + ".serialized", serializedConfig); startMessaging(); try (ModelManagerListener<?,?,?> listener = new ModelManagerListener<>()) { listener.init(mockContext); try { listener.contextInitialized(new ServletContextEvent(mockContext)); ServingModelManager<?> manager = (ServingModelManager<?>) mockContext.getAttribute(ModelManagerListener.MANAGER_KEY); assertNotNull(manager); assertFalse(manager.isReadOnly()); assertNotNull(manager.getConfig()); } finally { listener.contextDestroyed(new ServletContextEvent(mockContext)); } } }
/** {@inheritDoc} */ public Object getAttribute(String name) { if (servletContext == null) { return super.getAttribute(name); } if (name == null) { throw new IllegalArgumentException("Attribute name == null"); } return servletContext.getAttribute(name); }