@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")); }
/** {@inheritDoc} */ public String getRealPath(String path) { if (servletContext == null) { return super.getRealPath(path); } return servletContext.getRealPath(path); }
@Override public String getRealPath(String path) { try { URL url = getResource(path); if (url == null) { return null; } String protocol = url.getProtocol(); if ("jar".equals(protocol)) { return url.toString(); } } catch (MalformedURLException e) { // NOP } return super.getRealPath(path); }