use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeTasksEndpointTest method setup.
@Before
public void setup() {
userDir = temporaryFolder.getRoot().getAbsolutePath();
System.setProperty("user.dir", userDir);
System.clearProperty(PARAM_FRONTEND_DIR);
System.clearProperty(PARAM_GENERATED_DIR);
File src = new File(getClass().getClassLoader().getResource("java").getFile());
dir = new File(userDir);
File json = new File(dir, "api-file.json");
Lookup mockLookup = Mockito.mock(Lookup.class);
Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl()).when(mockLookup).lookup(EndpointGeneratorTaskFactory.class);
Mockito.doReturn(new DefaultClassFinder(Collections.singleton(ConnectEndpointsForTesting.class))).when(mockLookup).lookup(ClassFinder.class);
builder = new Builder(mockLookup, dir, TARGET).enablePackagesUpdate(false).enableImportsUpdate(false).withEmbeddableWebComponents(false).withEndpointSourceFolder(src).withEndpointGeneratedOpenAPIFile(json).withFrontendGeneratedFolder(new File(dir, "api"));
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class EndpointsValidator method process.
@Override
public void process(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException {
if (classSet == null) {
// an endpoint and dependencies are not added to the project.
return;
}
ClassFinder finder = new DefaultClassFinder(classSet);
Set<Class<?>> endpoints = finder.getAnnotatedClasses(Endpoint.class);
if (!endpoints.isEmpty()) {
try {
finder.loadClass(classToCheck);
} catch (ClassNotFoundException e) {
throw new ServletException("ERROR: Vaadin endpoints only work for Spring " + "enabled projects.\n" + "This is not a spring application but there are Vaadin endpoints in these classes: " + endpoints.stream().map(Class::getName).collect(Collectors.joining("\n - ")), e);
}
}
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeUpdateImportsTest method noFallBackScanner_fallbackIsNotImportedEvenIfTheFileExists.
@Test
public void noFallBackScanner_fallbackIsNotImportedEvenIfTheFileExists() throws Exception {
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));
// create fallback imports file:
// it is present after generated but the user is now running
// everything without fallback. The file should not be included into
// the imports
fallBackImportsFile.mkdirs();
fallBackImportsFile.createNewFile();
Assert.assertTrue(fallBackImportsFile.exists());
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 file is not imported in generated-flow-imports
MatcherAssert.assertThat(mainContent, CoreMatchers.not(CoreMatchers.containsString(FrontendUtils.FALLBACK_IMPORTS_NAME)));
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder 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;"));
}
use of com.vaadin.flow.server.frontend.scanner.ClassFinder.DefaultClassFinder in project flow by vaadin.
the class NodeTasksTest method should_UseDefaultFolders.
@Test
public void should_UseDefaultFolders() throws Exception {
Lookup mockedLookup = Mockito.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(Paths.get(userDir, TARGET, DEFAULT_GENERATED_DIR).toFile().getAbsolutePath(), ((File) getFieldValue(builder, "generatedFolder")).getAbsolutePath());
builder.build().execute();
Assert.assertTrue(Paths.get(userDir, TARGET, DEFAULT_GENERATED_DIR, IMPORTS_NAME).toFile().exists());
}
Aggregations