|
| 1 | +package sp.sd.fileoperations; |
| 2 | + |
| 3 | +import hudson.EnvVars; |
| 4 | +import hudson.FilePath; |
| 5 | +import hudson.model.FreeStyleBuild; |
| 6 | +import hudson.model.FreeStyleProject; |
| 7 | +import hudson.model.Result; |
| 8 | +import hudson.slaves.EnvironmentVariablesNodeProperty; |
| 9 | +import org.junit.Rule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.jvnet.hudson.test.JenkinsRule; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +public class FileRenameOperationTest { |
| 18 | + |
| 19 | + @Rule |
| 20 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testDefaults() { |
| 24 | + String source = "source.txt"; |
| 25 | + String destination = "destination.txt"; |
| 26 | + FileRenameOperation operation = new FileRenameOperation(source, destination); |
| 27 | + |
| 28 | + assertEquals(source, operation.getSource()); |
| 29 | + assertEquals(destination, operation.getDestination()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testRunFileRenameOperation() throws Exception { |
| 34 | + FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTest"); |
| 35 | + |
| 36 | + FilePath workspace = jenkins.jenkins.getWorkspaceFor(project); |
| 37 | + FilePath sourceFile = new FilePath(workspace, "source.txt"); |
| 38 | + FilePath destinationFile = new FilePath(workspace, "destination.txt"); |
| 39 | + |
| 40 | + sourceFile.write("Sample content", "UTF-8"); |
| 41 | + |
| 42 | + FileRenameOperation operation = new FileRenameOperation("source.txt", "destination.txt"); |
| 43 | + project.getBuildersList().add(new FileOperationsBuilder(List.of(operation))); |
| 44 | + |
| 45 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 46 | + assertEquals(Result.SUCCESS, build.getResult()); |
| 47 | + |
| 48 | + assertFalse("The source file should have been renamed", sourceFile.exists()); |
| 49 | + assertTrue("The destination file should exist", destinationFile.exists()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testRunFileRenameOperationWithTokens() throws Exception { |
| 54 | + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); |
| 55 | + EnvVars envVars = prop.getEnvVars(); |
| 56 | + envVars.put("SOURCE_FILE", "source.txt"); |
| 57 | + envVars.put("DESTINATION_FILE", "destination.txt"); |
| 58 | + jenkins.jenkins.getGlobalNodeProperties().add(prop); |
| 59 | + |
| 60 | + FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTestWithTokens"); |
| 61 | + |
| 62 | + FilePath workspace = jenkins.jenkins.getWorkspaceFor(project); |
| 63 | + FilePath sourceFile = new FilePath(workspace, "source.txt"); |
| 64 | + FilePath destinationFile = new FilePath(workspace, "destination.txt"); |
| 65 | + |
| 66 | + sourceFile.write("Sample content", "UTF-8"); |
| 67 | + |
| 68 | + FileRenameOperation operation = new FileRenameOperation("$SOURCE_FILE", "$DESTINATION_FILE"); |
| 69 | + project.getBuildersList().add(new FileOperationsBuilder(List.of(operation))); |
| 70 | + |
| 71 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 72 | + assertEquals(Result.SUCCESS, build.getResult()); |
| 73 | + |
| 74 | + assertFalse("The source file should have been renamed", sourceFile.exists()); |
| 75 | + assertTrue("The destination file should exist", destinationFile.exists()); |
| 76 | + } |
| 77 | +} |
0 commit comments