Search in sources :

Example 1 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class BootstrapHandlerDependenciesTest method setup.

@Before
public void setup() {
    BootstrapHandler.clientEngineFile = "foobar";
    DeploymentConfiguration deploymentConfiguration = new MockDeploymentConfiguration();
    service = Mockito.spy(new MockVaadinServletService(deploymentConfiguration));
    when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(deploymentConfiguration);
    VaadinUriResolverFactory factory = Mockito.mock(VaadinUriResolverFactory.class);
    VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
    Mockito.when(factory.getUriResolver(Mockito.any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(Mockito.anyString())).thenAnswer(i -> i.getArguments()[0]);
    Mockito.doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(Mockito.any(), Mockito.anyString());
    session.setAttribute(VaadinUriResolverFactory.class, factory);
    VaadinSession.setCurrent(session);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Matchers.anyString(org.mockito.Matchers.anyString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Before(org.junit.Before)

Example 2 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class BootstrapHandlerTest method testBootstrapListener.

@Test
public void testBootstrapListener() throws ServiceException {
    List<BootstrapListener> listeners = new ArrayList<>(3);
    AtomicReference<VaadinUriResolver> resolver = new AtomicReference<>();
    listeners.add(evt -> evt.getDocument().head().getElementsByTag("script").remove());
    listeners.add(evt -> {
        resolver.set(evt.getUriResolver());
        evt.getDocument().head().appendElement("script").attr("src", "testing.1");
    });
    listeners.add(evt -> evt.getDocument().head().appendElement("script").attr("src", "testing.2"));
    Mockito.when(service.createInstantiator()).thenReturn(new MockInstantiator(event -> listeners.forEach(event::addBootstrapListener)));
    initUI(testUI);
    BootstrapContext bootstrapContext = new BootstrapContext(request, null, session, testUI);
    Document page = BootstrapHandler.getBootstrapPage(bootstrapContext);
    Elements scripts = page.head().getElementsByTag("script");
    assertEquals(2, scripts.size());
    assertEquals("testing.1", scripts.get(0).attr("src"));
    assertEquals("testing.2", scripts.get(1).attr("src"));
    Assert.assertNotNull(resolver.get());
    Assert.assertEquals(bootstrapContext.getUriResolver(), resolver.get());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) AtomicReference(java.util.concurrent.atomic.AtomicReference) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 3 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class UidlWriterTest method initializeUIForDependenciesTest.

private UI initializeUIForDependenciesTest(UI ui) {
    ServletContext context = Mockito.mock(ServletContext.class);
    VaadinServletService service = new VaadinServletService(new VaadinServlet() {

        @Override
        public ServletContext getServletContext() {
            return context;
        }
    }, new MockDeploymentConfiguration()) {

        RouterInterface router = new com.vaadin.flow.router.legacy.Router();

        @Override
        public RouterInterface getRouter() {
            return router;
        }

        @Override
        public Iterable<DependencyFilter> getDependencyFilters() {
            return Collections.emptyList();
        }
    };
    service.getRouter().reconfigure(conf -> {
        conf.setRoute("", BaseClass.class);
        conf.setParentView(BaseClass.class, ParentClass.class);
        conf.setParentView(ParentClass.class, SuperParentClass.class);
    });
    MockVaadinSession session = new MockVaadinSession(service);
    session.lock();
    ui.getInternals().setSession(session);
    when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
    ui.doInit(vaadinRequestMock, 1);
    factory = mock(VaadinUriResolverFactory.class);
    VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
    Mockito.when(factory.getUriResolver(any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
    doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(any(), anyString());
    session.setAttribute(VaadinUriResolverFactory.class, factory);
    VaadinSession.setCurrent(session);
    return ui;
}
Also used : MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Matchers.anyString(org.mockito.Matchers.anyString) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) RouterInterface(com.vaadin.flow.router.RouterInterface) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 4 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class VaadinUriResolverTest method testContextProtocol.

@Test
public void testContextProtocol() {
    resolver = new VaadinUriResolver() {

        @Override
        protected String getFrontendRootUrl() {
            return null;
        }

        @Override
        protected String getContextRootUrl() {
            return "http://someplace/";
        }
    };
    assertEquals("http://someplace/my-component.html", resolver.resolveVaadinUri("context://my-component.html"));
}
Also used : VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Test(org.junit.Test)

Example 5 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class VaadinUriResolverTest method testFrontendProtocol.

@Test
public void testFrontendProtocol() {
    resolver = new VaadinUriResolver() {

        @Override
        protected String getFrontendRootUrl() {
            return "http://someplace/";
        }

        @Override
        protected String getContextRootUrl() {
            return null;
        }
    };
    assertEquals("http://someplace/my-component.html", resolver.resolveVaadinUri("frontend://my-component.html"));
}
Also used : VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Test(org.junit.Test)

Aggregations

VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)10 Test (org.junit.Test)4 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Set (java.util.Set)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)2 ApplicationConstants (com.vaadin.flow.shared.ApplicationConstants)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Component (com.vaadin.flow.component.Component)1 Html (com.vaadin.flow.component.Html)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1 UI (com.vaadin.flow.component.UI)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 BodySize (com.vaadin.flow.component.page.BodySize)1