Skip to content

Commit

Permalink
improve testability by updating to a newer testing framework
Browse files Browse the repository at this point in the history
now uses maven itf for integration tests, and makes most tests simple unit tests
  • Loading branch information
Postremus committed Dec 10, 2023
1 parent be7468a commit 5e341a7
Show file tree
Hide file tree
Showing 20 changed files with 1,015 additions and 1,143 deletions.
80 changes: 53 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
<version.plexus-compiler-api>2.13.0</version.plexus-compiler-api>
<version.maven-plugin-annotations>3.5.2</version.maven-plugin-annotations>
<version.maven-plugin-api>3.5.2</version.maven-plugin-api>
<version.junit>4.13.2</version.junit>
<version.maven-plugin-testing-harness>1.1</version.maven-plugin-testing-harness>
<version.plexus-utils>3.5.1</version.plexus-utils>
<version.itf>0.12.0</version.itf>
<version.slf4j>1.7.36</version.slf4j>

<!-- Plugin Versions -->
<version.maven-plugin-plugin>3.5.2</version.maven-plugin-plugin>
Expand Down Expand Up @@ -94,23 +93,15 @@
<!-- Test Dependencies -->
<!-- Need to be the first, otherwise guice can not be found during test execution -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<groupId>com.soebes.itf.jupiter.extension</groupId>
<artifactId>itf-jupiter-extension</artifactId>
<version>${version.itf}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>${version.maven-plugin-testing-harness}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>${version.plexus-utils}</version>
<groupId>com.soebes.itf.jupiter.extension</groupId>
<artifactId>itf-assertj</artifactId>
<version>${version.itf}</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -142,28 +133,63 @@
<version>${version.maven-plugin-annotations}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${version.slf4j}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
<version>${version.plexus-compiler-api}</version>
<scope>provided</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${version.maven-surefire-plugin}</version>
</dependency>
</dependencies>
<groupId>com.soebes.itf.jupiter.extension</groupId>
<artifactId>itf-maven-plugin</artifactId>
<version>${version.itf}</version>
<executions>
<execution>
<id>installing</id>
<phase>pre-integration-test</phase>
<goals>
<goal>install</goal>
<goal>resources-its</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemProperties>
<maven.version>${maven.version}</maven.version>
<maven.home>${maven.home}</maven.home>
</systemProperties>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=same_thread
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=6
</configurationParameters>
</properties>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,27 @@
import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.maven.plugin.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A task that compiles a Jasper sourcefile.
*/
public class CompileTask implements Callable<Void> {

private static final Logger LOGGER = LoggerFactory.getLogger(CompileTask.class);
private final File source;
private final File destination;
private final Log log;
private final boolean verbose;

/**
* @param source The source file.
* @param destination The destination file.
* @param log The logger.
* @param verbose If the output should be verbose.
*/
CompileTask(File source, File destination, Log log, boolean verbose) {
CompileTask(File source, File destination, boolean verbose) {
super();
this.source = source;
this.destination = destination;
this.log = log;
this.verbose = verbose;
}

Expand All @@ -61,7 +60,7 @@ public Void call() throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(source))) {
JasperCompileManager.compileReportToStream(in, out);
if (verbose) {
log.info("Compiling " + source.getName());
LOGGER.info("Compiling " + source.getName());
}
} catch (Exception e) {
cleanUpAndThrowError(destination, e);
Expand All @@ -70,7 +69,7 @@ public Void call() throws Exception {
}

private void cleanUpAndThrowError(File out, Exception e) throws JRException {
log.error("Could not compile " + source.getName() + " because " + e.getMessage(), e);
LOGGER.error("Could not compile " + source.getName() + " because " + e.getMessage(), e);
if (out != null && out.exists()) {
out.delete();
}
Expand Down
205 changes: 205 additions & 0 deletions src/main/java/com/pro_crafting/tools/jasperreport/JasperMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package com.pro_crafting.tools.jasperreport;

/*
* 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.
*/

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This plugin compiles jasper source files to the target folder. While doing
* so, it keeps the folder structure intact.
*/
@Mojo(defaultPhase = LifecyclePhase.PROCESS_RESOURCES, name = "jasper", requiresDependencyResolution =
ResolutionScope.COMPILE)
public class JasperMojo extends AbstractMojo {
private static final Logger LOGGER = LoggerFactory.getLogger(JasperMojo.class);

/**
* This is the java compiler used
*/
@Parameter(defaultValue = "net.sf.jasperreports.engine.design.JRJdtCompiler")
private String compiler;

/**
* This is where the .jasper files are written.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}/jasper")
private File outputDirectory;

/**
* This is where the xml report design files should be.
*/
@Parameter(defaultValue = "src/main/jasperreports")
private File sourceDirectory;

/**
* The extension of the source files to look for. Finds files with a .jrxml
* extension by default.
*/
@Parameter(defaultValue = ".jrxml")
private String sourceFileExt;

/**
* The extension of the compiled report files. Creates files with a .jasper
* extension by default.
*
*/
@Parameter(defaultValue = ".jasper")
private String outputFileExt;

/**
* Check the source files before compiling. Default value is true.
*
*/
@Parameter(defaultValue = "true")
private boolean xmlValidation;

/**
* Set this to "true" to bypass compiling reports. Default value is false.
*
*/
@Parameter( defaultValue = "false" )
private boolean skip;

/**
* If verbose is on the plug-in will report which reports it is compiling
* and which files are being skipped.
*
*/
@Parameter(defaultValue = "false")
private boolean verbose;

/**
* The number of threads the reporting will use. Default is 4 which is good
* for a lot of reports on a hard drive (in stead of SSD). If you only have
* a few, or if you have SSD, it might be faster to set it to 2.
*
*/
@Parameter(defaultValue = "4")
private int numberOfThreads;

@Parameter(property = "project.compileClasspathElements")
private List<String> classpathElements;

/**
* Use this parameter to add additional properties to the Jasper compiler.
* For example.
*
* <pre>
* {@code
* <configuration>
* ...
* <additionalProperties>
* <net.sf.jasperreports.awt.ignore.missing.font>true
* </net.sf.jasperreports.awt.ignore.missing.font>
* <net.sf.jasperreports.default.pdf.font.name>Courier</net.sf.jasperreports.default.pdf.font.name>
* <net.sf.jasperreports.default.pdf.encoding>UTF-8</net.sf.jasperreports.default.pdf.encoding>
* <net.sf.jasperreports.default.pdf.embedded>true</net.sf.jasperreports.default.pdf.embedded>
* </additionalProperties>
* </configuration>
* }
* </pre>
*
*/
@Parameter
private Map<String, String> additionalProperties;

/**
* If failOnMissingSourceDirectory is on the plug-in will fail the build if
* source directory does not exist. Default value is true.
*
*/
@Parameter(defaultValue = "true")
private boolean failOnMissingSourceDirectory = true;

/**
* This is the source inclusion scanner class used, a
* <code>org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner</code>
* implementation class.
*
*/
@Parameter(defaultValue = "org.codehaus.plexus.compiler.util.scan.StaleSourceScanner")
private String sourceScanner;

/**
* Provides the option to add additional JARs to the Classpath for compiling. This is handy in case you have
* references to external Java-Beans in your JasperReports.
*
* <pre>
* {@code
* <configuration>
* ...
* <additionalClasspath>/web/lib/ServiceBeans.jar;/web/lib/WebForms.jar</additionalClasspath>
* ...
* </configuration>
* }
* </pre>
*
*/
@Parameter
private String additionalClasspath;

@Override
public void execute() throws MojoExecutionException {
JasperMojoConfiguration configuration = mapConfiguration();
if (verbose) {
logConfiguration(configuration);
}

new JasperReportCompiler(configuration).compileReports();
}

JasperMojoConfiguration mapConfiguration() {
JasperMojoConfiguration configuration = new JasperMojoConfiguration();
configuration.compiler = compiler;
configuration.outputDirectory = outputDirectory;
configuration.sourceDirectory = sourceDirectory;
configuration.sourceFileExt = sourceFileExt;
configuration.outputFileExt = outputFileExt;
configuration.xmlValidation = xmlValidation;
configuration.skip = skip;
configuration.verbose = verbose;
configuration.numberOfThreads = numberOfThreads;
configuration.classpathElements = classpathElements;
configuration.additionalProperties = additionalProperties;
configuration.failOnMissingSourceDirectory = failOnMissingSourceDirectory;
configuration.sourceScanner = sourceScanner;
configuration.additionalClasspath = additionalClasspath;

return configuration;
}

private void logConfiguration(JasperMojoConfiguration configuration) {
LOGGER.info("Generating Jasper reports");
LOGGER.info("Output dir: " + configuration.outputDirectory.getAbsolutePath());
LOGGER.info("Source dir: " + configuration.sourceDirectory.getAbsolutePath());
LOGGER.info("Output ext: " + configuration.outputFileExt);
LOGGER.info("Source ext: " + configuration.sourceFileExt);
LOGGER.info("Additional properties: " + configuration.additionalProperties);
LOGGER.info("XML Validation: " + configuration.xmlValidation);
LOGGER.info("JasperReports Compiler: " + configuration.compiler);
LOGGER.info("Number of threads: " + configuration.numberOfThreads);
LOGGER.info("classpathElements: " + configuration.classpathElements);
LOGGER.info("Additional Classpath: " + configuration.additionalClasspath);
LOGGER.info("Source Scanner: " + configuration.sourceScanner);
}
}
Loading

0 comments on commit 5e341a7

Please sign in to comment.