Search in sources :

Example 6 with VaadinServletContext

use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.

the class AbstractUIScopedTest method mockUI.

protected UI mockUI() {
    VaadinSession session = mockSession();
    Router router = mock(Router.class);
    VaadinService service = session.getService();
    when(service.getRouter()).thenReturn(router);
    Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    when(service.getMainDivId(Mockito.any(), Mockito.any())).thenReturn(" - ");
    final Map<String, Object> attributeMap = new HashMap<>();
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).then(invocationOnMock -> attributeMap.get(invocationOnMock.getArguments()[0].toString()));
    Mockito.doAnswer(invocationOnMock -> attributeMap.put(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])).when(servletContext).setAttribute(Mockito.anyString(), Mockito.any());
    VaadinServletContext context = new VaadinServletContext(servletContext);
    Mockito.when(service.getContext()).thenReturn(context);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(appConfig, getClass(), initParameters);
    when(service.getDeploymentConfiguration()).thenReturn(config);
    ui = new UI();
    ui.getInternals().setSession(session);
    ui.doInit(null, 1);
    UI.setCurrent(ui);
    return ui;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinSession(com.vaadin.flow.server.VaadinSession) HashMap(java.util.HashMap) Router(com.vaadin.flow.router.Router) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 7 with VaadinServletContext

use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.

the class VaadinRouteScopeTest method mockServletContext.

private void mockServletContext(UI ui) {
    VaadinService service = ui.getSession().getService();
    VaadinServletContext context = ((VaadinServletContext) service.getContext());
    ServletContext servletContext = context.getContext();
    WebApplicationContext appContext = Mockito.mock(WebApplicationContext.class);
    Mockito.when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).thenReturn(appContext);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 8 with VaadinServletContext

use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.

the class VaadinServletContextInitializerTest method errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered.

@Test
public void errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered() throws Exception {
    // given
    initDefaultMocks();
    VaadinServletContextInitializer initializer = getStubbedVaadinServletContextInitializer();
    Runnable when = initRouteNotFoundMocksAndGetContextInitializedMockCall(initializer);
    class TestErrorView extends Component implements HasErrorParameter<NotFoundException> {

        @Override
        public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NotFoundException> parameter) {
            return 0;
        }
    }
    Mockito.doAnswer(invocation -> Stream.of(TestErrorView.class)).when(initializer).findBySuperType(Mockito.anyCollection(), Mockito.eq(HasErrorParameter.class));
    // when
    when.run();
    // then
    ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(new VaadinServletContext(servletContext));
    final Class<? extends Component> navigationTarget = registry.getErrorNavigationTarget(new NotFoundException()).get().getNavigationTarget();
    Assert.assertEquals(TestErrorView.class, navigationTarget);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) NotFoundException(com.vaadin.flow.router.NotFoundException) ErrorParameter(com.vaadin.flow.router.ErrorParameter) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) Component(com.vaadin.flow.component.Component) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Test(org.junit.Test)

Example 9 with VaadinServletContext

use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.

the class UidlWriterTest method initializeUIForDependenciesTest.

private UI initializeUIForDependenciesTest(UI ui) throws Exception {
    mocks = new MockServletServiceSessionSetup();
    VaadinServletContext context = (VaadinServletContext) mocks.getService().getContext();
    Lookup lookup = context.getAttribute(Lookup.class);
    Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(new RoutePathProviderImpl());
    VaadinSession session = mocks.getSession();
    session.lock();
    ui.getInternals().setSession(session);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        routeConfiguration.setAnnotatedRoute(BaseClass.class);
    });
    for (String type : new String[] { "html", "js", "css" }) {
        mocks.getServlet().addServletContextResource("inline." + type, "inline." + type);
    }
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    ui.doInit(vaadinRequestMock, 1);
    ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(vaadinRequestMock));
    return ui;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinSession(com.vaadin.flow.server.VaadinSession) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) RoutePathProviderImpl(com.vaadin.flow.server.MockVaadinContext.RoutePathProviderImpl) Lookup(com.vaadin.flow.di.Lookup) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup)

Example 10 with VaadinServletContext

use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.

the class ClassLoaderAwareServletContainerInitializer method onStartup.

/**
 * Overridden to use different classloaders if needed.
 * <p>
 * {@inheritDoc}
 */
@Override
default void onStartup(Set<Class<?>> set, ServletContext context) throws ServletException {
    // see DeferredServletContextIntializers
    DeferredServletContextInitializers.Initializer deferredInitializer = ctx -> {
        ClassLoader webClassLoader = ctx.getClassLoader();
        ClassLoader classLoader = getClass().getClassLoader();
        /*
             * Hack is needed to make a workaround for weird behavior of WildFly
             * with skinnywar See https://github.com/vaadin/flow/issues/7805
             */
        boolean noHack = false;
        while (classLoader != null) {
            if (classLoader.equals(webClassLoader)) {
                noHack = true;
                break;
            } else {
                /*
                     * The classloader which has loaded this class ({@code
                     * classLoader}) should be either the {@code webClassLoader}
                     * or its child: in this case it knows how to handle the
                     * classes loaded by the {@code webClassLoader} : it either
                     * is able to load them itself or delegate to its parent
                     * (which is the {@code webClassLoader}): in this case hack
                     * is not needed and the {@link #process(Set,
                     * ServletContext)} method can be called directly.
                     */
                classLoader = classLoader.getParent();
            }
        }
        if (noHack) {
            process(set, ctx);
            return;
        }
        try {
            Class<?> initializer = ctx.getClassLoader().loadClass(getClass().getName());
            String processMethodName = Stream.of(ClassLoaderAwareServletContainerInitializer.class.getDeclaredMethods()).filter(method -> !method.isDefault() && !method.isSynthetic()).findFirst().get().getName();
            Method operation = Stream.of(initializer.getMethods()).filter(method -> method.getName().equals(processMethodName)).findFirst().get();
            operation.invoke(initializer.newInstance(), new Object[] { set, ctx });
        } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
            throw new ServletException(e);
        }
    };
    if (requiresLookup()) {
        VaadinServletContext vaadinContext = new VaadinServletContext(context);
        synchronized (context) {
            if (vaadinContext.getAttribute(Lookup.class) == null) {
                DeferredServletContextInitializers initializers = vaadinContext.getAttribute(DeferredServletContextInitializers.class, () -> new DeferredServletContextInitializers());
                initializers.addInitializer(ctx -> deferredInitializer.init(ctx));
                return;
            }
        }
    }
    deferredInitializer.init(context);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Stream(java.util.stream.Stream) ServletException(javax.servlet.ServletException) Set(java.util.Set) Lookup(com.vaadin.flow.di.Lookup) ServletContext(javax.servlet.ServletContext) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Lookup(com.vaadin.flow.di.Lookup) Method(java.lang.reflect.Method)

Aggregations

VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)20 Lookup (com.vaadin.flow.di.Lookup)8 ServletContext (javax.servlet.ServletContext)8 VaadinService (com.vaadin.flow.server.VaadinService)4 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 ResourceProvider (com.vaadin.flow.di.ResourceProvider)3 ServletException (javax.servlet.ServletException)3 Before (org.junit.Before)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)2 NotFoundException (com.vaadin.flow.router.NotFoundException)2 MockServletServiceSessionSetup (com.vaadin.flow.server.MockServletServiceSessionSetup)2 StaticFileServer (com.vaadin.flow.server.StaticFileServer)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)2 Properties (java.util.Properties)2 Set (java.util.Set)2