Search in sources :

Example 6 with FallbackChunk

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);
}
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 7 with FallbackChunk

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());
}
Also used : FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 8 with FallbackChunk

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"));
}
Also used : FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with FallbackChunk

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);
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) Environment(org.springframework.core.env.Environment) Map(java.util.Map) ApplicationContext(org.springframework.context.ApplicationContext) DefaultApplicationConfigurationFactory(com.vaadin.flow.server.startup.DefaultApplicationConfigurationFactory) ApplicationContext(org.springframework.context.ApplicationContext) Environment(org.springframework.core.env.Environment)

Example 10 with FallbackChunk

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);
    }
}
Also used : FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) ExecutionFailedException(com.vaadin.flow.server.ExecutionFailedException) CompletionException(java.util.concurrent.CompletionException)

Aggregations

FallbackChunk (com.vaadin.flow.server.frontend.FallbackChunk)11 Map (java.util.Map)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 VaadinContext (com.vaadin.flow.server.VaadinContext)2 CssImportData (com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)2 File (java.io.File)2 ServletContext (javax.servlet.ServletContext)2 CssImport (com.vaadin.flow.component.dependency.CssImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 Lookup (com.vaadin.flow.di.Lookup)1 ResourceProvider (com.vaadin.flow.di.ResourceProvider)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 ExecutionFailedException (com.vaadin.flow.server.ExecutionFailedException)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)1 DefaultApplicationConfigurationFactory (com.vaadin.flow.server.startup.DefaultApplicationConfigurationFactory)1 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)1