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