List of usage examples for org.springframework.mock.web MockServletConfig MockServletConfig
public MockServletConfig(@Nullable ServletContext servletContext, String servletName)
From source file:com.github.carlomicieli.nerdmovies.MockWebApplicationContextLoader.java
@Override public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { final MockServletContext servletContext = new MockServletContext(configuration.webapp(), new FileSystemResourceLoader()); final MockServletConfig servletConfig = new MockServletConfig(servletContext, configuration.name()); final AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); webContext.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles()); webContext.setServletConfig(servletConfig); webContext.setConfigLocations(mergedConfig.getLocations()); webContext.register(mergedConfig.getClasses()); // Create a DispatcherServlet that uses the previously established // WebApplicationContext. @SuppressWarnings("serial") final DispatcherServlet dispatcherServlet = new DispatcherServlet() { @Override//from w w w .j a va 2 s.c o m protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) { return webContext; } }; // Add the DispatcherServlet (and anything else you want) to the // context. // Note: this doesn't happen until refresh is called below. webContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.registerResolvableDependency(DispatcherServlet.class, dispatcherServlet); // Register any other beans here, including a ViewResolver if // you are using JSPs. } }); // Have the context notify the servlet every time it is refreshed. webContext.addApplicationListener( new SourceFilteringListener(webContext, new ApplicationListener<ContextRefreshedEvent>() { @Override public void onApplicationEvent(ContextRefreshedEvent event) { dispatcherServlet.onApplicationEvent(event); } })); // Prepare the context. webContext.refresh(); webContext.registerShutdownHook(); // Initialize the servlet. dispatcherServlet.setContextConfigLocation(""); dispatcherServlet.init(servletConfig); return webContext; }
From source file:org.jasig.cas.web.init.SafeDispatcherServletTests.java
public void testServiceSucceeds() { this.mockConfig = new MockServletConfig(this.mockContext, "cas"); this.safeServlet.init(this.mockConfig); ServletRequest mockRequest = new MockHttpServletRequest(); ServletResponse mockResponse = new MockHttpServletResponse(); try {/*from w ww . j av a 2 s.co m*/ this.safeServlet.service(mockRequest, mockResponse); } catch (ApplicationContextException e) { System.out.println(e); fail("Unexpected exception."); } catch (Exception e) { return; } }
From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();// ww w. ja v a 2 s. c o m setServletContext(new MockServletContext("file:src/main/webapp")); getServletContext().addInitParameter("contextConfigLocation", "file:src/main/resources/META-INF/opennms/component-service.xml"); getServletContext().addInitParameter("parentContextKey", "testDaoContext"); ServletContextEvent e = new ServletContextEvent(getServletContext()); setContextListener(new ContextLoaderListener()); getContextListener().contextInitialized(e); getServletContext().setContextPath(contextPath); setServletConfig(new MockServletConfig(getServletContext(), "dispatcher")); /* getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.netmgt.ncs.rest"); */ try { MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext())); afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }
From source file:com.github.jrialland.ajpclient.AbstractTomcatTest.java
/** * binds a servlet on the tomcat instance. the 'mapping' has the same format * that the 'servlet-mapping' parameter in web.xml. the path of the servlet * will be relative to //*from www . j av a 2s .co m*/ * * {@link Servlet#init(javax.servlet.ServletConfig)} is called with a mock * object as parameter. * * @param mapping * @param servlet */ protected void addServlet(final String mapping, final Servlet servlet) { final MockServletContext servletContext = new MockServletContext("/"); final MockServletConfig config = new MockServletConfig(servletContext, servlet.toString()); try { servlet.init(config); } catch (final Exception e) { throw new IllegalStateException(e); } servlets.put(mapping, servlet); }
From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();/* w ww. j a va 2s.c o m*/ setUser("admin", new String[] { "ROLE_ADMIN" }); DaoTestConfigBean bean = new DaoTestConfigBean(); bean.afterPropertiesSet(); MockDatabase db = new MockDatabase(true); DataSourceFactory.setInstance(db); XADataSourceFactory.setInstance(db); try { MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); // Jersey /* setServletConfig(new MockServletConfig(servletContext, "dispatcher")); getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.codehaus.jackson.jaxrs;org.opennms.web.rest;org.opennms.web.rest.config"); getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter"); getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter"); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); */ // Apache CXF setServletConfig(new MockServletConfig(servletContext, "dispatcher")); getServletConfig().addInitParameter("config-location", m_cxfContextPath); CXFServlet servlet = new CXFServlet(); setDispatcher(servlet); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }
From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java
@Before public void setUp() throws Throwable { beforeServletStart();/*from ww w. jav a 2 s . co m*/ DaoTestConfigBean bean = new DaoTestConfigBean(); bean.afterPropertiesSet(); MockDatabase db = new MockDatabase(true); DataSourceFactory.setInstance(db); setServletContext(new MockServletContext("file:src/main/webapp")); getServletContext().addInitParameter("contextConfigLocation", "classpath:/org/opennms/web/rest/applicationContext-test.xml " + "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml " + "classpath:/META-INF/opennms/applicationContext-soa.xml " + "classpath*:/META-INF/opennms/component-service.xml " + "classpath*:/META-INF/opennms/component-dao.xml " + "classpath:/META-INF/opennms/applicationContext-reportingCore.xml " + "classpath:/META-INF/opennms/applicationContext-databasePopulator.xml " + "classpath:/org/opennms/web/svclayer/applicationContext-svclayer.xml " + "classpath:/META-INF/opennms/applicationContext-mockEventProxy.xml " + "classpath:/applicationContext-jersey-test.xml " + "classpath:/META-INF/opennms/applicationContext-reporting.xml " + "classpath:/META-INF/opennms/applicationContext-mock-usergroup.xml " + "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml " + "/WEB-INF/applicationContext-spring-security.xml " + "/WEB-INF/applicationContext-jersey.xml"); getServletContext().addInitParameter("parentContextKey", "daoContext"); ServletContextEvent e = new ServletContextEvent(getServletContext()); setContextListener(new ContextLoaderListener()); getContextListener().contextInitialized(e); getServletContext().setContextPath(contextPath); setServletConfig(new MockServletConfig(getServletContext(), "dispatcher")); getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.web.rest"); try { MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter"); setFilter(new OpenSessionInViewFilter()); getFilter().init(filterConfig); setDispatcher(new SpringServlet()); getDispatcher().init(getServletConfig()); } catch (ServletException se) { throw se.getRootCause(); } setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext())); afterServletStart(); System.err.println("------------------------------------------------------------------------------"); }