Skip to content

Commit a097c2f

Browse files
Migrate tests to JUnit Jupiter
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 76953c5 commit a097c2f

28 files changed

+787
-803
lines changed

src/test/java/org/jenkinsci/plugins/p4/DefaultEnvironment.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@
3838
import java.util.logging.LogRecord;
3939
import java.util.logging.Logger;
4040

41-
import static org.junit.Assert.assertEquals;
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
4242

43-
abstract public class DefaultEnvironment {
43+
public abstract class DefaultEnvironment {
4444

45-
private static Logger logger = Logger.getLogger(DefaultEnvironment.class.getName());
45+
private static final Logger LOGGER = Logger.getLogger(DefaultEnvironment.class.getName());
4646

47-
protected final static String R15_1 = "r15.1";
48-
protected final static String R17_1 = "r17.1";
49-
protected final static String R18_1 = "r18.1";
47+
protected static final String R15_1 = "r15.1";
48+
protected static final String R17_1 = "r17.1";
49+
protected static final String R18_1 = "r18.1";
5050

51-
protected final static String R19_1 = "r19.1";
52-
protected final static String R24_1_r15 = "r24.1_r15"; //Binaries R24_1 checkpoint r15
53-
protected final static String R24_1_r17 = "r24.1_r17"; //Binaries R24_1 checkpoint r17
51+
protected static final String R19_1 = "r19.1";
52+
protected static final String R24_1_r15 = "r24.1_r15"; //Binaries R24_1 checkpoint r15
53+
protected static final String R24_1_r17 = "r24.1_r17"; //Binaries R24_1 checkpoint r17
5454

55-
protected final static String CREDENTIAL = "id";
56-
protected final static int HTTP_PORT = 1888;
57-
protected final static String HTTP_URL = "http://localhost:" + HTTP_PORT;
55+
protected static final String CREDENTIAL = "id";
56+
protected static final int HTTP_PORT = 1888;
57+
protected static final String HTTP_URL = "http://localhost:" + HTTP_PORT;
5858
protected final int LOG_LIMIT = 1000;
5959

6060
protected P4PasswordImpl createCredentials(String user, String password, String p4port, String id) throws IOException {
@@ -153,7 +153,7 @@ private void setP4Ignore(ConnectionHelper p4, String p4ignore) {
153153
}
154154

155155
private ManualWorkspaceImpl createWorkspace(String path) {
156-
String filename = path.substring(path.lastIndexOf("/") + 1, path.length());
156+
String filename = path.substring(path.lastIndexOf("/") + 1);
157157

158158
// Create workspace
159159
String client = "submit.ws";
@@ -162,7 +162,7 @@ private ManualWorkspaceImpl createWorkspace(String path) {
162162
String view = "\"" + path + "\"" + " //" + client + "/" + filename;
163163
WorkspaceSpec spec = new WorkspaceSpec(true, true, false, false, false, false, stream, line, view, null, null, null, true);
164164
ManualWorkspaceImpl workspace = new ManualWorkspaceImpl("none", true, client, spec, false);
165-
workspace.setExpand(new HashMap<String, String>());
165+
workspace.setExpand(new HashMap<>());
166166

167167
File wsRoot = new File("target/submit.ws").getAbsoluteFile();
168168
workspace.setRootPath(wsRoot.toString());
@@ -171,19 +171,19 @@ private ManualWorkspaceImpl createWorkspace(String path) {
171171
}
172172

173173
private StreamWorkspaceImpl createStreamsWorkspace(String path, int depth) {
174-
String p[] = path.substring(2).split("/");
174+
String[] p = path.substring(2).split("/");
175175

176-
StringBuffer sb = new StringBuffer("//");
176+
StringBuilder sb = new StringBuilder("//");
177177
for (int i = 0; i < depth; i++) {
178178
sb.append(p[i]);
179179
sb.append("/");
180180
}
181181

182-
String stream = sb.toString().substring(0, sb.lastIndexOf("/"));
182+
String stream = sb.substring(0, sb.lastIndexOf("/"));
183183

184184
String client = "stream.ws";
185185
StreamWorkspaceImpl workspace = new StreamWorkspaceImpl("none", false, stream, client);
186-
workspace.setExpand(new HashMap<String, String>());
186+
workspace.setExpand(new HashMap<>());
187187

188188
File wsRoot = new File("target/stream.ws").getAbsoluteFile();
189189
workspace.setRootPath(wsRoot.toString());
@@ -192,7 +192,7 @@ private StreamWorkspaceImpl createStreamsWorkspace(String path, int depth) {
192192
}
193193

194194
private FilePath createFilePath(String path, String content, Workspace workspace) throws IOException, InterruptedException {
195-
String filename = path.substring(path.lastIndexOf("/") + 1, path.length());
195+
String filename = path.substring(path.lastIndexOf("/") + 1);
196196

197197
File wsRoot = new File(workspace.getRootPath()).getAbsoluteFile();
198198

@@ -205,7 +205,7 @@ private FilePath createFilePath(String path, String content, Workspace workspace
205205
}
206206

207207
protected void commitFile(JenkinsRule jenkins, String path, String content) throws Exception {
208-
String filename = path.substring(path.lastIndexOf("/") + 1, path.length());
208+
String filename = path.substring(path.lastIndexOf("/") + 1);
209209

210210
// Create workspace
211211
String client = "graphCommit.ws";
@@ -248,16 +248,16 @@ protected boolean waitForBuild(WorkflowJob job, int buildNumber, final int retry
248248
while (r > 0) {
249249
r--;
250250
if (job.getLastBuild().number == buildNumber) {
251-
logger.info("waitForBuild(): Attempts: " + (retry - r));
251+
LOGGER.info("waitForBuild(): Attempts: " + (retry - r));
252252
return true;
253253
}
254254
Thread.sleep(delay);
255255
}
256-
logger.severe("Gave up waiting for build");
256+
LOGGER.severe("Gave up waiting for build");
257257
return false;
258258
}
259259

260-
public class TestHandler extends Handler {
260+
public static class TestHandler extends Handler {
261261

262262
private StringBuffer sb = new StringBuffer();
263263

src/test/java/org/jenkinsci/plugins/p4/ExtendedJenkinsRule.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/test/java/org/jenkinsci/plugins/p4/PerforceScmTest.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@
99
import org.jenkinsci.plugins.p4.populate.Populate;
1010
import org.jenkinsci.plugins.p4.workspace.StaticWorkspaceImpl;
1111
import org.jenkinsci.plugins.p4.workspace.Workspace;
12-
import org.junit.Rule;
13-
import org.junit.Test;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
1414
import org.jvnet.hudson.test.JenkinsRule;
15+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1516

1617
import java.io.IOException;
1718

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

22-
public class PerforceScmTest extends DefaultEnvironment {
23+
@WithJenkins
24+
class PerforceScmTest extends DefaultEnvironment {
2325

24-
@Rule
25-
public JenkinsRule jenkins = new JenkinsRule();
26+
private JenkinsRule jenkins;
27+
28+
@BeforeEach
29+
void beforeEach(JenkinsRule rule) {
30+
jenkins = rule;
31+
}
2632

2733
@Test
28-
public void testConfigBasic() throws Exception {
34+
void testConfigBasic() throws Exception {
2935
FreeStyleProject project = jenkins.createFreeStyleProject();
3036

3137
String credential = "123";
@@ -44,7 +50,7 @@ public void testConfigBasic() throws Exception {
4450
}
4551

4652
@Test
47-
public void testIsBuildParent() throws IOException {
53+
void testIsBuildParent() throws IOException {
4854
MatrixProject project = new MatrixProject("MatrixTest");
4955

5056
String credential = "123";
@@ -54,16 +60,16 @@ public void testIsBuildParent() throws IOException {
5460
project.setScm(scm);
5561

5662
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl());
57-
assertFalse("isBuildParent should be false for default execution strategy",
58-
scm.isBuildParent(project));
63+
assertFalse(scm.isBuildParent(project),
64+
"isBuildParent should be false for default execution strategy");
5965

6066
project.setExecutionStrategy(new MatrixOptions(true, false, false));
61-
assertTrue("isBuildParent should be true when MatrixOptions#buildParent is true",
62-
scm.isBuildParent(project));
67+
assertTrue(scm.isBuildParent(project),
68+
"isBuildParent should be true when MatrixOptions#buildParent is true");
6369

6470
project.setExecutionStrategy(new MatrixOptions(false, true, true));
65-
assertFalse("isBuildParent should be false when MatrixOptions#buildParent is false",
66-
scm.isBuildParent(project));
71+
assertFalse(scm.isBuildParent(project),
72+
"isBuildParent should be false when MatrixOptions#buildParent is false");
6773
}
6874

6975
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.jenkinsci.plugins.p4;
2+
3+
import org.junit.jupiter.api.extension.AfterEachCallback;
4+
import org.junit.jupiter.api.extension.BeforeEachCallback;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
7+
import java.io.File;
8+
9+
public class SampleServerExtension extends SimpleTestServer implements BeforeEachCallback, AfterEachCallback {
10+
11+
public SampleServerExtension(String root, String version) {
12+
super(root, version);
13+
}
14+
15+
@Override
16+
public void beforeEach(ExtensionContext context) throws Exception {
17+
clean();
18+
extract(new File(getResources() + "/depot.tar.gz"));
19+
restore(new File(getResources() + "/checkpoint.gz"));
20+
upgrade();
21+
}
22+
23+
@Override
24+
public void afterEach(ExtensionContext context) throws Exception {
25+
destroy();
26+
}
27+
}

src/test/java/org/jenkinsci/plugins/p4/SampleServerRule.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/test/java/org/jenkinsci/plugins/p4/SimpleTestServer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
import java.io.FileInputStream;
1818
import java.io.FileOutputStream;
1919
import java.io.IOException;
20-
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2121
import java.util.regex.Matcher;
2222
import java.util.regex.Pattern;
2323

2424
public class SimpleTestServer {
2525

26-
private static Logger logger = LoggerFactory.getLogger(SimpleTestServer.class);
26+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTestServer.class);
2727

2828
private static final String RESOURCES = "src/test/resources/";
2929

@@ -32,7 +32,7 @@ public class SimpleTestServer {
3232
private final String p4ver;
3333

3434
public SimpleTestServer(String root, String version) {
35-
String p4d = new File(RESOURCES + version).getAbsolutePath().toString();
35+
String p4d = new File(RESOURCES + version).getAbsolutePath();
3636
String os = System.getProperty("os.name").toLowerCase();
3737
if (os.contains("win")) {
3838
p4d += "/bin.ntx64/p4d.exe";
@@ -84,7 +84,7 @@ public void extract(File archive) throws Exception {
8484
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
8585
while (tarEntry != null) {
8686
File node = new File(p4root, tarEntry.getName());
87-
logger.debug("extracting: " + node.getCanonicalPath());
87+
LOGGER.debug("extracting: {}", node.getCanonicalPath());
8888
if (tarEntry.isDirectory()) {
8989
node.mkdirs();
9090
} else {
@@ -140,7 +140,7 @@ public int getVersion() throws Exception {
140140
executor.execute(cmdLine);
141141

142142
int version = 0;
143-
for (String line : outputStream.toString(Charset.forName("UTF-8")).split("\\n")) {
143+
for (String line : outputStream.toString(StandardCharsets.UTF_8).split("\\n")) {
144144
if (line.startsWith("Rev. P4D")) {
145145
Pattern p = Pattern.compile("\\d{4}\\.\\d{1}");
146146
Matcher m = p.matcher(line);
@@ -151,7 +151,7 @@ public int getVersion() throws Exception {
151151
}
152152
}
153153
}
154-
logger.info("P4D Version: " + version);
154+
LOGGER.info("P4D Version: {}", version);
155155
return version;
156156
}
157157

@@ -164,7 +164,7 @@ private int exec(String[] args) throws Exception {
164164
cmdLine.addArgument(arg);
165165
}
166166

167-
logger.debug("EXEC: " + cmdLine.toString());
167+
LOGGER.debug("EXEC: {}", cmdLine);
168168

169169
DefaultExecutor executor = new DefaultExecutor();
170170
return executor.execute(cmdLine);

src/test/java/org/jenkinsci/plugins/p4/build/GetExecutorTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.jenkinsci.plugins.p4.build;
22

33
import hudson.FilePath;
4-
import org.junit.Test;
4+
import org.junit.jupiter.api.Test;
55

66
import java.io.File;
77

8-
import static org.junit.Assert.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
99

10-
public class GetExecutorTest {
10+
class GetExecutorTest {
1111

1212
@Test
13-
public void testGetExecutor() {
13+
void testGetExecutor() {
1414
//Windows path test
1515
File windowsWorkspaceDir = new File("C:\\Windows\\workspace\\path");
1616
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);
@@ -25,7 +25,7 @@ public void testGetExecutor() {
2525
}
2626

2727
@Test
28-
public void testGetExecutorWhenUsingSubDirectory() {
28+
void testGetExecutorWhenUsingSubDirectory() {
2929
//Windows path test
3030
File windowsWorkspaceDir = new File("C:\\Windows\\workspace\\path\\subDir");
3131
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);
@@ -40,7 +40,7 @@ public void testGetExecutorWhenUsingSubDirectory() {
4040
}
4141

4242
@Test
43-
public void testGetExecutorWhenWorkspaceHasAtSignIn() {
43+
void testGetExecutorWhenWorkspaceHasAtSignIn() {
4444
//Windows path test
4545
File windowsWorkspaceDir = new File("C:\\Windows\\worksp@ce\\path");
4646
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);
@@ -55,7 +55,7 @@ public void testGetExecutorWhenWorkspaceHasAtSignIn() {
5555
}
5656

5757
@Test
58-
public void testGetExecutorUsingNonStandardExecutor() {
58+
void testGetExecutorUsingNonStandardExecutor() {
5959
//Windows path test
6060
File windowsWorkspaceDir = new File("C:\\Windows\\workspace\\path@2");
6161
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);
@@ -70,7 +70,7 @@ public void testGetExecutorUsingNonStandardExecutor() {
7070
}
7171

7272
@Test
73-
public void testGetExecutorWhenUsingSubDirectoryAndNonStandardExecutor() {
73+
void testGetExecutorWhenUsingSubDirectoryAndNonStandardExecutor() {
7474
//Windows path test
7575
File windowsWorkspaceDir = new File("C:\\Windows\\workspace\\path@2\\subDir");
7676
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);
@@ -85,7 +85,7 @@ public void testGetExecutorWhenUsingSubDirectoryAndNonStandardExecutor() {
8585
}
8686

8787
@Test
88-
public void testGetExecutorWhenWorkspaceHasAtSignInUsingNonStandardExecutor() {
88+
void testGetExecutorWhenWorkspaceHasAtSignInUsingNonStandardExecutor() {
8989
//Windows path test
9090
File windowsWorkspaceDir = new File("C:\\Windows\\worksp@1ce\\path@2");
9191
FilePath windowsWorkspace = new FilePath(windowsWorkspaceDir);

0 commit comments

Comments
 (0)