Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
[JENKINS-30551] Added a test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed May 19, 2018
1 parent 52461a9 commit 59d761b
Showing 1 changed file with 78 additions and 5 deletions.
@@ -1,5 +1,6 @@
package io.jenkins.plugins.analysis.core.testutil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.Function;
Expand All @@ -15,7 +16,10 @@

import hudson.FilePath;
import hudson.model.Descriptor;
import hudson.model.Slave;
import hudson.model.TopLevelItem;
import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;

/**
* Base class for integration tests in Jenkins.
Expand All @@ -31,13 +35,55 @@ public abstract class IntegrationTest extends ResourceTest {
public final JenkinsRule j = new JenkinsRule();

/**
* 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.
* Creates a {@link DumbSlave agent} with the specified label.
*
* @param label
* the label of the agent
*
* @return the agent
*/
@SuppressWarnings("illegalcatch")
protected Slave createAgent(final String label) {
try {
return j.createOnlineSlave(new LabelAtom(label));
}
catch (Exception e) {
throw new AssertionError(e);
}
}

/**
* Creates a file with the specified content in the workspace.
*
* @param job
* the job to get the workspace for
* @param fileName
* the files to create
* @param content
* the content of the file
*/
protected void createFileInWorkspace(final TopLevelItem job, final String fileName, final String content) {
try {
FilePath workspace = j.jenkins.getWorkspaceFor(job);
assertThat(workspace).isNotNull();

FilePath child = workspace.child(fileName);
child.copyFrom(new ByteArrayInputStream(content.getBytes()));
}
catch (IOException | InterruptedException e) {
throw new AssertionError(e);
}
}

/**
* 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 copyMultipleFilesToWorkspaceWithSuffix(final TopLevelItem job, final String... fileNames) {
Expand Down Expand Up @@ -67,9 +113,33 @@ protected void copyMultipleFilesToWorkspace(final TopLevelItem job, final String
* the file name in the workspace
*/
protected void copySingleFileToWorkspace(final TopLevelItem job, final String from, final String to) {
FilePath workspace = j.jenkins.getWorkspaceFor(job);
assertThat(workspace).isNotNull();

copySingleFileToWorkspace(workspace, from, to);
}

/**
* Copies the specified files to the workspace. Uses the specified new file name in the workspace.
*
* @param agent
* the agent to get the workspace for
* @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 Slave agent, final TopLevelItem job, final String from, final String to) {
FilePath workspace = agent.getWorkspaceFor(job);
assertThat(workspace).isNotNull();

copySingleFileToWorkspace(workspace, from, to);
}

private void copySingleFileToWorkspace(final FilePath workspace, final String from, final String to) {
try {
FilePath workspace = j.jenkins.getWorkspaceFor(job);
assertThat(workspace).isNotNull();
workspace.child(to).copyFrom(asInputStream(from));
}
catch (IOException | InterruptedException e) {
Expand All @@ -78,14 +148,16 @@ protected void copySingleFileToWorkspace(final TopLevelItem job, final String fr
}

private void copy(final TopLevelItem job, final String[] fileNames, final Function<String, String> fileNameMapper) {
Arrays.stream(fileNames).forEach(fileName -> copySingleFileToWorkspace(job, fileName, fileNameMapper.apply(fileName)));
Arrays.stream(fileNames)
.forEach(fileName -> copySingleFileToWorkspace(job, fileName, fileNameMapper.apply(fileName)));
}

/**
* Creates a pre-defined filename for a workspace file.
*
* @param fileNamePrefix
* prefix of the filename
*
* @return the whole file name of the workspace file
*/
private String createWorkspaceFileName(final String fileNamePrefix) {
Expand All @@ -98,6 +170,7 @@ private String createWorkspaceFileName(final String fileNamePrefix) {
*
* @param tool
* the class of the tool to get the ID from
*
* @return the ID of the analysis tool
*/
protected String getIdOf(final Class<? extends StaticAnalysisTool> tool) {
Expand Down

0 comments on commit 59d761b

Please sign in to comment.