Search in sources :

Example 11 with VaadinServlet

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

the class RouterLinkTest method invalidRouteWhenConstructing.

@Test(expected = IllegalArgumentException.class)
public void invalidRouteWhenConstructing() throws ServletException {
    VaadinServlet servlet = new VaadinServlet();
    Properties initParams = new Properties();
    initParams.setProperty(Constants.SERVLET_PARAMETER_USING_NEW_ROUTING, "false");
    servlet.init(new MockServletConfig(initParams));
    try {
        VaadinService.setCurrent(servlet.getService());
        servlet.getService().getRouter().reconfigure(c -> c.setRoute("show/{bar}", TestView.class));
        new RouterLink("Show something", TestView.class);
    } finally {
        VaadinService.setCurrent(null);
    }
}
Also used : TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Properties(java.util.Properties) Test(org.junit.Test)

Example 12 with VaadinServlet

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

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    Router router = new Router();
    router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 13 with VaadinServlet

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

the class UITest method initUI.

private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) {
    try {
        VaadinRequest request = Mockito.mock(VaadinRequest.class);
        VaadinResponse response = Mockito.mock(VaadinResponse.class);
        String pathInfo;
        if (initialLocation.isEmpty()) {
            pathInfo = null;
        } else {
            Assert.assertFalse(initialLocation.startsWith("/"));
            pathInfo = "/" + initialLocation;
        }
        Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
        Properties initParams = new Properties();
        initParams.setProperty(Constants.SERVLET_PARAMETER_USING_NEW_ROUTING, "false");
        ServletConfig servletConfig = new MockServletConfig(initParams);
        VaadinServlet servlet = new VaadinServlet();
        servlet.init(servletConfig);
        VaadinService service = servlet.getService();
        service.setCurrentInstances(request, response);
        service.getRouter().reconfigure(c -> {
        });
        MockVaadinSession session = new MockVaadinSession(service);
        ui.getInternals().setSession(session);
        ui.doInit(request, 0);
        if (statusCodeCaptor != null) {
            Mockito.verify(response).setStatus(statusCodeCaptor.capture());
        }
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinService(com.vaadin.flow.server.VaadinService) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Properties(java.util.Properties)

Example 14 with VaadinServlet

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

the class DefaultTemplateParserTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
    HttpSession httpSession = Mockito.mock(HttpSession.class);
    Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return service;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(service.getServlet()).thenReturn(servlet);
    Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
    Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
    Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
    Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
    Mockito.when(request.getServletPath()).thenReturn("");
    Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
    CurrentInstance.set(VaadinRequest.class, request);
    CurrentInstance.set(VaadinSession.class, session);
    CurrentInstance.set(VaadinService.class, service);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletException(javax.servlet.ServletException) VaadinSession(com.vaadin.flow.server.VaadinSession) ServiceException(com.vaadin.flow.server.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) Before(org.junit.Before)

Aggregations

VaadinServlet (com.vaadin.flow.server.VaadinServlet)14 VaadinService (com.vaadin.flow.server.VaadinService)6 ServletException (javax.servlet.ServletException)6 Before (org.junit.Before)6 ServletConfig (javax.servlet.ServletConfig)5 Test (org.junit.Test)5 DependencyFilter (com.vaadin.flow.server.DependencyFilter)4 MockServletConfig (com.vaadin.flow.server.MockServletConfig)4 VaadinServletService (com.vaadin.flow.server.VaadinServletService)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Properties (java.util.Properties)4 ServletContext (javax.servlet.ServletContext)4 VaadinRequest (com.vaadin.flow.server.VaadinRequest)3 VaadinResponse (com.vaadin.flow.server.VaadinResponse)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 Dependency (com.vaadin.flow.shared.ui.Dependency)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3