@Test public void registerServletParamPropertySources_GenericWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value"); GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ctx.setServletContext(servletContext); ctx.refresh(); ConfigurableEnvironment environment = ctx.getEnvironment(); assertThat(environment, instanceOf(StandardServletEnvironment.class)); MutablePropertySources propertySources = environment.getPropertySources(); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); // ServletContext params are available assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pContext1"), is("pContext1Value")); // Servlet* PropertySources have precedence over System* PropertySources assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); // Replace system properties with a mock property source for convenience MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue"); mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value"); propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties); // assert that servletcontext init params resolve with higher precedence than sysprops assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value")); }
@Test public void registerServletParamPropertySources_AbstractRefreshableWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value");
@Test public void registerServletParamPropertySources_StaticWebApplicationContext() { MockServletContext servletContext = new MockServletContext(); servletContext.addInitParameter("pCommon", "pCommonContextValue"); servletContext.addInitParameter("pContext1", "pContext1Value");
@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 void addInitParameter(String name, String value) { if (servletContext == null) { super.addInitParameter(name, value); return; } servletContext.addInitParameter(name, value); }
MockServletContext mockServletContext = new MockServletContext(); mockServletContext.addInitParameter("contextConfigLocation", "path to your xml config")); ContextLoaderListener listener = new ContextLoaderListener(); listener.initWebApplicationContext(mockServletContext);
@Override protected void setUp() throws Exception { super.setUp(); ContextLoader contextLoader = new ContextLoader(); servletContext = new MockServletContext(); servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, getApplicationContextPath()); applicationContext = contextLoader.initWebApplicationContext(servletContext); }
@Test public void customRedirectStrategy() throws Exception { final AuthenticationFilter f = new AuthenticationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL); context.addInitParameter("service", CAS_SERVICE_URL); context.addInitParameter("authenticationRedirectStrategyClass", "org.jasig.cas.client.authentication.FacesCompatibleAuthenticationRedirectStrategy"); f.init(new MockFilterConfig(context)); }
@Test public void testAllowsRenewContextParam() throws Exception { final AuthenticationFilter f = new AuthenticationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL); context.addInitParameter("service", CAS_SERVICE_URL); context.addInitParameter("renew", "true"); f.init(new MockFilterConfig(context)); final Field renewField = AuthenticationFilter.class.getDeclaredField("renew"); renewField.setAccessible(true); assertTrue((Boolean) renewField.get(f)); }
public void testAllowsRenewContextParam() throws Exception { final Cas20ProxyReceivingTicketValidationFilter f = new Cas20ProxyReceivingTicketValidationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerUrlPrefix", "https://cas.example.com"); context.addInitParameter("renew", "true"); context.addInitParameter("service", "http://www.jasig.org"); final MockFilterConfig config = new MockFilterConfig(context); f.init(config); final TicketValidator validator = f.getTicketValidator(config); assertTrue(validator instanceof AbstractUrlBasedTicketValidator); assertTrue(((AbstractUrlBasedTicketValidator) validator).isRenew()); }
@Test public void testAllowsRenewContextParam() throws Exception { final Cas10TicketValidationFilter f = new Cas10TicketValidationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerUrlPrefix", "https://cas.example.com"); context.addInitParameter("renew", "true"); context.addInitParameter("service", "http://www.jasig.org"); final MockFilterConfig config = new MockFilterConfig(context); f.init(config); final TicketValidator validator = f.getTicketValidator(config); assertTrue(validator instanceof Cas10TicketValidator); assertTrue(((Cas10TicketValidator) validator).isRenew()); } }
@Test public void servletConfigValueForBoolean() { final ConfigurationKey<Boolean> key = ConfigurationKeys.ACCEPT_ANY_PROXY; final Boolean value = Boolean.TRUE; final MockServletContext context = (MockServletContext) this.filterConfig.getServletContext(); context.addInitParameter(key.getName(), value.toString()); assertEquals(value, this.impl.getBoolean(key)); }
@Test public void servletConfigValueForLong() { final ConfigurationKey<Long> key = ConfigurationKeys.TOLERANCE; final long value = 1500; final MockServletContext context = (MockServletContext) this.filterConfig.getServletContext(); context.addInitParameter(key.getName(), Long.toString(value)); assertEquals(value, this.impl.getLong(key)); }
@Test public void servletConfigValueForInt() { final ConfigurationKey<Integer> key = ConfigurationKeys.MILLIS_BETWEEN_CLEAN_UPS; final int value = 1500; final MockServletContext context = (MockServletContext) this.filterConfig.getServletContext(); context.addInitParameter(key.getName(), Integer.toString(value)); assertEquals(value, this.impl.getInt(key)); }
@Test public void testIgnorePatternsWithExactClassname() throws Exception { final AuthenticationFilter f = new AuthenticationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL); context.addInitParameter("ignorePattern", "=valueToIgnore"); context.addInitParameter("ignoreUrlPatternType", ContainsPatternUrlPatternMatcherStrategy.class.getName()); context.addInitParameter("service", CAS_SERVICE_URL); f.init(new MockFilterConfig(context)); final MockHttpServletRequest request = new MockHttpServletRequest(); final String URL = CAS_SERVICE_URL + "?param=valueToIgnore"; request.setRequestURI(URL); final MockHttpSession session = new MockHttpSession(); request.setSession(session); final MockHttpServletResponse response = new MockHttpServletResponse(); final FilterChain filterChain = new FilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { } }; f.doFilter(request, response, filterChain); assertNull(response.getRedirectedUrl()); }
@Test public void testIgnorePatternsWithContainsMatching() throws Exception { final AuthenticationFilter f = new AuthenticationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL); context.addInitParameter("ignorePattern", "=valueToIgnore"); context.addInitParameter("ignoreUrlPatternType", "CONTAINS"); context.addInitParameter("service", CAS_SERVICE_URL); f.init(new MockFilterConfig(context)); final MockHttpServletRequest request = new MockHttpServletRequest(); final String URL = CAS_SERVICE_URL + "?param=valueToIgnore"; request.setRequestURI(URL); final MockHttpSession session = new MockHttpSession(); request.setSession(session); final MockHttpServletResponse response = new MockHttpServletResponse(); final FilterChain filterChain = new FilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { } }; f.doFilter(request, response, filterChain); assertNull(response.getRedirectedUrl()); }
@Test public void servletConfigValueForString() { final ConfigurationKey<String> key = ConfigurationKeys.ARTIFACT_PARAMETER_NAME; final String value = "foobar"; final MockServletContext context = (MockServletContext) this.filterConfig.getServletContext(); context.addInitParameter(key.getName(), value); assertEquals(value, this.impl.getString(key)); }
public DispatcherServletChannelInitializer() throws ServletException { MockServletContext servletContext = new MockServletContext(); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); servletContext.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); //AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); //ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext(); wac.setServletContext(servletContext); wac.setServletConfig(servletConfig); wac.setConfigLocation("classpath:/servlet-context.xml"); //wac.register(WebConfig.class); wac.refresh(); this.dispatcherServlet = new DispatcherServlet(wac); this.dispatcherServlet.init(servletConfig); }
@Test public void testIgnorePatterns() throws Exception { final AuthenticationFilter f = new AuthenticationFilter(); final MockServletContext context = new MockServletContext(); context.addInitParameter("casServerLoginUrl", CAS_LOGIN_URL); context.addInitParameter("ignorePattern", "=valueTo(\\w+)"); context.addInitParameter("service", CAS_SERVICE_URL); f.init(new MockFilterConfig(context)); final MockHttpServletRequest request = new MockHttpServletRequest(); final String URL = CAS_SERVICE_URL + "?param=valueToIgnore"; request.setRequestURI(URL); final MockHttpSession session = new MockHttpSession(); request.setSession(session); final MockHttpServletResponse response = new MockHttpServletResponse(); final FilterChain filterChain = new FilterChain() { public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { } }; f.doFilter(request, response, filterChain); assertNull(response.getRedirectedUrl()); }