@Before public void before() throws Exception { servlet = new CrnkServlet(); servletContext = new MockServletContext(); ((MockServletContext) servletContext).setContextPath(""); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig .addInitParameter(CrnkProperties.RESOURCE_SEARCH_PACKAGE, RESOURCE_SEARCH_PACKAGE); servletConfig .addInitParameter(CrnkProperties.RESOURCE_DEFAULT_DOMAIN, RESOURCE_DEFAULT_DOMAIN); servletConfig .addInitParameter(CrnkProperties.REJECT_PLAIN_JSON, String.valueOf(true)); servlet.init(servletConfig); nodeRepository = new NodeRepository(); }
protected void startRepository() throws Exception { final MockServletContext mockServletContext = new MockServletContext(); WebApplicationContext webApplicationContext = new TestWebapplicationContext( applicationContext, mockServletContext ); mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext ); MockServletConfig mockServletConfig = new MockServletConfig() { @Override public ServletContext getServletContext() { return mockServletContext; } }; unauthenticatedRepositoryServlet.init( mockServletConfig ); }
@Before @Override public void setUp() throws Exception { final MockServletContext mockServletContext = new MockServletContext(); WebApplicationContext webApplicationContext = new TestWebapplicationContext( applicationContext, mockServletContext ); mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext ); MockServletConfig mockServletConfig = new MockServletConfig() { @Override public ServletContext getServletContext() { return mockServletContext; } }; rssFeedServlet.init( mockServletConfig ); }
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig, WebApplicationContext webAppContext, RequestBuilder defaultRequestBuilder, List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers, Boolean dispatchOptions) { ServletContext servletContext = webAppContext.getServletContext(); TestDispatcherServlet dispatcherServlet = new TestDispatcherServlet(webAppContext); dispatcherServlet.setDispatchOptionsRequest(dispatchOptions); try { dispatcherServlet.init(servletConfig); } catch (ServletException ex) { // should never happen.. throw new MockMvcBuildException("Failed to initialize TestDispatcherServlet", ex); } MockMvc mockMvc = new MockMvc(dispatcherServlet, filters, servletContext); mockMvc.setDefaultRequest(defaultRequestBuilder); mockMvc.setGlobalResultMatchers(globalResultMatchers); mockMvc.setGlobalResultHandlers(globalResultHandlers); return mockMvc; }
/** * Constructor for SessionServletTest, calls superclass constructor and sets up servlet * @param name */ public SessionServletTest(String name) { super(name); _servlet = new SessionServlet(); // Create mock objects for servlet context & config MockServletContext context = new MockServletContext("/"); MockServletConfig config = new MockServletConfig(context); config.addInitParameter("s2s-session-directory", "/tmp/s2s/sessions"); try { _servlet.init(config); } catch (ServletException e) { fail("Could not instantiate SessionServlet"); } }
@Override @Before public void setUp() throws Exception { requestPayload = readResource("request_add.txt"); responsePayload = readResource("response_add.txt"); MockServletContext servletContext = new MockServletContext(new ResourceLoaderSupport()); MockServletConfig servletConfig = new MockServletConfig(servletContext); applicationContext = new XmlWebApplicationContext(); applicationContext.setServletContext(servletContext); applicationContext.setServletConfig(servletConfig); applicationContext.setConfigLocations(new String[] { "src/main/webapp/WEB-INF/exporter-servlet.xml", "src/main/webapp/WEB-INF/applicationContext.xml" }); applicationContext.refresh(); handlerMapping = (SimpleUrlHandlerMapping) applicationContext .getBean("org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"); }
@Test public void testRequest() throws Exception { MockServletConfig config = new MockServletConfig(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); config.addInitParameter("contextConfigLocation", "classpath:spring-web-test.xml"); req.setMethod("GET"); req.setRequestURI("/"); req.setRemoteAddr("1.2.3.4"); DispatcherServlet servlet = new DispatcherServlet(); servlet.init(config); servlet.service(req, res); Method method = FrameworkServlet.class.getDeclaredMethod("doGet", HttpServletRequest.class, HttpServletResponse.class); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); verifier.verifyTrace(Expectations.event(SPRING_MVC, method)); verifier.verifyTraceCount(0); }
@Test public void testMockWebContext() throws ServletException, IOException { // create mock context that loads from the classpath MockServletConfig config = new MockServletConfig(); MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext()); request.setContentType("text/x-gwt-rpc"); request.setCharacterEncoding("UTF-8"); request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/" + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|" + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|" + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|" + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8")); request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2"); request.addHeader("X-GWT-Module-Base", "http://test/module/"); MockHttpServletResponse response = new MockHttpServletResponse(); defaultController.setServletConfig(config); defaultController.doPost(request, response); // expect the message of the out-dated 1.3 policy of GWT Assert.assertTrue(response.getContentAsString().contains( "Type \\x27org.geomajas.gwt.client.command.GwtCommand\\x27 was not assignable" + " to \\x27com.google.gwt.user.client.rpc.IsSerializable\\x27")); }
@Test public void testSerializationPolicy() throws UnsupportedEncodingException, ServletException { // create mock context that loads from the classpath MockServletConfig config = new MockServletConfig(); MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext()); request.setContentType("text/x-gwt-rpc"); request.setCharacterEncoding("UTF-8"); request.setContent(("6|0|10|http://apps.geomajas.org/explorer/be.geosparc.Explorer/" + "|54044FB0C988344F1715C8B91330B0A2|org.geomajas.gwt.client.GeomajasService|" + "execute|org.geomajas.gwt.client.command.GwtCommand/4093389776|command.configuration.GetMap|" + "org.geomajas.command.dto.GetMapConfigurationRequest/104733661|explorer|mainMap|" + "ss.TqRPfHFh24NVxB|1|2|3|4|1|5|5|6|7|8|9|0|10|").getBytes("UTF-8")); request.addHeader("X-GWT-Permutation", "54044FB0C988344F1715C8B91330B0A2"); request.addHeader("X-GWT-Module-Base", "http://test/module/"); MockHttpServletResponse response = new MockHttpServletResponse(); customController.setServletConfig(config); customController.doPost(request, response); // expect the message that the type is missing from our policy file Assert.assertTrue(response.getContentAsString().contains( "Type \\x27org.geomajas.gwt.client.command.GwtCommand\\x27 was not included in the set of types")); }
@Before public void init() { this.dispatcher = new DispatcherServlet() { protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.setParent(applicationContext); wac.refresh(); return wac; } }; try { this.dispatcher.init(new MockServletConfig()); } catch (ServletException e) { e.printStackTrace(); } log.debug("###### EgovExcelServiceControllerTest ######"); }
@Before public void setUp() throws Exception { this.safeServlet = new SafeDispatcherServlet(); this.mockContext = new MockServletContext(); this.mockConfig = new MockServletConfig(this.mockContext); }
@Before public void before() throws Exception { servlet = new CrnkServlet(); servletContext = new MockServletContext(); ((MockServletContext) servletContext).setContextPath(""); servletConfig = new MockServletConfig(servletContext); ((MockServletConfig) servletConfig) .addInitParameter(CrnkProperties.RESOURCE_SEARCH_PACKAGE, RESOURCE_SEARCH_PACKAGE); ((MockServletConfig) servletConfig) .addInitParameter(CrnkProperties.RESOURCE_DEFAULT_DOMAIN, RESOURCE_DEFAULT_DOMAIN); servlet.init(servletConfig); nodeRepository = new NodeRepository(); }
/** * this zero-arg constructor will be invoked if you use the 'configure * httpClientClass' option refer to the MockJerseyServletFactory for how you * can construct this manually and have full control over * dependency-injection specific to your environment * * @throws Exception */ public MockJerseyServlet() throws Exception { logger.info("auto construction of mock http servlet"); ServletConfig servletConfig = new MockServletConfig(); servletContext = new MockServletContext(); ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class); servlet = new ServletContainer(resourceConfig); servlet.init(servletConfig); }
private static Servlet initServlet() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(MockDemoConfig.class); context.setServletContext(SERVLET_CONTEXT); DispatcherServlet servlet = new DispatcherServlet(context); ServletConfig servletConfig = new MockServletConfig(); try { servlet.init(servletConfig); } catch (Exception e) { throw new RuntimeException(e); } return servlet; }
@Ignore @Test public void doPost() throws Exception { DwrSpringServlet servlet = new DwrSpringServlet(); MockServletConfig config = new MockServletConfig(new MockServletContext()); servlet.setIncludeDefaultConfig(false); servlet.init(config); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.doPost(request, response); }
/** * Build a {@link org.springframework.test.web.servlet.MockMvc} instance. */ @Override @SuppressWarnings("rawtypes") public final MockMvc build() { WebApplicationContext wac = initWebAppContext(); ServletContext servletContext = wac.getServletContext(); MockServletConfig mockServletConfig = new MockServletConfig(servletContext); for (MockMvcConfigurer configurer : this.configurers) { RequestPostProcessor processor = configurer.beforeMockMvcCreated(this, wac); if (processor != null) { if (this.defaultRequestBuilder == null) { this.defaultRequestBuilder = MockMvcRequestBuilders.get("/"); } if (this.defaultRequestBuilder instanceof ConfigurableSmartRequestBuilder) { ((ConfigurableSmartRequestBuilder) this.defaultRequestBuilder).with(processor); } } } Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]); return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatchOptions); }
@Before public void before() throws Exception { katharsisServlet = new KatharsisServlet(); servletContext = new MockServletContext(); ((MockServletContext) servletContext).setContextPath(""); servletConfig = new MockServletConfig(servletContext); ((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_SEARCH_PACKAGE, RESOURCE_SEARCH_PACKAGE); katharsisServlet.init(servletConfig); nodeRepository = new NodeRepository(); }
@Before public void before() throws Exception { katharsisServlet = new SampleKatharsisServlet(); servletContext = new MockServletContext(); ((MockServletContext) servletContext).setContextPath(""); servletConfig = new MockServletConfig(servletContext); ((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_SEARCH_PACKAGE, RESOURCE_SEARCH_PACKAGE); ((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_DEFAULT_DOMAIN, RESOURCE_DEFAULT_DOMAIN); katharsisServlet.init(servletConfig); nodeRepository = new NodeRepository(); }
@Before public void before() throws Exception { katharsisServlet = new KatharsisServlet(); servletContext = new MockServletContext(); ((MockServletContext) servletContext).setContextPath(""); servletConfig = new MockServletConfig(servletContext); ((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_SEARCH_PACKAGE, RESOURCE_SEARCH_PACKAGE); ((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_DEFAULT_DOMAIN, RESOURCE_DEFAULT_DOMAIN); katharsisServlet.init(servletConfig); nodeRepository = new NodeRepository(); }
@Before public void setup() throws Exception{ taskHub = mock(TaskHub.class); servletContext = new MockServletContext(); servletConfig = new MockServletConfig(servletContext); servletContext.setAttribute(TaskHub.class.getName(), taskHub); publicRelay = new PublicRelayServlet(); publicRelay.init(servletConfig); }
@Before public void setUp() throws ServletException { req = new MockHttpServletRequest(); req.setServletPath("/_ah/api"); req.addHeader("Host", API_SERVER_NAME); req.setServerName(API_SERVER_NAME); req.setServerPort(API_PORT); resp = new MockHttpServletResponse(); servlet = new EndpointsServlet(); MockServletConfig config = new MockServletConfig(); config.addInitParameter("services", TestApi.class.getName()); servlet.init(config); }
private ServletContext createServletContext () throws Exception { // Establish the servlet context and config final MockServletContext servletContext = new MockServletContext(WEB_APP_PATH, new FileSystemResourceLoader()); final MockServletConfig servletConfig = new MockServletConfig(servletContext); // Create a WebApplicationContext and initialize it with the xml and servlet configuration. final XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext); webApplicationContext.setServletConfig(servletConfig); // Create a DispatcherServlet that uses the previously established WebApplicationContext. @SuppressWarnings("serial") final DispatcherServlet dispatcherServlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) { return webApplicationContext; } }; // Prepare the context. webApplicationContext.refresh(); webApplicationContext.registerShutdownHook(); // Initialize the servlet. dispatcherServlet.setContextConfigLocation(""); dispatcherServlet.init(servletConfig); return dispatcherServlet.getServletContext(); }
public DispatcherServletChannelInitializer(Class<? extends WebMvcConfigurerAdapter> clasConfig) throws ServletException { MockServletContext servletContext = new MockServletContext(); MockServletConfig servletConfig = new MockServletConfig(servletContext); //the alternative for web.xml AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext(); wac.setServletContext(servletContext); wac.setServletConfig(servletConfig); wac.register(clasConfig); wac.refresh(); this.dispatcherServlet = new DispatcherServlet(wac); this.dispatcherServlet.init(servletConfig); }
public static void setUpEnvironment() throws Exception { System.out.println("Starting JUnit test StartServletTest..."); DatabaseHandler.cleanDatabase(); DatabaseHandler.writeTorquePropsToHome(); DatabaseHandler.startDbServer(); realPath = System.getProperty("user.dir")+"/src/main/webapp"; homeDir = new File(System.getProperty("user.dir")+"/homet"); if (!homeDir.isDirectory()) { homeDir.delete(); } if (!homeDir.exists()) { homeDir.mkdirs(); } System.setProperty("TRACKPLUS_HOME", System.getProperty("user.dir")+"/homet"); DatabaseHandler.setupContext(); ctx = DatabaseHandler.getMockServletContext(); config = new MockServletConfig(ctx); config.addInitParameter("attachmentRootDir", homeDir.getAbsolutePath()); config.addInitParameter("key2", "value2"); config.addInitParameter("list", "value1, value2"); config.addInitParameter("listesc", "value1\\,value2"); }
/** * * @throws Exception - if something is wrong this exception is thrown. */ @Before public void setUp() throws Exception { securityManager = new MockSecurityManager(); servletContext = new MockServletContext(); servletConfig = new MockServletConfig(servletContext); }
protected void setUp() throws Exception { super.setUp(); _servlet = new ProxyServlet(); // Create mock objects for servlet context & config MockServletContext context = new MockServletContext("/"); MockServletConfig config = new MockServletConfig(context); config.addInitParameter("s2s-properties",S2S_PROPERTIES); config.addInitParameter("s2s-sources-property",TEST_SOURCES_CONFIG); config.addInitParameter("s2s-caching-property",TEST_CACHING_PROP); _servlet.init(config); }
/** * Test of getContent method, of class fr.paris.lutece.portal.web.PortalJspBean. */ public void testGetContent( ) throws Exception { System.out.println( "getContent" ); HttpServletRequest request = new MockHttpServletRequest( ); LocalVariables.setLocal( new MockServletConfig( ), request, new MockHttpServletResponse( ) ); PortalJspBean instance = new PortalJspBean( ); String result = instance.getContent( request ); }
/** * Test of getAdminPagePreview method, of class fr.paris.lutece.portal.web.admin.AdminJspBean. * * @throws UserNotSignedException */ public void testGetAdminPagePreview( ) throws AccessDeniedException, UserNotSignedException { System.out.println( "getAdminPagePreview" ); MockHttpServletRequest request = new MockHttpServletRequest( ); // FIXME : MokeHttpServletRequest should be fixed to support attributes Map<String, Right> mapRights = new HashMap<String, Right>( ); Right right = new Right( ); right.setId( AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE ); mapRights.put( AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE, right ); AdminUser user = new AdminUser( ); user.setRights( mapRights ); user.setLocale( new Locale( "fr", "FR", "" ) ); request.getSession( true ).setAttribute( ATTRIBUTE_ADMIN_USER, user ); LocalVariables.setLocal( new MockServletConfig( ), request, new MockHttpServletResponse( ) ); AdminPageJspBean instance = new AdminPageJspBean( ); instance.init( request, AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE ); try { instance.getAdminPagePreview( request ); } catch( SiteMessageException e ) { // TODO Auto-generated catch block e.printStackTrace( ); } }
@Before public void setUp() throws Throwable { beforeServletStart(); 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("------------------------------------------------------------------------------"); }
public void testSetServletConfig() { ServletContext servletContext = new MockServletContext(); BundleContext bc = new MockBundleContext(); servletContext.setAttribute(OsgiBundleXmlWebApplicationContext.BUNDLE_CONTEXT_ATTRIBUTE, bc); ServletConfig servletConfig = new MockServletConfig(servletContext); context.setServletConfig(servletConfig); assertSame(bc, context.getBundleContext()); }
@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 testNonWebContext() throws ServletException, IOException { // create mock context that loads from the classpath MockServletConfig config = new MockServletConfig(); MockHttpServletRequest request = new MockHttpServletRequest(config.getServletContext()); MockHttpServletResponse response = new MockHttpServletResponse(); GeomajasController c = new GeomajasController(); c.init(config); try { c.doPost(request, response); Assert.fail("Should fail outside web context"); } catch (RuntimeException e) { } }