Search in sources :

Example 16 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class NodeUpdateImportsTest method extraComponentsInCP_componentsAreNotDiscoveredByMainScannerWrittenByFallback_fallbackIsGenerated.

@Test
public void extraComponentsInCP_componentsAreNotDiscoveredByMainScannerWrittenByFallback_fallbackIsGenerated() 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));
    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();
    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><style include=\"lumo-color lumo-typography\"></style></custom-style>', true);"));
    // Contains CSS import lines
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import $cssFromFile_0 from '@vaadin/vaadin-mixed-component/bar.css';"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("addCssBlock(`<style>${$css_0}</style>`);"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import $cssFromFile_5 from 'Frontend/foo.css';"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("registerStyles('foo-bar', $css_5, {moduleId: 'flow_css_mod'});"));
    // Contains theme imports
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import '@vaadin/vaadin-lumo-styles/color.js';"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import '@vaadin/vaadin-lumo-styles/typography.js';"));
    // Contains JS module imports
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import '@polymer/iron-icon/iron-icon.js';"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import '3rdparty/component.js';"));
    // Contains Javascript imports
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import 'javascript/a.js';"));
    MatcherAssert.assertThat(mainContent, CoreMatchers.containsString("import 'javascript/b.js';"));
    // 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');"));
    assertTrue(fallBackImportsFile.exists());
    // ============== 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');")));
    // Does not contains theme imports
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.not(CoreMatchers.containsString("import '@vaadin/vaadin-lumo-styles/color.js';")));
    // Does not contains CSS import lines
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.not(CoreMatchers.containsString("import $cssFromFile_0 from '@vaadin/vaadin-mixed-component/bar.css';")));
    // Contain lines to import exported modules from main file
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("export const addCssBlock = function(block, before = false) {"));
    // Contains CSS import lines from CP not discovered by byte scanner
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import $cssFromFile_0 from 'Frontend/b-css.css';"));
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("registerStyles('extra-foo', $css_2, {include: 'extra-bar', moduleId: 'fallback_flow_css_mod'});"));
    // Does not contains JS module imports
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.not(CoreMatchers.containsString("import '@polymer/iron-icon/iron-icon.js';")));
    // Contains JS module imports
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import '@polymer/a.js';"));
    // Does not contain Javascript imports
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.not(CoreMatchers.containsString("import 'javascript/a.js';")));
    // Contains Javascript imports
    MatcherAssert.assertThat(fallBackContent, CoreMatchers.containsString("import 'Frontend/extra-javascript.js';"));
    // ============== check token file with fallback chunk data ============
    String tokenContent = FileUtils.readFileToString(tokenFile, Charset.defaultCharset());
    JsonObject object = Json.parse(tokenContent);
    assertTokenFileWithFallBack(object);
    assertTokenFileWithFallBack(fallBackData);
    // ============== check definition file ============
    assertTrue(importsDefinitionFile.exists());
    String definitionContent = FileUtils.readFileToString(importsDefinitionFile, Charset.defaultCharset());
    MatcherAssert.assertThat(definitionContent, CoreMatchers.containsString("export declare const addCssBlock: (block: string, before?: boolean) => void;"));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) IMPORTS_D_TS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_D_TS_NAME) HashSet(java.util.HashSet) URLClassLoader(java.net.URLClassLoader) Charset(java.nio.charset.Charset) FrontendDependenciesScannerFactory(com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner.FrontendDependenciesScannerFactory) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) Logger(org.slf4j.Logger) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) TARGET(com.vaadin.flow.server.Constants.TARGET) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) Paths(java.nio.file.Paths) FeatureFlags(com.vaadin.experimental.FeatureFlags) JsonObject(elemental.json.JsonObject) FLOW_NPM_PACKAGE_NAME(com.vaadin.flow.server.frontend.FrontendUtils.FLOW_NPM_PACKAGE_NAME) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) FrontendDependenciesScannerFactory(com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner.FrontendDependenciesScannerFactory) URLClassLoader(java.net.URLClassLoader) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) JsonObject(elemental.json.JsonObject) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 17 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class AbstractNodeUpdateImportsTest method setup.

@Before
public void setup() throws Exception {
    File tmpRoot = temporaryFolder.getRoot();
    logger = new MockLogger();
    frontendDirectory = new File(tmpRoot, DEFAULT_FRONTEND_DIR);
    nodeModulesPath = new File(tmpRoot, NODE_MODULES);
    generatedPath = new File(tmpRoot, Paths.get(TARGET, DEFAULT_GENERATED_DIR).toString());
    importsFile = new File(generatedPath, IMPORTS_NAME);
    ClassFinder classFinder = getClassFinder();
    updater = new TaskUpdateImports(classFinder, getScanner(classFinder), finder -> null, tmpRoot, generatedPath, frontendDirectory, null, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class)) {

        @Override
        Logger log() {
            return logger;
        }
    };
    assertTrue(nodeModulesPath.mkdirs());
    createExpectedImports(frontendDirectory, nodeModulesPath);
    assertTrue(new File(nodeModulesPath, FLOW_NPM_PACKAGE_NAME + "ExampleConnector.js").exists());
}
Also used : DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) Arrays(java.util.Arrays) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) FrontendDependenciesScanner(com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner) Logger(org.slf4j.Logger) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Files(java.nio.file.Files) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) TARGET(com.vaadin.flow.server.Constants.TARGET) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) List(java.util.List) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Assert.assertFalse(org.junit.Assert.assertFalse) FeatureFlags(com.vaadin.experimental.FeatureFlags) FLOW_NPM_PACKAGE_NAME(com.vaadin.flow.server.frontend.FrontendUtils.FLOW_NPM_PACKAGE_NAME) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Logger(org.slf4j.Logger) File(java.io.File) Before(org.junit.Before)

Example 18 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class NodeTasksTest method should_IncludeExperimentalComponent_WhenFeatureEnabled.

@Test
public void should_IncludeExperimentalComponent_WhenFeatureEnabled() throws Exception {
    Class<?>[] classes = { FlagView.class, ExampleExperimentalComponent.class };
    File propertiesDir = temporaryFolder.newFolder();
    FileUtils.write(new File(propertiesDir, FeatureFlags.PROPERTIES_FILENAME), "com.vaadin.experimental.exampleFeatureFlag=true\n", StandardCharsets.UTF_8);
    Lookup mockedLookup = Mockito.mock(Lookup.class);
    ClassFinder finder = NodeUpdateTestUtil.getClassFinder(classes);
    Mockito.doReturn(finder).when(mockedLookup).lookup(ClassFinder.class);
    Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false).setJavaResourceFolder(propertiesDir);
    builder.build().execute();
    File importsFile = Paths.get(userDir, TARGET, DEFAULT_GENERATED_DIR, IMPORTS_NAME).toFile();
    String content = FileUtils.readFileToString(importsFile, Charset.defaultCharset());
    assertTrue(content.contains("@vaadin/example-flag/experimental-module-1.js"));
    assertTrue(content.contains("@vaadin/example-flag/experimental-module-2.js"));
    assertTrue(content.contains("experimental-Connector.js"));
}
Also used : Builder(com.vaadin.flow.server.frontend.NodeTasks.Builder) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File) Test(org.junit.Test)

Example 19 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class NodeTasksTest method should_ExcludeExperimentalComponent_WhenFeatureDisabled.

@Test
public void should_ExcludeExperimentalComponent_WhenFeatureDisabled() throws Exception {
    Class<?>[] classes = { FlagView.class, ExampleExperimentalComponent.class };
    Lookup mockedLookup = Mockito.mock(Lookup.class);
    ClassFinder finder = NodeUpdateTestUtil.getClassFinder(classes);
    Mockito.doReturn(finder).when(mockedLookup).lookup(ClassFinder.class);
    Builder builder = new Builder(mockedLookup, new File(userDir), TARGET).enablePackagesUpdate(false).enableImportsUpdate(true).runNpmInstall(false).withEmbeddableWebComponents(false);
    assertEquals(1, finder.getAnnotatedClasses(JsModule.class).size());
    assertEquals(1, finder.getAnnotatedClasses(JavaScript.class).size());
    builder.build().execute();
    File importsFile = Paths.get(userDir, TARGET, DEFAULT_GENERATED_DIR, IMPORTS_NAME).toFile();
    String content = FileUtils.readFileToString(importsFile, Charset.defaultCharset());
    assertFalse(content.contains("@vaadin/example-flag/experimental-module-1.js"));
    assertFalse(content.contains("@vaadin/example-flag/experimental-module-2.js"));
    assertFalse(content.contains("experimental-Connector.js"));
}
Also used : Builder(com.vaadin.flow.server.frontend.NodeTasks.Builder) DefaultClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File) Test(org.junit.Test)

Example 20 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.

the class AbstractUpdateImportsTest method setup.

@Before
public void setup() throws Exception {
    tmpRoot = temporaryFolder.getRoot();
    logger = new MockLogger();
    frontendDirectory = new File(tmpRoot, DEFAULT_FRONTEND_DIR);
    nodeModulesPath = new File(tmpRoot, NODE_MODULES);
    generatedPath = new File(tmpRoot, Paths.get(TARGET, DEFAULT_GENERATED_DIR).toString());
    File tokenFile = new File(tmpRoot, TOKEN_FILE);
    ClassFinder classFinder = getClassFinder();
    updater = new UpdateImports(classFinder, getScanner(classFinder), tmpRoot, tokenFile, true);
    assertTrue(nodeModulesPath.mkdirs());
    createExpectedImports(frontendDirectory, nodeModulesPath);
    assertTrue(new File(nodeModulesPath, FLOW_NPM_PACKAGE_NAME + "ExampleConnector.js").exists());
}
Also used : ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File) Before(org.junit.Before)

Aggregations

ClassFinder (com.vaadin.flow.server.frontend.scanner.ClassFinder)23 File (java.io.File)20 Test (org.junit.Test)14 FeatureFlags (com.vaadin.experimental.FeatureFlags)9 Before (org.junit.Before)9 Lookup (com.vaadin.flow.di.Lookup)8 DefaultClassFinder (com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder)8 IOException (java.io.IOException)8 TARGET (com.vaadin.flow.server.Constants.TARGET)7 DEFAULT_FRONTEND_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR)7 DEFAULT_GENERATED_DIR (com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR)7 IMPORTS_NAME (com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME)7 NODE_MODULES (com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES)7 Charset (java.nio.charset.Charset)7 Paths (java.nio.file.Paths)7 Set (java.util.Set)7 FileUtils (org.apache.commons.io.FileUtils)7 Assert (org.junit.Assert)7 Rule (org.junit.Rule)7 ExpectedException (org.junit.rules.ExpectedException)7