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