fileOperations = new ArrayList<>();
26 |
27 | public void fileCreateOperation(String fileName, String fileContent) {
28 | FileCreateOperation fileCreateOperation = new FileCreateOperation(fileName, fileContent);
29 | fileOperations.add(fileCreateOperation);
30 | }
31 |
32 | public void fileCopyOperation(String includes,
33 | String excludes,
34 | String targetLocation,
35 | boolean flattenFiles,
36 | boolean renameFiles,
37 | String sourceCaptureExpression,
38 | String targetNameExpression,
39 | boolean useDefaultExcludes) {
40 | FileCopyOperation fileCopyOperation = new FileCopyOperation(includes,
41 | excludes,
42 | targetLocation,
43 | flattenFiles,
44 | renameFiles,
45 | sourceCaptureExpression,
46 | targetNameExpression);
47 | fileCopyOperation.setUseDefaultExcludes(useDefaultExcludes);
48 | fileOperations.add(fileCopyOperation);
49 | }
50 |
51 | public void fileCopyOperation(String includes,
52 | String excludes,
53 | String targetLocation,
54 | boolean flattenFiles,
55 | boolean renameFiles,
56 | String sourceCaptureExpression,
57 | String targetNameExpression) {
58 | fileCopyOperation(includes,
59 | excludes,
60 | targetLocation,
61 | flattenFiles,
62 | renameFiles,
63 | sourceCaptureExpression,
64 | targetNameExpression,
65 | true);
66 | }
67 |
68 | public void fileDeleteOperation(String includes, String excludes, boolean useDefaultExcludes) {
69 | FileDeleteOperation fileDeleteOperation = new FileDeleteOperation(includes, excludes);
70 | fileDeleteOperation.setUseDefaultExcludes(useDefaultExcludes);
71 | fileOperations.add(fileDeleteOperation);
72 | }
73 |
74 | public void fileDeleteOperation(String includes, String excludes) {
75 | fileDeleteOperation(includes, excludes, true);
76 | }
77 |
78 | public void fileDownloadOperation(String url, String userName, String password, String targetLocation, String targetFileName, String proxyHost, String proxyPort) {
79 | FileDownloadOperation fileDownloadOperation = new FileDownloadOperation(url, userName, password, targetLocation, targetFileName, proxyHost, proxyPort);
80 | fileOperations.add(fileDownloadOperation);
81 | }
82 |
83 | public void fileJoinOperation(String sourceFile, String targetFile) {
84 | FileJoinOperation fileJoinOperation = new FileJoinOperation(sourceFile, targetFile);
85 | fileOperations.add(fileJoinOperation);
86 | }
87 |
88 | public void filePropertiesToJsonOperation(String sourceFile, String targetFile) {
89 | FilePropertiesToJsonOperation filePropertiesToJsonOperation = new FilePropertiesToJsonOperation(sourceFile, targetFile);
90 | fileOperations.add(filePropertiesToJsonOperation);
91 | }
92 |
93 | public void fileTransformOperation(String includes, String excludes, boolean useDefaultExcludes) {
94 | FileTransformOperation fileTransformOperation = new FileTransformOperation(includes, excludes);
95 | fileTransformOperation.setUseDefaultExcludes(useDefaultExcludes);
96 | fileOperations.add(fileTransformOperation);
97 | }
98 |
99 | public void fileTransformOperation(String includes, String excludes) {
100 | fileTransformOperation(includes, excludes, true);
101 | }
102 |
103 | public void fileUnTarOperation(String filePath, String targetLocation, boolean isGZIP) {
104 | FileUnTarOperation fileUnTarOperation = new FileUnTarOperation(filePath, targetLocation, isGZIP);
105 | fileOperations.add(fileUnTarOperation);
106 | }
107 |
108 | public void fileUnZipOperation(String filePath, String targetLocation) {
109 | FileUnZipOperation fileUnZipOperation = new FileUnZipOperation(filePath, targetLocation);
110 | fileOperations.add(fileUnZipOperation);
111 | }
112 |
113 | public void fileZipOperation(String folderPath, String outputFolderPath) {
114 | FileZipOperation fileZipOperation = new FileZipOperation(folderPath, outputFolderPath);
115 | fileOperations.add(fileZipOperation);
116 | }
117 |
118 | public void folderCopyOperation(String sourceFolderPath, String destinationFolderPath) {
119 | FolderCopyOperation folderCopyOperation = new FolderCopyOperation(sourceFolderPath, destinationFolderPath);
120 | fileOperations.add(folderCopyOperation);
121 | }
122 |
123 | public void folderCreateOperation(String folderPath) {
124 | FolderCreateOperation folderCreateOperation = new FolderCreateOperation(folderPath);
125 | fileOperations.add(folderCreateOperation);
126 | }
127 |
128 | public void folderDeleteOperation(String folderPath) {
129 | FolderDeleteOperation folderDeleteOperation = new FolderDeleteOperation(folderPath);
130 | fileOperations.add(folderDeleteOperation);
131 | }
132 |
133 | public void fileRenameOperation(String source, String destination) {
134 | FileRenameOperation fileRenameOperation = new FileRenameOperation(source, destination);
135 | fileOperations.add(fileRenameOperation);
136 | }
137 |
138 | public void folderRenameOperation(String source, String destination) {
139 | FolderRenameOperation folderRenameOperation = new FolderRenameOperation(source, destination);
140 | fileOperations.add(folderRenameOperation);
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/main/java/sp/sd/fileoperations/dsl/FileOperationsJobDslExtension.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations.dsl;
2 |
3 | import hudson.Extension;
4 | import javaposse.jobdsl.dsl.RequiresPlugin;
5 | import javaposse.jobdsl.dsl.helpers.step.StepContext;
6 | import javaposse.jobdsl.plugin.ContextExtensionPoint;
7 | import javaposse.jobdsl.plugin.DslExtensionMethod;
8 | import sp.sd.fileoperations.FileOperationsBuilder;
9 |
10 | @Extension(optional = true)
11 | public class FileOperationsJobDslExtension extends ContextExtensionPoint {
12 | @RequiresPlugin(id = "file-operations", minimumVersion = "1.2")
13 | @DslExtensionMethod(context = StepContext.class)
14 | public Object fileOperations(Runnable closure) {
15 | FileOperationsJobDslContext context = new FileOperationsJobDslContext();
16 | executeInContext(closure, context);
17 | return new FileOperationsBuilder(context.fileOperations);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/resources/index.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 | This plugin's main goal is to provide File Operations as Build Step.
4 |
5 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-excludes.html:
--------------------------------------------------------------------------------
1 |
2 | Files excluded from copying, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-flattenFiles.html:
--------------------------------------------------------------------------------
1 |
2 | If selected, files are copied directly to the target location without preserving
3 | source file sub-directory structure.
4 |
5 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-includes.html:
--------------------------------------------------------------------------------
1 |
2 | Files included to copy, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-renameFiles.html:
--------------------------------------------------------------------------------
1 |
2 | By default, the file name of the source file is preserved. When flattening
3 | files, this can cause problems if files of the same name exist in multiple
4 | source sub-directories. Selecting this option allows the output file name
5 | to be manipulated to avoid file name clashes. Only used for setting flattenFiles: true.
6 |
7 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-sourceCaptureExpression.html:
--------------------------------------------------------------------------------
1 |
2 | Java-style regular expression that is run against the workspace relative path of each
3 | matching source file. This should be used to capture parts of the path that
4 | will be used in the target name expression to make each file name unique
5 | across all subdirectories.
6 | If path not match the regex the file is copied directly to the target location.
7 | If the includes path is not handled within the regex the sub-directory structure will be preserved.
8 |
9 | Example:
10 |
11 |
12 | // folllowing structure:
13 | // dir1/info-app.txt
14 | // dir1/error-app.txt
15 | fileOperations([fileCopyOperation(
16 | includes: '**/dir1/*.txt',
17 | targetLocation: 'logs/',
18 | flattenFiles: true,
19 | renameFiles: true,
20 | sourceCaptureExpression: 'dir1/(.*)-app\\.txt$',
21 | targetNameExpression: '$1.log')
22 | ])
23 |
24 |
25 | will result in:
26 |
27 |
28 | logs/info.log
29 | logs/error.log
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-targetLocation.html:
--------------------------------------------------------------------------------
1 |
2 | Destination folder location to copy the files. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCopyOperation/help-targetNameExpression.html:
--------------------------------------------------------------------------------
1 |
2 | An expression that provides the desired target file name. This can reference
3 | variables captured in the source capture expression by using $1, $2 etc.
4 |
5 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCreateOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCreateOperation/help-fileContent.html:
--------------------------------------------------------------------------------
1 |
2 | File content to be created, use environment variables where needed.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileCreateOperation/help-fileName.html:
--------------------------------------------------------------------------------
1 |
2 | Path and Name of the file to be created in workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDeleteOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDeleteOperation/help-excludes.html:
--------------------------------------------------------------------------------
1 |
2 | Files excluded from deleting, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDeleteOperation/help-includes.html:
--------------------------------------------------------------------------------
1 |
2 | Files included to delete, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDownloadOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDownloadOperation/help-targetLocation.html:
--------------------------------------------------------------------------------
1 |
2 | Destination location to download the file. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileDownloadOperation/help-url.html:
--------------------------------------------------------------------------------
1 |
2 | Url of the file to download.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileJoinOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileJoinOperation/help-sourceFile.html:
--------------------------------------------------------------------------------
1 |
2 | Source file path to copy the content.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileJoinOperation/help-targetFile.html:
--------------------------------------------------------------------------------
1 |
2 | Target file path to append the content from source file.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileOperationsBuilder/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FilePropertiesToJsonOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FilePropertiesToJsonOperation/help-sourceFile.html:
--------------------------------------------------------------------------------
1 |
2 | Source file path of properties.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FilePropertiesToJsonOperation/help-targetFile.html:
--------------------------------------------------------------------------------
1 |
2 | Target file path to create or update with json data.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileRenameOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileRenameOperation/help-destination.html:
--------------------------------------------------------------------------------
1 |
2 | Destination file location to rename. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileRenameOperation/help-source.html:
--------------------------------------------------------------------------------
1 |
2 | File to be renamed.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileTransformOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileTransformOperation/help-excludes.html:
--------------------------------------------------------------------------------
1 |
2 | Files excluded from copying, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileTransformOperation/help-includes.html:
--------------------------------------------------------------------------------
1 |
2 | Files included to copy, this supports ant-style file pattern ex: target/*/final*.xml
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnTarOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnTarOperation/help-filePath.html:
--------------------------------------------------------------------------------
1 |
2 | Source tar file location.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnTarOperation/help-targetLocation.html:
--------------------------------------------------------------------------------
1 |
2 | Destination folder location to untar the files. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnZipOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnZipOperation/help-filePath.html:
--------------------------------------------------------------------------------
1 |
2 | Source zip file location.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileUnZipOperation/help-targetLocation.html:
--------------------------------------------------------------------------------
1 |
2 | Destination folder location to unzip the files. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileZipOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileZipOperation/help-folderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Path of the file or folder to create a zip file for, relative to the workspace directory.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FileZipOperation/help-outputFolderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Path to a target directory for the zip file, relative to the workspace directory.
3 | Defaults to workspace directory if not defined.
4 |
5 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderCopyOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderCopyOperation/help-destinationFolderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Destination folder location to copy the files. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderCopyOperation/help-sourceFolderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Folder to be copied.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderCreateOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderCreateOperation/help-folderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Path and Name of the folder to be created in workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderDeleteOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderDeleteOperation/help-folderPath.html:
--------------------------------------------------------------------------------
1 |
2 | Path and Name of the folder to be deleted in workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderRenameOperation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderRenameOperation/help-destination.html:
--------------------------------------------------------------------------------
1 |
2 | Destination folder name to rename. Base directory is workspace.
3 |
4 |
--------------------------------------------------------------------------------
/src/main/resources/sp/sd/fileoperations/FolderRenameOperation/help-source.html:
--------------------------------------------------------------------------------
1 |
2 | Folder to be renamed.
3 |
4 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileCopyOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.*;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Arrays;
7 | import java.util.List;
8 |
9 | import com.thoughtworks.xstream.XStream;
10 | import com.thoughtworks.xstream.security.AnyTypePermission;
11 | import org.junit.jupiter.api.BeforeEach;
12 | import org.junit.jupiter.api.Test;
13 | import org.jvnet.hudson.test.JenkinsRule;
14 | import org.jvnet.hudson.test.WithoutJenkins;
15 |
16 | import hudson.model.FreeStyleBuild;
17 | import hudson.model.FreeStyleProject;
18 | import hudson.model.Result;
19 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
20 |
21 | @WithJenkins
22 | class FileCopyOperationTest {
23 |
24 | private JenkinsRule jenkins;
25 |
26 | @BeforeEach
27 | void setUp(JenkinsRule rule) {
28 | jenkins = rule;
29 | }
30 |
31 | @Test
32 | @WithoutJenkins
33 | void testDefaults() {
34 | FileCopyOperation fco = new FileCopyOperation("**/*.config", "**/*.xml", "target", true, false, null, null);
35 | assertEquals("**/*.config", fco.getIncludes());
36 | assertEquals("**/*.xml", fco.getExcludes());
37 | assertEquals("target", fco.getTargetLocation());
38 | assertTrue(fco.getFlattenFiles());
39 | assertFalse(fco.getRenameFiles());
40 | assertNull(fco.getSourceCaptureExpression());
41 | assertNull(fco.getTargetNameExpression());
42 | assertTrue(fco.getUseDefaultExcludes());
43 | }
44 |
45 | @Test
46 | void testRunFileOperationWithFileOperationBuildStepNoFlatten() throws Exception {
47 | // Given
48 | List fop = new ArrayList<>();
49 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classA/TestA.xml", ""));
50 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classB/TestB.xml", ""));
51 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classB/TestB.xml", ""));
52 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classC/TestC.xml", ""));
53 | fop.add(new FileCopyOperation("test-results-xml/**/*.xml", "", "test-results", false, false, null, null));
54 |
55 | // When
56 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
57 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
58 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
59 |
60 | // Then
61 | assertEquals(Result.SUCCESS, build.getResult());
62 | assertTrue(build.getWorkspace().child("test-results/test-results-xml/pod-0/classA/TestA.xml").exists());
63 | assertTrue(build.getWorkspace().child("test-results/test-results-xml/pod-0/classB/TestB.xml").exists());
64 | assertTrue(build.getWorkspace().child("test-results/test-results-xml/pod-1/classB/TestB.xml").exists());
65 | assertTrue(build.getWorkspace().child("test-results/test-results-xml/pod-1/classC/TestC.xml").exists());
66 | }
67 |
68 |
69 | @Test
70 | void testRunFileOperationWithFileOperationBuildStepWithFlatten() throws Exception {
71 | // Given
72 | List fop = new ArrayList<>();
73 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classA/TestA.xml", ""));
74 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classB/TestB.xml", ""));
75 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classB/TestB.xml", ""));
76 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classC/TestC.xml", ""));
77 | fop.add(new FileCopyOperation("test-results-xml/**/*.xml", "", "test-results", true, false, null, null));
78 |
79 | // When
80 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
81 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
82 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
83 |
84 | // Then
85 | assertEquals(Result.SUCCESS, build.getResult());
86 | assertTrue(build.getWorkspace().child("test-results/TestA.xml").exists());
87 | assertTrue(build.getWorkspace().child("test-results/TestB.xml").exists());
88 | assertTrue(build.getWorkspace().child("test-results/TestC.xml").exists());
89 | }
90 |
91 | @Test
92 | void testRunFileOperationWithFileOperationBuildStepWithFlattenAndRename() throws Exception {
93 | // Required to handle test being run on either Windows or Unix systems
94 | String dirSep = "(?:\\\\|/)";
95 |
96 | // Given
97 | List fop = new ArrayList<>();
98 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classA/TestA.xml", ""));
99 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classB/TestB.xml", ""));
100 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classB/TestB.xml", ""));
101 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classC/TestC.xml", ""));
102 | fop.add(new FileCopyOperation(
103 | "test-results-xml/**/*.xml",
104 | "",
105 | "test-results",
106 | true,
107 | true,
108 | ".*" + dirSep + "test-results-xml" + dirSep + ".*-([\\d]+)" +
109 | dirSep + ".*" + dirSep + "([^" + dirSep + "]+)$",
110 | "$1-$2"));
111 |
112 | // When
113 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
114 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
115 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
116 |
117 | // Then
118 | assertEquals(Result.SUCCESS, build.getResult());
119 | assertTrue(build.getWorkspace().child("test-results/0-TestA.xml").exists());
120 | assertTrue(build.getWorkspace().child("test-results/0-TestB.xml").exists());
121 | assertTrue(build.getWorkspace().child("test-results/1-TestB.xml").exists());
122 | assertTrue(build.getWorkspace().child("test-results/1-TestC.xml").exists());
123 | }
124 |
125 | /**
126 | * @see "help-sourceCaptureExpression.html"
127 | */
128 | @Test
129 | void testFileCopyOperationForSourceCaptureExpressionExample() throws Exception {
130 | // Required to handle test being run on either Windows or Unix systems
131 | String dirSep = "(?:\\\\|/)";
132 |
133 | // Given
134 | List fop = new ArrayList<>();
135 | fop.add(new FileCreateOperation("dir1/info-app.txt", ""));
136 | fop.add(new FileCreateOperation("dir1/error-app.txt", ""));
137 | fop.add(new FileCopyOperation(
138 | "**/dir1/*.txt",
139 | "",
140 | "logs",
141 | true,
142 | true,
143 | "dir1" + dirSep + "(.*)-app\\.txt$",
144 | "$1.log"));
145 |
146 | // When
147 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
148 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
149 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
150 |
151 | // Then
152 | assertEquals(Result.SUCCESS, build.getResult());
153 | assertTrue(build.getWorkspace().child("logs/info.log").exists());
154 | assertTrue(build.getWorkspace().child("logs/error.log").exists());
155 | }
156 |
157 | /**
158 | * Files will not be flatten because includes path is not included in sourceCaptureExpression.
159 | *
160 | * @see "https://github.com/jenkinsci/file-operations-plugin/issues/101"
161 | */
162 | @Test
163 | void testFileCopyOperationWithFlattenAndRenameFileWithoutMatchingRegex() throws Exception {
164 | // Given
165 | List fop = new ArrayList<>();
166 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classA/TestA.xml", ""));
167 | fop.add(new FileCreateOperation("test-results-xml/pod-0/classA/Test-rename-A.xml", ""));
168 | fop.add(new FileCreateOperation("test-results-xml/pod-1/classB/TestB.xml", ""));
169 | fop.add(new FileCopyOperation(
170 | "test-results-xml/**/*.xml",
171 | "",
172 | "test-results",
173 | true,
174 | true,
175 | ".*Test-rename-(.*)\\.xml$",
176 | "Test$1.log"));
177 |
178 | // When
179 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
180 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
181 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
182 |
183 | // Then
184 | assertEquals(Result.SUCCESS, build.getResult());
185 | assertTrue(build.getWorkspace().child("test-results/TestA.xml").exists());
186 | assertTrue(build.getWorkspace().child("test-results/TestA.log").exists());
187 | assertTrue(build.getWorkspace().child("test-results/TestB.xml").exists());
188 | }
189 |
190 | @Test
191 | void testRunFileOperationWithFileOperationBuildStepWithDefaultExcludes() throws Exception {
192 | // Given
193 | FileCreateOperation fileCreateOperation = new FileCreateOperation(".gitignore", "");
194 | FileCopyOperation fileCopyOperation = new FileCopyOperation(".gitignore", "", "output", false, false, null, null);
195 | List fop = Arrays.asList(fileCreateOperation, fileCopyOperation);
196 |
197 | // When
198 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
199 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
200 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
201 |
202 | // Then
203 | assertEquals(Result.SUCCESS, build.getResult());
204 | assertTrue(build.getWorkspace().child(".gitignore").exists());
205 | assertFalse(build.getWorkspace().child("output/.gitignore").exists());
206 | }
207 |
208 | @Test
209 | void testRunFileOperationWithFileOperationBuildStepWithoutDefaultExcludes() throws Exception {
210 | // Given
211 | FileCreateOperation fileCreateOperation = new FileCreateOperation(".gitignore", "");
212 | FileCopyOperation fileCopyOperation = new FileCopyOperation(".gitignore", "", "output", false, false, null, null);
213 | fileCopyOperation.setUseDefaultExcludes(false);
214 | List fop = Arrays.asList(fileCreateOperation, fileCopyOperation);
215 |
216 | // When
217 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
218 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
219 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
220 |
221 | // Then
222 | assertEquals(Result.SUCCESS, build.getResult());
223 | assertTrue(build.getWorkspace().child(".gitignore").exists());
224 | assertTrue(build.getWorkspace().child("output/.gitignore").exists());
225 | }
226 |
227 | @Test
228 | @WithoutJenkins
229 | void testSerializeWithXStream() {
230 | // Given
231 | FileCopyOperation originalObject = new FileCopyOperation("include", "exclude", "output", false, false, null, null);
232 | originalObject.setUseDefaultExcludes(false);
233 |
234 | // When
235 | XStream xstream = new XStream();
236 | xstream.addPermission(AnyTypePermission.ANY);
237 | String serializedObjectXml = xstream.toXML(originalObject);
238 | FileCopyOperation deserializedObject = (FileCopyOperation)xstream.fromXML(serializedObjectXml);
239 |
240 | // Then
241 | assertEquals(originalObject.getIncludes(), deserializedObject.getIncludes());
242 | assertEquals(originalObject.getExcludes(), deserializedObject.getExcludes());
243 | assertEquals(originalObject.getTargetLocation(), deserializedObject.getTargetLocation());
244 | assertEquals(originalObject.getFlattenFiles(), deserializedObject.getFlattenFiles());
245 | assertEquals(originalObject.getRenameFiles(), deserializedObject.getRenameFiles());
246 | assertEquals(originalObject.getSourceCaptureExpression(), deserializedObject.getSourceCaptureExpression());
247 | assertEquals(originalObject.getTargetNameExpression(), deserializedObject.getTargetNameExpression());
248 | assertEquals(originalObject.getUseDefaultExcludes(), deserializedObject.getUseDefaultExcludes());
249 | }
250 |
251 | @Test
252 | @WithoutJenkins
253 | void testSerializeWithXStreamBackwardsCompatibility() {
254 | // Given
255 | String serializedObjectXml =
256 | "" +
257 | " include" +
258 | " exclude" +
259 | " output" +
260 | " false" +
261 | " false" +
262 | "";
263 |
264 | // When
265 | XStream xstream = new XStream();
266 | xstream.alias("FileCopyOperation", FileCopyOperation.class);
267 | xstream.addPermission(AnyTypePermission.ANY);
268 | FileCopyOperation deserializedObject = (FileCopyOperation)xstream.fromXML(serializedObjectXml);
269 |
270 | // Then
271 | assertTrue(deserializedObject.getUseDefaultExcludes());
272 | }
273 | }
274 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileCreateOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 | import static org.junit.jupiter.api.Assertions.assertTrue;
5 |
6 | import hudson.EnvVars;
7 | import hudson.model.FreeStyleBuild;
8 | import hudson.model.Result;
9 | import hudson.model.FreeStyleProject;
10 | import hudson.slaves.EnvironmentVariablesNodeProperty;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | import org.junit.jupiter.api.BeforeEach;
16 | import org.junit.jupiter.api.Test;
17 | import org.jvnet.hudson.test.JenkinsRule;
18 | import org.jvnet.hudson.test.WithoutJenkins;
19 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
20 |
21 | @WithJenkins
22 | class FileCreateOperationTest {
23 |
24 | private JenkinsRule jenkins;
25 |
26 | @BeforeEach
27 | void setUp(JenkinsRule rule) {
28 | jenkins = rule;
29 | }
30 |
31 | @Test
32 | @WithoutJenkins
33 | void testDefaults() {
34 | FileCreateOperation fco = new FileCreateOperation("NewFileName.txt", "This is File Content");
35 | assertEquals("NewFileName.txt", fco.getFileName());
36 | assertEquals("This is File Content", fco.getFileContent());
37 | }
38 |
39 | @Test
40 | void testRunFileOperationWithFileOperationBuildStep() throws Exception {
41 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
42 | FileCreateOperation fco = new FileCreateOperation("NewFileName.txt", "This is File Content");
43 | List fop = new ArrayList<>();
44 | fop.add(fco);
45 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
46 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
47 | assertEquals(Result.SUCCESS, build.getResult());
48 | assertTrue(build.getWorkspace().child("NewFileName.txt").exists());
49 | }
50 |
51 | @Test
52 | void testRunFileOperationWithFileOperationBuildStepWithTokens() throws Exception {
53 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
54 | EnvVars envVars = prop.getEnvVars();
55 | envVars.put("TextFileName", "NewFileName.txt");
56 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
57 |
58 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
59 | FileCreateOperation fco = new FileCreateOperation("$TextFileName", "This is File Content");
60 | List fop = new ArrayList<>();
61 | fop.add(fco);
62 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
63 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
64 | assertEquals(Result.SUCCESS, build.getResult());
65 | assertTrue(build.getWorkspace().child("NewFileName.txt").exists());
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileDeleteOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.*;
4 |
5 | import com.thoughtworks.xstream.XStream;
6 | import com.thoughtworks.xstream.security.AnyTypePermission;
7 | import hudson.EnvVars;
8 | import hudson.model.FreeStyleBuild;
9 | import hudson.model.Result;
10 | import hudson.model.FreeStyleProject;
11 | import hudson.slaves.EnvironmentVariablesNodeProperty;
12 |
13 | import java.util.ArrayList;
14 | import java.util.Arrays;
15 | import java.util.List;
16 |
17 | import org.junit.jupiter.api.BeforeEach;
18 | import org.junit.jupiter.api.Test;
19 | import org.jvnet.hudson.test.JenkinsRule;
20 | import org.jvnet.hudson.test.WithoutJenkins;
21 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
22 |
23 | @WithJenkins
24 | class FileDeleteOperationTest {
25 |
26 | private JenkinsRule jenkins;
27 |
28 | @BeforeEach
29 | void setUp(JenkinsRule rule) {
30 | jenkins = rule;
31 | }
32 |
33 | @Test
34 | @WithoutJenkins
35 | void testDefaults() {
36 | FileDeleteOperation fdo = new FileDeleteOperation("**/*.txt", "**/*.xml");
37 | assertEquals("**/*.txt", fdo.getIncludes());
38 | assertEquals("**/*.xml", fdo.getExcludes());
39 | assertTrue(fdo.getUseDefaultExcludes());
40 | }
41 |
42 | @Test
43 | void testRunFileOperationWithFileOperationBuildStep() throws Exception {
44 | // Given
45 | FileCreateOperation fco = new FileCreateOperation("NewFileName.txt", "This is File Content");
46 | FileDeleteOperation fdo = new FileDeleteOperation("**/*.txt", "**/*.xml");
47 | List fop = new ArrayList<>();
48 | fop.add(fco);
49 | fop.add(fdo);
50 |
51 | // When
52 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
53 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
54 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
55 |
56 | // Then
57 | assertEquals(Result.SUCCESS, build.getResult());
58 | assertFalse(build.getWorkspace().child("NewFileName.txt").exists());
59 | }
60 |
61 | @Test
62 | void testRunFileOperationWithFileOperationBuildStepWithTokens() throws Exception {
63 | // Given
64 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
65 | EnvVars envVars = prop.getEnvVars();
66 | envVars.put("TextFileName", "NewFileName.txt");
67 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
68 |
69 | FileCreateOperation fco = new FileCreateOperation("$TextFileName", "This is File Content");
70 | FileDeleteOperation fdo = new FileDeleteOperation("**/*.txt", "**/*.xml");
71 | List fop = new ArrayList<>();
72 | fop.add(fco);
73 | fop.add(fdo);
74 |
75 | // When
76 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
77 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
78 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
79 |
80 | // Then
81 | assertEquals(Result.SUCCESS, build.getResult());
82 | assertFalse(build.getWorkspace().child("NewFileName.txt").exists());
83 | }
84 |
85 | @Test
86 | void testRunFileOperationWithFileOperationBuildStepWithDefaultExcludes() throws Exception {
87 | // Given
88 | FileCreateOperation fco = new FileCreateOperation(".gitignore", "This is File Content");
89 | FileDeleteOperation fdo = new FileDeleteOperation(".gitignore", "");
90 | List fop = Arrays.asList(fco, fdo);
91 |
92 | // When
93 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
94 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
95 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
96 |
97 | // Then
98 | assertEquals(Result.SUCCESS, build.getResult());
99 | assertTrue(build.getWorkspace().child(".gitignore").exists());
100 | }
101 |
102 | @Test
103 | void testRunFileOperationWithFileOperationBuildStepWithoutDefaultExcludes() throws Exception {
104 | // Given
105 | FileCreateOperation fco = new FileCreateOperation(".gitignore", "This is File Content");
106 | FileDeleteOperation fdo = new FileDeleteOperation(".gitignore", "");
107 | fdo.setUseDefaultExcludes(false);
108 | List fop = Arrays.asList(fco, fdo);
109 |
110 | // When
111 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
112 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
113 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
114 |
115 | // Then
116 | assertEquals(Result.SUCCESS, build.getResult());
117 | assertFalse(build.getWorkspace().child(".gitignore").exists());
118 | }
119 |
120 | @Test
121 | @WithoutJenkins
122 | void testSerializeWithXStream() {
123 | // Given
124 | FileDeleteOperation originalObject = new FileDeleteOperation("include", "exclude");
125 | originalObject.setUseDefaultExcludes(false);
126 |
127 | // When
128 | XStream xstream = new XStream();
129 | xstream.addPermission(AnyTypePermission.ANY);
130 | String serializedObjectXml = xstream.toXML(originalObject);
131 | FileDeleteOperation deserializedObject = (FileDeleteOperation)xstream.fromXML(serializedObjectXml);
132 |
133 | // Then
134 | assertEquals(originalObject.getIncludes(), deserializedObject.getIncludes());
135 | assertEquals(originalObject.getExcludes(), deserializedObject.getExcludes());
136 | assertEquals(originalObject.getUseDefaultExcludes(), deserializedObject.getUseDefaultExcludes());
137 | }
138 |
139 | @Test
140 | @WithoutJenkins
141 | void testSerializeWithXStreamBackwardsCompatibility() {
142 | // Given
143 | String serializedObjectXml = "includeexclude";
144 |
145 | // When
146 | XStream xstream = new XStream();
147 | xstream.alias("FileDeleteOperation", FileDeleteOperation.class);
148 | xstream.addPermission(AnyTypePermission.ANY);
149 | FileDeleteOperation deserializedObject = (FileDeleteOperation)xstream.fromXML(serializedObjectXml);
150 |
151 | // Then
152 | assertTrue(deserializedObject.getUseDefaultExcludes());
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileDownloadOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
4 | import hidden.jth.org.apache.commons.lang3.RandomStringUtils;
5 | import hudson.model.FreeStyleProject;
6 | import hudson.model.Run;
7 | import hudson.model.TaskListener;
8 | import org.apache.hc.core5.http.HttpHeaders;
9 | import org.apache.hc.core5.http.HttpStatus;
10 | import org.junit.jupiter.api.BeforeEach;
11 | import org.junit.jupiter.api.Test;
12 | import org.junit.jupiter.api.extension.RegisterExtension;
13 | import org.jvnet.hudson.test.JenkinsRule;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import java.io.File;
17 |
18 | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19 | import static com.github.tomakehurst.wiremock.client.WireMock.get;
20 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
21 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
22 | import static org.junit.jupiter.api.Assertions.assertEquals;
23 | import static org.junit.jupiter.api.Assertions.assertFalse;
24 | import static org.junit.jupiter.api.Assertions.assertTrue;
25 |
26 | /**
27 | * Test class to validate {@link FileDownloadOperation}.
28 | */
29 | @WithJenkins
30 | class FileDownloadOperationTest {
31 |
32 | // matches the file in __files
33 | private static final String DUMMY_ZIP = "dummy.zip";
34 | private static final String TEST_PATH = "/test/" + DUMMY_ZIP;
35 |
36 | @RegisterExtension
37 | static WireMockExtension wireMock = WireMockExtension.newInstance()
38 | .options(options().dynamicPort())
39 | .build();
40 |
41 | private JenkinsRule jenkins;
42 |
43 | @BeforeEach
44 | void setUp(JenkinsRule rule) {
45 | jenkins = rule;
46 | }
47 |
48 | @Test
49 | void shouldDownload() throws Exception {
50 | // endpoint without authentication
51 | wireMock.stubFor(get(urlEqualTo(TEST_PATH))
52 | .willReturn(aResponse()
53 | .withStatus(HttpStatus.SC_OK)
54 | .withBodyFile(DUMMY_ZIP) // matches the file in __files
55 | .withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));
56 |
57 | // define operation & run
58 | FreeStyleProject project = jenkins.createFreeStyleProject();
59 | Run run = project.scheduleBuild2(0).get();
60 | FileDownloadOperation operation = new FileDownloadOperation(
61 | wireMock.baseUrl() + TEST_PATH,
62 | "",
63 | "",
64 | run.getRootDir().getAbsolutePath(),
65 | DUMMY_ZIP,
66 | null,
67 | null);
68 | boolean result = operation.runOperation(run, jenkins.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
69 | File download = new File(operation.getTargetLocation(), operation.getTargetFileName());
70 |
71 | // validate
72 | assertTrue(result);
73 | assertTrue(download.exists());
74 | assertTrue(download.length() > 0);
75 | }
76 |
77 | @Test
78 | void shouldDownloadWithBasicAuth() throws Exception {
79 | String username = RandomStringUtils.secure().nextAlphabetic(10);
80 | String password = RandomStringUtils.secure().nextAlphabetic(10);
81 |
82 | // endpoint with authentication
83 | wireMock.stubFor(get(urlEqualTo(TEST_PATH))
84 | .withBasicAuth(username, password)
85 | .willReturn(aResponse()
86 | .withStatus(HttpStatus.SC_OK)
87 | .withBodyFile(DUMMY_ZIP) // matches the file in __files
88 | .withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));
89 |
90 | // define operation & run
91 | FreeStyleProject project = jenkins.createFreeStyleProject();
92 | Run run = project.scheduleBuild2(0).get();
93 | FileDownloadOperation operation = new FileDownloadOperation(
94 | wireMock.baseUrl() + TEST_PATH,
95 | username,
96 | password,
97 | run.getRootDir().getAbsolutePath(),
98 | DUMMY_ZIP,
99 | null,
100 | null);
101 | boolean result = operation.runOperation(run, jenkins.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
102 | File download = new File(operation.getTargetLocation(), operation.getTargetFileName());
103 |
104 | // validate
105 | assertTrue(result);
106 | assertTrue(download.exists());
107 | assertTrue(download.length() > 0);
108 | }
109 |
110 | @Test
111 | void shouldFailWithBadCredentials() throws Exception {
112 | String username = RandomStringUtils.secure().nextAlphabetic(10);
113 | String password = RandomStringUtils.secure().nextAlphabetic(10);
114 |
115 | // endpoint with authentication
116 | wireMock.stubFor(get(urlEqualTo(TEST_PATH))
117 | .withBasicAuth(username, password)
118 | .willReturn(aResponse()
119 | .withStatus(HttpStatus.SC_OK)
120 | .withBodyFile(DUMMY_ZIP) // matches the file in __files
121 | .withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));
122 |
123 | // define operation & run
124 | FreeStyleProject project = jenkins.createFreeStyleProject();
125 | Run run = project.scheduleBuild2(0).get();
126 | FileDownloadOperation operation = new FileDownloadOperation(
127 | wireMock.baseUrl() + TEST_PATH,
128 | RandomStringUtils.secure().nextAlphabetic(10),
129 | RandomStringUtils.secure().nextAlphabetic(10),
130 | run.getRootDir().getAbsolutePath(),
131 | DUMMY_ZIP,
132 | null,
133 | null);
134 | boolean result = operation.runOperation(run, jenkins.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
135 | File download = new File(operation.getTargetLocation(), operation.getTargetFileName());
136 |
137 | // validate
138 | assertFalse(result);
139 | assertTrue(download.exists()); // empty file created
140 | assertEquals(0, download.length()); // empty file created
141 | }
142 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileJoinOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import hudson.EnvVars;
6 | import hudson.FilePath;
7 | import hudson.model.FreeStyleBuild;
8 | import hudson.model.FreeStyleProject;
9 | import hudson.model.Result;
10 | import hudson.slaves.EnvironmentVariablesNodeProperty;
11 | import org.junit.jupiter.api.BeforeEach;
12 | import org.junit.jupiter.api.Test;
13 | import org.jvnet.hudson.test.JenkinsRule;
14 | import org.jvnet.hudson.test.WithoutJenkins;
15 |
16 | import java.nio.charset.StandardCharsets;
17 | import java.util.List;
18 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
19 |
20 | @WithJenkins
21 | class FileJoinOperationTest {
22 |
23 | private JenkinsRule jenkins;
24 |
25 | @BeforeEach
26 | void setUp(JenkinsRule rule) {
27 | jenkins = rule;
28 | }
29 |
30 | @Test
31 | @WithoutJenkins
32 | void testDefaults() {
33 | FileJoinOperation fjo = new FileJoinOperation("source.txt", "target.txt");
34 | assertEquals("source.txt", fjo.getSourceFile());
35 | assertEquals("target.txt", fjo.getTargetFile());
36 | }
37 |
38 | @Test
39 | void testRunFileJoinOperation() throws Exception {
40 | FreeStyleProject project = jenkins.createFreeStyleProject("fileJoinTest");
41 |
42 | FilePath sourceFile = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "source.txt");
43 | sourceFile.write("Source File Content", StandardCharsets.UTF_8.name());
44 |
45 | FilePath targetFile = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "target.txt");
46 | targetFile.write("Target File Content", StandardCharsets.UTF_8.name());
47 |
48 | FileJoinOperation fileJoinOp = new FileJoinOperation("source.txt", "target.txt");
49 | project.getBuildersList().add(new FileOperationsBuilder(List.of(fileJoinOp)));
50 |
51 | FreeStyleBuild build = project.scheduleBuild2(0).get();
52 | assertEquals(Result.SUCCESS, build.getResult());
53 |
54 | String expectedContent = "Target File Content" + System.lineSeparator() + "Source File Content";
55 | String actualContent = targetFile.readToString();
56 | assertEquals(expectedContent, actualContent);
57 | }
58 |
59 | @Test
60 | void testRunFileJoinOperationWithTokens() throws Exception {
61 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
62 | EnvVars envVars = prop.getEnvVars();
63 | envVars.put("SOURCE_FILE", "source.txt");
64 | envVars.put("TARGET_FILE", "target.txt");
65 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
66 |
67 | FreeStyleProject project = jenkins.createFreeStyleProject("fileJoinTestWithTokens");
68 |
69 | FilePath sourceFile = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "source.txt");
70 | sourceFile.write("Source File Content", StandardCharsets.UTF_8.name());
71 |
72 | FilePath targetFile = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "target.txt");
73 | targetFile.write("Target File Content", StandardCharsets.UTF_8.name());
74 |
75 | FileJoinOperation fileJoinOp = new FileJoinOperation("$SOURCE_FILE", "$TARGET_FILE");
76 | project.getBuildersList().add(new FileOperationsBuilder(List.of(fileJoinOp)));
77 |
78 | FreeStyleBuild build = project.scheduleBuild2(0).get();
79 | assertEquals(Result.SUCCESS, build.getResult());
80 |
81 | String expectedContent = "Target File Content" + System.lineSeparator() + "Source File Content";
82 | String actualContent = targetFile.readToString();
83 | assertEquals(expectedContent, actualContent);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileOperationDescriptorTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.junit.jupiter.api.Test;
6 | import org.jvnet.hudson.test.WithoutJenkins;
7 |
8 | class FileOperationDescriptorTest {
9 |
10 | @Test
11 | @WithoutJenkins
12 | void testFileOperationDescriptor() {
13 | // Test with a real implementation that exists in the codebase
14 | FileOperationDescriptor descriptor = new FileZipOperation.DescriptorImpl();
15 | assertEquals("File Zip", descriptor.getDisplayName());
16 |
17 | // Test another implementation
18 | FileOperationDescriptor descriptor2 = new FileUnZipOperation.DescriptorImpl();
19 | assertEquals("Unzip", descriptor2.getDisplayName());
20 | }
21 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertFalse;
4 | import static org.junit.jupiter.api.Assertions.assertTrue;
5 |
6 | import org.junit.jupiter.api.Test;
7 | import org.jvnet.hudson.test.WithoutJenkins;
8 |
9 | import hudson.FilePath;
10 | import hudson.Launcher;
11 | import hudson.model.Run;
12 | import hudson.model.TaskListener;
13 |
14 | class FileOperationTest {
15 |
16 | @Test
17 | @WithoutJenkins
18 | void testAbstractFileOperation() {
19 | // Create a concrete implementation of the abstract class for testing
20 | FileOperation operation = new FileOperation() {
21 | @Override
22 | public boolean runOperation(Run, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
23 | // Test implementation that returns true
24 | return true;
25 | }
26 | };
27 |
28 | // Create a second implementation that returns false
29 | FileOperation operationFalse = new FileOperation() {
30 | @Override
31 | public boolean runOperation(Run, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
32 | // Test implementation that returns false
33 | return false;
34 | }
35 | };
36 |
37 | // Test that the operation methods work as expected
38 | assertTrue(operation.runOperation(null, null, null, null));
39 | assertFalse(operationFalse.runOperation(null, null, null, null));
40 | }
41 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileOperationsBuilderTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import hudson.model.FreeStyleBuild;
6 | import hudson.model.Result;
7 | import hudson.model.FreeStyleProject;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 | import org.jvnet.hudson.test.JenkinsRule;
15 | import org.jvnet.hudson.test.WithoutJenkins;
16 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
17 |
18 | @WithJenkins
19 | class FileOperationsBuilderTest {
20 |
21 | private JenkinsRule jenkins;
22 |
23 | @BeforeEach
24 | void setUp(JenkinsRule rule) {
25 | jenkins = rule;
26 | }
27 |
28 | @Test
29 | @WithoutJenkins
30 | void testDefaults() {
31 | FileOperationsBuilder fob = new FileOperationsBuilder(null);
32 | assertEquals(0, fob.getFileOperations().size());
33 | }
34 |
35 | @Test
36 | @WithoutJenkins
37 | void testSettersAndGetters() {
38 | List fo = new ArrayList<>();
39 | FileOperationsBuilder fob = new FileOperationsBuilder(fo);
40 | assertEquals(0, fob.getFileOperations().size());
41 | }
42 |
43 | @Test
44 | void testAddFileOperationToBuildSteps() throws Exception {
45 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
46 | p1.getBuildersList().add(new FileOperationsBuilder(null));
47 | assertEquals(1, p1.getBuildersList().size());
48 | }
49 |
50 | @Test
51 | void testRunWithOutFileOperationWithFileOperationBuildStep() throws Exception {
52 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
53 | p1.getBuildersList().add(new FileOperationsBuilder(null));
54 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
55 | assertEquals(Result.SUCCESS, build.getResult());
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FilePropertiesToJsonOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 | import static org.junit.jupiter.api.Assertions.assertTrue;
5 |
6 | import java.io.OutputStream;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.Properties;
10 |
11 | import org.junit.jupiter.api.BeforeEach;
12 | import org.junit.jupiter.api.Test;
13 | import org.jvnet.hudson.test.JenkinsRule;
14 | import org.jvnet.hudson.test.WithoutJenkins;
15 |
16 | import hudson.FilePath;
17 | import hudson.model.FreeStyleBuild;
18 | import hudson.model.FreeStyleProject;
19 | import hudson.model.Result;
20 | import com.fasterxml.jackson.databind.JsonNode;
21 | import com.fasterxml.jackson.databind.ObjectMapper;
22 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
23 |
24 | @WithJenkins
25 | class FilePropertiesToJsonOperationTest {
26 |
27 | private JenkinsRule jenkins;
28 |
29 | @BeforeEach
30 | void setUp(JenkinsRule rule) {
31 | jenkins = rule;
32 | }
33 |
34 | @Test
35 | @WithoutJenkins
36 | void testGetters() {
37 | FilePropertiesToJsonOperation operation = new FilePropertiesToJsonOperation("source.properties", "target.json");
38 | assertEquals("source.properties", operation.getSourceFile());
39 | assertEquals("target.json", operation.getTargetFile());
40 | }
41 |
42 | @Test
43 | void testRunOperationWithValidFile() throws Exception {
44 | FreeStyleProject project = jenkins.createFreeStyleProject("test-properties-to-json");
45 |
46 | // Get the workspace
47 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
48 |
49 | // Create a properties file in the workspace
50 | FilePath propsFile = workspace.child("test.properties");
51 |
52 | // Create properties content
53 | Properties props = new Properties();
54 | props.setProperty("stringProp", "value");
55 | props.setProperty("intProp", "123");
56 | props.setProperty("floatProp", "123.45");
57 | props.setProperty("boolProp", "true");
58 |
59 | // Save properties to file using FilePath API
60 | try (OutputStream os = propsFile.write()) {
61 | props.store(os, "Test Properties");
62 | }
63 |
64 | // Make sure the target file exists first
65 | FilePath targetFile = workspace.child("output.json");
66 | targetFile.write("{}", "UTF-8");
67 |
68 | // Configure the operation
69 | List operations = new ArrayList<>();
70 | operations.add(new FilePropertiesToJsonOperation("test.properties", "output.json"));
71 |
72 | // Add the operation to the project
73 | project.getBuildersList().add(new FileOperationsBuilder(operations));
74 |
75 | // Run the build
76 | FreeStyleBuild build = project.scheduleBuild2(0).get();
77 |
78 | // Assert build success
79 | assertEquals(Result.SUCCESS, build.getResult());
80 |
81 | // Verify JSON file exists and contains correct content
82 | FilePath jsonFile = workspace.child("output.json");
83 | assertTrue(jsonFile.exists());
84 |
85 | // Parse and verify JSON content
86 | ObjectMapper mapper = new ObjectMapper();
87 | JsonNode jsonNode = mapper.readTree(jsonFile.read());
88 | assertEquals("value", jsonNode.get("stringProp").asText());
89 | assertEquals(123, jsonNode.get("intProp").asInt());
90 | assertEquals(123.45f, jsonNode.get("floatProp").floatValue(), 0.001);
91 | assertTrue(jsonNode.get("boolProp").asBoolean());
92 | }
93 |
94 | @Test
95 | void testRunOperationWithNonExistentFile() throws Exception {
96 | FreeStyleProject project = jenkins.createFreeStyleProject("test-properties-nonexistent");
97 |
98 | // Get the workspace
99 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
100 |
101 | // Create target file with content
102 | FilePath targetFile = workspace.child("output.json");
103 | targetFile.write("{\"test\":\"original\"}", "UTF-8");
104 |
105 | // Configure the operation with non-existent source file
106 | List operations = new ArrayList<>();
107 | operations.add(new FilePropertiesToJsonOperation("nonexistent.properties", "output.json"));
108 |
109 | // Add the operation to the project
110 | project.getBuildersList().add(new FileOperationsBuilder(operations));
111 |
112 | // Run the build
113 | FreeStyleBuild build = project.scheduleBuild2(0).get();
114 |
115 | // Assert build failure due to missing file
116 | assertEquals(Result.FAILURE, build.getResult());
117 | }
118 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileRenameOperationTest.java:
--------------------------------------------------------------------------------
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.jupiter.api.BeforeEach;
10 | import org.junit.jupiter.api.Test;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import java.util.List;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import static org.junit.jupiter.api.Assertions.*;
17 |
18 | @WithJenkins
19 | class FileRenameOperationTest {
20 |
21 | private JenkinsRule jenkins;
22 |
23 | @BeforeEach
24 | void setUp(JenkinsRule rule) {
25 | jenkins = rule;
26 | }
27 |
28 | @Test
29 | void testDefaults() {
30 | String source = "source.txt";
31 | String destination = "destination.txt";
32 | FileRenameOperation operation = new FileRenameOperation(source, destination);
33 |
34 | assertEquals(source, operation.getSource());
35 | assertEquals(destination, operation.getDestination());
36 | }
37 |
38 | @Test
39 | void testRunFileRenameOperation() throws Exception {
40 | FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTest");
41 |
42 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
43 | FilePath sourceFile = new FilePath(workspace, "source.txt");
44 | FilePath destinationFile = new FilePath(workspace, "destination.txt");
45 |
46 | sourceFile.write("Sample content", "UTF-8");
47 |
48 | FileRenameOperation operation = new FileRenameOperation("source.txt", "destination.txt");
49 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
50 |
51 | FreeStyleBuild build = project.scheduleBuild2(0).get();
52 | assertEquals(Result.SUCCESS, build.getResult());
53 |
54 | assertFalse(sourceFile.exists(), "The source file should have been renamed");
55 | assertTrue(destinationFile.exists(), "The destination file should exist");
56 | }
57 |
58 | @Test
59 | void testRunFileRenameOperationWithTokens() throws Exception {
60 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
61 | EnvVars envVars = prop.getEnvVars();
62 | envVars.put("SOURCE_FILE", "source.txt");
63 | envVars.put("DESTINATION_FILE", "destination.txt");
64 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
65 |
66 | FreeStyleProject project = jenkins.createFreeStyleProject("fileRenameTestWithTokens");
67 |
68 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
69 | FilePath sourceFile = new FilePath(workspace, "source.txt");
70 | FilePath destinationFile = new FilePath(workspace, "destination.txt");
71 |
72 | sourceFile.write("Sample content", "UTF-8");
73 |
74 | FileRenameOperation operation = new FileRenameOperation("$SOURCE_FILE", "$DESTINATION_FILE");
75 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
76 |
77 | FreeStyleBuild build = project.scheduleBuild2(0).get();
78 | assertEquals(Result.SUCCESS, build.getResult());
79 |
80 | assertFalse(sourceFile.exists(), "The source file should have been renamed");
81 | assertTrue(destinationFile.exists(), "The destination file should exist");
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileTransformOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.*;
4 |
5 | import com.thoughtworks.xstream.XStream;
6 | import com.thoughtworks.xstream.security.AnyTypePermission;
7 | import hudson.EnvVars;
8 | import hudson.FilePath;
9 | import hudson.Launcher;
10 | import hudson.model.FreeStyleBuild;
11 | import hudson.model.FreeStyleProject;
12 | import hudson.model.Result;
13 | import hudson.model.Run;
14 | import hudson.model.TaskListener;
15 | import hudson.slaves.EnvironmentVariablesNodeProperty;
16 | import org.junit.jupiter.api.BeforeEach;
17 | import org.junit.jupiter.api.Test;
18 | import org.jvnet.hudson.test.JenkinsRule;
19 | import org.jvnet.hudson.test.WithoutJenkins;
20 |
21 | import java.util.Arrays;
22 | import java.util.List;
23 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
24 |
25 | @WithJenkins
26 | class FileTransformOperationTest {
27 |
28 | private JenkinsRule jenkins;
29 |
30 | @BeforeEach
31 | void setUp(JenkinsRule rule) {
32 | jenkins = rule;
33 | }
34 |
35 | @Test
36 | @WithoutJenkins
37 | void testDefaults() {
38 | FileTransformOperation fto = new FileTransformOperation("NewFileName.config", "**/*.xml");
39 | assertEquals("NewFileName.config", fto.getIncludes());
40 | assertEquals("**/*.xml", fto.getExcludes());
41 | assertTrue(fto.getUseDefaultExcludes());
42 | }
43 |
44 | static final class FileOperationPutEnvironment extends FileOperation {
45 | public final transient JenkinsRule jenkins;
46 | public final transient String key;
47 | public final transient String value;
48 |
49 | public FileOperationPutEnvironment(JenkinsRule jenkins, String key, String value) {
50 | this.jenkins = jenkins;
51 | this.key = key;
52 | this.value = value;
53 | }
54 |
55 | public boolean runOperation(Run, ?> run, FilePath buildWorkspace, Launcher launcher, TaskListener listener) {
56 | assertDoesNotThrow(() -> {
57 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
58 | EnvVars envVars = prop.getEnvVars();
59 | envVars.put(key, value);
60 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
61 | }, "Unexpected exception during environment put.");
62 | return true;
63 | }
64 | }
65 |
66 | @Test
67 | void testRunFileOperationWithFileOperationBuildStepWithTokens() throws Exception {
68 | // Given
69 | FileCreateOperation fco = new FileCreateOperation("TestFile.txt", "$Content");
70 | FileOperationPutEnvironment fpo = new FileOperationPutEnvironment(jenkins, "Content", "ReplacementContent");
71 | FileTransformOperation fto = new FileTransformOperation("TestFile.txt", "");
72 | List fop = Arrays.asList(fco, fpo, fto);
73 |
74 | // When
75 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
76 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
77 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
78 |
79 | // Then
80 | assertEquals(Result.SUCCESS, build.getResult());
81 | assertEquals("ReplacementContent", build.getWorkspace().child("TestFile.txt").readToString());
82 | }
83 |
84 | @Test
85 | void testRunFileOperationWithFileOperationBuildStepWithDefaultExcludes() throws Exception {
86 | // Given
87 | FileCreateOperation fco = new FileCreateOperation(".gitignore", "$Content");
88 | FileOperationPutEnvironment fpo = new FileOperationPutEnvironment(jenkins, "Content", "ReplacementContent");
89 | FileTransformOperation fto = new FileTransformOperation(".gitignore", "");
90 | List fop = Arrays.asList(fco, fpo, fto);
91 |
92 | // When
93 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
94 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
95 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
96 |
97 | // Then
98 | assertEquals(Result.SUCCESS, build.getResult());
99 | assertTrue(build.getWorkspace().child(".gitignore").exists());
100 | assertEquals("$Content", build.getWorkspace().child(".gitignore").readToString());
101 | }
102 |
103 | @Test
104 | void testRunFileOperationWithFileOperationBuildStepWithoutDefaultExcludes() throws Exception {
105 | // Given
106 | FileCreateOperation fco = new FileCreateOperation(".gitignore", "$Content");
107 | FileOperationPutEnvironment fpo = new FileOperationPutEnvironment(jenkins, "Content", "ReplacementContent");
108 | FileTransformOperation fto = new FileTransformOperation(".gitignore", "");
109 | fto.setUseDefaultExcludes(false);
110 | List fop = Arrays.asList(fco, fpo, fto);
111 |
112 | // When
113 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
114 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
115 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
116 |
117 | // Then
118 | assertEquals(Result.SUCCESS, build.getResult());
119 | assertEquals("ReplacementContent", build.getWorkspace().child(".gitignore").readToString());
120 | }
121 |
122 | @Test
123 | @WithoutJenkins
124 | void testSerializeWithXStream() {
125 | // Given
126 | FileTransformOperation originalObject = new FileTransformOperation("include", "exclude");
127 | originalObject.setUseDefaultExcludes(false);
128 |
129 | // When
130 | XStream xstream = new XStream();
131 | xstream.addPermission(AnyTypePermission.ANY);
132 | String serializedObjectXml = xstream.toXML(originalObject);
133 | FileTransformOperation deserializedObject = (FileTransformOperation)xstream.fromXML(serializedObjectXml);
134 |
135 | // Then
136 | assertEquals(originalObject.getIncludes(), deserializedObject.getIncludes());
137 | assertEquals(originalObject.getExcludes(), deserializedObject.getExcludes());
138 | assertEquals(originalObject.getUseDefaultExcludes(), deserializedObject.getUseDefaultExcludes());
139 | }
140 |
141 | @Test
142 | @WithoutJenkins
143 | void testSerializeWithXStreamBackwardsCompatibility() {
144 | // Given
145 | String serializedObjectXml = "includeexclude";
146 |
147 | // When
148 | XStream xstream = new XStream();
149 | xstream.alias("FileTransformOperation", FileTransformOperation.class);
150 | xstream.addPermission(AnyTypePermission.ANY);
151 | FileTransformOperation deserializedObject = (FileTransformOperation)xstream.fromXML(serializedObjectXml);
152 |
153 | // Then
154 | assertTrue(deserializedObject.getUseDefaultExcludes());
155 | }
156 | }
157 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileUnTarOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.*;
4 |
5 | import java.io.InputStream;
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
10 | import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
11 | import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
12 | import org.apache.commons.io.IOUtils;
13 | import org.junit.jupiter.api.BeforeEach;
14 | import org.junit.jupiter.api.Test;
15 | import org.jvnet.hudson.test.JenkinsRule;
16 | import org.jvnet.hudson.test.WithoutJenkins;
17 |
18 | import hudson.FilePath;
19 | import hudson.model.FreeStyleBuild;
20 | import hudson.model.FreeStyleProject;
21 | import hudson.model.Result;
22 |
23 | import java.io.FileOutputStream;
24 | import java.io.BufferedOutputStream;
25 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
26 |
27 | @WithJenkins
28 | class FileUnTarOperationTest {
29 |
30 | private JenkinsRule jenkins;
31 |
32 | @BeforeEach
33 | void setUp(JenkinsRule rule) {
34 | jenkins = rule;
35 | }
36 |
37 | @Test
38 | @WithoutJenkins
39 | void testGetters() {
40 | FileUnTarOperation operation = new FileUnTarOperation("test.tar", "target", true);
41 | assertEquals("test.tar", operation.getFilePath());
42 | assertEquals("target", operation.getTargetLocation());
43 | assertTrue(operation.getIsGZIP());
44 |
45 | // Test with isGZIP false
46 | FileUnTarOperation operation2 = new FileUnTarOperation("test.tar", "target", false);
47 | assertFalse(operation2.getIsGZIP());
48 | }
49 |
50 | @Test
51 | void testUnTarRegularTarFile() throws Exception {
52 | FreeStyleProject project = jenkins.createFreeStyleProject("test-untar-regular");
53 |
54 | // Get workspace
55 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
56 |
57 | // Create test content
58 | String testContent = "test content";
59 | FilePath sourceFile = workspace.child("source-file.txt");
60 | sourceFile.write(testContent, "UTF-8");
61 |
62 | // Create tar file
63 | FilePath tarFile = workspace.child("test.tar");
64 | try (TarArchiveOutputStream out = new TarArchiveOutputStream(
65 | new BufferedOutputStream(new FileOutputStream(tarFile.getRemote())))) {
66 | out.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
67 |
68 | // Create entry for the file
69 | TarArchiveEntry entry = new TarArchiveEntry("source-file.txt");
70 | entry.setSize(sourceFile.length());
71 | out.putArchiveEntry(entry);
72 |
73 | // Copy file content to tar
74 | try (InputStream in = sourceFile.read()) {
75 | IOUtils.copy(in, out);
76 | }
77 |
78 | // Close the entry
79 | out.closeArchiveEntry();
80 | out.finish();
81 | }
82 |
83 | // Configure the operation
84 | List operations = new ArrayList<>();
85 | operations.add(new FileUnTarOperation("test.tar", "extracted", false));
86 |
87 | // Add the operation to the project
88 | project.getBuildersList().add(new FileOperationsBuilder(operations));
89 |
90 | // Run the build
91 | FreeStyleBuild build = project.scheduleBuild2(0).get();
92 |
93 | // Assert build success
94 | assertEquals(Result.SUCCESS, build.getResult());
95 |
96 | // Verify extracted file exists
97 | FilePath extractedFile = workspace.child("extracted/source-file.txt");
98 | assertTrue(extractedFile.exists());
99 | assertEquals(testContent, extractedFile.readToString());
100 | }
101 |
102 | @Test
103 | void testUnTarGzipFile() throws Exception {
104 | FreeStyleProject project = jenkins.createFreeStyleProject("test-untar-gzip");
105 |
106 | // Get workspace
107 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
108 |
109 | // Create test content
110 | String testContent = "test gzip content";
111 | FilePath sourceFile = workspace.child("source-file.txt");
112 | sourceFile.write(testContent, "UTF-8");
113 |
114 | // Create tar.gz file
115 | FilePath tarGzFile = workspace.child("test.tar.gz");
116 | try (TarArchiveOutputStream out = new TarArchiveOutputStream(
117 | new GzipCompressorOutputStream(
118 | new BufferedOutputStream(new FileOutputStream(tarGzFile.getRemote()))))) {
119 | out.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
120 |
121 | // Create entry for the file
122 | TarArchiveEntry entry = new TarArchiveEntry("source-file.txt");
123 | entry.setSize(sourceFile.length());
124 | out.putArchiveEntry(entry);
125 |
126 | // Copy file content to tar
127 | try (InputStream in = sourceFile.read()) {
128 | IOUtils.copy(in, out);
129 | }
130 |
131 | // Close the entry
132 | out.closeArchiveEntry();
133 | out.finish();
134 | }
135 |
136 | // Configure the operation
137 | List operations = new ArrayList<>();
138 | operations.add(new FileUnTarOperation("test.tar.gz", "extracted-gz", true));
139 |
140 | // Add the operation to the project
141 | project.getBuildersList().add(new FileOperationsBuilder(operations));
142 |
143 | // Run the build
144 | FreeStyleBuild build = project.scheduleBuild2(0).get();
145 |
146 | // Assert build success
147 | assertEquals(Result.SUCCESS, build.getResult());
148 |
149 | // Verify extracted file exists
150 | FilePath extractedFile = workspace.child("extracted-gz/source-file.txt");
151 | assertTrue(extractedFile.exists());
152 | assertEquals(testContent, extractedFile.readToString());
153 | }
154 |
155 | @Test
156 | void testUnTarNonExistentFile() throws Exception {
157 | FreeStyleProject project = jenkins.createFreeStyleProject("test-untar-nonexistent");
158 |
159 | // Configure the operation with non-existent source file
160 | List operations = new ArrayList<>();
161 | operations.add(new FileUnTarOperation("nonexistent.tar", "extracted", false));
162 |
163 | // Add the operation to the project
164 | project.getBuildersList().add(new FileOperationsBuilder(operations));
165 |
166 | // Run the build
167 | FreeStyleBuild build = project.scheduleBuild2(0).get();
168 |
169 | // Assert build failure due to missing file
170 | assertEquals(Result.FAILURE, build.getResult());
171 | }
172 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileUnZipOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 | import static org.junit.jupiter.api.Assertions.assertTrue;
5 |
6 | import java.io.OutputStream;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.zip.ZipEntry;
10 | import java.util.zip.ZipOutputStream;
11 |
12 | import org.junit.jupiter.api.BeforeEach;
13 | import org.junit.jupiter.api.Test;
14 | import org.jvnet.hudson.test.JenkinsRule;
15 | import org.jvnet.hudson.test.WithoutJenkins;
16 |
17 | import hudson.FilePath;
18 | import hudson.model.FreeStyleBuild;
19 | import hudson.model.FreeStyleProject;
20 | import hudson.model.Result;
21 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
22 |
23 | @WithJenkins
24 | class FileUnZipOperationTest {
25 |
26 | private JenkinsRule jenkins;
27 |
28 | @BeforeEach
29 | void setUp(JenkinsRule rule) {
30 | jenkins = rule;
31 | }
32 |
33 | @Test
34 | @WithoutJenkins
35 | void testGetters() {
36 | FileUnZipOperation operation = new FileUnZipOperation("test.zip", "target");
37 | assertEquals("test.zip", operation.getFilePath());
38 | assertEquals("target", operation.getTargetLocation());
39 | }
40 |
41 | @Test
42 | void testUnzipFile() throws Exception {
43 | FreeStyleProject project = jenkins.createFreeStyleProject("test-unzip");
44 |
45 | // Get workspace
46 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
47 |
48 | // Create test content
49 | String testContent = "test content for zip file";
50 | String nestedContent = "nested content";
51 |
52 | // Create a zip file directly in the workspace
53 | FilePath zipFile = workspace.child("test.zip");
54 |
55 | try (OutputStream os = zipFile.write();
56 | ZipOutputStream zipOut = new ZipOutputStream(os)) {
57 |
58 | // Add a file entry
59 | ZipEntry entry = new ZipEntry("test-file.txt");
60 | zipOut.putNextEntry(entry);
61 | zipOut.write(testContent.getBytes());
62 | zipOut.closeEntry();
63 |
64 | // Add a directory entry
65 | ZipEntry dirEntry = new ZipEntry("test-dir/");
66 | zipOut.putNextEntry(dirEntry);
67 | zipOut.closeEntry();
68 |
69 | // Add a file in the directory
70 | ZipEntry nestedEntry = new ZipEntry("test-dir/nested-file.txt");
71 | zipOut.putNextEntry(nestedEntry);
72 | zipOut.write(nestedContent.getBytes());
73 | zipOut.closeEntry();
74 | }
75 |
76 | // Configure the operation
77 | List operations = new ArrayList<>();
78 | operations.add(new FileUnZipOperation("test.zip", "extracted"));
79 |
80 | // Add the operation to the project
81 | project.getBuildersList().add(new FileOperationsBuilder(operations));
82 |
83 | // Run the build
84 | FreeStyleBuild build = project.scheduleBuild2(0).get();
85 |
86 | // Assert build success
87 | assertEquals(Result.SUCCESS, build.getResult());
88 |
89 | // Verify extracted files exist
90 | FilePath extractedDir = workspace.child("extracted");
91 | assertTrue(extractedDir.exists());
92 |
93 | FilePath extractedFile = extractedDir.child("test-file.txt");
94 | assertTrue(extractedFile.exists());
95 | assertEquals(testContent, extractedFile.readToString());
96 |
97 | FilePath extractedNestedDir = extractedDir.child("test-dir");
98 | assertTrue(extractedNestedDir.exists());
99 |
100 | FilePath extractedNestedFile = extractedNestedDir.child("nested-file.txt");
101 | assertTrue(extractedNestedFile.exists());
102 | assertEquals(nestedContent, extractedNestedFile.readToString());
103 | }
104 |
105 | @Test
106 | void testUnzipToExistingDirectory() throws Exception {
107 | FreeStyleProject project = jenkins.createFreeStyleProject("test-unzip-existing-dir");
108 |
109 | // Get workspace
110 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
111 |
112 | // Create a pre-existing target directory with a file
113 | FilePath existingDir = workspace.child("existing-dir");
114 | existingDir.mkdirs();
115 | FilePath existingFile = existingDir.child("existing-file.txt");
116 | existingFile.write("existing content", "UTF-8");
117 |
118 | // Create a zip file in the workspace
119 | FilePath zipFile = workspace.child("test.zip");
120 | String newContent = "new content";
121 |
122 | try (OutputStream os = zipFile.write();
123 | ZipOutputStream zipOut = new ZipOutputStream(os)) {
124 |
125 | ZipEntry entry = new ZipEntry("new-file.txt");
126 | zipOut.putNextEntry(entry);
127 | zipOut.write(newContent.getBytes());
128 | zipOut.closeEntry();
129 | }
130 |
131 | // Configure the operation
132 | List operations = new ArrayList<>();
133 | operations.add(new FileUnZipOperation("test.zip", "existing-dir"));
134 |
135 | // Add the operation to the project
136 | project.getBuildersList().add(new FileOperationsBuilder(operations));
137 |
138 | // Run the build
139 | FreeStyleBuild build = project.scheduleBuild2(0).get();
140 |
141 | // Assert build success
142 | assertEquals(Result.SUCCESS, build.getResult());
143 |
144 | // Verify both files exist
145 | assertTrue(existingFile.exists());
146 | assertEquals("existing content", existingFile.readToString());
147 |
148 | FilePath newFile = existingDir.child("new-file.txt");
149 | assertTrue(newFile.exists());
150 | assertEquals(newContent, newFile.readToString());
151 | }
152 |
153 | @Test
154 | void testUnzipNonExistentFile() throws Exception {
155 | FreeStyleProject project = jenkins.createFreeStyleProject("test-unzip-nonexistent");
156 |
157 | // Configure the operation with non-existent source file
158 | List operations = new ArrayList<>();
159 | operations.add(new FileUnZipOperation("nonexistent.zip", "extracted"));
160 |
161 | // Add the operation to the project
162 | project.getBuildersList().add(new FileOperationsBuilder(operations));
163 |
164 | // Run the build
165 | FreeStyleBuild build = project.scheduleBuild2(0).get();
166 |
167 | // Assert build failure due to missing file
168 | assertEquals(Result.FAILURE, build.getResult());
169 | }
170 | }
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FileZipOperationTest.java:
--------------------------------------------------------------------------------
1 | package sp.sd.fileoperations;
2 |
3 | import static org.junit.jupiter.api.Assertions.*;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import org.junit.jupiter.api.BeforeEach;
9 | import org.junit.jupiter.api.Test;
10 | import org.jvnet.hudson.test.JenkinsRule;
11 | import org.jvnet.hudson.test.WithoutJenkins;
12 |
13 | import hudson.model.FreeStyleBuild;
14 | import hudson.model.FreeStyleProject;
15 | import hudson.model.Result;
16 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
17 |
18 | @WithJenkins
19 | class FileZipOperationTest {
20 |
21 | private JenkinsRule jenkins;
22 |
23 | @BeforeEach
24 | void setUp(JenkinsRule rule) {
25 | jenkins = rule;
26 | }
27 |
28 | @Test
29 | @WithoutJenkins
30 | void testDefaults() {
31 | FileZipOperation fzo = new FileZipOperation("source", null);
32 | assertEquals("source", fzo.getFolderPath());
33 | assertNull(fzo.getOutputFolderPath());
34 | }
35 |
36 | @Test
37 | void testRunFileOperationZipDirectoryToDefaultOutput() throws Exception {
38 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
39 | List fop = new ArrayList<>();
40 | fop.add(new FileCreateOperation("source-directory/nested-folder1/TestFile1-1", ""));
41 | fop.add(new FileCreateOperation("source-directory/nested-folder2/TestFile2-1", ""));
42 | fop.add(new FileCreateOperation("source-directory/nested-folder2/TestFile2-2", ""));
43 | fop.add(new FileZipOperation("source-directory", null));
44 | fop.add(new FileUnZipOperation("source-directory.zip", "unzipped-directory"));
45 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
46 |
47 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
48 |
49 | assertEquals(Result.SUCCESS, build.getResult());
50 | assertTrue(build.getWorkspace().child("unzipped-directory").exists());
51 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder1/TestFile1-1").exists());
52 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder2/TestFile2-1").exists());
53 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder2/TestFile2-2").exists());
54 | }
55 |
56 | @Test
57 | void testRunFileOperationZipFileToDefaultOutput() throws Exception {
58 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
59 | List fop = new ArrayList<>();
60 | fop.add(new FileCreateOperation("source-directory/nested-folder1/TestFile1-1", ""));
61 | fop.add(new FileZipOperation("source-directory/nested-folder1/TestFile1-1", null));
62 | fop.add(new FileUnZipOperation("TestFile1-1.zip", "unzipped-directory"));
63 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
64 |
65 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
66 |
67 | assertEquals(Result.SUCCESS, build.getResult());
68 | assertTrue(build.getWorkspace().child("unzipped-directory").exists());
69 | assertTrue(build.getWorkspace().child("unzipped-directory/TestFile1-1").exists());
70 | }
71 |
72 | @Test
73 | void testRunFileOperationZipDirectoryToCustomOutput() throws Exception {
74 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
75 | List fop = new ArrayList<>();
76 | fop.add(new FileCreateOperation("source-directory/nested-folder1/TestFile1-1", ""));
77 | fop.add(new FileCreateOperation("source-directory/nested-folder2/TestFile2-1", ""));
78 | fop.add(new FileCreateOperation("source-directory/nested-folder2/TestFile2-2", ""));
79 | fop.add(new FileZipOperation("source-directory", "output-directory/nested-output-folder1"));
80 | fop.add(new FileUnZipOperation(
81 | "output-directory/nested-output-folder1/source-directory.zip",
82 | "unzipped-directory"));
83 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
84 |
85 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
86 |
87 | assertEquals(Result.SUCCESS, build.getResult());
88 | assertTrue(build.getWorkspace().child("unzipped-directory").exists());
89 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder1/TestFile1-1").exists());
90 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder2/TestFile2-1").exists());
91 | assertTrue(build.getWorkspace().child("unzipped-directory/source-directory/nested-folder2/TestFile2-2").exists());
92 | }
93 |
94 | @Test
95 | void testRunFileOperationZipFileToCustomOutput() throws Exception {
96 | FreeStyleProject p1 = jenkins.createFreeStyleProject("build1");
97 | List fop = new ArrayList<>();
98 | fop.add(new FileCreateOperation("source-directory/nested-folder1/TestFile1-1", ""));
99 | fop.add(new FileZipOperation(
100 | "source-directory/nested-folder1/TestFile1-1",
101 | "output-directory/nested-output-folder1"));
102 | fop.add(new FileUnZipOperation(
103 | "output-directory/nested-output-folder1/TestFile1-1.zip",
104 | "unzipped-directory"));
105 | p1.getBuildersList().add(new FileOperationsBuilder(fop));
106 |
107 | FreeStyleBuild build = p1.scheduleBuild2(0).get();
108 |
109 | assertEquals(Result.SUCCESS, build.getResult());
110 | assertTrue(build.getWorkspace().child("unzipped-directory").exists());
111 | assertTrue(build.getWorkspace().child("unzipped-directory/TestFile1-1").exists());
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FolderCopyOperationTest.java:
--------------------------------------------------------------------------------
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.jupiter.api.BeforeEach;
10 | import org.junit.jupiter.api.Test;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import java.util.List;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import static org.junit.jupiter.api.Assertions.assertEquals;
17 | import static org.junit.jupiter.api.Assertions.assertTrue;
18 |
19 | @WithJenkins
20 | class FolderCopyOperationTest {
21 |
22 | private JenkinsRule jenkins;
23 |
24 | @BeforeEach
25 | void setUp(JenkinsRule rule) {
26 | jenkins = rule;
27 | }
28 |
29 | @Test
30 | void testDefaults() {
31 | String sourceFolderPath = "sourceFolder";
32 | String destinationFolderPath = "destinationFolder";
33 | FolderCopyOperation operation = new FolderCopyOperation(sourceFolderPath, destinationFolderPath);
34 |
35 | assertEquals(sourceFolderPath, operation.getSourceFolderPath());
36 | assertEquals(destinationFolderPath, operation.getDestinationFolderPath());
37 | }
38 |
39 | @Test
40 | void testRunFolderCopyOperation() throws Exception {
41 | FreeStyleProject project = jenkins.createFreeStyleProject("folderCopyTest");
42 |
43 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
44 | FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
45 | FilePath destinationFolder = new FilePath(workspace, "destinationFolder");
46 |
47 | sourceFolder.mkdirs();
48 | FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
49 | fileInSource.write("Sample content", "UTF-8");
50 |
51 | FolderCopyOperation operation = new FolderCopyOperation("sourceFolder", "destinationFolder");
52 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
53 |
54 | FreeStyleBuild build = project.scheduleBuild2(0).get();
55 | assertEquals(Result.SUCCESS, build.getResult());
56 |
57 | FilePath copiedFile = new FilePath(destinationFolder, "file.txt");
58 | assertTrue(destinationFolder.exists(), "The destination folder should have been created");
59 | assertTrue(copiedFile.exists(), "The file should have been copied to the destination folder");
60 | assertEquals("Sample content", copiedFile.readToString());
61 | }
62 |
63 | @Test
64 | void testRunFolderCopyOperationWithTokens() throws Exception {
65 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
66 | EnvVars envVars = prop.getEnvVars();
67 | envVars.put("SOURCE_FOLDER", "sourceFolder");
68 | envVars.put("DESTINATION_FOLDER", "destinationFolder");
69 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
70 |
71 | FreeStyleProject project = jenkins.createFreeStyleProject("folderCopyTestWithTokens");
72 |
73 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
74 | FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
75 | FilePath destinationFolder = new FilePath(workspace, "destinationFolder");
76 |
77 | sourceFolder.mkdirs();
78 | FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
79 | fileInSource.write("Sample content", "UTF-8");
80 |
81 | FolderCopyOperation operation = new FolderCopyOperation("$SOURCE_FOLDER", "$DESTINATION_FOLDER");
82 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
83 |
84 | FreeStyleBuild build = project.scheduleBuild2(0).get();
85 | assertEquals(Result.SUCCESS, build.getResult());
86 |
87 | FilePath copiedFile = new FilePath(destinationFolder, "file.txt");
88 | assertTrue(destinationFolder.exists(), "The destination folder should have been created");
89 | assertTrue(copiedFile.exists(), "The file should have been copied to the destination folder");
90 | assertEquals("Sample content", copiedFile.readToString());
91 | }
92 | }
93 |
94 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FolderCreateOperationTest.java:
--------------------------------------------------------------------------------
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.jupiter.api.BeforeEach;
10 | import org.junit.jupiter.api.Test;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import java.util.List;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import static org.junit.jupiter.api.Assertions.assertEquals;
17 | import static org.junit.jupiter.api.Assertions.assertTrue;
18 |
19 | @WithJenkins
20 | class FolderCreateOperationTest {
21 |
22 | private JenkinsRule jenkins;
23 |
24 | @BeforeEach
25 | void setUp(JenkinsRule rule) {
26 | jenkins = rule;
27 | }
28 | @Test
29 | void testDefaults() {
30 | String defaultFolderPath = "defaultFolder";
31 | FolderCreateOperation operation = new FolderCreateOperation(defaultFolderPath);
32 |
33 | assertEquals(defaultFolderPath, operation.getFolderPath());
34 | }
35 |
36 | @Test
37 | void testRunFolderCreateOperation() throws Exception {
38 | FreeStyleProject project = jenkins.createFreeStyleProject("folderCreateTest");
39 |
40 | String folderPath = "newFolder";
41 | FolderCreateOperation operation = new FolderCreateOperation(folderPath);
42 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
43 |
44 | FreeStyleBuild build = project.scheduleBuild2(0).get();
45 | assertEquals(Result.SUCCESS, build.getResult());
46 |
47 | FilePath folder = new FilePath(jenkins.jenkins.getWorkspaceFor(project), folderPath);
48 | assertTrue(folder.exists() && folder.isDirectory(), "The folder should have been created");
49 | }
50 |
51 | @Test
52 | void testRunFolderCreateOperationWithTokens() throws Exception {
53 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
54 | EnvVars envVars = prop.getEnvVars();
55 | envVars.put("FOLDER_PATH", "tokenFolder");
56 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
57 |
58 | FreeStyleProject project = jenkins.createFreeStyleProject("folderCreateTestWithTokens");
59 |
60 | FolderCreateOperation operation = new FolderCreateOperation("$FOLDER_PATH");
61 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
62 |
63 | FreeStyleBuild build = project.scheduleBuild2(0).get();
64 | assertEquals(Result.SUCCESS, build.getResult());
65 |
66 | FilePath folder = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "tokenFolder");
67 | assertTrue(folder.exists() && folder.isDirectory(), "The folder should have been created");
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FolderDeleteOperationTest.java:
--------------------------------------------------------------------------------
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.jupiter.api.BeforeEach;
10 | import org.junit.jupiter.api.Test;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import java.util.List;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import static org.junit.jupiter.api.Assertions.assertEquals;
17 | import static org.junit.jupiter.api.Assertions.assertFalse;
18 |
19 | @WithJenkins
20 | class FolderDeleteOperationTest {
21 |
22 | private JenkinsRule jenkins;
23 |
24 | @BeforeEach
25 | void setUp(JenkinsRule rule) {
26 | jenkins = rule;
27 | }
28 |
29 | @Test
30 | void testDefaults() {
31 | String folderPath = "folderToDelete";
32 | FolderDeleteOperation operation = new FolderDeleteOperation(folderPath);
33 |
34 | assertEquals(folderPath, operation.getFolderPath());
35 | }
36 |
37 | @Test
38 | void testRunFolderDeleteOperation() throws Exception {
39 | FreeStyleProject project = jenkins.createFreeStyleProject("folderDeleteTest");
40 |
41 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
42 | FilePath folderToDelete = new FilePath(workspace, "folderToDelete");
43 |
44 | folderToDelete.mkdirs();
45 | FilePath fileInFolder = new FilePath(folderToDelete, "file.txt");
46 | fileInFolder.write("Sample content", "UTF-8");
47 |
48 | FolderDeleteOperation operation = new FolderDeleteOperation("folderToDelete");
49 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
50 |
51 | FreeStyleBuild build = project.scheduleBuild2(0).get();
52 | assertEquals(Result.SUCCESS, build.getResult());
53 |
54 | assertFalse(folderToDelete.exists(), "The folder should have been deleted");
55 | }
56 |
57 | @Test
58 | void testRunFolderDeleteOperationWithTokens() throws Exception {
59 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
60 | EnvVars envVars = prop.getEnvVars();
61 | envVars.put("FOLDER_TO_DELETE", "folderToDelete");
62 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
63 |
64 | FreeStyleProject project = jenkins.createFreeStyleProject("folderDeleteTestWithTokens");
65 |
66 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
67 | FilePath folderToDelete = new FilePath(workspace, "folderToDelete");
68 |
69 | folderToDelete.mkdirs();
70 | FilePath fileInFolder = new FilePath(folderToDelete, "file.txt");
71 | fileInFolder.write("Sample content", "UTF-8");
72 |
73 | FolderDeleteOperation operation = new FolderDeleteOperation("$FOLDER_TO_DELETE");
74 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
75 |
76 | FreeStyleBuild build = project.scheduleBuild2(0).get();
77 | assertEquals(Result.SUCCESS, build.getResult());
78 |
79 | assertFalse(folderToDelete.exists(), "The folder should have been deleted");
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/test/java/sp/sd/fileoperations/FolderRenameOperationTest.java:
--------------------------------------------------------------------------------
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.jupiter.api.BeforeEach;
10 | import org.junit.jupiter.api.Test;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import java.util.List;
14 | import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
15 |
16 | import static org.junit.jupiter.api.Assertions.*;
17 |
18 | @WithJenkins
19 | class FolderRenameOperationTest {
20 |
21 | private JenkinsRule jenkins;
22 |
23 | @BeforeEach
24 | void setUp(JenkinsRule rule) {
25 | jenkins = rule;
26 | }
27 |
28 | @Test
29 | void testDefaults() {
30 | String sourceFolder = "sourceFolder";
31 | String destinationFolder = "destinationFolder";
32 | FolderRenameOperation operation = new FolderRenameOperation(sourceFolder, destinationFolder);
33 |
34 | assertEquals(sourceFolder, operation.getSource());
35 | assertEquals(destinationFolder, operation.getDestination());
36 | }
37 |
38 | @Test
39 | void testRunFolderRenameOperation() throws Exception {
40 | FreeStyleProject project = jenkins.createFreeStyleProject("folderRenameTest");
41 |
42 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
43 | FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
44 | FilePath destinationFolder = new FilePath(workspace, "destinationFolder");
45 |
46 | sourceFolder.mkdirs();
47 | FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
48 | fileInSource.write("Sample content", "UTF-8");
49 |
50 | FolderRenameOperation operation = new FolderRenameOperation("sourceFolder", "destinationFolder");
51 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
52 |
53 | FreeStyleBuild build = project.scheduleBuild2(0).get();
54 | assertEquals(Result.SUCCESS, build.getResult());
55 |
56 | assertFalse(sourceFolder.exists(), "The source folder should have been renamed");
57 | FilePath renamedFile = new FilePath(destinationFolder, "file.txt");
58 | assertTrue(renamedFile.exists(), "The file should have been moved to the destination folder");
59 | assertEquals("Sample content", renamedFile.readToString());
60 | }
61 |
62 | @Test
63 | void testRunFolderRenameOperationWithTokens() throws Exception {
64 | EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
65 | EnvVars envVars = prop.getEnvVars();
66 | envVars.put("SOURCE_FOLDER", "sourceFolder");
67 | envVars.put("DESTINATION_FOLDER", "destinationFolder");
68 | jenkins.jenkins.getGlobalNodeProperties().add(prop);
69 |
70 | FreeStyleProject project = jenkins.createFreeStyleProject("folderRenameTestWithTokens");
71 |
72 | FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
73 | FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
74 | FilePath destinationFolder = new FilePath(workspace, "destinationFolder");
75 |
76 | sourceFolder.mkdirs();
77 | FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
78 | fileInSource.write("Sample content", "UTF-8");
79 |
80 | FolderRenameOperation operation = new FolderRenameOperation("$SOURCE_FOLDER", "$DESTINATION_FOLDER");
81 | project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));
82 |
83 | FreeStyleBuild build = project.scheduleBuild2(0).get();
84 | assertEquals(Result.SUCCESS, build.getResult());
85 |
86 | assertFalse(sourceFolder.exists(), "The source folder should have been renamed");
87 | FilePath renamedFile = new FilePath(destinationFolder, "file.txt");
88 | assertTrue(renamedFile.exists(), "The file should have been moved to the destination folder");
89 | assertEquals("Sample content", renamedFile.readToString());
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/test/resources/__files/dummy.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jenkinsci/file-operations-plugin/afd7a2b52f885b99cfcd2f81b3dfeb3be6a869f2/src/test/resources/__files/dummy.zip
--------------------------------------------------------------------------------