From 61eacf9485210a568b74b207637f5e638eb456da Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 28 Aug 2025 15:05:05 +0200 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- pom.xml | 5 +- .../plugins/unity3d/IntegrationTests.java | 47 +++++---- .../plugins/unity3d/Unity3dBuilderTest.java | 95 +++++++++---------- .../PipeFileAfterModificationActionTest.java | 32 +++---- .../plugins/unity3d/io/PipeTest.java | 40 ++++---- .../unity3d/logs/EditorLogParserImplTest.java | 30 +++--- 6 files changed, 127 insertions(+), 122 deletions(-) diff --git a/pom.xml b/pom.xml index 1657c027..e0217649 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 5.17 + 5.24 @@ -35,6 +35,7 @@ 2.479 ${jenkins.baseline}.3 false + false @@ -42,7 +43,7 @@ io.jenkins.tools.bom bom-${jenkins.baseline}.x - 4948.vcf1d17350668 + 5054.v620b_5d2b_d5e6 pom import diff --git a/src/test/java/org/jenkinsci/plugins/unity3d/IntegrationTests.java b/src/test/java/org/jenkinsci/plugins/unity3d/IntegrationTests.java index 60ef63da..2edf4607 100644 --- a/src/test/java/org/jenkinsci/plugins/unity3d/IntegrationTests.java +++ b/src/test/java/org/jenkinsci/plugins/unity3d/IntegrationTests.java @@ -1,41 +1,47 @@ package org.jenkinsci.plugins.unity3d; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.Result; import java.io.File; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; /** * @author Jerome Lacoste */ -public class IntegrationTests { +@WithJenkins +class IntegrationTests { - @Rule - public JenkinsRule rule = new JenkinsRule(); + private JenkinsRule r; + + @BeforeEach + void beforeEach(JenkinsRule rule) { + r = rule; + } @Test @LocalData - public void testEditorException() throws Exception { + void testEditorException() throws Exception { ensureUnityHomeExists(); - FreeStyleProject job = (FreeStyleProject) rule.jenkins.getItem("test_unity3d"); + FreeStyleProject job = (FreeStyleProject) r.jenkins.getItem("test_unity3d"); assertNotNull(job); FreeStyleBuild build = job.scheduleBuild2(0).get(); - rule.assertLogContains("Exception: Simulated Exception", build); + r.assertLogContains("Exception: Simulated Exception", build); } private void ensureUnityHomeExists() { - Unity3dInstallation[] installations = rule.jenkins + Unity3dInstallation[] installations = r.jenkins .getDescriptorByType(Unity3dInstallation.DescriptorImpl.class) .getInstallations(); assertEquals(1, installations.length); @@ -43,31 +49,34 @@ private void ensureUnityHomeExists() { Unity3dInstallation inst = installations[0]; String unityHome = inst.getHome(); - assumeTrue(new File(unityHome).exists()); // skip test if doesn't have unity + assumeTrue( + unityHome != null && new File(unityHome).exists(), + "Skip test due to missing unity installation: " + unityHome); } @Test @LocalData - public void testEditorExceptionWithCustomLogFile() throws Exception { + void testEditorExceptionWithCustomLogFile() throws Exception { ensureUnityHomeExists(); - FreeStyleProject job = (FreeStyleProject) rule.jenkins.getItem("test_unity3d"); + FreeStyleProject job = (FreeStyleProject) r.jenkins.getItem("test_unity3d"); assertNotNull(job); FreeStyleBuild build = job.scheduleBuild2(0).get(); - rule.assertLogContains("Exception: Simulated Exception", build); + r.assertLogContains("Exception: Simulated Exception", build); } @Test @LocalData - public void testExpectADifferentExitCode() throws Exception { + void testExpectADifferentExitCode() throws Exception { ensureUnityHomeExists(); - FreeStyleProject job = (FreeStyleProject) rule.jenkins.getItem("test_unity3d"); + + FreeStyleProject job = (FreeStyleProject) r.jenkins.getItem("test_unity3d"); assertNotNull(job); FreeStyleBuild build = job.scheduleBuild2(0).get(); - rule.assertBuildStatus(Result.UNSTABLE, build); + r.assertBuildStatus(Result.UNSTABLE, build); } } diff --git a/src/test/java/org/jenkinsci/plugins/unity3d/Unity3dBuilderTest.java b/src/test/java/org/jenkinsci/plugins/unity3d/Unity3dBuilderTest.java index 4f456342..542ddf2c 100644 --- a/src/test/java/org/jenkinsci/plugins/unity3d/Unity3dBuilderTest.java +++ b/src/test/java/org/jenkinsci/plugins/unity3d/Unity3dBuilderTest.java @@ -1,7 +1,8 @@ package org.jenkinsci.plugins.unity3d; import static java.util.Arrays.asList; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import hudson.EnvVars; import hudson.util.ArgumentListBuilder; @@ -9,41 +10,36 @@ import java.util.Hashtable; import java.util.List; import java.util.Map; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * @author Jerome Lacoste */ -public class Unity3dBuilderTest { +class Unity3dBuilderTest { - private String exe = "/Applications/Unity/Unity.app"; - private String moduleRootRemote = "/Users/Shared/Jenkins/Home/jobs/project1/workspace"; - - private String argLine; - - private List expectedArgs; + private static final String EXE = "/Applications/Unity/Unity.app"; + private static final String MODULE_ROOT_REMOTE = "/Users/Shared/Jenkins/Home/jobs/project1/workspace"; @Test - public void typicalExecuteMethodArgumentsAddMissingProjectPath() { - argLine = "-quit -batchmode -nographics -executeMethod ExecuteClass.ExecuteMethod"; - expectedArgs = asList( - exe, + void typicalExecuteMethodArgumentsAddMissingProjectPath() { + String argLine = "-quit -batchmode -nographics -executeMethod ExecuteClass.ExecuteMethod"; + List expectedArgs = asList( + EXE, "-projectPath", - moduleRootRemote, + MODULE_ROOT_REMOTE, "-quit", "-batchmode", "-nographics", "-executeMethod", "ExecuteClass.ExecuteMethod"); - ensureCreateCommandlineArgs(expectedArgs); + ensureCreateCommandlineArgs(argLine, expectedArgs); } @Test - public void typicalExecuteMethodArgumentsWithCustomProjectPath() { - argLine = "-quit -batchmode -nographics -executeMethod ExecuteClass.ExecuteMethod -projectPath XXXX"; - expectedArgs = asList( - exe, + void typicalExecuteMethodArgumentsWithCustomProjectPath() { + String argLine = "-quit -batchmode -nographics -executeMethod ExecuteClass.ExecuteMethod -projectPath XXXX"; + List expectedArgs = asList( + EXE, "-quit", "-batchmode", "-nographics", @@ -51,32 +47,33 @@ public void typicalExecuteMethodArgumentsWithCustomProjectPath() { "ExecuteClass.ExecuteMethod", "-projectPath", "XXXX"); - ensureCreateCommandlineArgs(expectedArgs); + ensureCreateCommandlineArgs(argLine, expectedArgs); } @Test - public void buildWindowsPlayerAddMissingProjectPath() { - argLine = "-buildWindowsPlayer \"C:\\Temp\\The Win32.exe\""; - expectedArgs = asList(exe, "-projectPath", moduleRootRemote, "-buildWindowsPlayer", "C:\\Temp\\The Win32.exe"); - ensureCreateCommandlineArgs(expectedArgs); + void buildWindowsPlayerAddMissingProjectPath() { + String argLine = "-buildWindowsPlayer \"C:\\Temp\\The Win32.exe\""; + List expectedArgs = + asList(EXE, "-projectPath", MODULE_ROOT_REMOTE, "-buildWindowsPlayer", "C:\\Temp\\The Win32.exe"); + ensureCreateCommandlineArgs(argLine, expectedArgs); } @Test - public void buildOSXPlayerAddMissingProjectPath() { - argLine = "-buildOSXPlayer the\\ dir.app"; - expectedArgs = asList(exe, "-projectPath", moduleRootRemote, "-buildOSXPlayer", "the dir.app"); - ensureCreateCommandlineArgs(expectedArgs); + void buildOSXPlayerAddMissingProjectPath() { + String argLine = "-buildOSXPlayer the\\ dir.app"; + List expectedArgs = asList(EXE, "-projectPath", MODULE_ROOT_REMOTE, "-buildOSXPlayer", "the dir.app"); + ensureCreateCommandlineArgs(argLine, expectedArgs); } - private void ensureCreateCommandlineArgs(List expectedArgs1) { + private void ensureCreateCommandlineArgs(String argLine, List expectedArgs) { Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", argLine, ""); ArgumentListBuilder commandlineArgs = - builder.createCommandlineArgs(exe, moduleRootRemote, new EnvVars(), new Hashtable<>()); - assertEquals(expectedArgs1, commandlineArgs.toList()); + builder.createCommandlineArgs(EXE, MODULE_ROOT_REMOTE, new EnvVars(), new Hashtable<>()); + assertEquals(expectedArgs, commandlineArgs.toList()); } @Test - public void environmentAndBuildVariablesParsing() { + void environmentAndBuildVariablesParsing() { EnvVars vars = new EnvVars(); vars.put("param1", "value1"); vars.put("param2", "value2"); @@ -86,36 +83,37 @@ public void environmentAndBuildVariablesParsing() { Map buildParameters = new Hashtable<>(); buildParameters.put("param2", param2overwrittenValue); - argLine = "-param1 $param1 -param2 $param2 -projectPath XXXX"; - expectedArgs = asList(exe, "-param1", "value1", "-param2", param2overwrittenValue, "-projectPath", "XXXX"); + String argLine = "-param1 $param1 -param2 $param2 -projectPath XXXX"; + List expectedArgs = + asList(EXE, "-param1", "value1", "-param2", param2overwrittenValue, "-projectPath", "XXXX"); Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", argLine, ""); ArgumentListBuilder commandlineArgs = - builder.createCommandlineArgs(exe, moduleRootRemote, vars, buildParameters); + builder.createCommandlineArgs(EXE, MODULE_ROOT_REMOTE, vars, buildParameters); assertEquals(expectedArgs, commandlineArgs.toList()); - assertEquals("Serialized arg line not modified", argLine, builder.getArgLine()); + assertEquals(argLine, builder.getArgLine(), "Serialized arg line not modified"); } @Test - public void environmentAndBuildVariablesParsingWithEnvVarsThatReferencesBuildParameters() { + void environmentAndBuildVariablesParsingWithEnvVarsThatReferencesBuildParameters() { EnvVars vars = new EnvVars(); vars.put("ARGS", "-projectPath $param"); Map buildParameters = new Hashtable<>(); buildParameters.put("param", "XXXX"); - argLine = "-p1 v1 $ARGS"; - expectedArgs = asList(exe, "-p1", "v1", "-projectPath", "XXXX"); + String argLine = "-p1 v1 $ARGS"; + List expectedArgs = asList(EXE, "-p1", "v1", "-projectPath", "XXXX"); Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", argLine, ""); ArgumentListBuilder commandlineArgs = - builder.createCommandlineArgs(exe, moduleRootRemote, vars, buildParameters); + builder.createCommandlineArgs(EXE, MODULE_ROOT_REMOTE, vars, buildParameters); assertEquals(expectedArgs, commandlineArgs.toList()); - assertEquals("Serialized arg line not modified", argLine, builder.getArgLine()); + assertEquals(argLine, builder.getArgLine(), "Serialized arg line not modified"); } @Test - public void unstableErrorCodesParsing() throws Exception { + void unstableErrorCodesParsing() { ensureUnstableReturnCodesParsingWorks(new Integer[] {}, ""); ensureUnstableReturnCodesParsingWorks(new Integer[] {2, 3}, "2,3"); ensureUnstableReturnCodesParsingWorks(new Integer[] {-1}, "-1"); @@ -125,17 +123,12 @@ public void unstableErrorCodesParsing() throws Exception { } private void ensureUnstableReturnCodesParsingWorks(Integer[] expectedResultCodes, String unstableReturnCodes) { - Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", argLine, unstableReturnCodes); + Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", null, unstableReturnCodes); assertEquals(new HashSet<>(asList(expectedResultCodes)), builder.toUnstableReturnCodesSet()); } private void ensureUnstableReturnCodesParsingFails(String unstableReturnCodes) { - Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", argLine, unstableReturnCodes); - try { - builder.toUnstableReturnCodesSet(); - Assert.fail("Expected failure"); - } catch (Exception expected) { - // - } + Unity3dBuilder builder = new Unity3dBuilder("Unity 3.5", null, unstableReturnCodes); + assertThrows(Exception.class, builder::toUnstableReturnCodesSet); } } diff --git a/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeFileAfterModificationActionTest.java b/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeFileAfterModificationActionTest.java index 8ef07208..3033b15e 100644 --- a/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeFileAfterModificationActionTest.java +++ b/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeFileAfterModificationActionTest.java @@ -1,51 +1,49 @@ package org.jenkinsci.plugins.unity3d.io; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import hudson.util.ByteArrayOutputStream2; import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardOpenOption; import java.util.concurrent.atomic.AtomicLong; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * @author Jerome Lacoste */ -public class PipeFileAfterModificationActionTest { - private String originalContent = +class PipeFileAfterModificationActionTest { + + private static final String ORIGINAL_CONTENT = """ The original content of the file Multiple lines of\s Build information"""; - private String newContent = + private static final String NEW_CONTENT = """ The new content of the file Multiple lines of\s """; - private String newContent2 = "Build information"; - - public static final Charset UTF_8 = StandardCharsets.UTF_8; + private static final String NEW_CONTENT_2 = "Build information"; @Test - public void simulateEditorLogRewritten() throws Exception { + void simulateEditorLogRewritten() throws Exception { testRewriteFile(0); } @Test - public void simulateEditorLogSlowlyMoved() throws Exception { + void simulateEditorLogSlowlyMoved() throws Exception { testRewriteFile(100); } - private void testRewriteFile(int timeToWaitAfterRename) throws IOException, InterruptedException { + private void testRewriteFile(int timeToWaitAfterRename) throws Exception { // Given File fakeEditorLog = File.createTempFile("fake_editor", "log"); - Files.writeString(fakeEditorLog.toPath(), originalContent, StandardCharsets.UTF_8); + Files.writeString(fakeEditorLog.toPath(), ORIGINAL_CONTENT, StandardCharsets.UTF_8); ByteArrayOutputStream2 collectedContent = new ByteArrayOutputStream2(); @@ -69,11 +67,11 @@ private void testRewriteFile(int timeToWaitAfterRename) throws IOException, Inte Thread.sleep(timeToWaitAfterRename); - Files.writeString(fakeEditorLog.toPath(), newContent, StandardCharsets.UTF_8); + Files.writeString(fakeEditorLog.toPath(), NEW_CONTENT, StandardCharsets.UTF_8); Thread.sleep(20); - Files.writeString(fakeEditorLog.toPath(), newContent2, StandardCharsets.UTF_8, StandardOpenOption.APPEND); + Files.writeString(fakeEditorLog.toPath(), NEW_CONTENT_2, StandardCharsets.UTF_8, StandardOpenOption.APPEND); Thread.sleep(80); - String expectedContent = newContent + newContent2; + String expectedContent = NEW_CONTENT + NEW_CONTENT_2; // simulate remote cancellation. Using the remoting API, we cancel the task and this interrupts the remote // thread @@ -81,7 +79,7 @@ private void testRewriteFile(int timeToWaitAfterRename) throws IOException, Inte // give us the time to terminate properly the task Thread.sleep(50); - assertEquals(expectedContent, new String(collectedContent.getBuffer(), UTF_8)); + assertEquals(expectedContent, new String(collectedContent.getBuffer(), StandardCharsets.UTF_8)); Long read = (long) expectedContent.length(); assertEquals(read, (Long) nbBytesRead.get()); diff --git a/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeTest.java b/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeTest.java index f297e9dd..750e7e7e 100644 --- a/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeTest.java +++ b/src/test/java/org/jenkinsci/plugins/unity3d/io/PipeTest.java @@ -1,6 +1,8 @@ package org.jenkinsci.plugins.unity3d.io; -import static org.junit.Assert.assertEquals; +import static hudson.Functions.isWindows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assumptions.assumeFalse; import hudson.Launcher; import hudson.remoting.Future; @@ -9,14 +11,13 @@ import hudson.util.StreamCopyThread; import hudson.util.StreamTaskListener; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; -import java.util.concurrent.ExecutionException; import jenkins.security.MasterToSlaveCallable; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; /** * This test was written to find a solution to the piping issue. @@ -26,13 +27,18 @@ * * @author Jerome Lacoste */ -public class PipeTest { +@WithJenkins +class PipeTest { - @Rule - public JenkinsRule rule = new JenkinsRule(); + private JenkinsRule r; + + @BeforeEach + void beforeEach(JenkinsRule rule) { + r = rule; + } private VirtualChannel createSlaveChannel() throws Exception { - DumbSlave s = rule.createSlave(); + DumbSlave s = r.createSlave(); s.toComputer().connect(false).get(); VirtualChannel ch = null; while (ch == null) { @@ -43,26 +49,22 @@ private VirtualChannel createSlaveChannel() throws Exception { } @Test - public void testPipingFromRemoteWithLocalLaunch() throws Exception { + void testPipingFromRemoteWithLocalLaunch() throws Exception { doPipingFromRemoteTest( new Launcher.LocalLauncher(new StreamTaskListener(System.out, Charset.defaultCharset()))); } - private static boolean isRunningOnWindows() { - return System.getProperty("os.name").toLowerCase().startsWith("windows"); - } - @Test - public void testPipingFromRemoteWithRemoteLaunch() throws Exception { - // Windows cant delete open log files, so ignore this test because of - // java.io.IOException: Unable to delete ... - if (isRunningOnWindows()) return; + void testPipingFromRemoteWithRemoteLaunch() throws Exception { + assumeFalse( + isWindows(), + "Windows cant delete open log files, so ignore this test because of java.io.IOException: Unable to delete "); doPipingFromRemoteTest(new Launcher.RemoteLauncher( new StreamTaskListener(System.out, Charset.defaultCharset()), createSlaveChannel(), true)); } - private void doPipingFromRemoteTest(Launcher l) throws IOException, InterruptedException, ExecutionException { + private void doPipingFromRemoteTest(Launcher l) throws Exception { Pipe pipe = Pipe.createRemoteToLocal(l); Future piping = l.getChannel().callAsync(new PipingCallable(pipe.getOut())); ByteArrayOutputStream os = new ByteArrayOutputStream(); diff --git a/src/test/java/org/jenkinsci/plugins/unity3d/logs/EditorLogParserImplTest.java b/src/test/java/org/jenkinsci/plugins/unity3d/logs/EditorLogParserImplTest.java index 38b4c2dc..9917343e 100644 --- a/src/test/java/org/jenkinsci/plugins/unity3d/logs/EditorLogParserImplTest.java +++ b/src/test/java/org/jenkinsci/plugins/unity3d/logs/EditorLogParserImplTest.java @@ -5,7 +5,7 @@ import java.io.InputStreamReader; import org.jenkinsci.plugins.unity3d.logs.block.MatchedBlock; import org.jenkinsci.plugins.unity3d.logs.line.Line; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Created by IntelliJ IDEA. @@ -14,13 +14,13 @@ * Time: 8:11 AM * To change this template use File | Settings | File Templates. */ -public class EditorLogParserImplTest { - private EditorLogParserImpl parser = new EditorLogParserImpl(); - private EditorLogParserImpl.LogListener listener; +class EditorLogParserImplTest { + + private static final EditorLogParserImpl PARSER = new EditorLogParserImpl(); @Test - public void testLog() throws Exception { - listener = new EditorLogParserImpl.LogListener() { + void testLog() throws Exception { + EditorLogParserImpl.LogListener listener = new EditorLogParserImpl.LogListener() { public void activityStarted(MatchedBlock block) { System.out.println("BLOCK START: " + block.getName()); } @@ -30,17 +30,19 @@ public void activityFinished(MatchedBlock block) { } public void logMessage(String line, Line.Type type) { - if (type != Line.Type.Normal) System.out.println("=== " + type + " => " + line); + if (type != Line.Type.Normal) { + System.out.println("=== " + type + " => " + line); + } } }; - parser.setListener(listener); - InputStream is = findResource("/example_Editor.log"); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - String line; - while ((line = reader.readLine()) != null) { - parser.log(line); + PARSER.setListener(listener); + try (InputStream is = findResource("/example_Editor.log"); + BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { + String line; + while ((line = reader.readLine()) != null) { + PARSER.log(line); + } } - is.close(); } private InputStream findResource(String resourceName) {