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);
}
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");
}
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;
}
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;
}
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());
}
Aggregations