Added some more methods to copy files to the workspace.
package io.jenkins.plugins.analysis.core.testutil;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.function.Function;
import org.apache.commons.io.FilenameUtils;
import org.junit.Rule;
@@ -22,12 +24,28 @@ import hudson.model.TopLevelItem;
*/
@Tag("IntegrationTest")
public abstract class IntegrationTest extends ResourceTest {
+ private static final String FILE_NAME_PATTERN = "%s-issues.txt";
+
/** Starts Jenkins and provides several useful helper methods. */
@Rule
public final JenkinsRule j = new JenkinsRule();
/**
- * Copies the specified files to the workspace using a generated file name.
+ * Copies the specified files to the workspace using a generated file name that uses the same suffix. So a
+ * pattern in the static analysis configuration can use the same regular expression for all types of tools.
+ *
+ * @param job
+ * the job to get the workspace for
+ * @param fileNames
+ * the files to copy
+ * @see #FILE_NAME_PATTERN
+ */
+ protected void copyFilesToWorkspaceWithSuffix(final TopLevelItem job, final String... fileNames) {
+ copy(job, fileNames, this::createWorkspaceFileName);
+ }
+
+ /**
+ * Copies the specified files to the workspace. The same file name will be used in the workspace.
*
* @param job
* the job to get the workspace for
@@ -35,18 +53,34 @@ public abstract class IntegrationTest extends ResourceTest {
* the files to copy
*/
protected void copyFilesToWorkspace(final TopLevelItem job, final String... fileNames) {
+ copy(job, fileNames, Function.identity());
+ }
+
+ /**
+ * Copies the specified files to the workspace. Uses the specified new file name in the workspace.
+ *
+ * @param job
+ * the job to get the workspace for
+ * @param from
+ * the file to copy
+ * @param to
+ * the file name in the workspace
+ */
+ protected void copySingleFileToWorkspace(final TopLevelItem job, final String from, final String to) {
try {
FilePath workspace = j.jenkins.getWorkspaceFor(job);
assertThat(workspace).isNotNull();
- for (String fileName : fileNames) {
- workspace.child(createWorkspaceFileName(fileName)).copyFrom(asInputStream(fileName));
- }
+ workspace.child(to).copyFrom(asInputStream(from));
}
catch (IOException | InterruptedException e) {
throw new AssertionError(e);
}
}
+ private void copy(final TopLevelItem job, final String[] fileNames, final Function<String, String> fileNameMapper) {
+ Arrays.stream(fileNames).forEach(fileName -> copyFilesToWorkspace(job, fileName, fileNameMapper.apply(fileName)));
+ }
+
/**
* Creates a pre-defined filename for a workspace file.
*
@@ -55,7 +89,7 @@ public abstract class IntegrationTest extends ResourceTest {
* @return the whole file name of the workspace file
*/
private String createWorkspaceFileName(final String fileNamePrefix) {
- return String.format("%s-issues.txt", FilenameUtils.getBaseName(fileNamePrefix));
+ return String.format(FILE_NAME_PATTERN, FilenameUtils.getBaseName(fileNamePrefix));
}
/**
