use of com.vaadin.flow.server.frontend.scanner.ClassFinder 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 in project flow by vaadin.
the class AbstractNodeUpdatePackagesTest method versionsMatch_forceCleanUp_cleanUp.
@Test
public void versionsMatch_forceCleanUp_cleanUp() throws IOException {
// Generate package json in a proper format first
packageCreator.execute();
packageUpdater.execute();
makeNodeModulesAndPackageLock();
ClassFinder classFinder = getClassFinder();
// create a new package updater, with forced clean up enabled
packageUpdater = new TaskUpdatePackages(classFinder, getScanner(classFinder), baseDir, generatedDir, resourcesDir, true, false, TARGET, featureFlags);
packageUpdater.execute();
// clean up happened
assertCleanUp();
}
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));
}
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));
}
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder in project flow by vaadin.
the class BuildFrontendMojoTest method setup.
@Before
public void setup() throws Exception {
MavenProject project = Mockito.mock(MavenProject.class);
Mockito.when(project.getRuntimeClasspathElements()).thenReturn(getClassPath());
List<String> packages = Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparatorChar + "")).collect(Collectors.toList());
Mockito.when(project.getRuntimeClasspathElements()).thenReturn(packages);
Mockito.when(project.getCompileClasspathElements()).thenReturn(Collections.emptyList());
projectBase = temporaryFolder.getRoot();
tokenFile = new File(temporaryFolder.getRoot(), VAADIN_SERVLET_RESOURCES + TOKEN_FILE);
File npmFolder = temporaryFolder.getRoot();
generatedFolder = new File(npmFolder, "target/" + DEFAULT_GENERATED_DIR);
importsFile = new File(generatedFolder, IMPORTS_NAME);
nodeModulesPath = new File(npmFolder, NODE_MODULES);
flowResourcesFolder = new File(npmFolder, "target/" + DEFAULT_FLOW_RESOURCES_FOLDER);
File frontendDirectory = new File(npmFolder, DEFAULT_FRONTEND_DIR);
packageJson = new File(npmFolder, PACKAGE_JSON).getAbsolutePath();
webpackConfig = new File(npmFolder, WEBPACK_CONFIG).getAbsolutePath();
webpackGenerated = new File(npmFolder, WEBPACK_GENERATED).getAbsolutePath();
webpackOutputDirectory = new File(projectBase, VAADIN_WEBAPP_RESOURCES);
resourceOutputDirectory = new File(projectBase, VAADIN_SERVLET_RESOURCES);
projectFrontendResourcesDirectory = new File(npmFolder, "flow_resources");
defaultJavaSource = new File(".", "src/test/java");
openApiJsonFile = new File(npmFolder, "target/generated-resources/openapi.json");
generatedTsFolder = new File(npmFolder, "frontend/generated");
Assert.assertTrue("Failed to create a test project resources", projectFrontendResourcesDirectory.mkdirs());
Assert.assertTrue("Failed to create a test project file", new File(projectFrontendResourcesDirectory, TEST_PROJECT_RESOURCE_JS).createNewFile());
ReflectionUtils.setVariableValueInObject(mojo, "frontendResourcesDirectory", projectFrontendResourcesDirectory);
ReflectionUtils.setVariableValueInObject(mojo, "project", project);
ReflectionUtils.setVariableValueInObject(mojo, "webpackOutputDirectory", webpackOutputDirectory);
ReflectionUtils.setVariableValueInObject(mojo, "resourceOutputDirectory", resourceOutputDirectory);
ReflectionUtils.setVariableValueInObject(mojo, "generatedFolder", generatedFolder);
ReflectionUtils.setVariableValueInObject(mojo, "frontendDirectory", frontendDirectory);
ReflectionUtils.setVariableValueInObject(mojo, "generateEmbeddableWebComponents", false);
ReflectionUtils.setVariableValueInObject(mojo, "npmFolder", npmFolder);
ReflectionUtils.setVariableValueInObject(mojo, "generateBundle", false);
ReflectionUtils.setVariableValueInObject(mojo, "runNpmInstall", false);
ReflectionUtils.setVariableValueInObject(mojo, "optimizeBundle", true);
ReflectionUtils.setVariableValueInObject(mojo, "openApiJsonFile", openApiJsonFile);
ReflectionUtils.setVariableValueInObject(mojo, "applicationProperties", new File(npmFolder, "src/main/resources/application.properties"));
ReflectionUtils.setVariableValueInObject(mojo, "javaSourceFolder", defaultJavaSource);
ReflectionUtils.setVariableValueInObject(mojo, "generatedTsFolder", generatedTsFolder);
ReflectionUtils.setVariableValueInObject(mojo, "nodeVersion", FrontendTools.DEFAULT_NODE_VERSION);
ReflectionUtils.setVariableValueInObject(mojo, "nodeDownloadRoot", NodeInstaller.DEFAULT_NODEJS_DOWNLOAD_ROOT);
ReflectionUtils.setVariableValueInObject(mojo, "projectBasedir", projectBase);
ReflectionUtils.setVariableValueInObject(mojo, "projectBuildDir", Paths.get(projectBase.toString(), "target").toString());
flowResourcesFolder.mkdirs();
generatedFolder.mkdirs();
setProject(mojo, npmFolder);
// Install all imports used in the tests on node_modules so as we don't
// need to run `npm install`
createExpectedImports(frontendDirectory, nodeModulesPath);
FileUtils.fileWrite(packageJson, "UTF-8", TestUtils.getInitalPackageJson().toJson());
Lookup lookup = Mockito.mock(Lookup.class);
Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl()).when(lookup).lookup(EndpointGeneratorTaskFactory.class);
Mockito.doAnswer(invocation -> {
Mockito.doReturn((ClassFinder) invocation.getArguments()[0]).when(lookup).lookup(ClassFinder.class);
return lookup;
}).when(mojo).createLookup(Mockito.any(ClassFinder.class));
}
Aggregations