Search in sources :

Example 21 with ResourceProvider

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

the class ServletDeployerTest method getContextEvent.

@SuppressWarnings({ "unchecked", "rawtypes" })
private ServletContextEvent getContextEvent(ServletRegistration... servletRegistrations) throws Exception {
    ServletRegistration.Dynamic dynamicMock = Mockito.mock(ServletRegistration.Dynamic.class);
    Mockito.when(dynamicMock.addMapping(Mockito.anyString())).thenAnswer(answer -> {
        String mappings = answer.getArgument(0);
        this.servletMappings.addAll(Arrays.asList(mappings));
        return Collections.emptySet();
    });
    ServletContext contextMock = Mockito.mock(ServletContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(contextMock.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(resourceProvider.getApplicationResources(Mockito.any())).thenReturn(Collections.emptyList());
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.isProductionMode()).thenReturn(false);
    FallbackChunk chunk = Mockito.mock(FallbackChunk.class);
    Mockito.when(appConfig.getFallbackChunk()).thenReturn(chunk);
    Mockito.when(appConfig.disableAutomaticServletRegistration()).thenReturn(disableAutomaticServletRegistration);
    Mockito.when(contextMock.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Mockito.when(contextMock.getContextPath()).thenReturn("");
    Mockito.when(contextMock.getClassLoader()).thenReturn(this.getClass().getClassLoader());
    Mockito.when(contextMock.addServlet(Mockito.anyString(), Mockito.any(Class.class))).thenAnswer(answer -> {
        String servletName = answer.getArgument(0);
        servletNames.add(servletName);
        return dynamicMock;
    });
    // seems to be a compiler bug, since fails to compile with the
    // actual
    // types specified (or being inlined) but works with raw type
    @SuppressWarnings({ "rawtypes", "serial" }) Map hack = Stream.of(servletRegistrations).collect(Collectors.toMap(Registration::getName, Function.identity()));
    Mockito.when(contextMock.getServletRegistrations()).thenReturn(hack);
    File token = tempFolder.newFile();
    FileUtils.write(token, "{}", StandardCharsets.UTF_8);
    Mockito.when(contextMock.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList(FrontendUtils.PARAM_TOKEN_FILE)));
    Mockito.when(contextMock.getInitParameter(FrontendUtils.PARAM_TOKEN_FILE)).thenReturn(token.getPath());
    return new ServletContextEvent(contextMock);
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) ResourceProvider(com.vaadin.flow.di.ResourceProvider) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) File(java.io.File) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 22 with ResourceProvider

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

the class FrontendUtilsTest method getStatsAssetsByChunkName_getStatsFromClassPath_delegateToGetApplicationResource.

@Test
public void getStatsAssetsByChunkName_getStatsFromClassPath_delegateToGetApplicationResource() throws IOException {
    VaadinServletService service = mockServletService();
    ResourceProvider provider = mockResourceProvider(service);
    FrontendUtils.getStatsAssetsByChunkName(service);
    Mockito.verify(provider).getApplicationResource("foo");
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Test(org.junit.Test)

Example 23 with ResourceProvider

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

the class FrontendUtilsTest method mockResourceProvider.

private ResourceProvider mockResourceProvider(VaadinService service) {
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
    Mockito.when(service.getContext()).thenReturn(context);
    Mockito.when(config.isProductionMode()).thenReturn(true);
    Mockito.when(config.getStringProperty(SERVLET_PARAMETER_STATISTICS_JSON, VAADIN_SERVLET_RESOURCES + STATISTICS_JSON_DEFAULT)).thenReturn("foo");
    return provider;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 24 with ResourceProvider

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

the class FrontendUtilsTest method getServiceWithResource.

private VaadinService getServiceWithResource(String content) throws ServiceException, IOException {
    MockDeploymentConfiguration configuration = new MockDeploymentConfiguration();
    configuration.setProductionMode(true);
    MockVaadinServletService service = new MockVaadinServletService(configuration);
    VaadinContext context = service.getContext();
    Lookup lookup = Mockito.mock(Lookup.class);
    context.setAttribute(Lookup.class, lookup);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    if (content != null) {
        File tmpFile = tmpDir.newFile();
        try (FileOutputStream outputStream = new FileOutputStream(tmpFile)) {
            IOUtils.write(content, outputStream, StandardCharsets.UTF_8);
        }
        Mockito.when(provider.getApplicationResource(VAADIN_SERVLET_RESOURCES + STATISTICS_JSON_DEFAULT)).thenReturn(tmpFile.toURI().toURL());
    }
    return service;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) ResourceProvider(com.vaadin.flow.di.ResourceProvider) FileOutputStream(java.io.FileOutputStream) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File)

Example 25 with ResourceProvider

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

the class DefaultApplicationConfigurationFactory method getPossibleJarResource.

/**
 * Check if the webpack.generated.js resources is inside 2 jars
 * (flow-server.jar and application.jar) if this is the case then we can
 * accept a build info file from inside jar with a single jar in the path.
 * <p>
 * Else we will accept any flow-build-info and log a warning that it may not
 * be the correct file, but it's the best we could find.
 */
private String getPossibleJarResource(VaadinContext context, List<URL> resources) throws IOException {
    Objects.requireNonNull(resources);
    Lookup lookup = context.getAttribute(Lookup.class);
    ResourceProvider resourceProvider = lookup.lookup(ResourceProvider.class);
    assert !resources.isEmpty() : "Possible jar resource requires resources to be available.";
    URL webpackGenerated = resourceProvider.getApplicationResource(FrontendUtils.WEBPACK_GENERATED);
    // running from a jar
    if (webpackGenerated != null && countInstances(webpackGenerated.getPath(), "jar!/") >= 2) {
        for (URL resource : resources) {
            // build info with a single jar in the path
            if (countInstances(resource.getPath(), "jar!/") == 1) {
                return FrontendUtils.streamToString(resource.openStream());
            }
        }
    }
    URL firstResource = resources.get(0);
    if (resources.size() > 1) {
        String warningMessage = String.format("Unable to fully determine correct flow-build-info.%n" + "Accepting file '%s' first match of '%s' possible.%n" + "Please verify flow-build-info file content.", firstResource.getPath(), resources.size());
        getLogger().warn(warningMessage);
    } else {
        String debugMessage = String.format("Unable to fully determine correct flow-build-info.%n" + "Accepting file '%s'", firstResource.getPath());
        getLogger().debug(debugMessage);
    }
    return FrontendUtils.streamToString(firstResource.openStream());
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) URL(java.net.URL)

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