This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
IntegrationTest.java
181 lines (162 loc) · 5.88 KB
/
IntegrationTest.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package io.jenkins.plugins.analysis.core.testutil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.Function;
import org.apache.commons.io.FilenameUtils;
import org.junit.Rule;
import org.junit.jupiter.api.Tag;
import org.jvnet.hudson.test.JenkinsRule;
import static edu.hm.hafner.analysis.assertj.Assertions.*;
import edu.hm.hafner.util.ResourceTest;
import io.jenkins.plugins.analysis.core.model.StaticAnalysisTool;
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.
*
* @author Ullrich Hafner
*/
@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();
/**
* 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) {
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
* @param fileNames
* the files to copy
*/
protected void copyMultipleFilesToWorkspace(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) {
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 {
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 -> 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) {
return String.format(FILE_NAME_PATTERN, FilenameUtils.getBaseName(fileNamePrefix));
}
/**
* Returns the ID of a static analysis tool that is given by its class file. Uses the associated descriptor to
* obtain the ID.
*
* @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) {
Descriptor<?> descriptor = j.jenkins.getDescriptor(tool);
assertThat(descriptor).as("Descriptor for '%s' not found").isNotNull();
return descriptor.getId();
}
}