Search in sources :

Example 1 with ClassFinder

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

the class FrontendResourcesAreCopiedAfterCleaningTest method copyResources.

private void copyResources() throws ExecutionFailedException {
    ClassFinder classFinder = new ClassFinder.DefaultClassFinder(FrontendResourcesAreCopiedAfterCleaningTest.class.getClassLoader());
    Lookup mockLookup = Mockito.mock(Lookup.class);
    Mockito.doReturn(classFinder).when(mockLookup).lookup(ClassFinder.class);
    NodeTasks.Builder builder = new NodeTasks.Builder(mockLookup, npmFolder, TARGET);
    File resourcesFolder = new File(npmFolder, Paths.get(TARGET, DEFAULT_FLOW_RESOURCES_FOLDER).toString());
    builder.withEmbeddableWebComponents(false).enableImportsUpdate(false).createMissingPackageJson(true).enableImportsUpdate(true).runNpmInstall(false).enablePackagesUpdate(true).withFlowResourcesFolder(resourcesFolder).copyResources(Collections.singleton(testJar)).build().execute();
}
Also used : ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Lookup(com.vaadin.flow.di.Lookup) File(java.io.File)

Example 2 with ClassFinder

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

the class UpdateThemedImportsTest method setup.

@Before
public void setup() throws Exception {
    File tmpRoot = temporaryFolder.getRoot();
    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);
    Assert.assertTrue(nodeModulesPath.mkdirs());
    createImport("./src/subfolder/sub-template.js", "");
    createImport("./src/client-side-template.js", "import 'xx' from './subfolder/sub-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    createImport("./src/client-side-no-themed-template.js", "");
    createImport("./src/main-template.js", "import 'xx' from './client-side-template.js';" + "import \"./client-side-no-themed-template.js\";" + "import './src/wrong-themed-template.js';" + "import '@vaadin/vaadin-button/src/vaadin-button.js'");
    // create themed modules
    createImport("./theme/myTheme/subfolder/sub-template.js", "");
    createImport("./theme/myTheme/client-side-template.js", "");
    createImport("./theme/myTheme/main-template.js", "");
    // wrong-themed-template.js should not be resolved inside node_modules.
    // It should be searched only inside frontend directory
    createImport("theme/myTheme/wrong-themed-template.js", "");
    // create css files to avoid exception when files not found during the
    // test
    createImport("./foo.css", "");
    createImport("@vaadin/vaadin-mixed-component/bar.css", "");
    // make external component's module and its themed version
    createImport("@vaadin/vaadin-button/src/vaadin-button.js", "");
    createImport("@vaadin/vaadin-button/theme/myTheme/vaadin-button.js", "");
    ClassFinder finder = getClassFinder();
    FrontendDependencies deps = new FrontendDependencies(finder) {

        @Override
        public List<String> getModules() {
            return Stream.of("./src/main-template.js").collect(Collectors.toList());
        }

        @Override
        public Set<String> getScripts() {
            return Collections.emptySet();
        }

        @Override
        public AbstractTheme getTheme() {
            return new MyTheme();
        }

        @Override
        public ThemeDefinition getThemeDefinition() {
            return new ThemeDefinition(MyTheme.class, "", "");
        }
    };
    updater = new TaskUpdateImports(finder, deps, cf -> null, tmpRoot, generatedPath, frontendDirectory, null, null, false, TARGET, true, false, Mockito.mock(FeatureFlags.class));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DEFAULT_FRONTEND_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR) DEFAULT_GENERATED_DIR(com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_GENERATED_DIR) StringUtils(org.apache.commons.lang3.StringUtils) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Charset(java.nio.charset.Charset) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) ExpectedException(org.junit.rules.ExpectedException) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) Files(java.nio.file.Files) NODE_MODULES(com.vaadin.flow.server.frontend.FrontendUtils.NODE_MODULES) Set(java.util.Set) 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) IMPORTS_NAME(com.vaadin.flow.server.frontend.FrontendUtils.IMPORTS_NAME) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) Paths(java.nio.file.Paths) FeatureFlags(com.vaadin.experimental.FeatureFlags) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) File(java.io.File) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) ThemeDefinition(com.vaadin.flow.theme.ThemeDefinition) Before(org.junit.Before)

Example 3 with ClassFinder

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

the class TaskInstallWebpackPluginsTest method pluginsAddedToPackageJson.

@Test
public void pluginsAddedToPackageJson() throws IOException {
    File resourceFolder = temporaryFolder.newFolder();
    ClassFinder finder = Mockito.mock(ClassFinder.class);
    NodeUpdater nodeUpdater = new NodeUpdater(finder, Mockito.mock(FrontendDependencies.class), rootFolder, new File(""), resourceFolder, BUILD_DIRECTORY, Mockito.mock(FeatureFlags.class)) {

        @Override
        public void execute() {
        }
    };
    task.execute();
    final JsonObject packageJson = nodeUpdater.getPackageJson();
    final JsonObject devDependencies = packageJson.getObject(DEV_DEPENDENCIES);
    for (String plugin : WebpackPluginsUtil.getPlugins()) {
        Assert.assertTrue("packageJson is missing " + plugin, devDependencies.hasKey("@vaadin/" + plugin));
        final String pluginFolder = "./" + rootFolder.toPath().relativize(getPluginFolder(plugin).toPath()).toString().replace('\\', '/');
        Assert.assertEquals("Plugin is pointing to wrong directory", pluginFolder, devDependencies.getString("@vaadin/" + plugin));
    }
}
Also used : ClassFinder(com.vaadin.flow.server.frontend.scanner.ClassFinder) JsonObject(elemental.json.JsonObject) FeatureFlags(com.vaadin.experimental.FeatureFlags) File(java.io.File) FrontendDependencies(com.vaadin.flow.server.frontend.scanner.FrontendDependencies) Test(org.junit.Test)

Example 4 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder 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());
}
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) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 5 with ClassFinder

use of com.vaadin.flow.server.frontend.scanner.ClassFinder 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);
}
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) JsonObject(elemental.json.JsonObject) Logger(org.slf4j.Logger) JsonArray(elemental.json.JsonArray) 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) Test(org.junit.Test)

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