Search in sources :

Example 41 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project cas by apereo.

the class Cas20ResponseViewTests method verifyView.

@Test
public void verifyView() throws Exception {
    val modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl(RegisteredServiceTestUtils.getService("https://www.casinthecloud.com"));
    val req = new MockHttpServletRequest(new MockServletContext());
    req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, new GenericWebApplicationContext(req.getServletContext()));
    val resp = new MockHttpServletResponse();
    val delegatedView = new View() {

        @Override
        public String getContentType() {
            return MediaType.TEXT_HTML_VALUE;
        }

        @Override
        public void render(final Map<String, ?> map, final HttpServletRequest request, final HttpServletResponse response) {
            map.forEach(request::setAttribute);
        }
    };
    val view = new Cas20ResponseView(true, new NoOpProtocolAttributeEncoder(), null, delegatedView, new DefaultAuthenticationAttributeReleasePolicy("attribute"), new DefaultAuthenticationServiceSelectionPlan(), NoOpProtocolAttributesRenderer.INSTANCE);
    view.render(modelAndView.getModel(), req, resp);
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_CHAINED_AUTHENTICATIONS));
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_PRIMARY_AUTHENTICATION));
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_PRINCIPAL));
    assertNotNull(req.getAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_PROXY_GRANTING_TICKET_IOU));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAuthenticationAttributeReleasePolicy(org.apereo.cas.authentication.DefaultAuthenticationAttributeReleasePolicy) NoOpProtocolAttributeEncoder(org.apereo.cas.authentication.support.NoOpProtocolAttributeEncoder) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 42 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project cas by apereo.

the class Cas30ResponseViewTests method renderView.

protected Map<?, ?> renderView() throws Exception {
    val modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl(DEFAULT_SERVICE);
    LOGGER.debug("Retrieved model and view [{}]", modelAndView.getModel());
    val req = new MockHttpServletRequest(new MockServletContext());
    req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, new GenericWebApplicationContext(req.getServletContext()));
    val encoder = new DefaultCasProtocolAttributeEncoder(this.servicesManager, CipherExecutor.noOpOfStringToString());
    val view = getCasViewToRender(encoder, getDelegatedView());
    val resp = new MockHttpServletResponse();
    view.render(modelAndView.getModel(), req, resp);
    return getRenderedViewModelMap(req);
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultCasProtocolAttributeEncoder(org.apereo.cas.authentication.support.DefaultCasProtocolAttributeEncoder) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 43 with GenericWebApplicationContext

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

the class EnvironmentSystemIntegrationTests method webApplicationContext.

@Test
void webApplicationContext() {
    GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
    assertHasStandardServletEnvironment(ctx);
    ctx.setEnvironment(prodWebEnv);
    ctx.refresh();
    assertHasEnvironment(ctx, prodWebEnv);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 44 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project flow by vaadin.

the class DevModeBrowserLauncher method launchBrowserInDevelopmentMode.

/**
 * Launch the default browser and open the given application base URL if
 * running in development mode.
 *
 * Does nothing if the application is running in production mode.
 *
 * @param appContext
 *            the application context
 */
private void launchBrowserInDevelopmentMode(ApplicationContext appContext) {
    if (isLaunched()) {
        // Only launch browser on startup, not on reload
        return;
    }
    if (!(appContext instanceof GenericWebApplicationContext)) {
        getLogger().warn("Unable to determine production mode for an Spring Boot application context of type " + appContext.getClass().getName());
        return;
    }
    GenericWebApplicationContext webAppContext = (GenericWebApplicationContext) appContext;
    if (!DevModeBrowserLauncher.isProductionMode(webAppContext)) {
        String location = getUrl(webAppContext);
        String outputOnFailure = "Application started at " + location;
        try {
            BrowserLauncher.launch(location, outputOnFailure);
            setLaunched();
        } catch (Exception | NoClassDefFoundError e) {
            // NOSONAR
            // NoClassDefFoundError happens if vaadin-dev-server is not on
            // the classpath
            getLogger().info(outputOnFailure);
        }
    }
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 45 with GenericWebApplicationContext

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

the class RequestHeaderMethodArgumentResolverTests method setUp.

@Before
@SuppressWarnings("resource")
public void setUp() throws Exception {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.refresh();
    resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());
    Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
    paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
    paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
    paramSystemProperty = new SynthesizingMethodParameter(method, 2);
    paramContextPath = new SynthesizingMethodParameter(method, 3);
    paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 4);
    paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 5);
    paramNamedValueMap = new SynthesizingMethodParameter(method, 6);
    paramDate = new SynthesizingMethodParameter(method, 7);
    paramInstant = new SynthesizingMethodParameter(method, 8);
    servletRequest = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.setRequestAttributes(webRequest);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

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