Search in sources :

Example 16 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method typeNestedSetBinding.

@Test
public void typeNestedSetBinding() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
            csDef.getPropertyValues().add("converters", new TestBeanConverter());
            RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
            wbiDef.getPropertyValues().add("conversionService", csDef);
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
            context.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, NestedSetController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    request.addParameter("testBeanSet", new String[] { "1", "2" });
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 17 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method defaultExpressionParameters.

@Test
public void defaultExpressionParameters() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition ppc = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
            ppc.getPropertyValues().add("properties", "myKey=foo");
            context.registerBeanDefinition("ppc", ppc);
        }
    }, DefaultExpressionValueParamController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myPath.do");
    request.setContextPath("/myApp");
    MockHttpServletResponse response = new MockHttpServletResponse();
    System.setProperty("myHeader", "bar");
    try {
        getServlet().service(request, response);
    } finally {
        System.clearProperty("myHeader");
    }
    assertEquals("foo-bar-/myApp", response.getContentAsString());
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 18 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method createContext.

private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setIncludeAnnotationConfig(false);
    scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {

        @Override
        public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
            ScopeMetadata metadata = new ScopeMetadata();
            if (definition instanceof AnnotatedBeanDefinition) {
                AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
                for (String type : annDef.getMetadata().getAnnotationTypes()) {
                    if (type.equals(javax.inject.Singleton.class.getName())) {
                        metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
                        break;
                    } else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
                        metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
                        metadata.setScopedProxyMode(scopedProxyMode);
                        break;
                    } else if (type.startsWith("javax.inject")) {
                        metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
                    }
                }
            }
            return metadata;
        }
    });
    // Scan twice in order to find errors in the bean definition compatibility check.
    scanner.scan(getClass().getPackage().getName());
    scanner.scan(getClass().getPackage().getName());
    context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
    context.refresh();
    return context;
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) ScopeMetadata(org.springframework.context.annotation.ScopeMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 19 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project webservices-axiom by apache.

the class ScenarioTestCase method setUp.

@Override
protected void setUp() throws Exception {
    server = new Server();
    // Set up a custom thread pool to improve thread names (for logging purposes)
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setName("jetty");
    server.setThreadPool(threadPool);
    Connector connector = new SelectChannelConnector();
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    ServletContextHandler handler = new ServletContextHandler(server, "/");
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setContextClass(GenericWebApplicationContext.class);
    servlet.setContextInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {

        public void initialize(ConfigurableApplicationContext applicationContext) {
            configureContext((GenericWebApplicationContext) applicationContext, config.getServerMessageFactoryConfigurator(), new ClassPathResource("server.xml", ScenarioTestCase.this.getClass()));
        }
    });
    ServletHolder servletHolder = new ServletHolder(servlet);
    servletHolder.setName("spring-ws");
    servletHolder.setInitOrder(1);
    handler.addServlet(servletHolder, "/*");
    server.start();
    context = new GenericXmlApplicationContext();
    MockPropertySource propertySource = new MockPropertySource("client-properties");
    propertySource.setProperty("port", connector.getLocalPort());
    context.getEnvironment().getPropertySources().replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, propertySource);
    configureContext(context, config.getClientMessageFactoryConfigurator(), new ClassPathResource("client.xml", getClass()));
    context.refresh();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) MessageDispatcherServlet(org.springframework.ws.transport.http.MessageDispatcherServlet) ClassPathResource(org.springframework.core.io.ClassPathResource) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MockPropertySource(org.springframework.mock.env.MockPropertySource) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 20 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-security by spring-projects.

the class OAuth2ResourceServerSpecTests method getJwtDecoderWhenNoBeansAndNoDslWiredThenWiringException.

@Test
public void getJwtDecoderWhenNoBeansAndNoDslWiredThenWiringException() {
    GenericWebApplicationContext context = autowireWebServerGenericWebApplicationContext();
    ServerHttpSecurity http = new ServerHttpSecurity();
    http.setApplicationContext(context);
    ServerHttpSecurity.OAuth2ResourceServerSpec.JwtSpec jwt = http.oauth2ResourceServer().jwt();
    assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> jwt.getJwtDecoder());
}
Also used : NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)93 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)33 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)32 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)31 Test (org.junit.Test)30 Test (org.junit.jupiter.api.Test)30 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)9 MockServletContext (org.springframework.mock.web.MockServletContext)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 Method (java.lang.reflect.Method)5 HttpSession (javax.servlet.http.HttpSession)5 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)5 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)5 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4