Skip to content

Commit

Permalink
test: add test project
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Sep 7, 2024
1 parent 524baa8 commit 1888af0
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 42 deletions.
20 changes: 1 addition & 19 deletions .ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,7 @@ echo "###################################################"
echo "# Determining GIT branch...... #"
echo "###################################################"
GIT_BRANCH=$(git branch --show-current)
echo " -> GIT Branch: $GIT_BRANCH"; echo


echo
echo "###################################################"
echo "# Configuring JDK Class Data Sharing... #"
echo "###################################################"
java_version=$(java -version 2>&1)
echo "$java_version"
# https://docs.oracle.com/javase/8/docs/technotes/guides/vm/class-data-sharing.html
jdk_version_checksum=$(echo "$java_version" | md5sum | cut -f1 -d" ")
if [[ ! -f $HOME/.xshare/$jdk_version_checksum ]]; then
echo " -> Generating shared class data archive..."
mkdir -p $HOME/.xshare
java -Xshare:dump -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=$HOME/.xshare/$jdk_version_checksum
else
echo " -> Reusing shared class data archive..."
fi
export JAVA_TOOL_OPTIONS="${JAVA_TOOL_OPTIONS:-} -Xshare:on -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=$HOME/.xshare/$jdk_version_checksum"
echo " -> GIT Branch: $GIT_BRANCH"


echo
Expand Down
14 changes: 12 additions & 2 deletions .ci/maven-toolchains.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
<toolchain>
<type>jdk</type>
<provides>
<version>${env.JAVA_VERSION}</version>
<version>17</version>
<vendor>default</vendor>
</provides>
<configuration>
<jdkHome>${env.JAVA_HOME}</jdkHome>
<jdkHome>${env.JAVA17_HOME}</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>21</version>
<vendor>default</vendor>
</provides>
<configuration>
<jdkHome>${env.JAVA21_HOME}</jdkHome>
</configuration>
</toolchain>
</toolchains>
33 changes: 17 additions & 16 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,26 @@ jobs:
uses: actions/checkout@v4 # https://github.com/actions/checkout


- name: "Install: JDK ${{ env.JAVA_VERSION }} ☕"
- name: "Install: JDK 21 ☕" # to run example unit tests
uses: actions/setup-java@v4 # https://github.com/actions/setup-java
id: setup-java-21
with:
distribution: temurin
java-version: ${{ env.JAVA_VERSION }}


# reusing CDS archives of the same JVM randomly fails in GitHub Actions with
# "An error has occurred while processing the shared archive file. shared class paths mismatch"
#- name: Calculate Java version checksum
# id: java-version-checksum
# run: |
# echo "md5sum=$(java -version 2>&1 | md5sum | cut -f1 -d" ")" >> $GITHUB_OUTPUT
#- name: Cache Java CDS archive
# uses: actions/cache@v3
# with:
# path: |
# ~/.xshare/${{ steps.java-version-checksum.outputs.md5sum }}
# key: ${{ runner.os }}-xshare-${{ steps.java-version-checksum.outputs.md5sum }}
java-version: 21


- name: "Install: JDK 17 ☕" # to run Maven and plugins and compile test project
uses: actions/setup-java@v4 # https://github.com/actions/setup-java
id: setup-java-17
with:
distribution: temurin
java-version: 17


- name: Set JAVA_HOME env vars
run: |
echo "JAVA17_HOME=${{ steps.setup-java-17.outputs.path }}" >> $GITHUB_ENV
echo "JAVA21_HOME=${{ steps.setup-java-21.outputs.path }}" >> $GITHUB_ENV
- name: "Cache: Maven Binary"
Expand Down
22 changes: 17 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<url>https://github.com/vegardit/vegardit-maven-parent/actions</url>
</ciManagement>

<modules>
<module>src/test/project</module>
</modules>

<properties>
<!--
Expand Down Expand Up @@ -171,6 +174,8 @@
<skip.report.maven-project-info-reports-plugin>false</skip.report.maven-project-info-reports-plugin>
<skip.report.taglist-maven-plugin>false</skip.report.taglist-maven-plugin>

<github.site.skip>${maven.deploy.skip}</github.site.skip>

<!-- https://groovy.github.io/gmaven/groovy-maven-plugin/variables.html -->
<groovy.script.onInitialize><![CDATA[
/*
Expand Down Expand Up @@ -284,7 +289,9 @@
*/
def javaVersion = getProjectProp("java.version");
def javaMajorVersion = Integer.parseInt(javaVersion.indexOf(".") > -1 ? javaVersion.substring(0, javaVersion.indexOf(".")) : javaVersion);
def ecjVersion = javaMajorVersion < 17 ? "3.33.0" : project.properties["eclipse-java-compiler.version"]; def ecjArtifactMeta = artifactFactory.createArtifactWithClassifier("org.eclipse.jdt", "ecj", ecjVersion, "jar", null); def ecjArtifact = artifactResolver.resolve(new org.apache.maven.artifact.resolver.ArtifactResolutionRequest()
def ecjVersion = javaMajorVersion < 17 ? "3.33.0" : project.properties["eclipse-java-compiler.version"];
def ecjArtifactMeta = artifactFactory.createArtifactWithClassifier("org.eclipse.jdt", "ecj", ecjVersion, "jar", null);
def ecjArtifact = artifactResolver.resolve(new org.apache.maven.artifact.resolver.ArtifactResolutionRequest()
.setArtifact(ecjArtifactMeta)
.setLocalRepository(session.localRepository)
.setRemoteRepositories(project.remoteArtifactRepositories)
Expand Down Expand Up @@ -359,12 +366,16 @@
}
/* in Maven 4: session.topLevelProject.basedir https://github.com/apache/maven/pull/1061 */
topdir = project.properties['maven.multiModuleProjectDirectory']
/*
* loading/locating checkstyle config
*/
if(!project.properties.containsKey("checkstyle.config.path")) {
if(new File("src/etc/checkstyle.xml").exists()) {
setProjectProp("checkstyle.config.path", "src/etc/checkstyle.xml");
if(new File("$topdir/src/etc/checkstyle.xml").exists()) {
setProjectProp("checkstyle.config.path", "$topdir/src/etc/checkstyle.xml");
} else {
def checkstyleConfigArtifactMeta = null;
if(project.properties.containsKey("checkstyle.config.artifact")) {
Expand Down Expand Up @@ -397,8 +408,8 @@
* loading eclipse formatter config
*/
if(!project.properties.containsKey("eclipse-formatter.config.path")) {
if(new File("src/etc/eclipse-formatter.xml").exists()) {
setProjectProp("eclipse-formatter.config.path", "src/etc/eclipse-formatter.xml");
if(new File("$topdir/src/etc/eclipse-formatter.xml").exists()) {
setProjectProp("eclipse-formatter.config.path", "$topdir/src/etc/eclipse-formatter.xml");
} else {
def eclipseFormatterConfigArtifactMeta = null;
if(project.properties.containsKey("eclipse-formatter.config.artifact")) {
Expand Down Expand Up @@ -1960,6 +1971,7 @@
<goal>execute</goal>
</goals>
<configuration>
<skipScriptExecution>${github.site.skip}</skipScriptExecution>
<scripts>
<script><![CDATA[
def setProjectProp = { String name, String value ->
Expand Down
46 changes: 46 additions & 0 deletions src/test/project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-FileContributor: Sebastian Thomschke
SPDX-License-Identifier: Apache-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.vegardit.maven</groupId>
<artifactId>vegardit-maven-parent</artifactId>
<version>5.0.1-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>vegardit-maven-parent.test</artifactId>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<java.version.unit-tests>21</java.version.unit-tests>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.vegardit.maven.parent.test;

import java.util.Objects;

/**
* @author Sebastian Thomschke
*/
public abstract sealed class Shape {

public static final class Circle extends Shape {
private final double radius;

public Circle(final double radius) {
this.radius = radius;
}

@Override
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final Circle circle = (Circle) o;
return Double.compare(circle.radius, radius) == 0;
}

@Override
public int hashCode() {
return Objects.hash(radius);
}

public double radius() {
return radius;
}
}

public static final class Rectangle extends Shape {
private final double length;
private final double width;

public Rectangle(final double length, final double width) {
this.length = length;
this.width = width;
}

@Override
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final Rectangle rectangle = (Rectangle) o;
return Double.compare(rectangle.length, length) == 0 && Double.compare(rectangle.width, width) == 0;
}

@Override
public int hashCode() {
return Objects.hash(length, width);
}

public double length() {
return length;
}

public double width() {
return width;
}
}

public static String describeShape(final Shape shape) {
if (shape instanceof final Circle c)
return "Circle with radius: " + c.radius();
else if (shape instanceof final Rectangle r)
return "Rectangle with length: " + r.length() + " and width: " + r.width();
return "Unknown shape";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.vegardit.maven.parent.test;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

/**
* @author Sebastian Thomschke
*/
class RuntimeJavaVersionTest {

/**
* This tests if the following config in the pom.xml is effective
* <pre>{@code
* <properties>
* <java.version.unit-tests>21</java.version.unit-tests>
* </properties>
* }</pre>
*/
@Test
void ensureJava21IsUsed() {
final String javaVersion = System.getProperty("java.version");
System.out.println("java.version = " + javaVersion);
assertTrue(javaVersion.startsWith("21."), "Java 21 is expected for unit tests.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.vegardit.maven.parent.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.vegardit.maven.parent.test.Shape.Circle;
import com.vegardit.maven.parent.test.Shape.Rectangle;

/**
* @author Sebastian Thomschke
*/
class ShapeTests {
private Shape circle;
private Shape rectangle;

@BeforeEach
void setup() {
circle = new Circle(5.0);
rectangle = new Rectangle(10.0, 20.0);
}

@Test
void testDescribeCircle() {
final String description = Shape.describeShape(circle);
assertEquals("Circle with radius: 5.0", description);
}

@Test
void testDescribeRectangle() {
final String description = Shape.describeShape(rectangle);
assertEquals("Rectangle with length: 10.0 and width: 20.0", description);
}
}

0 comments on commit 1888af0

Please sign in to comment.