Search in sources :

Example 6 with ResourceProvider

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

the class BootstrapHandlerTest method runViteFeatureProdMode_bundleAddedToHead.

@Test
public void runViteFeatureProdMode_bundleAddedToHead() throws IOException {
    initUI(testUI);
    enableViteFeature(true);
    deploymentConfiguration.setProductionMode(true);
    final Lookup lookup = service.getContext().getAttribute(Lookup.class);
    ResourceProvider resourceProvider = lookup.lookup(ResourceProvider.class);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(new ByteArrayInputStream(("<html lang=\"en\">\n" + "<head>\n" + "  <meta charset=\"UTF-8\" />\n" + "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" + "  <style>\n" + "    body, #outlet {\n" + "      height: 100vh;\n" + "      width: 100%;\n" + "      margin: 0;\n" + "    }\n" + "  </style>\n" + "  <script async type=\"module\" crossorigin src=\"VAADIN/build/main.d253dd35.js\"></script>\n" + "  <link rel=\"stylesheet\" href=\"VAADIN/build/main.688a5538.css\">\n" + "</head>").getBytes()));
    final Document bootstrapPage = pageBuilder.getBootstrapPage(context);
    Assert.assertFalse("@vite/client should not be added in productionMode", bootstrapPage.head().toString().contains("VAADIN/@vite/client"));
    Assert.assertTrue("Bundle should be gotten from index and added to bootstrap page", bootstrapPage.head().toString().contains("src=\"VAADIN/build/main.d253dd35.js\""));
    Assert.assertTrue("Bundled css should be gotten from index and added to bootstrap page", bootstrapPage.head().toString().contains("href=\"VAADIN/build/main.688a5538.css\""));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) Document(org.jsoup.nodes.Document) URL(java.net.URL) Test(org.junit.Test)

Example 7 with ResourceProvider

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

the class BootstrapHandlerDependenciesTest method setup.

@Before
public void setup() throws Exception {
    mocks = new MockServletServiceSessionSetup();
    service = mocks.getService();
    service.setRouter(createRouter());
    TestVaadinServlet servlet = mocks.getServlet();
    for (String type : new String[] { "js", "css" }) {
        servlet.addServletContextResource("inline." + type, "inline." + type);
        servlet.addServletContextResource("1." + type, "1." + type);
        servlet.addServletContextResource("2." + type, "2." + type);
    }
    servlet.addServletContextResource("new.js");
    ResourceProvider resourceProvider = service.getContext().getAttribute(Lookup.class).lookup(ResourceProvider.class);
    InputStream stream = resourceProvider.getClientResourceAsStream("META-INF/resources/" + ApplicationConstants.CLIENT_ENGINE_PATH + "/compile.properties");
    Properties properties = new Properties();
    properties.load(stream);
    clientEngine = ApplicationConstants.CLIENT_ENGINE_PATH + "/" + properties.getProperty("jsFile");
    stream.close();
}
Also used : TestVaadinServlet(com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServlet) InputStream(java.io.InputStream) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) Before(org.junit.Before)

Example 8 with ResourceProvider

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

the class AbstractDevModeTest method setup.

@Before
public void setup() throws Exception {
    baseDir = temporaryFolder.getRoot().getPath();
    npmFolder = temporaryFolder.getRoot();
    Boolean enablePnpm = Boolean.TRUE;
    appConfig = Mockito.mock(ApplicationConfiguration.class);
    servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servletContext.getClass().getClassLoader());
    vaadinContext = new VaadinServletContext(servletContext);
    mockApplicationConfiguration(appConfig, enablePnpm);
    lookup = Mockito.mock(Lookup.class);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    devModeHandlerManager = new DevModeHandlerManagerImpl();
    Mockito.when(lookup.lookup(DevModeHandlerManager.class)).thenReturn(devModeHandlerManager);
    configuration = new MockDeploymentConfiguration();
    Mockito.when(lookup.lookup(DeploymentConfiguration.class)).thenReturn(configuration);
    Mockito.when(lookup.lookup(ApplicationConfiguration.class)).thenReturn(appConfig);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(service -> new StaticFileServer(service));
    vaadinService = Mockito.mock(VaadinService.class);
    Mockito.when(vaadinService.getContext()).thenReturn(vaadinContext);
    Mockito.when(vaadinService.getDeploymentConfiguration()).thenReturn(configuration);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) DevModeHandlerManagerImpl(com.vaadin.base.devserver.DevModeHandlerManagerImpl) MockDeploymentConfiguration(com.vaadin.base.devserver.MockDeploymentConfiguration) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Before(org.junit.Before)

Example 9 with ResourceProvider

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

the class NpmTemplateParserTest method init.

@Before
public void init() throws Exception {
    MockitoAnnotations.initMocks(this);
    Mockito.when(configuration.getStringProperty(Mockito.anyString(), Mockito.anyString())).thenAnswer(invocation -> invocation.getArgument(1));
    Mockito.when(configuration.getFlowResourcesFolder()).thenReturn("target/" + FrontendUtils.DEFAULT_FLOW_RESOURCES_FOLDER);
    Properties properties = new Properties();
    Mockito.when(configuration.getInitParameters()).thenReturn(properties);
    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 = service.getContext().getAttribute(Lookup.class).lookup(ResourceProvider.class);
    Mockito.when(resourceProvider.getApplicationResource(Mockito.anyString())).thenAnswer(invocation -> NpmTemplateParserTest.class.getResource("/" + invocation.getArgument(0)));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Instantiator(com.vaadin.flow.di.Instantiator) Properties(java.util.Properties) Before(org.junit.Before)

Example 10 with ResourceProvider

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

the class FeatureFlags method loadProperties.

/**
 * Read the feature flag properties files and updates the enable property of
 * each feature object.
 */
public void loadProperties() {
    final ResourceProvider resourceProvider = lookup.lookup(ResourceProvider.class);
    if (resourceProvider != null) {
        final URL applicationResource = resourceProvider.getApplicationResource(PROPERTIES_FILENAME);
        if (applicationResource != null) {
            getLogger().debug("Properties loaded from classpath.");
            try (InputStream propertiesStream = applicationResource.openStream()) {
                loadProperties(propertiesStream);
                return;
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to read properties file from classpath", e);
            }
        }
    }
    File featureFlagFile = getFeatureFlagFile();
    if (featureFlagFile == null || !featureFlagFile.exists()) {
        // Disable all features if no file exists
        for (Feature f : features) {
            f.setEnabled(false);
        }
        return;
    }
    try (FileInputStream propertiesStream = new FileInputStream(featureFlagFile)) {
        getLogger().debug("Loading properties from file '{}'", featureFlagFile);
        loadProperties(propertiesStream);
    } catch (IOException e) {
        throw new UncheckedIOException("Failed to read properties file from filesystem", e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResourceProvider(com.vaadin.flow.di.ResourceProvider) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

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