Skip to content

Commit acc7208

Browse files
BananeweizenCalixte
authored andcommitted
infra: convert assertions to AssertJ
Tests are way more readable and specific with AssertJ assertions. * add assertj and dependencies to target * add assertj as compile time dependency, so it doesn't become part of the released plugin * use IllegalImport to avoid plain JUnit assertions
1 parent 2b1f103 commit acc7208

File tree

8 files changed

+130
-48
lines changed

8 files changed

+130
-48
lines changed

config/checkstyle_checks.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,12 @@
389389
<!-- Imports -->
390390
<module name="AvoidStarImport"/>
391391
<module name="AvoidStaticImport"/>
392-
<module name="IllegalImport"/>
392+
<module name="IllegalImport">
393+
<property name="illegalClasses"
394+
value="org.junit.jupiter.api.Assertions"/>
395+
<message key="import.illegal"
396+
value="Use org.assertj.core.api.Assertions instead."/>
397+
</module>
393398
<module name="ImportControl">
394399
<property name="id" value="ImportControlMain"/>
395400
<property name="file" value="${checkstyle.importcontrol.file}"/>

net.sf.eclipsecs.checkstyle/build.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ bin.includes = META-INF/,\
66
jars.compile.order = .
77
source.. = metadata/
88
javacDefaultEncoding.. = UTF-8
9+
additional.bundles = assertj-core,\
10+
net.bytebuddy.byte-buddy

net.sf.eclipsecs.checkstyle/test/net/sf/eclipsecs/checkstyle/ChecksTest.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
package net.sf.eclipsecs.checkstyle;
2222

2323
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static org.junit.jupiter.api.Assertions.assertEquals;
25-
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.assertj.core.api.Assertions.assertThat;
2625

2726
import java.io.File;
2827
import java.io.FileInputStream;
@@ -56,13 +55,13 @@ public void testMetadataFiles() throws Exception {
5655

5756
final Set<String> packages = CheckUtil.getPackages(modules);
5857

59-
assertTrue(modules.size() > 0, "no modules");
58+
assertThat(modules).as("modules").isNotEmpty();
6059

6160
for (String p : packages) {
62-
assertTrue(new File(getEclipseCsPath(p, "")).exists(), "folder " + p + " must exist in eclipsecs");
61+
assertThat(new File(getEclipseCsPath(p, ""))).exists();
6362

6463
final Set<Class<?>> packgeModules = CheckUtil.getModulesInPackage(modules, p);
65-
assertTrue(packgeModules.size() > 0, "package must have modules");
64+
assertThat(packgeModules).as("package modules").isNotEmpty();
6665

6766
validateEclipseCsMetaXmlFile(new File(getEclipseCsPath(p, "/checkstyle-metadata.xml")), p);
6867

@@ -72,36 +71,48 @@ public void testMetadataFiles() throws Exception {
7271
}
7372

7473
private static void validateEclipseCsMetaXmlFile(File file, String packge) throws Exception {
75-
assertTrue(file.exists(), "'checkstyle-metadata.xml' must exist in eclipsecs inside " + packge);
74+
assertThat(file)
75+
.withFailMessage(
76+
() -> "'checkstyle-metadata.xml' must exist in eclipsecs inside " + packge)
77+
.exists();
7678

7779
final String input = new String(Files.readAllBytes(file.toPath()), UTF_8);
7880
final Document document = XmlUtil.getRawXml(file.getAbsolutePath(), input, input);
7981

8082
final NodeList ruleGroups = document.getElementsByTagName("rule-group-metadata");
8183

82-
assertEquals(1, ruleGroups.getLength(), packge + " checkstyle-metadata.xml must contain only one rule group");
84+
assertThat(ruleGroups.getLength())
85+
.withFailMessage(
86+
() -> packge + " checkstyle-metadata.xml must contain only one rule group")
87+
.isEqualTo(1);
8388

8489
for (int position = 0; position < ruleGroups.getLength(); position++) {
8590
final Node ruleGroup = ruleGroups.item(position);
8691
final Set<Node> children = XmlUtil.getChildrenElements(ruleGroup);
8792

88-
assertEquals(0, children.size(), packge + " checkstyle-metadata.xml must contain no rules");
93+
assertThat(children)
94+
.withFailMessage(() -> packge + " checkstyle-metadata.xml must contain no rules")
95+
.isEmpty();
8996
}
9097
}
9198

9299
private static void validateEclipseCsMetaPropFile(File file, String packge) throws Exception {
93-
assertTrue(file.exists(), "'checkstyle-metadata.properties' must exist in eclipsecs inside " + packge);
100+
assertThat(file).withFailMessage(
101+
() -> "'checkstyle-metadata.properties' must exist in eclipsecs inside " + packge)
102+
.exists();
94103

95104
final Properties prop = new Properties();
96105
prop.load(new FileInputStream(file));
97106

98107
final Set<Object> properties = new HashSet<>(Collections.list(prop.keys()));
99108

100-
assertEquals(1, properties.size(),
101-
packge + " checkstyle-metadata.properties must contain only the rule group name");
109+
assertThat(properties).withFailMessage(
110+
() -> packge + " checkstyle-metadata.properties must contain only the rule group name")
111+
.hasSize(1);
102112

103-
assertTrue(properties.iterator().next().toString().endsWith(".group"),
104-
packge + " checkstyle-metadata.properties must contain only the rule group name");
113+
assertThat(properties.iterator().next().toString()).withFailMessage(
114+
() -> packge + " checkstyle-metadata.properties must contain only the rule group name")
115+
.endsWith(".group");
105116
}
106117

107118
private static String getEclipseCsPath(String packageName, String fileName) throws IOException {

net.sf.eclipsecs.checkstyle/test/net/sf/eclipsecs/checkstyle/utils/XmlUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package net.sf.eclipsecs.checkstyle.utils;
2222

23-
import static org.junit.jupiter.api.Assertions.fail;
23+
import static org.assertj.core.api.Assertions.fail;
2424

2525
import java.io.IOException;
2626
import java.io.StringReader;

net.sf.eclipsecs.target/net.sf.eclipsecs.target.target

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<?pde?>
33
<!-- generated with https://github.com/eclipse-cbi/targetplatform-dsl -->
4-
<target name="Eclipse Checkstyle" sequenceNumber="1695316836">
4+
<target name="Eclipse Checkstyle" sequenceNumber="1695470918">
55
<locations>
66
<location includeMode="slicer" includeAllPlatforms="false" includeSource="true" includeConfigurePhase="true" type="InstallableUnit">
77
<unit id="org.eclipse.jdt.feature.group" version="3.18.800.v20210611-1600"/>
@@ -26,26 +26,38 @@
2626
<unit id="org.junit.platform.suite.api" version="1.7.1.v20210222-1948"/>
2727
<repository location="https://download.eclipse.org/tools/orbit/downloads/drops/R20210602031627/repository"/>
2828
</location>
29-
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="MavenDependencies">
29+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="SnakeYaml">
3030
<dependencies>
3131
<dependency>
32-
<groupId>org.dom4j</groupId>
33-
<artifactId>dom4j</artifactId>
34-
<version>2.1.3</version>
32+
<groupId>org.yaml</groupId>
33+
<artifactId>snakeyaml</artifactId>
34+
<version>1.33</version>
3535
<type>jar</type>
3636
</dependency>
37+
</dependencies>
38+
</location>
39+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="Javassist">
40+
<dependencies>
3741
<dependency>
3842
<groupId>org.javassist</groupId>
3943
<artifactId>javassist</artifactId>
4044
<version>3.29.2-GA</version>
4145
<type>jar</type>
4246
</dependency>
47+
</dependencies>
48+
</location>
49+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="DOM4J">
50+
<dependencies>
4351
<dependency>
44-
<groupId>org.yaml</groupId>
45-
<artifactId>snakeyaml</artifactId>
46-
<version>1.33</version>
52+
<groupId>org.dom4j</groupId>
53+
<artifactId>dom4j</artifactId>
54+
<version>2.1.3</version>
4755
<type>jar</type>
4856
</dependency>
57+
</dependencies>
58+
</location>
59+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="JFreeCharts">
60+
<dependencies>
4961
<dependency>
5062
<groupId>org.jfree</groupId>
5163
<artifactId>jcommon</artifactId>
@@ -70,6 +82,20 @@
7082
<version>1.1.0</version>
7183
<type>jar</type>
7284
</dependency>
85+
</dependencies>
86+
</location>
87+
<location includeDependencyDepth="direct" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="AssertJ">
88+
<dependencies>
89+
<dependency>
90+
<groupId>org.assertj</groupId>
91+
<artifactId>assertj-core</artifactId>
92+
<version>3.24.2</version>
93+
<type>jar</type>
94+
</dependency>
95+
</dependencies>
96+
</location>
97+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="ApacheCommons">
98+
<dependencies>
7399
<dependency>
74100
<groupId>org.apache.commons</groupId>
75101
<artifactId>commons-lang3</artifactId>

net.sf.eclipsecs.target/net.sf.eclipsecs.target.tpd

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,36 @@ location "https://download.eclipse.org/tools/orbit/downloads/drops/R202106020316
3535

3636
// If the following part has errors and no syntax highlighting, then please use Help>About>Installation>Installed Software>Target Platform DSL>Uninstall.
3737
// After restarting please install the current version from the URL in line 1.
38-
maven MavenDependencies
38+
maven ApacheCommons
3939
scope=compile
4040
dependencyDepth=none
4141
missingManifest=generate
4242
includeSources
4343
{
4444
dependency {
45-
groupId="org.dom4j"
46-
artifactId="dom4j"
47-
version="2.1.3"
48-
}
49-
dependency {
50-
groupId="org.javassist"
51-
artifactId="javassist"
52-
version="3.29.2-GA"
45+
groupId="org.apache.commons"
46+
artifactId="commons-lang3"
47+
version="3.12.0"
5348
}
49+
}
50+
maven AssertJ
51+
scope=compile
52+
dependencyDepth=direct
53+
missingManifest=generate
54+
includeSources
55+
{
5456
dependency {
55-
groupId="org.yaml"
56-
artifactId="snakeyaml"
57-
version="1.33"
57+
groupId="org.assertj"
58+
artifactId="assertj-core"
59+
version="3.24.2"
5860
}
61+
}
62+
maven JFreeCharts
63+
scope=compile
64+
dependencyDepth=none
65+
missingManifest=generate
66+
includeSources
67+
{
5968
dependency {
6069
groupId="org.jfree"
6170
artifactId="jcommon"
@@ -76,9 +85,40 @@ maven MavenDependencies
7685
artifactId="swtgraphics2d"
7786
version="1.1.0"
7887
}
88+
}
89+
maven DOM4J
90+
scope=compile
91+
dependencyDepth=none
92+
missingManifest=generate
93+
includeSources
94+
{
7995
dependency {
80-
groupId="org.apache.commons"
81-
artifactId="commons-lang3"
82-
version="3.12.0"
96+
groupId="org.dom4j"
97+
artifactId="dom4j"
98+
version="2.1.3"
99+
}
100+
}
101+
maven Javassist
102+
scope=compile
103+
dependencyDepth=none
104+
missingManifest=generate
105+
includeSources
106+
{
107+
dependency {
108+
groupId="org.javassist"
109+
artifactId="javassist"
110+
version="3.29.2-GA"
111+
}
112+
}
113+
maven SnakeYaml
114+
scope=compile
115+
dependencyDepth=none
116+
missingManifest=generate
117+
includeSources
118+
{
119+
dependency {
120+
groupId="org.yaml"
121+
artifactId="snakeyaml"
122+
version="1.33"
83123
}
84124
}

net.sf.eclipsecs.ui/build.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ bin.includes = icons/,\
88
jars.compile.order = .
99
source.. = src/
1010
additional.bundles = org.eclipse.jdt.core,\
11-
org.eclipse.core.runtime
11+
org.eclipse.core.runtime,\
12+
assertj-core
1213
javacDefaultEncoding.. = UTF-8

net.sf.eclipsecs.ui/test/net/sf/eclipsecs/ui/quickfixes/AbstractQuickfixTestCase.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
package net.sf.eclipsecs.ui.quickfixes;
2222

23-
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.assertj.core.api.Assertions.assertThat;
2524

2625
import java.io.InputStream;
2726
import java.util.ArrayList;
@@ -50,13 +49,10 @@ public abstract class AbstractQuickfixTestCase {
5049

5150
protected void testQuickfix(final String testDataXml, final AbstractASTResolution quickfix)
5251
throws Exception {
53-
InputStream stream = getClass().getResourceAsStream(testDataXml);
54-
assertNotNull(stream, "Cannot find resource " + testDataXml + " in package "
55-
+ getClass().getPackage().getName());
56-
try {
52+
try (InputStream stream = getClass().getResourceAsStream(testDataXml)) {
53+
assertThat(stream).withFailMessage(() -> "Cannot find resource " + testDataXml + " in package "
54+
+ getClass().getPackage().getName()).isNotNull();
5755
testQuickfix(stream, quickfix);
58-
} finally {
59-
stream.close();
6056
}
6157
}
6258

@@ -88,7 +84,8 @@ protected void testQuickfix(InputStream testdataStream, AbstractASTResolution qu
8884
TextEdit edit = compUnit.rewrite(doc, options);
8985
edit.apply(doc);
9086

91-
assertEquals(testdata[i].result, doc.get().lines().map(String::stripTrailing).collect(Collectors.joining("\n")));
87+
String trailingSpaceRemoved = doc.get().lines().map(String::stripTrailing).collect(Collectors.joining("\n"));
88+
assertThat(trailingSpaceRemoved).isEqualTo(testdata[i].result);
9289
}
9390

9491
}

0 commit comments

Comments
 (0)