Skip to content

Commit

Permalink
infra: convert assertions to AssertJ
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Bananeweizen authored and Calixte committed Sep 29, 2023
1 parent 2b1f103 commit acc7208
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 48 deletions.
7 changes: 6 additions & 1 deletion config/checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,12 @@
<!-- Imports -->
<module name="AvoidStarImport"/>
<module name="AvoidStaticImport"/>
<module name="IllegalImport"/>
<module name="IllegalImport">
<property name="illegalClasses"
value="org.junit.jupiter.api.Assertions"/>
<message key="import.illegal"
value="Use org.assertj.core.api.Assertions instead."/>
</module>
<module name="ImportControl">
<property name="id" value="ImportControlMain"/>
<property name="file" value="${checkstyle.importcontrol.file}"/>
Expand Down
2 changes: 2 additions & 0 deletions net.sf.eclipsecs.checkstyle/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ bin.includes = META-INF/,\
jars.compile.order = .
source.. = metadata/
javacDefaultEncoding.. = UTF-8
additional.bundles = assertj-core,\
net.bytebuddy.byte-buddy
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
package net.sf.eclipsecs.checkstyle;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -56,13 +55,13 @@ public void testMetadataFiles() throws Exception {

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

assertTrue(modules.size() > 0, "no modules");
assertThat(modules).as("modules").isNotEmpty();

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

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

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

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

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

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

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

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

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

assertEquals(0, children.size(), packge + " checkstyle-metadata.xml must contain no rules");
assertThat(children)
.withFailMessage(() -> packge + " checkstyle-metadata.xml must contain no rules")
.isEmpty();
}
}

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

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

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

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

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

private static String getEclipseCsPath(String packageName, String fileName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package net.sf.eclipsecs.checkstyle.utils;

import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.fail;

import java.io.IOException;
import java.io.StringReader;
Expand Down
42 changes: 34 additions & 8 deletions net.sf.eclipsecs.target/net.sf.eclipsecs.target.target
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde?>
<!-- generated with https://github.com/eclipse-cbi/targetplatform-dsl -->
<target name="Eclipse Checkstyle" sequenceNumber="1695316836">
<target name="Eclipse Checkstyle" sequenceNumber="1695470918">
<locations>
<location includeMode="slicer" includeAllPlatforms="false" includeSource="true" includeConfigurePhase="true" type="InstallableUnit">
<unit id="org.eclipse.jdt.feature.group" version="3.18.800.v20210611-1600"/>
Expand All @@ -26,26 +26,38 @@
<unit id="org.junit.platform.suite.api" version="1.7.1.v20210222-1948"/>
<repository location="https://download.eclipse.org/tools/orbit/downloads/drops/R20210602031627/repository"/>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="MavenDependencies">
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="SnakeYaml">
<dependencies>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="Javassist">
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.29.2-GA</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="DOM4J">
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="JFreeCharts">
<dependencies>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
Expand All @@ -70,6 +82,20 @@
<version>1.1.0</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="direct" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="AssertJ">
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<type>jar</type>
</dependency>
</dependencies>
</location>
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven" label="ApacheCommons">
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
70 changes: 55 additions & 15 deletions net.sf.eclipsecs.target/net.sf.eclipsecs.target.tpd
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,36 @@ location "https://download.eclipse.org/tools/orbit/downloads/drops/R202106020316

// If the following part has errors and no syntax highlighting, then please use Help>About>Installation>Installed Software>Target Platform DSL>Uninstall.
// After restarting please install the current version from the URL in line 1.
maven MavenDependencies
maven ApacheCommons
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="org.dom4j"
artifactId="dom4j"
version="2.1.3"
}
dependency {
groupId="org.javassist"
artifactId="javassist"
version="3.29.2-GA"
groupId="org.apache.commons"
artifactId="commons-lang3"
version="3.12.0"
}
}
maven AssertJ
scope=compile
dependencyDepth=direct
missingManifest=generate
includeSources
{
dependency {
groupId="org.yaml"
artifactId="snakeyaml"
version="1.33"
groupId="org.assertj"
artifactId="assertj-core"
version="3.24.2"
}
}
maven JFreeCharts
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="org.jfree"
artifactId="jcommon"
Expand All @@ -76,9 +85,40 @@ maven MavenDependencies
artifactId="swtgraphics2d"
version="1.1.0"
}
}
maven DOM4J
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="org.apache.commons"
artifactId="commons-lang3"
version="3.12.0"
groupId="org.dom4j"
artifactId="dom4j"
version="2.1.3"
}
}
maven Javassist
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="org.javassist"
artifactId="javassist"
version="3.29.2-GA"
}
}
maven SnakeYaml
scope=compile
dependencyDepth=none
missingManifest=generate
includeSources
{
dependency {
groupId="org.yaml"
artifactId="snakeyaml"
version="1.33"
}
}
3 changes: 2 additions & 1 deletion net.sf.eclipsecs.ui/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ bin.includes = icons/,\
jars.compile.order = .
source.. = src/
additional.bundles = org.eclipse.jdt.core,\
org.eclipse.core.runtime
org.eclipse.core.runtime,\
assertj-core
javacDefaultEncoding.. = UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

package net.sf.eclipsecs.ui.quickfixes;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,13 +49,10 @@ public abstract class AbstractQuickfixTestCase {

protected void testQuickfix(final String testDataXml, final AbstractASTResolution quickfix)
throws Exception {
InputStream stream = getClass().getResourceAsStream(testDataXml);
assertNotNull(stream, "Cannot find resource " + testDataXml + " in package "
+ getClass().getPackage().getName());
try {
try (InputStream stream = getClass().getResourceAsStream(testDataXml)) {
assertThat(stream).withFailMessage(() -> "Cannot find resource " + testDataXml + " in package "
+ getClass().getPackage().getName()).isNotNull();
testQuickfix(stream, quickfix);
} finally {
stream.close();
}
}

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

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

}
Expand Down

0 comments on commit acc7208

Please sign in to comment.