Search in sources :

Example 11 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class LitTemplateParserImplTest method init.

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    Mockito.when(configuration.getStringProperty(Mockito.anyString(), Mockito.anyString())).thenAnswer(invocation -> invocation.getArgument(1));
    Properties properties = new Properties();
    Mockito.when(configuration.getInitParameters()).thenReturn(properties);
    Mockito.when(configuration.getFlowResourcesFolder()).thenReturn(Paths.get("target", FrontendUtils.DEFAULT_FLOW_RESOURCES_FOLDER).toString());
    Instantiator instantiator = Mockito.mock(Instantiator.class);
    Mockito.when(instantiator.getServiceInitListeners()).thenReturn(Stream.empty());
    Mockito.when(instantiator.getDependencyFilters(Mockito.any())).thenReturn(Stream.empty());
    Mockito.when(instantiator.getBootstrapListeners(Mockito.any())).thenReturn(Stream.empty());
    Mockito.when(instantiator.getIndexHtmlRequestListeners(Mockito.any())).thenReturn(Stream.empty());
    service = new MockVaadinServletService(configuration);
    service.init(instantiator);
    ResourceProvider resourceProvider = service.getContext().getAttribute(Lookup.class).lookup(ResourceProvider.class);
    Mockito.when(resourceProvider.getApplicationResource(Mockito.anyString())).thenAnswer(invoc -> LitTemplateParserImpl.class.getClassLoader().getResource(invoc.getArgument(0)));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Instantiator(com.vaadin.flow.di.Instantiator) Lookup(com.vaadin.flow.di.Lookup) Properties(java.util.Properties) Before(org.junit.Before)

Example 12 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class LitTemplateParserImpl method getResourceStream.

private InputStream getResourceStream(VaadinService service, String url) {
    ResourceProvider resourceProvider = service.getContext().getAttribute(Lookup.class).lookup(ResourceProvider.class);
    URL resourceUrl = resourceProvider.getApplicationResource(url);
    if (resourceUrl != null) {
        try {
            return resourceUrl.openStream();
        } catch (IOException e) {
            getLogger().warn("Exception accessing resource " + resourceUrl, e);
        }
    }
    return null;
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) IOException(java.io.IOException) URL(java.net.URL)

Example 13 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class BootstrapUtils method getInlineResourceStream.

private static InputStream getInlineResourceStream(VaadinService service, String file) {
    ResourceProvider resourceProvider = service.getContext().getAttribute(Lookup.class).lookup(ResourceProvider.class);
    URL appResource = resourceProvider.getApplicationResource(file);
    InputStream stream = null;
    try {
        stream = appResource == null ? null : appResource.openStream();
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Couldn't open application resource '%s' for inline resource", file), e);
    }
    if (stream == null) {
        throw new IllegalStateException(String.format("Application resource '%s' for inline resource is not available", file));
    }
    return stream;
}
Also used : InputStream(java.io.InputStream) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) IOException(java.io.IOException) URL(java.net.URL)

Example 14 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class IndexHtmlRequestHandlerTest method should_attachWebpackErrors.

@Test
public void should_attachWebpackErrors() throws Exception {
    deploymentConfiguration.setEnableDevServer(true);
    deploymentConfiguration.setProductionMode(false);
    DevModeHandler devServer = Mockito.mock(DevModeHandler.class);
    Mockito.when(devServer.getFailedOutput()).thenReturn("Failed to compile");
    Mockito.when(devServer.prepareConnection(Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(HttpURLConnection.class));
    service.setContext(context);
    DevModeHandlerManager devModeHandlerManager = new DevModeHandlerManager() {

        @Override
        public Class<?>[] getHandlesTypes() {
            return new Class[0];
        }

        @Override
        public void initDevModeHandler(Set<Class<?>> classes, VaadinContext context) throws VaadinInitializerException {
        }

        @Override
        public void setDevModeHandler(DevModeHandler devModeHandler) {
        }

        @Override
        public DevModeHandler getDevModeHandler() {
            return devServer;
        }
    };
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Lookup lookup = Lookup.compose(Lookup.of(devModeHandlerManager, DevModeHandlerManager.class), Lookup.of(resourceProvider, ResourceProvider.class));
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    mockApplicationConfiguration(appConfig);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(new ByteArrayInputStream("<html><head></head></html>".getBytes()));
    // Send the request
    indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response);
    String indexHtml = responseOutput.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue("Should have a system error dialog", indexHtml.contains("<div class=\"v-system-error\">"));
    Assert.assertTrue("Should show webpack failure error", indexHtml.contains("Failed to compile"));
}
Also used : DevModeHandlerManager(com.vaadin.flow.internal.DevModeHandlerManager) VaadinContext(com.vaadin.flow.server.VaadinContext) HttpURLConnection(java.net.HttpURLConnection) Set(java.util.Set) ByteArrayInputStream(java.io.ByteArrayInputStream) DevModeHandler(com.vaadin.flow.internal.DevModeHandler) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 15 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class IndexHtmlRequestHandlerTest method serveNotFoundIndexHtml_requestWithRootPath_failsWithIOException.

@Test
public void serveNotFoundIndexHtml_requestWithRootPath_failsWithIOException() throws IOException {
    VaadinServletService vaadinService = Mockito.mock(VaadinServletService.class);
    Mockito.when(vaadinService.getDeploymentConfiguration()).thenReturn(deploymentConfiguration);
    Mockito.when(vaadinService.getContext()).thenReturn(context);
    final Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(null);
    VaadinServletRequest vaadinRequest = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(vaadinRequest.getService()).thenReturn(vaadinService);
    String path = DEFAULT_FRONTEND_DIR + "index.html";
    String expectedError = String.format("Failed to load content of '%1$s'. " + "It is required to have '%1$s' file when " + "using client side bootstrapping.", path);
    exceptionRule.expect(IOException.class);
    exceptionRule.expectMessage(expectedError);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, vaadinRequest, response);
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Lookup(com.vaadin.flow.di.Lookup) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ResourceProvider (com.vaadin.flow.di.ResourceProvider)30 Lookup (com.vaadin.flow.di.Lookup)22 Test (org.junit.Test)11 URL (java.net.URL)10 VaadinContext (com.vaadin.flow.server.VaadinContext)8 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)5 File (java.io.File)5 IOException (java.io.IOException)5 Before (org.junit.Before)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Instantiator (com.vaadin.flow.di.Instantiator)3 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 ServletContext (javax.servlet.ServletContext)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 StaticFileServer (com.vaadin.flow.server.StaticFileServer)2