BuildFrontendMojo extends FlowModeAbstractMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { updateBuildFile(); long start = System.nanoTime(); try { runNodeUpdater(); } catch (ExecutionFailedException exception) { throw new MojoFailureException( "Could not execute build-frontend goal", exception); } if (generateBundle) { try { runWebpack(); } catch (IllegalStateException exception) { throw new MojoExecutionException(exception.getMessage(), exception); } } long ms = (System.nanoTime() - start) / 1000000; getLog().info("update-frontend took " + ms + "ms."); }  @Override void execute();  }
@Test public void should_ContainLumoThemeFiles() throws Exception { mojo.execute(); assertContainsImports(true, "@vaadin/vaadin-lumo-styles/color.js", "@vaadin/vaadin-lumo-styles/typography.js", "@vaadin/vaadin-lumo-styles/sizing.js", "@vaadin/vaadin-lumo-styles/spacing.js", "@vaadin/vaadin-lumo-styles/style.js", "@vaadin/vaadin-lumo-styles/icons.js"); }
@Test public void shouldNot_ContainExternalUrls() throws Exception { mojo.execute(); assertContainsImports(false, "https: assertContainsImports(false, " }
@Test public void should_AddImports() throws Exception { mojo.execute(); removeImports("@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); assertContainsImports(false, "@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); mojo.execute(); assertContainsImports(true, "@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); }
@Test public void should_removeImports() throws Exception { mojo.execute(); addImports("./added-import.js"); assertContainsImports(true, "./added-import.js"); mojo.execute(); assertContainsImports(false, "./added-import.js"); }
@Test public void should_AddRemove_Imports() throws Exception { mojo.execute(); removeImports("@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); addImports("./added-import.js"); assertContainsImports(false, "@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); assertContainsImports(true, "./added-import.js"); mojo.execute(); assertContainsImports(true, "@vaadin/vaadin-lumo-styles/sizing.js", "./local-template.js"); assertContainsImports(false, "./added-import.js"); }
@Test public void mavenGoalWhenPackageJsonContainsDependencies_onlyFrameworkHandledDependencyIsTouched() throws Exception { JsonObject json = TestUtils.getInitalPackageJson(); JsonObject dependencies = Json.createObject(); dependencies.put("foo", "bar"); dependencies.put("bar", "foo"); json.getObject("vaadin").getObject("dependencies").put("foo", "bar"); json.put("dependencies", dependencies); FileUtils.fileWrite(packageJson, "UTF-8", json.toJson()); mojo.execute(); JsonObject packageJsonObject = getPackageJson(packageJson); dependencies = packageJsonObject.getObject("dependencies"); assertContainsPackage(dependencies, "@vaadin/vaadin-button", "@vaadin/vaadin-element-mixin"); Assert.assertFalse("Foo should have been removed", dependencies.hasKey("foo")); Assert.assertTrue("Bar should remain", dependencies.hasKey("bar")); }
@Test public void existingTokenFile_enableDevServerShouldBeAdded() throws IOException, IllegalAccessException, MojoExecutionException, MojoFailureException { File projectBase = temporaryFolder.getRoot(); File webpackOutputDirectory = new File(projectBase, VAADIN_SERVLET_RESOURCES); ReflectionUtils.setVariableValueInObject(mojo, "generatedFolder", projectBase); ReflectionUtils.setVariableValueInObject(mojo, "webpackOutputDirectory", webpackOutputDirectory); JsonObject initialBuildInfo = Json.createObject(); initialBuildInfo.put(SERVLET_PARAMETER_PRODUCTION_MODE, false); initialBuildInfo.put(Constants.NPM_TOKEN, "npm"); initialBuildInfo.put(Constants.GENERATED_TOKEN, "generated"); initialBuildInfo.put(Constants.FRONTEND_TOKEN, "frontend"); initialBuildInfo.put(Constants.SERVLET_PARAMETER_ENABLE_PNPM, true); initialBuildInfo.put(Constants.REQUIRE_HOME_NODE_EXECUTABLE, true); initialBuildInfo .put(Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE, true); org.apache.commons.io.FileUtils.forceMkdir(tokenFile.getParentFile()); org.apache.commons.io.FileUtils.write(tokenFile, JsonUtil.stringify(initialBuildInfo, 2) + "\n", "UTF-8"); mojo.execute(); String json = org.apache.commons.io.FileUtils .readFileToString(tokenFile, "UTF-8"); JsonObject buildInfo = JsonUtil.parse(json); Assert.assertNotNull("devMode token should be available", buildInfo.get(SERVLET_PARAMETER_ENABLE_DEV_SERVER)); Assert.assertNotNull("productionMode token should be available", buildInfo.get(SERVLET_PARAMETER_PRODUCTION_MODE)); Assert.assertNull("npmFolder should have been removed", buildInfo.get(Constants.NPM_TOKEN)); Assert.assertNull("generatedFolder should have been removed", buildInfo.get(Constants.GENERATED_TOKEN)); Assert.assertNull("frontendFolder should have been removed", buildInfo.get(Constants.FRONTEND_TOKEN)); Assert.assertNull( Constants.SERVLET_PARAMETER_ENABLE_PNPM + "should have been removed", buildInfo.get(Constants.SERVLET_PARAMETER_ENABLE_PNPM)); Assert.assertNull( Constants.REQUIRE_HOME_NODE_EXECUTABLE + "should have been removed", buildInfo.get(Constants.REQUIRE_HOME_NODE_EXECUTABLE)); Assert.assertNull( Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE + "should have been removed", buildInfo.get( Constants.SERVLET_PARAMETER_DEVMODE_OPTIMIZE_BUNDLE)); }
@Test public void noTokenFile_noTokenFileShouldBeCreated() throws MojoExecutionException, MojoFailureException { mojo.execute(); Assert.assertFalse(tokenFile.exists()); }
@Test public void mavenGoal_generateOpenApiJson_when_itIsInClientSideMode() throws Exception { Assert.assertFalse(FileUtils.fileExists(openApiJsonFile)); mojo.execute(); Assert.assertTrue(FileUtils.fileExists(openApiJsonFile)); }
@Test public void mavenGoal_notGenerateOpenApiJson_when_usingDeprecatedV14Bootstrapping() throws Exception { ReflectionUtils.setVariableValueInObject(mojo, "useDeprecatedV14Bootstrapping", "true"); Assert.assertFalse(FileUtils.fileExists(openApiJsonFile)); mojo.execute(); Assert.assertFalse(FileUtils.fileExists(openApiJsonFile)); }
@Test public void mavenGoal_generateTsFiles_when_enabled() throws Exception { File connectClientApi = new File(generatedTsFolder, "connect-client.default.ts"); File endpointClientApi = new File(generatedTsFolder, "MyEndpoint.ts"); Assert.assertFalse(connectClientApi.exists()); Assert.assertFalse(endpointClientApi.exists()); mojo.execute(); Assert.assertTrue(connectClientApi.exists()); Assert.assertTrue(endpointClientApi.exists()); }
@Test public void should_copyProjectFrontendResources() throws MojoExecutionException, MojoFailureException { List<File> initialFiles = gatherFiles(flowResourcesFolder); initialFiles.forEach(file -> Assert.assertFalse(String.format( "Test resource shouldn't exist before running mojo.", file), TEST_PROJECT_RESOURCE_JS.equals(file.getName()))); mojo.execute(); Set<String> projectFrontendResources = Stream .of(projectFrontendResourcesDirectory.listFiles()) .map(File::getName).collect(Collectors.toSet()); Set<String> filesInFlowResourcesFolder = Stream .of(flowResourcesFolder.listFiles()) .map(File::getName).collect(Collectors.toSet()); projectFrontendResources.forEach(fileName -> { Assert.assertTrue(String.format( "Expected the copied file '%s' to be in the project resources", fileName), filesInFlowResourcesFolder.contains(fileName)); }); }
@Test public void should_UpdateMainJsFile() throws Exception { Assert.assertFalse(importsFile.exists()); List<String> expectedLines = new ArrayList<>(Arrays.asList( "export const addCssBlock = function(block, before = false) {", " const tpl = document.createElement('template');", " tpl.innerHTML = block;", " document.head[before ? 'insertBefore' : 'appendChild'](tpl.content, document.head.firstChild);", "};", "addCssBlock('<custom-style><style include=\"lumo-color lumo-typography\"></style></custom-style>', true);", "document.documentElement.setAttribute('theme', 'dark');")); expectedLines.addAll(getExpectedImports()); mojo.execute(); assertContainsImports(true, expectedLines.toArray(new String[0])); File flowFrontend = new File(nodeModulesPath, FLOW_NPM_PACKAGE_NAME); Assert.assertTrue( new File(flowFrontend, "/ExampleConnector.js").exists()); }
@Test public void shouldNot_UpdateJsFile_when_NoChanges() throws Exception { mojo.execute(); long timestamp1 = importsFile.lastModified(); sleep(1000); mojo.execute(); long timestamp2 = importsFile.lastModified(); Assert.assertEquals(timestamp1, timestamp2); }