use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeUpdateImportsTest method noFallBackScanner_fallbackIsNotGenerated.
@Test
public void noFallBackScanner_fallbackIsNotGenerated() throws IOException {
Stream<Class<?>> classes = Stream.concat(Stream.of(NodeTestComponents.class.getDeclaredClasses()), Stream.of(ExtraNodeTestComponents.class.getDeclaredClasses()));
ClassFinder classFinder = new DefaultClassFinder(new URLClassLoader(getClassPath()), classes.toArray(Class<?>[]::new));
updater = new TaskUpdateImports(classFinder, new FrontendDependenciesScannerFactory().createScanner(false, classFinder, true), finder -> null, tmpRoot, generatedPath, frontendDirectory, tokenFile, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class)) {
@Override
Logger log() {
return logger;
}
};
updater.execute();
assertTrue(importsFile.exists());
String mainContent = FileUtils.readFileToString(importsFile, Charset.defaultCharset());
// fallback chunk load function is not generated
MatcherAssert.assertThat(mainContent, CoreMatchers.not(CoreMatchers.containsString("window.Vaadin.Flow.loadFallback = function loadFallback(){")));
Assert.assertFalse(fallBackImportsFile.exists());
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeUpdateImportsTest method tokenFileIsStable.
@Test
public void tokenFileIsStable() throws Exception {
Stream<Class<?>> classes = Stream.concat(Stream.of(ExtraNodeTestComponents.class.getDeclaredClasses()), Stream.of(NodeTestComponents.class.getDeclaredClasses()));
ClassFinder classFinder = new DefaultClassFinder(new URLClassLoader(getClassPath()), classes.toArray(Class<?>[]::new));
JsonObject fallBackData = Json.createObject();
updater = new TaskUpdateImports(classFinder, new FrontendDependenciesScannerFactory().createScanner(false, classFinder, true), finder -> new FrontendDependenciesScannerFactory().createScanner(true, finder, true), tmpRoot, generatedPath, frontendDirectory, tokenFile, fallBackData, false, TARGET, true, false, Mockito.mock(FeatureFlags.class)) {
@Override
Logger log() {
return logger;
}
};
updater.execute();
JsonObject fallback = fallBackData.getObject("chunks").getObject("fallback");
JsonArray jsModules = fallback.getArray("jsModules");
JsonArray cssImports = fallback.getArray("cssImports");
String expectedJsModules = "[\"@polymer/e.js\",\"@polymer/D.js\",\"@polymer/c.js\",\"@polymer/b.js\",\"@polymer/a.js\",\"./extra-javascript.js\"]";
String expectedCssImports = "[{\"value\":\"./b-css.css\"},{\"include\":\"a-a\",\"value\":\"./a-css.css\"},{\"include\":\"extra-bar\",\"themeFor\":\"extra-foo\",\"value\":\"./extra-css.css\"}]";
Assert.assertEquals(expectedJsModules, jsModules.toJson());
Assert.assertEquals(expectedCssImports, cssImports.toJson());
String actual = FileUtils.readFileToString(tokenFile, StandardCharsets.UTF_8);
String expected = //
"{\n" + //
" \"chunks\": {\n" + //
" \"fallback\": {\n" + //
" \"jsModules\": [\n" + //
" \"@polymer/e.js\",\n" + //
" \"@polymer/D.js\",\n" + //
" \"@polymer/c.js\",\n" + //
" \"@polymer/b.js\",\n" + //
" \"@polymer/a.js\",\n" + //
" \"./extra-javascript.js\"\n" + //
" ],\n" + //
" \"cssImports\": [\n" + //
" {\n" + //
" \"value\": \"./b-css.css\"\n" + //
" },\n" + //
" {\n" + //
" \"include\": \"a-a\",\n" + //
" \"value\": \"./a-css.css\"\n" + //
" },\n" + //
" {\n" + //
" \"include\": \"extra-bar\",\n" + //
" \"themeFor\": \"extra-foo\",\n" + //
" \"value\": \"./extra-css.css\"\n" + //
" }\n" + //
" ]\n" + //
" }\n" + //
" }\n" + "}";
Assert.assertEquals(expected, actual);
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeUpdateImportsTest method emptyByteCodeScannerData_themeIsDiscovered_fallbackIsGenerated.
@Test
public void emptyByteCodeScannerData_themeIsDiscovered_fallbackIsGenerated() throws IOException {
ClassFinder classFinder = new DefaultClassFinder(new URLClassLoader(getClassPath()), EmptyByteScannerDataTestComponents.class.getDeclaredClasses());
updater = new TaskUpdateImports(classFinder, new FrontendDependenciesScannerFactory().createScanner(false, classFinder, true), finder -> new FrontendDependenciesScannerFactory().createScanner(true, finder, true), tmpRoot, generatedPath, frontendDirectory, tokenFile, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class)) {
@Override
Logger log() {
return logger;
}
};
updater.execute();
assertTrue(importsFile.exists());
String mainContent = FileUtils.readFileToString(importsFile, Charset.defaultCharset());
// ============== check main generated imports file ============
// Contains theme lines
MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("export const addCssBlock = function(block, before = false) {"));
MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("addCssBlock('<custom-style>foo</custom-style>', true);"));
// fallback chunk load function is generated
MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("fallbacks[thisScript.getAttribute('data-app-id')].loadFallback = function loadFallback() {"));
MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("return import('./generated-flow-imports-fallback.js');"));
// ============== check fallback generated imports file ============
String fallBackContent = FileUtils.readFileToString(fallBackImportsFile, Charset.defaultCharset());
// Does not Contains theme lines
MatcherAssert.assertThat(fallBackContent, CoreMatchers.not(CoreMatchers.containsString("const div = document.createElement('div');")));
// Contains CSS import lines from CP not discovered by byte
// scanner
MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import $cssFromFile_0 from 'Frontend/foo.css';"));
MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("registerStyles('', $css_0, {include: 'bar', moduleId: 'baz'});"));
// Contains JS module imports
MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import '@vaadin/vaadin-lumo-styles/icons.js';"));
MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import 'Frontend/common-js-file.js';"));
// Contains Javascript imports
MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import '@vaadin/flow-frontend/ExampleConnector.js';"));
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeTasksTest method should_BeAbleToCustomizeFolders.
@Test
public void should_BeAbleToCustomizeFolders() throws Exception {
System.setProperty(PARAM_FRONTEND_DIR, "my_custom_sources_folder");
System.setProperty(PARAM_GENERATED_DIR, "my/custom/generated/folder");
Lookup mockedLookup = mock(Lookup.class);
Mockito.doReturn(new DefaultClassFinder(this.getClass().getClassLoader())).when(mockedLookup).lookup(ClassFinder.class);
Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false);
Assert.assertEquals(new File(userDir, "my_custom_sources_folder").getAbsolutePath(), ((File) getFieldValue(builder, "frontendDirectory")).getAbsolutePath());
Assert.assertEquals(new File(userDir, "my/custom/generated/folder").getAbsolutePath(), ((File) getFieldValue(builder, "generatedFolder")).getAbsolutePath());
builder.build().execute();
Assert.assertTrue(new File(userDir, "my/custom/generated/folder/" + IMPORTS_NAME).exists());
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeTasksTest method should_generateServiceWorkerWhenPwa.
@Test
public void should_generateServiceWorkerWhenPwa() throws Exception {
Lookup mockedLookup = mock(Lookup.class);
Mockito.doReturn(new DefaultClassFinder(this.getClass().getClassLoader())).when(mockedLookup).lookup(ClassFinder.class);
Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false);
Assert.assertEquals(new File(userDir, DEFAULT_FRONTEND_DIR).getAbsolutePath(), ((File) getFieldValue(builder, "frontendDirectory")).getAbsolutePath());
Assert.assertEquals(new File(new File(userDir, TARGET), DEFAULT_GENERATED_DIR).getAbsolutePath(), ((File) getFieldValue(builder, "generatedFolder")).getAbsolutePath());
builder.build().execute();
Assert.assertTrue(new File(userDir, Paths.get(TARGET, DEFAULT_GENERATED_DIR, IMPORTS_NAME).toString()).exists());
}
Aggregations