List of usage examples for org.springframework.mock.web MockServletContext setAttribute
@Override public void setAttribute(String name, @Nullable Object value)
From source file:org.springside.modules.test.spring.SpringWebTestHelper.java
/** * ServletContext?Spring WebApplicationContext. * /* w w w . ja va2 s .c om*/ * @param applicationContext ApplicationContext. */ public static void initWebApplicationContext(MockServletContext servletContext, ApplicationContext applicationContext) { ConfigurableWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(applicationContext); wac.setServletContext(servletContext); wac.setConfigLocation(""); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); wac.refresh(); }
From source file:grails.util.GrailsUtil.java
private static ApplicationContext createGrailsApplicationContext(ApplicationContext parent, GrailsApplication application) { GrailsRuntimeConfigurator config = new GrailsRuntimeConfigurator(application, parent); MockServletContext servletContext = new MockServletContext(new MockResourceLoader()); ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext) config.configure(servletContext); servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, appCtx); Assert.notNull(appCtx);//from www. j av a 2s . c o m return appCtx; }
From source file:org.springsource.restbucks.PaymentProcessIntegrationTest.java
@BeforeClass public static void beforeClass() { MockServletContext servletContext = new MockServletContext(); osivFilter = new OpenEntityManagerInViewFilter(); osivFilter.setServletContext(servletContext); servletApplicationContext = new AnnotationConfigWebApplicationContext(); servletApplicationContext.register(WebConfiguration.class); servletApplicationContext.setParent(new AnnotationConfigApplicationContext(ApplicationConfig.class)); servletApplicationContext.setServletContext(servletContext); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, servletApplicationContext);//from w ww . j av a2 s . c om servletApplicationContext.refresh(); mvc = MockMvcBuilders.webAppContextSetup(servletApplicationContext). // addFilter(new ShallowEtagHeaderFilter()). // addFilter(osivFilter). // build(); links = new DefaultLinkDiscoverer(); }
From source file:org.parancoe.web.test.junit4.WebXmlContextLoader.java
/** * Loads a Spring ApplicationContext from the supplied * <code>locations</code>. and creates a standard {@link GenericWebApplicationContext} instance. * * @return a new application context/*w w w.ja va 2 s .c o m*/ * @see org.springframework.test.context.ContextLoader#loadContext * @see GenericWebApplicationContext * @see #createBeanDefinitionReader(GenericApplicationContext) * @see BeanDefinitionReader */ @Override public final ConfigurableApplicationContext loadContext(String... locations) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Loading ApplicationContext for locations [{}].", StringUtils.arrayToCommaDelimitedString(locations)); } GenericWebApplicationContext context = new GenericWebApplicationContext(); createBeanDefinitionReader(context).loadBeanDefinitions(locations); MockServletContext servletContext = new MockServletContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); context.setServletContext(servletContext); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); context.registerShutdownHook(); return context; }
From source file:fi.okm.mpass.idp.authn.impl.SocialUserAuthServletTest.java
@BeforeMethod public void initTests() throws Exception { nullAuthenticator = "/method1"; throwingAuthenticator = "/method2"; subjectAuthenticator = "/method3"; throwingEvent = "throwingEvent"; username = "mockUser"; authnRedirectUrl = "http://localhost/redirect"; servlet = new SocialUserAuthServlet(); SocialIdentityFactory factory = new SocialIdentityFactory(); Map<String, Object> authenticators = new HashMap<String, Object>(); authenticators.put(nullAuthenticator, initNullAuthenticator()); authenticators.put(throwingAuthenticator, initThrowingAuthenticator()); authenticators.put(subjectAuthenticator, initSubjectAuthenticator()); factory.setSocialImplBeans(authenticators); MockServletContext servletContext = new MockServletContext(); servletContext.setAttribute("socialUserImplementationFactoryBeanInServletContext", factory); MockServletConfig servletConfig = new MockServletConfig(servletContext); servlet.init(servletConfig);/*from ww w .j a v a2s .co m*/ }
From source file:org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfigurationTests.java
private int getResponseStatus(AssertableWebApplicationContext context, String path) throws IOException, javax.servlet.ServletException { FilterChainProxy filterChainProxy = context.getBean(FilterChainProxy.class); MockServletContext servletContext = new MockServletContext(); MockHttpServletResponse response = new MockHttpServletResponse(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); MockHttpServletRequest request = new MockHttpServletRequest(servletContext); request.setServletPath(path);/*from w ww .j av a 2 s . c o m*/ request.setMethod("GET"); filterChainProxy.doFilter(request, response, new MockFilterChain()); return response.getStatus(); }
From source file:com.xinlv.test.MockContextLoader.java
@Override public ConfigurableApplicationContext loadContext(String... locations) throws Exception { ConfigurableWebApplicationContext context = new XmlWebApplicationContext(); MockServletContext servletContext = new MockServletContext("") { @Override// ww w. j av a 2 s .c o m public String getRealPath(String arg0) { return System.getProperty("java.io.tmpdir"); } }; servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); context.setServletContext(servletContext); String configLocation = "/xinlv-test-context.xml"; context.setConfigLocation(configLocation); /*String[] configLocations = PortalContextLoaderListener.locateConfigLocations(locations); context.setConfigLocations(configLocations);*/ context.refresh(); context.registerShutdownHook(); return context; }
From source file:com.sourcesense.alfresco.opensso.AlfrescoOpenSSOFilterTest.java
private AlfrescoFacade mockAlfrescoFacade() { GenericWebApplicationContext webApplicationContext = new MockAlfrescoApplicationContext(); MockServletContext mockServletContext = new MockServletContext(); mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);//from w w w . j a v a 2 s . c o m AlfrescoFacade mockAlfrescoFacade = new AlfrescoFacade(mockServletContext) { public ArrayList<String> users = new ArrayList<String>(); public HashMap<String, List<String>> groups = new HashMap<String, List<String>>(); @Override public void createUser(String username, String email, String firstName, String lastName) { users.add(username); } @Override public Boolean existUser(String username) { return users.contains(username); } public ArrayList<String> getUserGroups(String username) { return (ArrayList<String>) groups.get(username); } @Override protected void setLocale(HttpSession session) { // TODO Auto-generated method stub } @Override public void createOrUpdateGroups(String principal, List<String> openSSOGroups) { groups.remove(principal); groups.put(principal, openSSOGroups); } @Override protected void setAuthenticatedUser(HttpServletRequest req, HttpServletResponse res, HttpSession httpSess, String userName) { NodeRef nodeRef = new NodeRef("workspace://SpacesStore/386f7ece-4127-42b5-8543-3de2e2a20d7e"); User user = new User(userName, "ticket", nodeRef); populateSession(httpSess, user); } @Override public void authenticateAsGuest(HttpSession session) { } }; return mockAlfrescoFacade; }
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 av a 2 s . co 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.hdiv.web.servlet.tags.AbstractTagTests.java
protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext(); wac.setServletContext(sc);//from www . j av a2 s . c o m wac.setNamespace("test"); wac.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(sc); MockHttpServletResponse response = new MockHttpServletResponse(); if (inDispatcherServlet()) { request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); LocaleResolver lr = new AcceptHeaderLocaleResolver(); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr); ThemeResolver tr = new FixedThemeResolver(); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr); request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac); } else { sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } return new MockPageContext(sc, request, response); }