Skip to content

Commit 74ade32

Browse files
committed
Update all tests to JUnit 5
1 parent 427e06e commit 74ade32

11 files changed

Lines changed: 134 additions & 165 deletions

lib/pom.xml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@
7272
<!-- unit tests -->
7373
<dependency>
7474
<groupId>org.junit.jupiter</groupId>
75-
<artifactId>junit-jupiter-engine</artifactId>
76-
<scope>test</scope>
77-
</dependency>
78-
<dependency>
79-
<groupId>org.junit.vintage</groupId>
80-
<artifactId>junit-vintage-engine</artifactId>
75+
<artifactId>junit-jupiter</artifactId>
8176
<scope>test</scope>
8277
</dependency>
8378
<dependency>
@@ -119,12 +114,6 @@
119114
<version>1.5.6</version>
120115
<scope>test</scope>
121116
</dependency>
122-
<dependency>
123-
<groupId>org.awaitility</groupId>
124-
<artifactId>awaitility</artifactId>
125-
<version>4.2.0</version>
126-
<scope>test</scope>
127-
</dependency>
128117
</dependencies>
129118

130119
<build>

lib/src/test/java/org/sonarsource/scanner/lib/DirsTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@
2222
import java.io.File;
2323
import java.util.HashMap;
2424
import java.util.Map;
25-
import org.junit.Rule;
26-
import org.junit.Test;
27-
import org.junit.rules.TemporaryFolder;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
3029

31-
public class DirsTest {
30+
class DirsTest {
3231

3332
private final Map<String, String> p = new HashMap<>();
3433

35-
@Rule
36-
public TemporaryFolder temp = new TemporaryFolder();
37-
3834
@Test
39-
public void should_init_default_project_dirs() throws Exception {
35+
void should_init_default_project_dirs() throws Exception {
4036
new Dirs().init(p);
4137

4238
File projectDir = new File(p.get(AnalysisProperties.PROJECT_BASEDIR));
@@ -50,8 +46,7 @@ public void should_init_default_project_dirs() throws Exception {
5046
}
5147

5248
@Test
53-
public void should_set_relative_path_to_project_work_dir() throws Exception {
54-
File initialProjectDir = temp.getRoot();
49+
void should_set_relative_path_to_project_work_dir(@TempDir File initialProjectDir) throws Exception {
5550
p.put(ScannerProperties.WORK_DIR, "relative/path");
5651
p.put(AnalysisProperties.PROJECT_BASEDIR, initialProjectDir.getAbsolutePath());
5752
new Dirs().init(p);

lib/src/test/java/org/sonarsource/scanner/lib/internal/BootstrapIndexDownloaderTest.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,30 @@
2222
import java.io.IOException;
2323
import java.util.Collection;
2424
import org.assertj.core.groups.Tuple;
25-
import org.junit.Before;
26-
import org.junit.Rule;
27-
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
2927
import org.sonarsource.scanner.lib.internal.BootstrapIndexDownloader.JarEntry;
3028
import org.sonarsource.scanner.lib.internal.http.ServerConnection;
3129

3230
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3332
import static org.mockito.Mockito.mock;
3433
import static org.mockito.Mockito.times;
3534
import static org.mockito.Mockito.verify;
3635
import static org.mockito.Mockito.when;
3736

38-
public class BootstrapIndexDownloaderTest {
37+
class BootstrapIndexDownloaderTest {
3938
private ServerConnection connection;
4039
private BootstrapIndexDownloader bootstrapIndexDownloader;
4140

42-
@Rule
43-
public ExpectedException exception = ExpectedException.none();
44-
45-
@Before
46-
public void setUp() {
41+
@BeforeEach
42+
void setUp() {
4743
connection = mock(ServerConnection.class);
4844
bootstrapIndexDownloader = new BootstrapIndexDownloader(connection);
4945
}
5046

5147
@Test
52-
public void should_download_jar_files() throws Exception {
48+
void should_download_jar_files() throws Exception {
5349
// index of the files to download
5450
when(connection.callWebApi("/batch/index")).thenReturn(
5551
"cpd.jar|CA124VADFSDS\n" +
@@ -63,37 +59,38 @@ public void should_download_jar_files() throws Exception {
6359
}
6460

6561
@Test
66-
public void test_invalid_index() throws Exception {
62+
void test_invalid_index() throws Exception {
6763
when(connection.callWebApi("/batch/index")).thenReturn("cpd.jar\n");
6864

69-
exception.expect(IllegalStateException.class);
70-
exception.expectMessage("Fail to parse entry in bootstrap index: cpd.jar");
71-
72-
bootstrapIndexDownloader.getIndex();
65+
assertThatThrownBy(() -> bootstrapIndexDownloader.getIndex())
66+
.isInstanceOf(IllegalStateException.class)
67+
.hasMessage("Fail to parse entry in bootstrap index: cpd.jar");
7368
}
7469

7570
@Test
76-
public void test_handles_empty_line_gracefully() throws Exception {
71+
void test_handles_empty_line_gracefully() throws Exception {
7772
when(connection.callWebApi("/batch/index")).thenReturn("\n");
7873

7974
Collection<JarEntry> index = bootstrapIndexDownloader.getIndex();
8075
assertThat(index).hasSize(0);
8176
verify(connection, times(1)).callWebApi("/batch/index");
8277
}
8378

84-
@Test(expected = IllegalStateException.class)
85-
public void test_handles_empty_string_with_exception() throws Exception {
79+
@Test
80+
void test_handles_empty_string_with_exception() throws Exception {
8681
when(connection.callWebApi("/batch/index")).thenReturn("");
8782

88-
bootstrapIndexDownloader.getIndex();
83+
assertThatThrownBy(() -> bootstrapIndexDownloader.getIndex())
84+
.isInstanceOf(IllegalStateException.class)
85+
.hasMessage("Fail to parse entry in bootstrap index: ");
8986
}
9087

9188
@Test
92-
public void should_fail() throws IOException {
89+
void should_fail() throws IOException {
9390
when(connection.callWebApi("/batch/index")).thenThrow(new IOException("io error"));
9491

95-
exception.expect(IllegalStateException.class);
96-
exception.expectMessage("Fail to get bootstrap index from server");
97-
bootstrapIndexDownloader.getIndex();
92+
assertThatThrownBy(() -> bootstrapIndexDownloader.getIndex())
93+
.isInstanceOf(IllegalStateException.class)
94+
.hasMessage("Fail to get bootstrap index from server");
9895
}
9996
}

lib/src/test/java/org/sonarsource/scanner/lib/internal/ClassloadRulesTest.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,26 @@
1919
*/
2020
package org.sonarsource.scanner.lib.internal;
2121

22-
import static org.assertj.core.api.Assertions.assertThat;
23-
2422
import java.util.HashSet;
2523
import java.util.Set;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2626

27-
import org.junit.Test;
28-
import org.junit.Before;
27+
import static org.assertj.core.api.Assertions.assertThat;
2928

30-
public class ClassloadRulesTest {
29+
class ClassloadRulesTest {
3130
private ClassloadRules rules;
3231
private Set<String> maskRules;
3332
private Set<String> unmaskRules;
3433

35-
@Before
36-
public void setUp() {
34+
@BeforeEach
35+
void setUp() {
3736
maskRules = new HashSet<>();
3837
unmaskRules = new HashSet<>();
3938
}
4039

4140
@Test
42-
public void testUnmask() {
41+
void testUnmask() {
4342
unmaskRules.add("org.apache.ant.");
4443
rules = new ClassloadRules(maskRules, unmaskRules);
4544

@@ -50,12 +49,12 @@ public void testUnmask() {
5049
assertThat(rules.canLoad("org.apache.ant.Foo")).isTrue();
5150
assertThat(rules.canLoad("org.apache.ant.project.Project")).isTrue();
5251
}
53-
52+
5453
@Test
55-
public void testUnmaskAll() {
54+
void testUnmaskAll() {
5655
unmaskRules.add("");
5756
rules = new ClassloadRules(maskRules, unmaskRules);
58-
57+
5958
assertThat(rules.canLoad("org.sonar.runner.Foo")).isTrue();
6059
assertThat(rules.canLoad("org.objectweb.asm.ClassVisitor")).isTrue();
6160
assertThat(rules.canLoad("org.apache")).isTrue();
@@ -65,13 +64,13 @@ public void testUnmaskAll() {
6564
}
6665

6766
@Test
68-
public void testDefault() {
67+
void testDefault() {
6968
rules = new ClassloadRules(maskRules, unmaskRules);
7069
assertThat(rules.canLoad("org.sonar.runner.Foo")).isFalse();
7170
}
7271

7372
@Test
74-
public void testMaskAndUnmask() throws ClassNotFoundException {
73+
void testMaskAndUnmask() {
7574
unmaskRules.add("org.apache.ant.");
7675
maskRules.add("org.apache.ant.foo.");
7776
rules = new ClassloadRules(maskRules, unmaskRules);
@@ -80,27 +79,27 @@ public void testMaskAndUnmask() throws ClassNotFoundException {
8079
assertThat(rules.canLoad("org.apache.ant.foo.something")).isFalse();
8180
assertThat(rules.canLoad("org.apache")).isFalse();
8281
}
83-
82+
8483
@Test
85-
public void testUsedByMaven() {
86-
maskRules.add( "org.slf4j.LoggerFactory" );
84+
void testUsedByMaven() {
85+
maskRules.add("org.slf4j.LoggerFactory");
8786
// Include slf4j Logger that is exposed by some Sonar components
88-
unmaskRules.add( "org.slf4j.Logger" );
89-
unmaskRules.add( "org.slf4j.ILoggerFactory" );
87+
unmaskRules.add("org.slf4j.Logger");
88+
unmaskRules.add("org.slf4j.ILoggerFactory");
9089
// Exclude other slf4j classes
9190
// .unmask("org.slf4j.impl.")
92-
maskRules.add( "org.slf4j." );
91+
maskRules.add("org.slf4j.");
9392
// Exclude logback
94-
maskRules.add( "ch.qos.logback." );
95-
maskRules.add( "org.sonar." );
93+
maskRules.add("ch.qos.logback.");
94+
maskRules.add("org.sonar.");
9695
unmaskRules.add("org.sonar.runner.batch.");
9796
// Guava is not the same version in SonarQube classloader
98-
maskRules.add( "com.google.common" );
97+
maskRules.add("com.google.common");
9998
// Include everything else
100-
unmaskRules.add( "" );
101-
99+
unmaskRules.add("");
100+
102101
rules = new ClassloadRules(maskRules, unmaskRules);
103-
102+
104103
assertThat(rules.canLoad("org.sonar.runner.batch.IsolatedLauncher")).isTrue();
105104
}
106105
}

lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedClassloaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ void should_use_isolated_system_classloader_when_parent_is_excluded() throws Cla
5050
// JUnit is available in the parent classloader (classpath used to execute this test) but not in the core JVM
5151
assertThat(classLoader.loadClass("java.lang.String", false)).isNotNull();
5252

53-
assertThatThrownBy(() -> classLoader.loadClass("org.junit.Test", false)).isInstanceOf(ClassNotFoundException.class).hasMessageContaining("org.junit.Test");
53+
assertThatThrownBy(() -> classLoader.loadClass("org.junit.jupiter.api.Test", false)).isInstanceOf(ClassNotFoundException.class).hasMessageContaining("org.junit.jupiter.api.Test");
5454
classLoader.close();
5555
}
5656

5757
@Test
5858
void should_use_parent_to_load() throws ClassNotFoundException {
5959
ClassloadRules rules = mock(ClassloadRules.class);
60-
when(rules.canLoad("org.junit.Test")).thenReturn(true);
60+
when(rules.canLoad("org.junit.jupiter.api.Test")).thenReturn(true);
6161
classLoader = new IsolatedClassloader(getClass().getClassLoader(), rules);
62-
assertThat(classLoader.loadClass("org.junit.Test", false)).isNotNull();
62+
assertThat(classLoader.loadClass("org.junit.jupiter.api.Test", false)).isNotNull();
6363
}
6464

6565
@Test

lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherProxyTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,51 @@
2222
import java.net.URL;
2323
import java.net.URLClassLoader;
2424
import java.util.concurrent.Callable;
25-
import org.junit.Before;
26-
import org.junit.Test;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2930

30-
public class IsolatedLauncherProxyTest {
31+
class IsolatedLauncherProxyTest {
3132
private ClassLoader cl = null;
3233

33-
@Before
34+
@BeforeEach
3435
public void setUp() {
3536
cl = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader());
3637
}
3738

3839
@Test
39-
public void delegate_proxied() {
40+
void delegate_proxied() {
4041
String str = "test";
4142
CharSequence s = IsolatedLauncherProxy.create(cl, str, CharSequence.class);
4243
assertThat(s).isEqualTo(str);
4344
}
4445

45-
@Test(expected = IllegalStateException.class)
46-
public void exceptions_unwrapped() throws ReflectiveOperationException {
46+
@Test
47+
void exceptions_unwrapped() throws ReflectiveOperationException {
4748
Runnable r = IsolatedLauncherProxy.create(cl, Runnable.class, ExceptionThrower.class.getName());
48-
r.run();
49+
50+
assertThatThrownBy(r::run)
51+
.isInstanceOf(IllegalStateException.class);
4952
}
5053

5154
@Test
52-
public void create_proxied() throws Exception {
55+
void create_proxied() throws Exception {
5356
Callable<?> c = IsolatedLauncherProxy.create(cl, Callable.class, SimpleClass.class.getName());
5457
assertThat(c.getClass().getClassLoader()).isEqualTo(cl);
5558
assertThat(c.getClass().getClassLoader()).isNotEqualTo(Thread.currentThread().getContextClassLoader());
5659
assertThat(c.call()).isEqualTo(URLClassLoader.class.getSimpleName());
5760
}
5861

59-
public static class ExceptionThrower implements Runnable {
62+
static class ExceptionThrower implements Runnable {
6063
@Override
6164
public void run() {
6265
throw new IllegalStateException("message");
6366
}
6467
}
6568

66-
public static class SimpleClass implements Callable<String> {
69+
static class SimpleClass implements Callable<String> {
6770
@Override
6871
public String call() throws Exception {
6972
return Thread.currentThread().getContextClassLoader().getClass().getSimpleName();

lib/src/test/java/org/sonarsource/scanner/lib/internal/JarExtractorTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919
*/
2020
package org.sonarsource.scanner.lib.internal;
2121

22-
import org.junit.Test;
23-
import java.nio.charset.StandardCharsets;
2422
import java.nio.file.Files;
2523
import java.nio.file.Path;
26-
import static org.assertj.core.api.Assertions.*;
27-
import static org.junit.Assert.fail;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
29+
class JarExtractorTest {
30+
31+
private final JarExtractor underTest = new JarExtractor();
2832

29-
public class JarExtractorTest {
3033
@Test
31-
public void test_extract() throws Exception {
32-
Path jarFile = new JarExtractor().extractToTemp("fake");
34+
void test_extract() throws Exception {
35+
Path jarFile = underTest.extractToTemp("fake");
3336
assertThat(jarFile).exists();
34-
assertThat(new String(Files.readAllBytes(jarFile), StandardCharsets.UTF_8)).isEqualTo("Fake jar for unit tests");
37+
assertThat(Files.readString(jarFile)).isEqualTo("Fake jar for unit tests");
3538
assertThat(jarFile.toUri().toURL().toString()).doesNotContain("jar:file");
3639
}
3740

3841
@Test
39-
public void should_fail_to_extract() throws Exception {
40-
try {
41-
new JarExtractor().extractToTemp("unknown");
42-
fail();
43-
} catch (IllegalStateException e) {
44-
assertThat(e).hasMessage("Fail to extract unknown.jar");
45-
}
42+
void should_fail_to_extract() {
43+
assertThatThrownBy(() -> underTest.extractToTemp("unknown"))
44+
.isInstanceOf(IllegalStateException.class)
45+
.hasMessage("Fail to extract unknown.jar");
4646
}
4747
}

0 commit comments

Comments
 (0)