Skip to content

Commit 8636ce9

Browse files
Migrate tests to JUnit5 (jenkinsci#171)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent ddca1b8 commit 8636ce9

9 files changed

Lines changed: 291 additions & 352 deletions

src/test/java/hudson/plugins/plot/CSVReaderTest.java

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,42 @@
55

66
package hudson.plugins.plot;
77

8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.fail;
8+
import static hudson.plugins.plot.SeriesTestUtils.WORKSPACE_ROOT_DIR;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertFalse;
1011

1112
import com.opencsv.CSVReader;
12-
import com.opencsv.exceptions.CsvValidationException;
1313
import hudson.FilePath;
14-
import java.io.IOException;
1514
import java.io.InputStream;
1615
import java.io.InputStreamReader;
1716
import java.util.logging.Logger;
18-
import org.apache.commons.io.IOUtils;
19-
import org.junit.Test;
17+
import org.junit.jupiter.api.Test;
2018

2119
/**
2220
* Test a CSV series.
2321
*
2422
* @author Allen Reese
2523
*/
26-
public class CSVReaderTest extends SeriesTestCase {
24+
class CSVReaderTest {
2725
private static final Logger LOGGER = Logger.getLogger(CSVReaderTest.class.getName());
2826

2927
private static final String[] FILES = {"test.csv", "test_trailing_spaces.csv", "test_trailing_semicolon.csv"};
3028
private static final int[] LINES = {2, 3, 2};
3129
private static final int[] COLUMNS = {8, 3, 9};
3230

3331
@Test
34-
public void testCSVReader() {
32+
void testCSVReader() throws Exception {
3533
for (int index = 0; index < FILES.length; index++) {
36-
CSVReader csvReader = null;
37-
InputStream inputStream = null;
38-
InputStreamReader inputReader = null;
3934

40-
FilePath[] seriesFiles;
41-
try {
42-
seriesFiles = workspaceRootDir.list(FILES[index]);
35+
FilePath[] seriesFiles = WORKSPACE_ROOT_DIR.list(FILES[index]);
36+
assertFalse(
37+
seriesFiles.length < 1,
38+
"No plot data file found: " + WORKSPACE_ROOT_DIR.getName() + " " + FILES[index]);
4339

44-
if (seriesFiles != null && seriesFiles.length < 1) {
45-
fail("No plot data file found: " + workspaceRootDir.getName() + " " + FILES[index]);
46-
}
47-
48-
LOGGER.info("Loading plot series data from: " + FILES[index]);
49-
50-
inputStream = seriesFiles[0].read();
51-
52-
inputReader = new InputStreamReader(inputStream);
53-
csvReader = new CSVReader(inputReader);
40+
LOGGER.info("Loading plot series data from: " + FILES[index]);
41+
try (InputStream inputStream = seriesFiles[0].read();
42+
InputStreamReader inputReader = new InputStreamReader(inputStream);
43+
CSVReader csvReader = new CSVReader(inputReader)) {
5444

5545
// save the header line to use it for the plot labels.
5646
String[] nextLine;
@@ -75,23 +65,11 @@ public void testCSVReader() {
7565
.append(",");
7666
}
7767
msg.append("' length ").append(nextLine.length);
78-
assertEquals(msg.toString(), COLUMNS[index], nextLine.length);
68+
assertEquals(COLUMNS[index], nextLine.length, msg.toString());
7969
}
8070
++lineNum;
8171
}
82-
assertEquals("Line count is not equal " + lineNum + " expected " + LINES[index], LINES[index], lineNum);
83-
} catch (CsvValidationException | IOException | InterruptedException e) {
84-
fail("Exception " + e);
85-
} finally {
86-
try {
87-
if (csvReader != null) {
88-
csvReader.close();
89-
}
90-
} catch (IOException e) {
91-
fail("Exception " + e);
92-
}
93-
IOUtils.closeQuietly(inputReader);
94-
IOUtils.closeQuietly(inputStream);
72+
assertEquals(LINES[index], lineNum, "Line count is not equal " + lineNum + " expected " + LINES[index]);
9573
}
9674
}
9775
}

src/test/java/hudson/plugins/plot/CSVSeriesTest.java

Lines changed: 53 additions & 81 deletions
Large diffs are not rendered by default.

src/test/java/hudson/plugins/plot/PlotBuildActionTest.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
package hudson.plugins.plot;
22

3-
import static org.junit.Assert.fail;
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
44

55
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
66
import hudson.model.Run;
77
import java.util.ArrayList;
8-
import java.util.ConcurrentModificationException;
98
import java.util.List;
109
import java.util.Random;
1110
import java.util.concurrent.CountDownLatch;
12-
import java.util.concurrent.ExecutionException;
1311
import java.util.concurrent.ExecutorService;
1412
import java.util.concurrent.Executors;
1513
import java.util.concurrent.FutureTask;
16-
import org.junit.Before;
17-
import org.junit.Rule;
18-
import org.junit.Test;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1916
import org.jvnet.hudson.test.Issue;
2017
import org.jvnet.hudson.test.JenkinsRule;
18+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2119

22-
public class PlotBuildActionTest {
23-
24-
@Rule
25-
public JenkinsRule r = new JenkinsRule();
20+
@WithJenkins
21+
class PlotBuildActionTest {
2622

2723
private PlotBuildAction plotBuildAction;
2824

29-
@Before
30-
public void setUp() throws Exception {
25+
@BeforeEach
26+
void setUp(JenkinsRule r) throws Exception {
3127
final Run<?, ?> run = r.buildAndAssertSuccess(r.createFreeStyleProject());
3228
final List<Plot> plots = new ArrayList<>();
3329
for (int i = 0; i < 30; i++) {
@@ -40,7 +36,7 @@ public void setUp() throws Exception {
4036

4137
@Issue("JENKINS-48465")
4238
@Test
43-
public void checksNoConcurrentModificationExceptionIsThrownForPlotsListAccess() throws Exception {
39+
void checksNoConcurrentModificationExceptionIsThrownForPlotsListAccess() throws Exception {
4440
int tasksCount = 10;
4541
ExecutorService executorService = Executors.newFixedThreadPool(2);
4642
List<FutureTask<Object>> tasks = new ArrayList<>();
@@ -65,7 +61,7 @@ private void simulateConcurrentModificationException(
6561
// close to "real world"
6662
PureJavaReflectionProvider provider = new PureJavaReflectionProvider();
6763
provider.visitSerializableFields(plotBuildAction, (fieldName, fieldType, definedIn, value) -> {
68-
if (value != null && value instanceof List) {
64+
if (value instanceof List) {
6965
List<Plot> plots = (List<Plot>) value;
7066
// simulate ConcurrentModificationException
7167
for (Plot p : plots) {
@@ -85,25 +81,21 @@ private void simulateConcurrentModificationException(
8581
}
8682
}
8783

88-
private void waitForAllThreadsToFinish(ExecutorService executorService, CountDownLatch latch) {
89-
try {
90-
latch.await();
91-
} catch (InterruptedException e) {
92-
e.printStackTrace();
93-
}
84+
private static void waitForAllThreadsToFinish(ExecutorService executorService, CountDownLatch latch)
85+
throws Exception {
86+
latch.await();
9487
executorService.shutdown();
9588
}
9689

97-
private void assertNoConcurrentModificationExceptionThrown(List<FutureTask<Object>> tasks)
98-
throws InterruptedException {
99-
try {
100-
// we expect here no ConcurrentModificationException
101-
// otherwise access to plots list is not synchronized
102-
for (FutureTask task : tasks) {
103-
task.get();
104-
}
105-
} catch (ExecutionException | ConcurrentModificationException e) {
106-
fail("Access to PlotBuildAction#plots list is not synchronized");
107-
}
90+
private static void assertNoConcurrentModificationExceptionThrown(List<FutureTask<Object>> tasks) {
91+
assertDoesNotThrow(
92+
() -> {
93+
// we expect here no ConcurrentModificationException
94+
// otherwise access to plots list is not synchronized
95+
for (FutureTask<?> task : tasks) {
96+
task.get();
97+
}
98+
},
99+
"Access to PlotBuildAction#plots list is not synchronized");
108100
}
109101
}

src/test/java/hudson/plugins/plot/PlotBuilderTest.java

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,91 @@
22

33
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
44
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
5-
import org.junit.Rule;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
76
import org.jvnet.hudson.test.JenkinsRule;
7+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
88

9-
public class PlotBuilderTest {
10-
11-
@Rule
12-
public JenkinsRule r = new JenkinsRule();
9+
@WithJenkins
10+
class PlotBuilderTest {
1311

1412
@Test
15-
public void testWithMinimalPipelineArgs() throws Exception {
13+
void testWithMinimalPipelineArgs(JenkinsRule r) throws Exception {
1614
WorkflowJob p = r.createProject(WorkflowJob.class, "projectUnderTest");
1715
p.setDefinition(new CpsFlowDefinition(
18-
"node { \n"
19-
+ " plot csvFileName: 'plot-minimal.csv',\n"
20-
+ " group: 'My Data',\n"
21-
+ " style: 'line'\n"
22-
+ "}",
16+
"""
17+
node { \s
18+
plot csvFileName: 'plot-minimal.csv',
19+
group: 'My Data',
20+
style: 'line'
21+
}""",
2322
true));
2423
r.buildAndAssertSuccess(p);
2524
}
2625

2726
@Test
28-
public void testWithXML() throws Exception {
27+
void testWithXML(JenkinsRule r) throws Exception {
2928
WorkflowJob p = r.createProject(WorkflowJob.class, "projectUnderTest");
3029
p.setDefinition(new CpsFlowDefinition(
31-
"node { \n"
32-
+ " def content = '<my_data>'\n"
33-
+ " content += '<test value1=\"123.456\"/>'\n"
34-
+ " content += '<test value2=\"321.654\"/>'\n"
35-
+ " content += '</my_data>'\n"
36-
+ " writeFile file: 'data.xml', text: content\n"
37-
+ " plot csvFileName: 'plot-xml.csv',\n"
38-
+ " group: 'My Data',\n"
39-
+ " title: 'Useful Title',\n"
40-
+ " style: 'line',\n"
41-
+ " yaxis: 'arbitrary',\n"
42-
+ " xmlSeries: [\n"
43-
+ " [file: 'data.xml',\n"
44-
+ " nodeType: 'NODESET',\n"
45-
+ " xpath: 'my_data/test/@*']\n"
46-
+ " ]\n"
47-
+ "}",
30+
"""
31+
node { \s
32+
def content = '<my_data>'
33+
content += '<test value1="123.456"/>'
34+
content += '<test value2="321.654"/>'
35+
content += '</my_data>'
36+
writeFile file: 'data.xml', text: content
37+
plot csvFileName: 'plot-xml.csv',
38+
group: 'My Data',
39+
title: 'Useful Title',
40+
style: 'line',
41+
yaxis: 'arbitrary',
42+
xmlSeries: [
43+
[file: 'data.xml',
44+
nodeType: 'NODESET',
45+
xpath: 'my_data/test/@*']
46+
]
47+
}""",
4848
true));
4949
r.buildAndAssertSuccess(p);
5050
}
5151

5252
@Test
53-
public void testWithCSV() throws Exception {
53+
void testWithCSV(JenkinsRule r) throws Exception {
5454
WorkflowJob p = r.createProject(WorkflowJob.class, "projectUnderTest");
5555
p.setDefinition(new CpsFlowDefinition(
56-
"node { \n"
57-
+ " def content = 'Avg,Median,90,min,max,samples,errors,error %'\n"
58-
+ " content += '515.33,196,1117,2,16550,97560,360,0.37'\n"
59-
+ " writeFile file: 'data.csv', text: content\n"
60-
+ " plot csvFileName: 'plot-csv.csv',\n"
61-
+ " group: 'My Data',\n"
62-
+ " title: 'Useful Title',\n"
63-
+ " style: 'line',\n"
64-
+ " yaxis: 'arbitrary',\n"
65-
+ " csvSeries: [[file: 'data.csv']]\n"
66-
+ "}",
56+
"""
57+
node { \s
58+
def content = 'Avg,Median,90,min,max,samples,errors,error %'
59+
content += '515.33,196,1117,2,16550,97560,360,0.37'
60+
writeFile file: 'data.csv', text: content
61+
plot csvFileName: 'plot-csv.csv',
62+
group: 'My Data',
63+
title: 'Useful Title',
64+
style: 'line',
65+
yaxis: 'arbitrary',
66+
csvSeries: [[file: 'data.csv']]
67+
}""",
6768
true));
6869
r.buildAndAssertSuccess(p);
6970
}
7071

7172
@Test
72-
public void testWithProperties() throws Exception {
73+
void testWithProperties(JenkinsRule r) throws Exception {
7374
WorkflowJob p = r.createProject(WorkflowJob.class, "projectUnderTest");
7475
p.setDefinition(new CpsFlowDefinition(
75-
"node { \n"
76-
+ " def content = 'YVALUE=1'\n"
77-
+ " writeFile file: 'data.properties', text: content\n"
78-
+ " plot csvFileName: 'plot-properties.csv',\n"
79-
+ " group: 'My Data',\n"
80-
+ " title: 'Useful Title',\n"
81-
+ " style: 'line',\n"
82-
+ " yaxis: 'arbitrary',\n"
83-
+ " propertiesSeries: [\n"
84-
+ " [file: 'data.properties',\n"
85-
+ " label: 'My Label']\n"
86-
+ " ]\n"
87-
+ "}",
76+
"""
77+
node { \s
78+
def content = 'YVALUE=1'
79+
writeFile file: 'data.properties', text: content
80+
plot csvFileName: 'plot-properties.csv',
81+
group: 'My Data',
82+
title: 'Useful Title',
83+
style: 'line',
84+
yaxis: 'arbitrary',
85+
propertiesSeries: [
86+
[file: 'data.properties',
87+
label: 'My Label']
88+
]
89+
}""",
8890
true));
8991
r.buildAndAssertSuccess(p);
9092
}

0 commit comments

Comments
 (0)