Skip to content

Commit

Permalink
Merge pull request #114 from ivamly/tests
Browse files Browse the repository at this point in the history
Add Tests for FileRenameOperation
  • Loading branch information
jonesbusy authored Jul 28, 2024
2 parents 3521baf + 113f3b5 commit 56f0607
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/test/java/sp/sd/fileoperations/FileRenameOperationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package sp.sd.fileoperations;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;

import static org.junit.Assert.*;

public class FileRenameOperationTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void testDefaults() {
String source = "source.txt";
String destination = "destination.txt";
FileRenameOperation operation = new FileRenameOperation(source, destination);

assertEquals(source, operation.getSource());
assertEquals(destination, operation.getDestination());
}

@Test
public void testRunFileRenameOperation() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTest");

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath sourceFile = new FilePath(workspace, "source.txt");
FilePath destinationFile = new FilePath(workspace, "destination.txt");

sourceFile.write("Sample content", "UTF-8");

FileRenameOperation operation = new FileRenameOperation("source.txt", "destination.txt");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

assertFalse("The source file should have been renamed", sourceFile.exists());
assertTrue("The destination file should exist", destinationFile.exists());
}

@Test
public void testRunFileRenameOperationWithTokens() throws Exception {
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
EnvVars envVars = prop.getEnvVars();
envVars.put("SOURCE_FILE", "source.txt");
envVars.put("DESTINATION_FILE", "destination.txt");
jenkins.jenkins.getGlobalNodeProperties().add(prop);

FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTestWithTokens");

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath sourceFile = new FilePath(workspace, "source.txt");
FilePath destinationFile = new FilePath(workspace, "destination.txt");

sourceFile.write("Sample content", "UTF-8");

FileRenameOperation operation = new FileRenameOperation("$SOURCE_FILE", "$DESTINATION_FILE");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

assertFalse("The source file should have been renamed", sourceFile.exists());
assertTrue("The destination file should exist", destinationFile.exists());
}
}

0 comments on commit 56f0607

Please sign in to comment.