Skip to content

Commit

Permalink
Add actual junit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Nov 18, 2023
1 parent f191557 commit c469af4
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 171 deletions.
28 changes: 9 additions & 19 deletions .github/workflows/build-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,14 @@ on:
- opened
- ready_for_review
- reopened
push:
branches:
- 'feature/**'
workflow_dispatch:

jobs:
jdk17:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1000
fetch-tags: true

- name: Setup JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'

- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: build
build:
uses: neoforged/actions/.github/workflows/build-prs.yml@main
with:
java: 17
gradle_tasks: test
16 changes: 7 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ permissions:
statuses: write

jobs:
build:
release:
uses: neoforged/actions/.github/workflows/gradle-publish.yml@main
with:
java: 17
# pre_gradle_tasks: ':test' TODO - proper unit tests
gradle_tasks: ':publish closeAndReleaseSonatypeStagingRepository'
pre_gradle_tasks: test
gradle_tasks: publish closeAndReleaseSonatypeStagingRepository
secrets:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}

MAVEN_USER: ${{ secrets.MAVEN_USER }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}

GPG_SUBKEY: ${{ secrets.GPG_SUBKEY }}
GPG_SUBKEY_ID: ${{ secrets.GPG_SUBKEY_ID }}
GPG_SUBKEY_PASSWORD: ${{ secrets.GPG_SUBKEY_PASSWORD }}
SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}

GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_KEY_PASSWORD: ${{ secrets.GPG_KEY_PASSWORD }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build/
.settings/
bin/
*.launch
!src/test/resources
91 changes: 26 additions & 65 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import net.neoforged.gradleutils.PomUtilsExtension.License

plugins {
id 'java-library'
id 'net.neoforged.gradleutils'
id 'net.neoforged.gradleutils' version '3.+'
id 'maven-publish'
id 'signing'
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
}

group = 'net.neoforged'
version = gradleutils.getTagOffsetVersion()
version = gradleutils.version

repositories {
mavenCentral()
}

gradleutils {
setupSigning()
setupCentralPublishing()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
Expand All @@ -22,9 +27,13 @@ java {
}

dependencies {
annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:0.4.2'
compileOnly 'com.github.bsideup.jabel:jabel-javac-plugin:0.4.2'
annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:1.0.0'
annotationProcessor("net.java.dev.jna:jna-platform:5.13.0")
compileOnly 'com.github.bsideup.jabel:jabel-javac-plugin:1.0.0'
compileOnly 'org.jetbrains:annotations:24.0.1'

testImplementation(libs.junit.api)
testRuntimeOnly(libs.junit.engine)
}

// Uses https://github.com/bsideup/jabel to compile to Java 8 from Java 16 source.
Expand All @@ -50,25 +59,11 @@ tasks.withType(Javadoc).configureEach {
options.addStringOption('Xdoclint:all,-missing', '-public')
}

tasks.register('hello') {
doLast {
println 'Hello from Gradle!'
if (project.hasProperty('helloReply')){
println helloReply
}
// Test for --no-daemon launches to verify that the task was invoked.
new File(projectDir, "hello.txt").text = "Hello from Gradle inside a file!"
}
}

nexusPublishing {
repositories {
sonatype {
username.set(System.getenv('SONATYPE_USER') ?: '')
password.set(System.getenv('SONATYPE_PASSWORD') ?: '')
nexusUrl.set(uri('https://s01.oss.sonatype.org/service/local/'))
}
}
test {
javaLauncher.set(javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(17)
})
useJUnitPlatform()
}

publishing {
Expand All @@ -79,52 +74,18 @@ publishing {
pom {
name = project.name
description = 'A Java library for generating Eclipse Launch Configurations (.launch files).'
url = 'https://github.com/NeoForged/EclipseLaunchConfigs'

scm {
url = 'https://github.com/NeoForged/EclipseLaunchConfigs'
connection = 'scm:git:git://github.com/NeoForged/EclipseLaunchConfigs.git'
developerConnection = 'scm:git:[email protected]:NeoForged/EclipseLaunchConfigs.git'
}

issueManagement {
system = 'github'
url = 'https://github.com/NeoForged/EclipseLaunchConfigs/issues'
}

licenses {
license {
name = 'LGPL 2.1'
url = 'https://github.com/NeoForged/EclipseLaunchConfigs/blob/main/LICENSE'
distribution = 'repo'
}
}

developers {
developer {
id = 'neoforged'
name = 'NeoForged'
email = '[email protected]'
url = 'https://github.com/NeoForged/'
}
}

pomUtils.neoForgedDeveloper(it)
pomUtils.license(it, License.LGPL_v2)
pomUtils.githubRepo(it, 'EclipseLaunchConfigs')
}
}
}
repositories {
maven gradleutils.getPublishingForgeMaven()
maven gradleutils.publishingMaven
}
}

changelog {
fromTag '0.1'
}

if (System.getenv('GPG_PRIVATE_KEY')) {
signing {
final signingKey = System.getenv('GPG_PRIVATE_KEY') ?: ''
final signingPassword = System.getenv('GPG_KEY_PASSWORD') ?: ''
useInMemoryPgpKeys(signingKey, signingPassword)
sign publishing.publications.mavenJava
}
from '0.1'
}
1 change: 0 additions & 1 deletion hello.txt

This file was deleted.

15 changes: 7 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = 'https://maven.neoforged.net/' }
}

plugins {
id 'net.neoforged.gradleutils' version '2.0.+'
dependencyResolutionManagement {
versionCatalogs {
libs {
final jupiter = version 'jupiter', '5.7.2'
library('junit-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef(jupiter)
library('junit-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef(jupiter)
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/net/neoforged/elc/tests/GradleLaunchConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.neoforged.elc.tests;

import net.neoforged.elc.configs.GradleLaunchConfig;
import org.junit.jupiter.api.Test;

public class GradleLaunchConfigTest extends RunTest {
@Test
public void testGradleConfig() throws Exception {
assertExpectedConfig(GradleLaunchConfig.builder("EclipseLaunchConfigs")
.tasks(":hello")
.showConsole(false)
.offlineMode(true)
.args("--console=plain")
.property("helloReply", "Hello from Eclipse!")
.jvmArgs("-Xmx256M")
.build(), "runGradle.launch");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.neoforged.elc.tests;

import net.neoforged.elc.EclipseVariables;
import net.neoforged.elc.configs.JavaApplicationLaunchConfig;
import org.junit.jupiter.api.Test;

public class JavaApplicationLaunchConfigTest extends RunTest {
@Test
public void testJavaConfig() throws Exception {
assertExpectedConfig(JavaApplicationLaunchConfig.builder("EclipseLaunchConfigs")
.envVar("PROMPT_VALUE", EclipseVariables.prompt("Enter Something", "Some"))
.envVar("CURRENT_DATE", EclipseVariables.currentDate("yyyy/MM/dd"))
.vmArgs("-Xmx256M")
.args("--doStuff=true")
.build("net.neoforged.elc.tests.Main"), "runJava.launch");
}
}
22 changes: 22 additions & 0 deletions src/test/java/net/neoforged/elc/tests/LaunchGroupConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.neoforged.elc.tests;

import net.neoforged.elc.configs.LaunchGroup;
import org.junit.jupiter.api.Test;

public class LaunchGroupConfigTest extends RunTest {
@Test
public void testGroupConfig() throws Exception {
assertExpectedConfig(LaunchGroup.builder()
.entry(LaunchGroup.entry("EclipseLaunchConfigs - runGradle")
.enabled(true)
.adoptIfRunning(false)
.mode(LaunchGroup.Mode.RUN)
.action(LaunchGroup.Action.delay(2)))
.entry(LaunchGroup.entry("EclipseLaunchConfigs - runTest")
.enabled(true)
.adoptIfRunning(false)
.mode(LaunchGroup.Mode.DEBUG)
.action(LaunchGroup.Action.waitForTermination()))
.build(), "runGroup.launch");
}
}
69 changes: 0 additions & 69 deletions src/test/java/net/neoforged/elc/tests/Main.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/test/java/net/neoforged/elc/tests/RunTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.neoforged.elc.tests;

import net.neoforged.elc.configs.LaunchConfig;
import org.junit.jupiter.api.Assertions;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

public abstract class RunTest {
protected final void assertExpectedConfig(LaunchConfig config, String name) throws XMLStreamException, ParserConfigurationException, IOException, SAXException {
final StringWriter writer = new StringWriter();
config.write(writer);
final var factory = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final var original = factory.parse(new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)));
final var in = RunTest.class.getResourceAsStream("/" + name);
final var fromFile = factory.parse(in);
in.close();

original.normalizeDocument();
fromFile.normalizeDocument();
Assertions.assertTrue(original.isEqualNode(fromFile), "Documents did not match!");
}
}

0 comments on commit c469af4

Please sign in to comment.