/** * Create new MockServletConfig. * @param servletContext the ServletContext that the JSP page runs in * @param request the current HttpServletRequest * @param response the current HttpServletResponse * @param servletConfig the ServletConfig (hardly ever accessed from within a tag) */ public MockPageContext(@Nullable ServletContext servletContext, @Nullable HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable ServletConfig servletConfig) { this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); this.request = (request != null ? request : new MockHttpServletRequest(servletContext)); this.response = (response != null ? response : new MockHttpServletResponse()); this.servletConfig = (servletConfig != null ? servletConfig : new MockServletConfig(servletContext)); }
public void initializeContext(ConfigurableWebApplicationContext context, String yamlPath) { MockServletContext servletContext = new MockServletContext() { @Override public <Type extends EventListener> void addListener(Type t) { //no op } }; MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("environmentConfigDefaults", yamlPath); context.setServletContext(servletContext); context.setServletConfig(servletConfig); new YamlServletProfileInitializer().initialize(context); } }
private void createServlet(Class<?> servletClass, ApplicationContext applicationContext, Map<String, String> initParameters) { if (servletCache.containsKey(applicationContext)) { servlet = servletCache.get(applicationContext); return; } MockServletConfig config = new MockServletConfig(); config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, new ApplicationContextWrapper(applicationContext, config.getServletContext())); if (initParameters!=null) { for (Map.Entry<String, String> param: initParameters.entrySet()) { config.addInitParameter(param.getKey(), param.getValue()); } } try { servlet = (HttpServlet) BeanUtils.instantiate(servletClass); servlet.init(config); servletCache.put(applicationContext, servlet); } catch (Exception e) { throw new IllegalArgumentException("Error when creating servlet "+servletClass.getName(),e); } }
private HttpJspBase loadJsp(String forwardedUrl, File compilationDir) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, ServletException { String jspClassName = toJspClassName(forwardedUrl); URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { compilationDir.toURI().toURL() }); Class<?> cls = Class.forName(jspClassName, true, classLoader); HttpJspBase instance = (HttpJspBase) cls.newInstance(); MockServletConfig servletConfig = new MockServletConfig(new MockServletContext()); servletConfig.getServletContext().setAttribute(InstanceManager.class.getName(), new InstanceManagerImplementation()); JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext( servletConfig.getServletContext()); // Register the ELResolver with JSP jspApplicationContext.addELResolver(manager.getELResolver()); // Register ELContextListener with JSP jspApplicationContext.addELContextListener(Reflections .<ELContextListener> newInstance("org.jboss.weld.el.WeldELContextListener")); // Push the wrapped expression factory into the servlet context so that // Tomcat or Jetty can hook it in using a container code servletConfig.getServletContext().setAttribute(EXPRESSION_FACTORY_NAME, manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory())); instance.init(servletConfig); JspFactory.setDefaultFactory(new org.apache.jasper.runtime.JspFactoryImpl()); instance._jspInit(); return instance; }
/** * 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[0]); return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers); }
servletContext.addInitParameter("pContext1", "pContext1Value"); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("pCommon", "pCommonConfigValue"); servletConfig.addInitParameter("pConfig1", "pConfig1Value");
protected DispatcherServlet getDispatcher() throws Exception { // create an instance of the spring dispatcher ServletContext context = applicationContext.getServletContext(); MockServletConfig config = new MockServletConfig(context, "dispatcher"); DispatcherServlet dispatcher = new DispatcherServlet(); dispatcher.setContextConfigLocation( GeoServerAbstractTestSupport.class .getResource("dispatcher-servlet.xml") .toString()); dispatcher.init(config); return dispatcher; }
servletContext.addInitParameter("pContext1", "pContext1Value"); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("pCommon", "pCommonConfigValue"); servletConfig.addInitParameter("pConfig1", "pConfig1Value");
protected DispatcherServlet buildDispatcher() throws ServletException { // create an instance of the spring dispatcher ServletContext context = applicationContext.getServletContext(); MockServletConfig config = new MockServletConfig(context, "dispatcher"); DispatcherServlet dispatcher = new DispatcherServlet(applicationContext); dispatcher.setContextConfigLocation( GeoServerAbstractTestSupport.class .getResource("dispatcher-servlet.xml") .toString()); dispatcher.init(config); return dispatcher; }
MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("environmentConfigLocations", StringUtils.arrayToCommaDelimitedString(yamlFiles)); context.setServletConfig(servletConfig);
private void loadConfig(Class<?>... configs) { context = new AnnotationConfigWebApplicationContext(); context.register(configs); context.setServletConfig(new MockServletConfig()); context.refresh(); }
private void prepareSVN() throws IOException, SVNException { File tempRepo = new File(System.getProperty("java.io.tmpdir"), "repo"); fileEntityRepository.setUserRepository(new File(tempRepo, getTestUser().getUserId())); tempRepo.deleteOnExit(); File testUserRoot = fileEntityRepository.getUserRepoDirectory(getTestUser()).getParentFile(); FileUtils.deleteQuietly(testUserRoot); testUserRoot.mkdirs(); CompressionUtils.unzip(new ClassPathResource("TEST_USER.zip").getFile(), testUserRoot); testUserRoot.deleteOnExit(); MockServletConfig servletConfig = new MockServletConfig(); servletConfig.addInitParameter("SVNParentPath", testUserRoot.getAbsolutePath()); DAVConfig davConfig = new DAVConfig(servletConfig); svnController.setDAVConfig(davConfig); }
private void loadConfig(Class<?>... configs) { context = new AnnotationConfigWebApplicationContext(); context.register(configs); context.register(WebSocketConfig.class, SyncExecutorConfig.class); context.setServletConfig(new MockServletConfig()); context.refresh(); }
@Override protected void setUp() throws Exception { super.setUp(); servlet = (HttpServlet)Class.forName("org.apache.wink.server.internal.servlet.RestServlet") .newInstance(); MockServletConfig servletConfig = new MockServletConfig(); servletConfig.addInitParameter("javax.ws.rs.Application", getApplicationClassName()); String propertiesFile = getPropertiesFile(); if (propertiesFile != null) { servletConfig.addInitParameter("propertiesLocation", propertiesFile); } String deploymentConfigClass = getDeploymentConfigurationClassName(); if (deploymentConfigClass != null) { servletConfig.addInitParameter("deploymentConfiguration", deploymentConfigClass); } tls.set(this); servlet.init(servletConfig); }
@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"); } }
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); }
@Before public void setUp() throws Exception { fileServlet = new StagemonitorFileServlet(); fileServlet.init(new MockServletConfig(new MockServletContext())); request = new MockHttpServletRequest("GET", "/stagemonitor/static/test.js"); response = new MockHttpServletResponse(); }
setServletConfig(new MockServletConfig(servletContext, "dispatcher")); getServletConfig().addInitParameter("config-location", m_cxfContextPath); CXFServlet servlet = new CXFServlet(); setDispatcher(servlet);
public void autowire() { this.context.setServletContext(new MockServletContext()); this.context.setServletConfig(new MockServletConfig()); this.context.refresh(); if (this.context.containsBean(SPRING_SECURITY_FILTER_CHAIN)) { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .apply(new AddFilter()).build(); this.context.getBeanFactory() .registerResolvableDependency(MockMvc.class, mockMvc); } AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(this.context.getBeanFactory()); bpp.processInjection(this.test); }
protected void setUp() throws Exception { super.setUp(); servlet = (HttpServlet)Class.forName("org.apache.wink.server.internal.servlet.RestServlet") .newInstance(); MockServletConfig servletConfig = new MockServletConfig(servletContext); servlet.init(servletConfig); }