Search in sources :

Example 11 with VaadinServletContext

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

the class VaadinAppShellInitializerTest method setup.

@Before
public void setup() throws Exception {
    logger = mockLog(VaadinAppShellInitializer.class);
    mocks = new MockServletServiceSessionSetup();
    servletContext = mocks.getServletContext();
    appConfig = mockApplicationConfiguration();
    Lookup lookup = (Lookup) servletContext.getAttribute(Lookup.class.getName());
    Mockito.when(lookup.lookup(AppShellPredicate.class)).thenReturn(AppShellConfigurator.class::isAssignableFrom);
    attributeMap.put(Lookup.class.getName(), servletContext.getAttribute(Lookup.class.getName()));
    attributeMap.put(ApplicationConfiguration.class.getName(), appConfig);
    service = mocks.getService();
    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());
    ServletRegistration registration = Mockito.mock(ServletRegistration.class);
    context = new VaadinServletContext(servletContext);
    classes = new HashSet<>();
    Map<String, ServletRegistration> registry = new HashMap<>();
    registry.put("foo", registration);
    Mockito.when(servletContext.getServletRegistrations()).thenReturn((Map) registry);
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    initializer = new VaadinAppShellInitializer();
    document = Document.createShell("");
    pushConfiguration = Mockito.mock(PushConfiguration.class);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) PushConfiguration(com.vaadin.flow.component.PushConfiguration) AppShellConfigurator(com.vaadin.flow.component.page.AppShellConfigurator) ServletRegistration(javax.servlet.ServletRegistration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Lookup(com.vaadin.flow.di.Lookup) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup) Before(org.junit.Before)

Example 12 with VaadinServletContext

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

the class RouteRegistryInitializerTest method firstInitRouteRegistry.

private ApplicationRouteRegistry firstInitRouteRegistry() throws VaadinInitializerException {
    vaadinContext = new VaadinServletContext(servletContext) {

        private Map<Class<?>, Object> map = new HashMap<>();

        @Override
        public <T> T getAttribute(Class<T> type) {
            if (Lookup.class.equals(type)) {
                return type.cast(lookup);
            } else {
                return type.cast(map.get(type));
            }
        }

        @Override
        public <T> void setAttribute(Class<T> clazz, T value) {
            map.put(clazz, value);
        }
    };
    // first time initialization
    routeRegistryInitializer.initialize(Collections.singleton(OldRouteTarget.class), vaadinContext);
    ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(vaadinContext);
    List<RouteData> routes = registry.getRegisteredRoutes();
    // self check
    Assert.assertEquals(1, routes.size());
    RouteData data = routes.get(0);
    Assert.assertEquals("foo-bar", data.getTemplate());
    List<RouteAliasData> aliases = data.getRouteAliases();
    Assert.assertEquals(1, aliases.size());
    RouteAliasData alias = aliases.get(0);
    Assert.assertEquals("baz", alias.getTemplate());
    return registry;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) HashMap(java.util.HashMap) Lookup(com.vaadin.flow.di.Lookup) RouteData(com.vaadin.flow.router.RouteData) RouteAliasData(com.vaadin.flow.router.RouteAliasData)

Example 13 with VaadinServletContext

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

the class RouteRegistryInitializerTest method init.

@Before
public void init() {
    pathProvider = Mockito.mock(RoutePathProvider.class);
    routeRegistryInitializer = new RouteRegistryInitializer();
    registry = new TestRouteRegistry();
    servletContext = Mockito.mock(ServletContext.class);
    lookup = Mockito.mock(Lookup.class);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    vaadinContext = new VaadinServletContext(servletContext);
    registry = ApplicationRouteRegistry.getInstance(vaadinContext);
    Mockito.when(vaadinContext.getAttribute(ApplicationRouteRegistry.ApplicationRouteRegistryWrapper.class)).thenReturn(new ApplicationRouteRegistry.ApplicationRouteRegistryWrapper(registry));
    Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(pathProvider);
    Mockito.doAnswer(invocation -> {
        Class clazz = invocation.getArgument(0, Class.class);
        Annotation route = clazz.getAnnotation(Route.class);
        return ((Route) route).value();
    }).when(pathProvider).getRoutePath(Mockito.any());
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) RoutePathProvider(com.vaadin.flow.router.RoutePathProvider) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Annotation(java.lang.annotation.Annotation) Route(com.vaadin.flow.router.Route) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) Before(org.junit.Before)

Example 14 with VaadinServletContext

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

the class VaadinServletContextInitializerTest method errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewExtendingRouteNotFoundError_customRouteNotFoundViewIsRegistered.

@Test
public void errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewExtendingRouteNotFoundError_customRouteNotFoundViewIsRegistered() throws Exception {
    // given
    initDefaultMocks();
    VaadinServletContextInitializer initializer = getStubbedVaadinServletContextInitializer();
    Runnable when = initRouteNotFoundMocksAndGetContextInitializedMockCall(initializer);
    class TestErrorView extends RouteNotFoundError {
    }
    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) RouteNotFoundError(com.vaadin.flow.router.RouteNotFoundError) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Test(org.junit.Test)

Example 15 with VaadinServletContext

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

the class DevModeEndpointTest method setup.

@Before
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setup() throws Exception {
    super.setup();
    assertFalse("No DevModeHandler should be available at test start", DevModeHandlerManager.getDevModeHandler(new VaadinServletContext(servletContext)).isPresent());
    createStubNode(false, true, baseDir);
    createStubWebpackServer("Compiled", 500, baseDir, true);
    // Prevent TaskRunNpmInstall#cleanUp from deleting node_modules
    new File(baseDir, "node_modules/.modules.yaml").createNewFile();
    ServletRegistration vaadinServletRegistration = Mockito.mock(ServletRegistration.class);
    Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl()).when(lookup).lookup(EndpointGeneratorTaskFactory.class);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    Mockito.when(vaadinServletRegistration.getClassName()).thenReturn(VaadinServletSubClass.class.getName());
    classes = new HashSet<>();
    classes.add(this.getClass());
    Map registry = new HashMap();
    // Adding extra registrations to make sure that
    // DevModeInitializer picks
    // the correct registration which is a VaadinServlet
    // registration.
    registry.put("extra1", Mockito.mock(ServletRegistration.class));
    registry.put("foo", vaadinServletRegistration);
    registry.put("extra2", Mockito.mock(ServletRegistration.class));
    Mockito.when(servletContext.getServletRegistrations()).thenReturn(registry);
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader());
    FileUtils.forceMkdir(new File(baseDir, DEFAULT_CONNECT_JAVA_SOURCE_FOLDER));
    devModeStartupListener = new DevModeStartupListener();
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletRegistration(javax.servlet.ServletRegistration) EndpointGeneratorTaskFactoryImpl(dev.hilla.frontend.EndpointGeneratorTaskFactoryImpl) HashMap(java.util.HashMap) DevModeStartupListener(com.vaadin.base.devserver.startup.DevModeStartupListener) ResourceProvider(com.vaadin.flow.di.ResourceProvider) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

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