Skip to content
Permalink
Browse files
FREEMARKER-156 [freemarker-generator] The Maven plugin unit tests fai…
…led randomly (#26)
  • Loading branch information
sgoeschl committed Jan 27, 2021
1 parent 14bbb5e commit 4962dcb184226a63d991d57c53e98edd2adb1088
Show file tree
Hide file tree
Showing 21 changed files with 609 additions and 785 deletions.
@@ -16,14 +16,27 @@
*/
package org.apache.freemarker.generator.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Map;
import java.util.Properties;

/**
* Various helper methods to load property files.
*/
public class PropertiesFactory {

public static Properties create(File file) {
try (InputStream is = new FileInputStream(file)) {
return create(is);
} catch (IOException e) {
throw new RuntimeException("Failed to load properties file: " + file.getAbsolutePath());
}
}

public static Properties create(InputStream is) {
try {
final Properties properties = new Properties();
@@ -141,6 +141,7 @@ public void shouldCreateOutputGenerator() {
assertNotNull(outputGenerator.getTemplateOutput().getWriter());
assertNull(outputGenerator.getTemplateOutput().getFile());
}

private static String fixSeparators(String str) {
if (OperatingSystem.isWindows()) {
return FilenameUtils.separatorsToWindows(str);
@@ -9,8 +9,12 @@ All notable changes to this project will be documented in this file. We try to a
* [FREEMARKER-128] Update `freemarker-maven-plugin` to Apache FreeMarker 2.3.29

### Fixed
* [FREEMARKER-151] Ensure that build and and examples are running on Windows
* [FREEMARKER-151] Ensure that build and examples are running on Windows

### Internal
* [FREEMARKER-156] Replaced JMockit and TestNG with Mockito and JUnit

[FREEMARKER-128]: https://issues.apache.org/jira/browse/FREEMARKER-128
[FREEMARKER-129]: https://issues.apache.org/jira/browse/FREEMARKER-129
[FREEMARKER-151]: https://issues.apache.org/jira/browse/FREEMARKER-151
[FREEMARKER-156]: https://issues.apache.org/jira/browse/FREEMARKER-156
@@ -32,14 +32,12 @@
<description>Maven plugin for Apache FreeMarker</description>

<properties>
<maven-core.version>3.5.2</maven-core.version>
<maven-plugin-api.version>3.5.2</maven-plugin-api.version>
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
<fastutil.version>8.1.0</fastutil.version>
<maven-core.version>3.6.3</maven-core.version>
<maven-plugin-api.version>3.6.3</maven-plugin-api.version>
<maven-plugin-annotations.version>3.6.0</maven-plugin-annotations.version>
<gson.version>2.8.6</gson.version>
<jmockit.version>1.32</jmockit.version>
<org.testng.version>6.8</org.testng.version>
<assertj-core.version>3.8.0</assertj-core.version>
<mockito.version>3.7.7</mockito.version>
<assertj-core.version>3.18.1</assertj-core.version>
</properties>

<dependencies>
@@ -80,20 +78,14 @@
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<version>0.3.3</version>
<version>0.3.4</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
@@ -104,9 +96,14 @@
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${org.testng.version}</version>
<groupId>org.apache.freemarker.generator</groupId>
<artifactId>freemarker-generator-base</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -115,6 +112,12 @@
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.freemarker.generator.maven;

import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Version;
import org.apache.freemarker.generator.base.util.PropertiesFactory;
import org.apache.freemarker.generator.base.util.Validate;

import java.io.File;
import java.util.Properties;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public class ConfigurationSupplier implements Supplier<Configuration> {

private final String freeMarkerVersion;
private final File templateDirectory;
private final File sourceDirectory;

public ConfigurationSupplier(String freeMarkerVersion, File templateDirectory, File sourceDirectory) {
Validate.notEmpty(freeMarkerVersion, "freeMarkerVersion is required");
Validate.fileExists(templateDirectory, "Required template directory does not exist");

this.freeMarkerVersion = requireNonNull(freeMarkerVersion);
this.templateDirectory = requireNonNull(templateDirectory);
this.sourceDirectory = requireNonNull(sourceDirectory);
}

@Override
public Configuration get() {
final Configuration configuration = new Configuration(new Version(freeMarkerVersion));
configuration.setDefaultEncoding("UTF-8");

try {
configuration.setTemplateLoader(new FileTemplateLoader(templateDirectory));
} catch (Throwable t) {
throw new RuntimeException("Could not establish file template loader for directory: " + templateDirectory, t);
}

final File freeMarkerProps = new File(sourceDirectory, "freemarker.properties");

if (freeMarkerProps.isFile()) {
final Properties configProperties = PropertiesFactory.create(freeMarkerProps);
try {
configuration.setSettings(configProperties);
} catch (Throwable t) {
throw new RuntimeException("Invalid setting(s) in " + freeMarkerProps, t);
}
}

return configuration;
}
}

This file was deleted.

@@ -16,26 +16,21 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.freemarker.generator.maven;

import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class FreeMarkerMojo extends AbstractMojo {
@@ -63,46 +58,21 @@ public class FreeMarkerMojo extends AbstractMojo {
private MojoExecution mojo;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {

if (freeMarkerVersion == null || freeMarkerVersion.isEmpty()) {
throw new MojoExecutionException("freeMarkerVersion is required");
}

if (!generatorDirectory.isDirectory()) {
throw new MojoExecutionException("Required directory does not exist: " + generatorDirectory);
throw new MojoExecutionException("Required generator directory does not exist: " + generatorDirectory);
}

final Configuration config = FactoryUtil.createConfiguration(freeMarkerVersion);

config.setDefaultEncoding("UTF-8");

if (!templateDirectory.isDirectory()) {
throw new MojoExecutionException("Required directory does not exist: " + templateDirectory);
}
try {
config.setTemplateLoader(new FileTemplateLoader(templateDirectory));
} catch (Throwable t) {
getLog().error("Could not establish file template loader for directory: " + templateDirectory, t);
throw new MojoExecutionException("Could not establish file template loader for directory: " + templateDirectory);
throw new MojoExecutionException("Required template directory does not exist: " + templateDirectory);
}

final File freeMarkerProps = FactoryUtil.createFile(sourceDirectory, "freemarker.properties");
if (freeMarkerProps.isFile()) {
final Properties configProperties = new Properties();
try (InputStream is = FactoryUtil.createFileInputStream(freeMarkerProps)) {
configProperties.load(is);
} catch (Throwable t) {
getLog().error("Failed to load " + freeMarkerProps, t);
throw new MojoExecutionException("Failed to load " + freeMarkerProps);
}
try {
config.setSettings(configProperties);
} catch (Throwable t) {
getLog().error("Invalid setting(s) in " + freeMarkerProps, t);
throw new MojoExecutionException("Invalid setting(s) in " + freeMarkerProps);
}
}
final Configuration configuration = configuration();

if ("generate-sources".equals(mojo.getLifecyclePhase())) {
session.getCurrentProject().addCompileSourceRoot(outputDirectory.toString());
@@ -113,12 +83,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
final Map<String, OutputGeneratorPropertiesProvider> extensionToBuilders = new HashMap<>(1);
extensionToBuilders.put(".json", JsonPropertiesProvider.create(generatorDirectory, templateDirectory, outputDirectory));

final GeneratingFileVisitor fileVisitor = GeneratingFileVisitor.create(config, session, extensionToBuilders);
final GeneratingFileVisitor fileVisitor = GeneratingFileVisitor.create(configuration, session, extensionToBuilders);
try {
Files.walkFileTree(generatorDirectory.toPath(), fileVisitor);
} catch (Throwable t) {
getLog().error("Failed to process files in generator dir: " + generatorDirectory, t);
throw new MojoExecutionException("Failed to process files in generator dir: " + generatorDirectory);
}
}

private Configuration configuration() {
return new ConfigurationSupplier(freeMarkerVersion, templateDirectory, sourceDirectory).get();
}
}
@@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.freemarker.generator.maven;

import freemarker.template.Configuration;
@@ -30,7 +29,7 @@

/**
* FileVisitor designed to process json data files. The json file parsed into
* a map and given to FreeMarker to
* a map and given to FreeMarker to.
*/
public class GeneratingFileVisitor extends SimpleFileVisitor<Path> {

@@ -52,8 +51,8 @@ private GeneratingFileVisitor(Configuration config, MavenSession session, Map<St
/**
* Factory method that calls constructor, added to facilitate testing with jmockit.
*
* @param config FreeMarker configuration
* @param session Maven session
* @param config FreeMarker configuration
* @param session Maven session
* @param extensionToBuilder builder extension
* @return GeneratingFileVisitor instance
*/
@@ -68,8 +67,8 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
.addGeneratorLocation(path)
.addPomLastModifiedTimestamp(pomLastModifiedTimestamp);
final String fileName = path.getFileName().toString();
final String extenstion = fileName.substring(fileName.lastIndexOf('.'));
final OutputGeneratorPropertiesProvider pathProcessor = extensionToBuilder.get(extenstion);
final String extension = fileName.substring(fileName.lastIndexOf('.'));
final OutputGeneratorPropertiesProvider pathProcessor = extensionToBuilder.get(extension);
if (pathProcessor == null) {
throw new RuntimeException("Unknown file extension: " + path);
}
@@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.freemarker.generator.maven;

import com.google.gson.Gson;

0 comments on commit 4962dcb

Please sign in to comment.