diff --git a/src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java b/src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java index 9db20c5..4cf6e09 100644 --- a/src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java +++ b/src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java @@ -30,10 +30,10 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.FilePath; @@ -69,28 +69,34 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestBuilder; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class CleanupTest { +@WithJenkins +class CleanupTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; - @Rule - public TemporaryFolder ws = new EnhancedTemporaryFolder(); + @TempDir + private File ws; + + @BeforeEach + void setUp(JenkinsRule rule) { + j = rule; + } // "IllegalArgumentException: Illegal group reference" observed when filename contained '$'; @Test - public void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exception { + void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exception { String filename = "\\s! Dozen for $5 only!"; FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); - populateWorkspace(p, filename); + populateWorkspace(j, p, filename); p.getBuildWrappersList() .add(new PreBuildCleanup( @@ -104,9 +110,9 @@ public void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exce @Test @Issue("JENKINS-20056") - public void wipeOutWholeWorkspaceBeforeBuild() throws Exception { + void wipeOutWholeWorkspaceBeforeBuild() throws Exception { FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); - populateWorkspace(p, "content.txt"); + populateWorkspace(j, p, "content.txt"); p.getBuildWrappersList().add(new PreBuildCleanup(Collections.emptyList(), false, null, null, false)); FreeStyleBuild b = j.buildAndAssertSuccess(p); @@ -115,7 +121,7 @@ public void wipeOutWholeWorkspaceBeforeBuild() throws Exception { @Test @Issue("JENKINS-20056") - public void wipeOutWholeWorkspaceAfterBuild() throws Exception { + void wipeOutWholeWorkspaceAfterBuild() throws Exception { FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); p.getBuildersList().add(getTouchBuilder("content.txt")); @@ -126,7 +132,7 @@ public void wipeOutWholeWorkspaceAfterBuild() throws Exception { @Test @Issue("JENKINS-20056") - public void wipeOutWholeWorkspaceAfterBuildMatrix() throws Exception { + void wipeOutWholeWorkspaceAfterBuildMatrix() throws Exception { MatrixProject p = j.jenkins.createProject(MatrixProject.class, "sut"); p.setAxes(new AxisList(new TextAxis("name", "a b"))); p.getBuildWrappersList().add(new MatrixWsPopulator()); @@ -141,7 +147,7 @@ public void wipeOutWholeWorkspaceAfterBuildMatrix() throws Exception { @Test @Issue("JENKINS-20056") - public void workspaceShouldNotBeManipulated() throws Exception { + void workspaceShouldNotBeManipulated() throws Exception { int ITERATIONS = 50; FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); @@ -176,7 +182,7 @@ public void workspaceShouldNotBeManipulated() throws Exception { } @Test - public void deleteWorkspaceWithNonAsciiCharacters() throws Exception { + void deleteWorkspaceWithNonAsciiCharacters() throws Exception { FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); p.getBuildersList().add(getTouchBuilder("a¶‱ﻷ.txt")); @@ -189,11 +195,11 @@ public void deleteWorkspaceWithNonAsciiCharacters() throws Exception { @Test @Issue("JENKINS-26250") - public void doNotFailToWipeoutWhenRenameFails() throws Exception { + void doNotFailToWipeoutWhenRenameFails() throws Exception { assumeTrue(!Functions.isWindows()); // In MSFT we can't disable renaming a folder without enable to delete it FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); - populateWorkspace(p, "content.txt"); + populateWorkspace(j, p, "content.txt"); p.getPublishersList().add(wipeoutPublisher()); FilePath workspace = p.getLastBuild().getWorkspace(); @@ -201,7 +207,7 @@ public void doNotFailToWipeoutWhenRenameFails() throws Exception { try { workspace.renameTo(workspace.withSuffix("2")); - assumeTrue("Rename operation should fail", workspace.exists()); + assumeTrue(workspace.exists(), "Rename operation should fail"); } catch (java.nio.file.AccessDeniedException ade) { // expected on Java 9 + } @@ -211,7 +217,7 @@ public void doNotFailToWipeoutWhenRenameFails() throws Exception { } @Test - public void reportCleanupCommandFailure() throws Exception { + void reportCleanupCommandFailure() throws Exception { String command = Functions.isWindows() ? "cmd /c md %s" : "mkdir %s"; FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); @@ -246,12 +252,12 @@ public void reportCleanupCommandFailure() throws Exception { @Test @Issue("JENKINS-28454") - public void pipelineWorkspaceCleanup() throws Exception { + void pipelineWorkspaceCleanup() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "') { \n" + " try { \n" + + sanitizeFilePath(ws) + "') { \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'foobar' \n" + " } finally { \n" + " step([$class: 'WsCleanup']) \n" @@ -263,17 +269,17 @@ public void pipelineWorkspaceCleanup() throws Exception { j.assertLogContains("[WS-CLEANUP] Deleting project workspace...", run); j.assertLogContains("[WS-CLEANUP] done", run); - assertThat(ws.getRoot().listFiles(), nullValue()); + assertThat(ws.listFiles(), nullValue()); } @Test @Issue("JENKINS-28454") - public void pipelineWorkspaceCleanupUsingPattern() throws Exception { + void pipelineWorkspaceCleanupUsingPattern() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "') { \n" + " try { \n" + + sanitizeFilePath(ws) + "') { \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'first file' \n" + " writeFile file: 'bar.txt', text: 'second file' \n" + " } finally { \n" @@ -291,12 +297,12 @@ public void pipelineWorkspaceCleanupUsingPattern() throws Exception { @Test @Issue("JENKINS-28454") - public void pipelineWorkspaceCleanupUnlessBuildFails() throws Exception { + void pipelineWorkspaceCleanupUnlessBuildFails() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "'){ \n" + " try { \n" + + sanitizeFilePath(ws) + "'){ \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'foobar' \n" + " throw new Exception() \n" + " } catch (err) { \n" @@ -317,12 +323,12 @@ public void pipelineWorkspaceCleanupUnlessBuildFails() throws Exception { @Test @Issue("JENKINS-37054") - public void symbolAnnotationWorkspaceCleanup() throws Exception { + void symbolAnnotationWorkspaceCleanup() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "') { \n" + " try { \n" + + sanitizeFilePath(ws) + "') { \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'foobar' \n" + " } finally { \n" + " cleanWs() \n" @@ -334,17 +340,17 @@ public void symbolAnnotationWorkspaceCleanup() throws Exception { j.assertLogContains("[WS-CLEANUP] Deleting project workspace...", run); j.assertLogContains("[WS-CLEANUP] done", run); - assertThat(ws.getRoot().listFiles(), nullValue()); + assertThat(ws.listFiles(), nullValue()); } @Test @Issue("JENKINS-37054") - public void symbolWorkspaceCleanupAnnotationUsingPattern() throws Exception { + void symbolWorkspaceCleanupAnnotationUsingPattern() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "') { \n" + " try { \n" + + sanitizeFilePath(ws) + "') { \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'first file' \n" + " writeFile file: 'bar.txt', text: 'second file' \n" + " } finally { \n" @@ -362,12 +368,12 @@ public void symbolWorkspaceCleanupAnnotationUsingPattern() throws Exception { @Test @Issue("JENKINS-37054") - public void symbolAnnotationWorkspaceCleanupUnlessBuildFails() throws Exception { + void symbolAnnotationWorkspaceCleanupUnlessBuildFails() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition( - "" + "node { \n" + "node { \n" + " ws ('" - + ws.getRoot() + "'){ \n" + " try { \n" + + sanitizeFilePath(ws) + "'){ \n" + " try { \n" + " writeFile file: 'foo.txt', text: 'foobar' \n" + " error 'error'\n" + " } catch (err) { \n" @@ -387,11 +393,11 @@ public void symbolAnnotationWorkspaceCleanupUnlessBuildFails() throws Exception } @Test - public void doNotRunExternalCommandWhenNull() throws Exception { + void doNotRunExternalCommandWhenNull() throws Exception { String command = null; FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); - populateWorkspace(p, "content.txt"); + populateWorkspace(j, p, "content.txt"); p.getBuildWrappersList().add(new PreBuildCleanup(Collections.emptyList(), false, null, command, false)); @@ -401,11 +407,11 @@ public void doNotRunExternalCommandWhenNull() throws Exception { } @Test - public void doNotRunExternalCommandWhenWhitespace() throws Exception { + void doNotRunExternalCommandWhenWhitespace() throws Exception { String command = " \n "; FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); - populateWorkspace(p, "content.txt"); + populateWorkspace(j, p, "content.txt"); p.getBuildWrappersList().add(new PreBuildCleanup(Collections.emptyList(), false, null, command, false)); @@ -415,7 +421,7 @@ public void doNotRunExternalCommandWhenWhitespace() throws Exception { } @Test - public void retryAsyncDirDeletion() throws Exception { + void retryAsyncDirDeletion() throws Exception { FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut"); p.getPublishersList().add(CleanupTest.wipeoutPublisher()); @@ -441,7 +447,6 @@ void performDelete(FilePath workspace) throws IOException { Wipeout.INSTANCE = new Wipeout(); - //noinspection deprecation disposer.reschedule(); Thread.sleep(100); @@ -450,7 +455,7 @@ void performDelete(FilePath workspace) throws IOException { } @Test - public void deferredWipeoutAfterBuild() throws Exception { + void deferredWipeoutAfterBuild() throws Exception { // Deferred wipeout enabled FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut1"); p.getBuildersList().add(getTouchBuilder("content.txt")); @@ -514,7 +519,7 @@ public void deferredWipeoutAfterBuild() throws Exception { } private void verifyFileExists(String fileName) { - File[] files = ws.getRoot().listFiles(); + File[] files = ws.listFiles(); assertThat(files, notNullValue()); assertThat(files, arrayWithSize(1)); assertThat(files[0].getName(), is(fileName)); @@ -528,7 +533,7 @@ public static WsCleanup wipeoutPublisher() { return wsCleanup; } - private TestBuilder getTouchBuilder(String filename) { + private static TestBuilder getTouchBuilder(String filename) { return new TestBuilder() { @Override public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) @@ -539,25 +544,31 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen }; } - private void populateWorkspace(FreeStyleProject p, String filename) throws Exception { + private static void populateWorkspace(JenkinsRule j, FreeStyleProject p, String filename) throws Exception { p.getBuildersList().add(getTouchBuilder(filename)); FreeStyleBuild b = j.buildAndAssertSuccess(p); p.getBuildersList().clear(); - assertFalse("Workspace populated", b.getWorkspace().list().isEmpty()); + assertFalse(b.getWorkspace().list().isEmpty(), "Workspace populated"); } - private void assertWorkspaceCleanedUp(AbstractBuild b) throws Exception { + private static void assertWorkspaceCleanedUp(AbstractBuild b) throws Exception { FilePath workspace = b.getWorkspace(); if (workspace == null) { return; // removed } List files = workspace.list(); - if (files == null) { - return; // removed - } - assertTrue("Workspace contains: " + files, files.isEmpty()); + assertTrue(files.isEmpty(), "Workspace contains: " + files); + } + + /** + * Replaces {@code \} with {@code /} on Windows. + * @param file the file + * @return the sanitized path + */ + private static String sanitizeFilePath(File file) { + return Functions.isWindows() ? file.getAbsolutePath().replaceAll("\\\\", "/") : file.getAbsolutePath(); } /** @@ -591,23 +602,4 @@ public String getDisplayName() { } } } - - private static final class EnhancedTemporaryFolder extends TemporaryFolder { - @Override - public EnhancedFile getRoot() { - return new EnhancedFile(super.getRoot()); - } - - private static final class EnhancedFile extends File { - public EnhancedFile(File f) { - super(f.getPath()); - } - - @Override - public String toString() { - String org = super.getPath(); - return Functions.isWindows() ? org.replaceAll("\\\\", "/") : org; - } - } - } } diff --git a/src/test/java/hudson/plugins/ws_cleanup/JobDslTest.java b/src/test/java/hudson/plugins/ws_cleanup/JobDslTest.java index e3d0c31..0238c10 100644 --- a/src/test/java/hudson/plugins/ws_cleanup/JobDslTest.java +++ b/src/test/java/hudson/plugins/ws_cleanup/JobDslTest.java @@ -26,9 +26,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.iterableWithSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import hudson.model.FreeStyleProject; import java.io.File; @@ -36,25 +36,23 @@ import java.util.List; import javaposse.jobdsl.dsl.DslScriptLoader; import javaposse.jobdsl.plugin.JenkinsJobManagement; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class JobDslTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); +@WithJenkins +class JobDslTest { - private void applyScript(String s) throws java.io.IOException { - JenkinsJobManagement jjm = new JenkinsJobManagement(System.out, Collections.emptyMap(), new File(".")); - new DslScriptLoader(jjm).runScript(s); - } + private static JenkinsRule j; - private FreeStyleProject getJob(String emptyCleanWs) { - return j.jenkins.getItem(emptyCleanWs, j.jenkins, FreeStyleProject.class); + @BeforeAll + static void setUp(JenkinsRule rule) { + j = rule; } @Test - public void emptyCleanWs() throws Exception { + void emptyCleanWs() throws Exception { applyScript("job('emptyCleanWs') { publishers { cleanWs() } }"); WsCleanup wsc = getJob("emptyCleanWs").getPublishersList().get(WsCleanup.class); @@ -72,30 +70,32 @@ public void emptyCleanWs() throws Exception { } @Test - public void fullCleanWs() throws Exception { - applyScript("job('fullCleanWs') {" + "publishers {\n" - + " cleanWs {\n" - + " cleanWhenAborted(true)\n" - + " cleanWhenFailure(true)\n" - + " cleanWhenNotBuilt(false)\n" - + " cleanWhenSuccess(true)\n" - + " cleanWhenUnstable(true)\n" - + " deleteDirs(true)\n" - + " notFailBuild(true)\n" - + " disableDeferredWipeout(true)\n" - + " patterns {\n" - + " pattern {\n" - + " type('EXCLUDE')\n" - + " pattern('.propsfile')\n" - + " }\n" - + " pattern {\n" - + " type('INCLUDE')\n" - + " pattern('.gitignore')\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n" - + "}"); + void fullCleanWs() throws Exception { + applyScript( + """ + job('fullCleanWs') {publishers { + cleanWs { + cleanWhenAborted(true) + cleanWhenFailure(true) + cleanWhenNotBuilt(false) + cleanWhenSuccess(true) + cleanWhenUnstable(true) + deleteDirs(true) + notFailBuild(true) + disableDeferredWipeout(true) + patterns { + pattern { + type('EXCLUDE') + pattern('.propsfile') + } + pattern { + type('INCLUDE') + pattern('.gitignore') + } + } + } + } + }"""); WsCleanup wsc = getJob("fullCleanWs").getPublishersList().get(WsCleanup.class); @@ -120,7 +120,7 @@ public void fullCleanWs() throws Exception { } @Test - public void emptyPreBuildCleanup() throws Exception { + void emptyPreBuildCleanup() throws Exception { applyScript("job('emptyPreBuildCleanup') { wrappers { preBuildCleanup() } }"); PreBuildCleanup pbc = @@ -133,15 +133,18 @@ public void emptyPreBuildCleanup() throws Exception { } @Test - public void examplePreBuildCleanup() throws Exception { - applyScript("job('examplePreBuildCleanup') {\n" + " wrappers {\n" - + " preBuildCleanup {\n" - + " includePattern('**/target/**')\n" - + " deleteDirectories()\n" - + " cleanupParameter('CLEANUP')\n" - + " }\n" - + " }\n" - + "}"); + void examplePreBuildCleanup() throws Exception { + applyScript( + """ + job('examplePreBuildCleanup') { + wrappers { + preBuildCleanup { + includePattern('**/target/**') + deleteDirectories() + cleanupParameter('CLEANUP') + } + } + }"""); PreBuildCleanup pbc = getJob("examplePreBuildCleanup").getBuildWrappersList().get(PreBuildCleanup.class); @@ -156,4 +159,13 @@ public void examplePreBuildCleanup() throws Exception { assertEquals(Pattern.PatternType.INCLUDE, ex.getType()); assertEquals("**/target/**", ex.getPattern()); } + + private static void applyScript(String s) throws java.io.IOException { + JenkinsJobManagement jjm = new JenkinsJobManagement(System.out, Collections.emptyMap(), new File(".")); + new DslScriptLoader(jjm).runScript(s); + } + + private static FreeStyleProject getJob(String emptyCleanWs) { + return j.jenkins.getItem(emptyCleanWs, j.jenkins, FreeStyleProject.class); + } } diff --git a/src/test/java/hudson/plugins/ws_cleanup/PreBuildCleanupTest.java b/src/test/java/hudson/plugins/ws_cleanup/PreBuildCleanupTest.java index ba6e2e5..1e09587 100644 --- a/src/test/java/hudson/plugins/ws_cleanup/PreBuildCleanupTest.java +++ b/src/test/java/hudson/plugins/ws_cleanup/PreBuildCleanupTest.java @@ -4,9 +4,9 @@ */ package hudson.plugins.ws_cleanup; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import hudson.Functions; import hudson.model.FreeStyleBuild; @@ -18,34 +18,40 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -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 lucinka */ -public class PreBuildCleanupTest { +@WithJenkins +class PreBuildCleanupTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + + @BeforeEach + void setUp(JenkinsRule rule) { + j = rule; + } @Test @LocalData - public void testCleanAllWorkspace() throws Exception { + void testCleanAllWorkspace() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); PreBuildCleanup cleanup = new PreBuildCleanup(new ArrayList<>(), false, null, null, false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); } @Test @LocalData - public void testCleanWorkspaceByDeleteCommandIncludeDirectories() throws Exception { + void testCleanWorkspaceByDeleteCommandIncludeDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -53,12 +59,12 @@ public void testCleanWorkspaceByDeleteCommandIncludeDirectories() throws Excepti new ArrayList<>(), true, null, Functions.isWindows() ? "cmd /c rd /s /q %s" : "rm -rf %s", false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File delete-me in workspace should not exists.", new File(workspace, "delete-me").exists()); + assertFalse(new File(workspace, "delete-me").exists(), "File delete-me in workspace should not exists."); } @Test @LocalData - public void testCleanWorkspaceByDeleteCommandInNodePropertyIncludeDirectories() throws Exception { + void testCleanWorkspaceByDeleteCommandInNodePropertyIncludeDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); Slave slave = j.createOnlineSlave(); project.setAssignedLabel(slave.getSelfLabel()); @@ -70,12 +76,12 @@ public void testCleanWorkspaceByDeleteCommandInNodePropertyIncludeDirectories() PreBuildCleanup cleanup = new PreBuildCleanup(new ArrayList<>(), true, null, "delete", false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File delete-me in workspace should not exists.", new File(workspace, "delete-me").exists()); + assertFalse(new File(workspace, "delete-me").exists(), "File delete-me in workspace should not exists."); } @Test @LocalData - public void testCleanWorkspaceByDeleteCommandWithoutDirectory() throws Exception { + void testCleanWorkspaceByDeleteCommandWithoutDirectory() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -83,14 +89,14 @@ public void testCleanWorkspaceByDeleteCommandWithoutDirectory() throws Exception new ArrayList<>(), false, null, Functions.isWindows() ? "cmd /c del %s" : "rm -rf %s", false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File file1.txt in workspace should not exists.", new File(workspace, "file1.txt").exists()); - assertFalse("File file2.txt in workspace should not exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File not-delete-me in workspace should exists.", new File(workspace, "not-delete-me").exists()); + assertFalse(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should not exists."); + assertFalse(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should not exists."); + assertTrue(new File(workspace, "not-delete-me").exists(), "File not-delete-me in workspace should exists."); } @Test @LocalData - public void testCleanWorkspaceByDeleteCommandInNodePropertyWithoutDirectory() throws Exception { + void testCleanWorkspaceByDeleteCommandInNodePropertyWithoutDirectory() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); Slave slave = j.createOnlineSlave(); project.setAssignedLabel(slave.getSelfLabel()); @@ -102,14 +108,14 @@ public void testCleanWorkspaceByDeleteCommandInNodePropertyWithoutDirectory() th PreBuildCleanup cleanup = new PreBuildCleanup(new ArrayList<>(), false, null, "delete", false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File file1.txt in workspace should not exists.", new File(workspace, "file1.txt").exists()); - assertFalse("File file2.txt in workspace should not exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File not-delete-me in workspace should exists.", new File(workspace, "not-delete-me").exists()); + assertFalse(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should not exists."); + assertFalse(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should not exists."); + assertTrue(new File(workspace, "not-delete-me").exists(), "File not-delete-me in workspace should exists."); } @Test @LocalData - public void testCleanWorkspaceByDeleteCommandWithPattern() throws Exception { + void testCleanWorkspaceByDeleteCommandWithPattern() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -120,26 +126,26 @@ public void testCleanWorkspaceByDeleteCommandWithPattern() throws Exception { project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); assertFalse( - "File file1.txt in workspace should not exists.", - new File(workspace.getAbsolutePath() + "/delete-me/file1.txt").exists()); + new File(workspace.getAbsolutePath() + "/delete-me/file1.txt").exists(), + "File file1.txt in workspace should not exists."); assertFalse( - "File file2.txt in workspace should exists.", - new File(workspace.getAbsolutePath() + "/delete-me/file2.txt").exists()); + new File(workspace.getAbsolutePath() + "/delete-me/file2.txt").exists(), + "File file2.txt in workspace should exists."); assertTrue( - "File exclude1.txt in workspace should exists.", - new File(workspace.getAbsolutePath() + "/delete-me/exclude1.txt").exists()); + new File(workspace.getAbsolutePath() + "/delete-me/exclude1.txt").exists(), + "File exclude1.txt in workspace should exists."); assertTrue( - "File exclude2.txt in workspace should exists.", - new File(workspace.getAbsolutePath() + "/delete-me/exclude2.txt").exists()); - assertTrue("File file1.txt in workspace should exists.", new File(workspace, "file1.txt").exists()); - assertTrue("File file2.txt in workspace should exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File exclude1.txt in workspace should exists.", new File(workspace, "exclude1.txt").exists()); - assertTrue("File exclude2.txt in workspace should exists.", new File(workspace, "exclude2.txt").exists()); + new File(workspace.getAbsolutePath() + "/delete-me/exclude2.txt").exists(), + "File exclude2.txt in workspace should exists."); + assertTrue(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should exists."); + assertTrue(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should exists."); + assertTrue(new File(workspace, "exclude1.txt").exists(), "File exclude1.txt in workspace should exists."); + assertTrue(new File(workspace, "exclude2.txt").exists(), "File exclude2.txt in workspace should exists."); } @Test @LocalData - public void testCleanWorkspaceByPatternIncludeWithoutDirectories() throws Exception { + void testCleanWorkspaceByPatternIncludeWithoutDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -148,14 +154,14 @@ public void testCleanWorkspaceByPatternIncludeWithoutDirectories() throws Except PreBuildCleanup cleanup = new PreBuildCleanup(patterns, false, null, null, false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File file1.txt in workspace should not exists.", new File(workspace, "file1.txt").exists()); - assertTrue("File file2.txt in workspace should exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File not-delete-me in workspace should exists.", new File(workspace, "not-delete-me").exists()); + assertFalse(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should not exists."); + assertTrue(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should exists."); + assertTrue(new File(workspace, "not-delete-me").exists(), "File not-delete-me in workspace should exists."); } @Test @LocalData - public void testCleanWorkspaceByPatternExcludeWithoutDirectories() throws Exception { + void testCleanWorkspaceByPatternExcludeWithoutDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -164,14 +170,14 @@ public void testCleanWorkspaceByPatternExcludeWithoutDirectories() throws Except PreBuildCleanup cleanup = new PreBuildCleanup(patterns, false, null, null, false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertTrue("File file1.txt in workspace should exists.", new File(workspace, "file1.txt").exists()); - assertFalse("File file2.txt in workspace should not exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File not-delete-me in workspace should exists.", new File(workspace, "not-delete-me").exists()); + assertTrue(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should exists."); + assertFalse(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should not exists."); + assertTrue(new File(workspace, "not-delete-me").exists(), "File not-delete-me in workspace should exists."); } @Test @LocalData - public void testCleanWorkspaceByPatternWithDirectories() throws Exception { + void testCleanWorkspaceByPatternWithDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -180,14 +186,14 @@ public void testCleanWorkspaceByPatternWithDirectories() throws Exception { PreBuildCleanup cleanup = new PreBuildCleanup(patterns, true, null, null, false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File file1.txt in workspace should exists.", new File(workspace, "file1.txt").exists()); - assertFalse("File file2.txt in workspace should exists.", new File(workspace, "file2.txt").exists()); - assertFalse("File delete-me in workspace should not exists.", new File(workspace, "delete-me").exists()); + assertFalse(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should exists."); + assertFalse(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should exists."); + assertFalse(new File(workspace, "delete-me").exists(), "File delete-me in workspace should not exists."); } @Test @LocalData - public void testCleanWorkspaceByPatternWithoutDirectories() throws Exception { + void testCleanWorkspaceByPatternWithoutDirectories() throws Exception { FreeStyleProject project = j.createFreeStyleProject("project1"); File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); project.setCustomWorkspace(workspace.getAbsolutePath()); @@ -196,14 +202,14 @@ public void testCleanWorkspaceByPatternWithoutDirectories() throws Exception { PreBuildCleanup cleanup = new PreBuildCleanup(patterns, false, null, null, false); project.getBuildWrappersList().add(cleanup); j.buildAndAssertSuccess(project); - assertFalse("File file1.txt in workspace should not exists.", new File(workspace, "file1.txt").exists()); - assertFalse("File file2.txt in workspace should not exists.", new File(workspace, "file2.txt").exists()); - assertTrue("File delete-me in workspace should exists.", new File(workspace, "delete-me").exists()); + assertFalse(new File(workspace, "file1.txt").exists(), "File file1.txt in workspace should not exists."); + assertFalse(new File(workspace, "file2.txt").exists(), "File file2.txt in workspace should not exists."); + assertTrue(new File(workspace, "delete-me").exists(), "File delete-me in workspace should exists."); } @Test @LocalData - public void disableDeferredWipeout() throws Exception { + void disableDeferredWipeout() throws Exception { File workspace = new File(j.jenkins.getRootDir().getAbsolutePath(), "workspace"); // Deferred wipeout enabled @@ -211,7 +217,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, true)); FreeStyleBuild build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is disabled by the job configuration...", build); // Deferred wipeout disabled @@ -219,7 +225,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, false)); build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is used...", build); // Deferred wipeout default setting @@ -227,7 +233,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, false)); build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is used...", build); // Attach a DisableDeferredWipeout node property to the master node @@ -238,7 +244,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, true)); build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is disabled by the job configuration...", build); // Deferred wipeout disabled @@ -246,7 +252,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, false)); build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is disabled by the node property...", build); // Deferred wipeout default setting @@ -254,7 +260,7 @@ public void disableDeferredWipeout() throws Exception { project.setCustomWorkspace(workspace.getAbsolutePath()); project.getBuildWrappersList().add(new PreBuildCleanup(new ArrayList<>(), false, null, null, false)); build = j.buildAndAssertSuccess(project); - assertEquals("Workspace should not contains any file.", 0, workspace.listFiles().length); + assertEquals(0, workspace.listFiles().length, "Workspace should not contains any file."); j.assertLogContains("Deferred wipeout is disabled by the node property...", build); } }