use of com.vaadin.flow.server.frontend.FallbackChunk 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.server.frontend.FallbackChunk in project flow by vaadin.
the class DeploymentConfigurationFactoryTest method createInitParameters_servletConfigDefinesTokenFile_fallbackChunkObjectIsInInitParams.
@Test
public void createInitParameters_servletConfigDefinesTokenFile_fallbackChunkObjectIsInInitParams() throws IOException {
ServletContext context = Mockito.mock(ServletContext.class);
ServletConfig config = Mockito.mock(ServletConfig.class);
Mockito.when(config.getServletContext()).thenReturn(context);
Mockito.when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singleton(FrontendUtils.PARAM_TOKEN_FILE)));
File tokenFile = temporaryFolder.newFile();
Mockito.when(config.getInitParameter(FrontendUtils.PARAM_TOKEN_FILE)).thenReturn(tokenFile.getPath());
Files.write(tokenFile.toPath(), Collections.singletonList("{ 'chunks': { " + "'fallback': {" + " 'jsModules': ['foo', 'bar']," + " 'cssImports': [ { 'value' :'foo-value' , 'id': 'bar-id'}]" + "}}" + "}"));
Mockito.when(context.getInitParameter(FrontendUtils.PARAM_TOKEN_FILE)).thenReturn(tokenFile.getPath());
Properties properties = new DeploymentConfigurationFactory().createInitParameters(Object.class, new VaadinServletConfig(config));
Object object = properties.get(DeploymentConfigurationFactory.FALLBACK_CHUNK);
Assert.assertTrue(object instanceof FallbackChunk);
FallbackChunk chunk = (FallbackChunk) object;
Set<String> modules = chunk.getModules();
Assert.assertEquals(2, modules.size());
Assert.assertTrue(modules.contains("foo"));
Assert.assertTrue(modules.contains("bar"));
Set<CssImportData> cssImports = chunk.getCssImports();
Assert.assertEquals(1, cssImports.size());
CssImportData data = cssImports.iterator().next();
Assert.assertEquals("foo-value", data.getValue());
Assert.assertEquals("bar-id", data.getId());
}
use of com.vaadin.flow.server.frontend.FallbackChunk in project flow by vaadin.
the class DevModeInitializerTest method shouldUseByteCodeScannerIfPropertySet.
@Test
public void shouldUseByteCodeScannerIfPropertySet() throws Exception {
Mockito.when(appConfig.getBooleanProperty(InitParameters.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, false)).thenReturn(true);
devModeStartupListener = new DevModeStartupListener();
final Set<Class<?>> classes = new HashSet<>();
classes.add(NotVisitedWithDeps.class);
classes.add(Visited.class);
classes.add(RoutedWithReferenceToVisited.class);
devModeStartupListener.onStartup(classes, servletContext);
handler = getDevModeHandler();
waitForDevServer();
ArgumentCaptor<? extends FallbackChunk> arg = ArgumentCaptor.forClass(FallbackChunk.class);
Mockito.verify(servletContext, Mockito.atLeastOnce()).setAttribute(Mockito.eq(FallbackChunk.class.getName()), arg.capture());
FallbackChunk fallbackChunk = arg.getValue();
Assert.assertFalse(fallbackChunk.getModules().contains("foo"));
Assert.assertTrue(fallbackChunk.getModules().contains("bar"));
}
use of com.vaadin.flow.server.frontend.FallbackChunk in project flow by vaadin.
the class SpringApplicationConfigurationFactory method doCreate.
@Override
protected ApplicationConfigurationImpl doCreate(VaadinContext context, FallbackChunk chunk, Map<String, String> properties) {
ApplicationContext appContext = SpringLookupInitializer.getApplicationContext(context);
Environment env = appContext.getBean(Environment.class);
// Collect any vaadin.XZY properties from application.properties
SpringServlet.PROPERTY_NAMES.stream().filter(name -> env.getProperty("vaadin." + name) != null).forEach(name -> properties.put(name, env.getProperty("vaadin." + name)));
return super.doCreate(context, chunk, properties);
}
use of com.vaadin.flow.server.frontend.FallbackChunk in project flow by vaadin.
the class DevModeInitializer method runNodeTasks.
private static void runNodeTasks(VaadinContext vaadinContext, JsonObject tokenFileData, NodeTasks tasks) {
try {
tasks.execute();
FallbackChunk chunk = FrontendUtils.readFallbackChunk(tokenFileData);
if (chunk != null) {
vaadinContext.setAttribute(chunk);
}
} catch (ExecutionFailedException exception) {
log().debug("Could not initialize dev mode handler. One of the node tasks failed", exception);
throw new CompletionException(exception);
}
}
Aggregations