[test-schema] Migrate test to JUnit5

This commit is contained in:
Andreas Dangel
2022-10-13 17:28:42 +02:00
parent 2d48dabcd0
commit 0170cc3073
2 changed files with 24 additions and 30 deletions

View File

@ -38,17 +38,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<artifactId>system-lambda</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -4,17 +4,15 @@
package net.sourceforge.pmd.test.schema;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.StringReader;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.jupiter.api.Test;
import org.xml.sax.InputSource;
import net.sourceforge.pmd.RuleContext;
@ -22,17 +20,15 @@ import net.sourceforge.pmd.lang.PlainTextLanguage;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import com.github.stefanbirkner.systemlambda.SystemLambda;
/**
* @author Clément Fournier
*/
public class TestSchemaParserTest {
@Rule
public final SystemErrRule errStreamCaptor = new SystemErrRule().muteForSuccessfulTests();
class TestSchemaParserTest {
@Test
public void testSchemaSimple() throws IOException {
void testSchemaSimple() throws IOException {
String file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<test-data\n"
+ " xmlns=\"http://pmd.sourceforge.net/rule-tests\"\n"
@ -59,11 +55,10 @@ public class TestSchemaParserTest {
RuleTestCollection parsed = parseFile(file);
assertEquals(2, parsed.getTests().size());
}
@Test
public void testSchemaDeprecatedAttr() throws IOException {
void testSchemaDeprecatedAttr() throws Exception {
String file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<test-data\n"
+ " xmlns=\"http://pmd.sourceforge.net/rule-tests\"\n"
@ -79,16 +74,17 @@ public class TestSchemaParserTest {
+ " </test-code>\n"
+ "</test-data>\n";
errStreamCaptor.enableLog();
RuleTestCollection parsed = parseFile(file);
String log = SystemLambda.tapSystemErr(() -> {
RuleTestCollection parsed = parseFile(file);
assertEquals(1, parsed.getTests().size());
});
assertEquals(1, parsed.getTests().size());
MatcherAssert.assertThat(errStreamCaptor.getLog(), containsString(" 6| <test-code regressionTest='false'>\n"
+ " ^^^^^^^^^^^^^^ Attribute 'regressionTest' is deprecated, use 'disabled' with inverted value\n"));
assertThat(log, containsString(" 6| <test-code regressionTest='false'>\n"
+ " ^^^^^^^^^^^^^^ Attribute 'regressionTest' is deprecated, use 'disabled' with inverted value\n"));
}
@Test
public void testUnknownProperty() throws IOException {
void testUnknownProperty() throws Exception {
String file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<test-data\n"
+ " xmlns=\"http://pmd.sourceforge.net/rule-tests\"\n"
@ -105,11 +101,12 @@ public class TestSchemaParserTest {
+ " </test-code>\n"
+ "</test-data>\n";
errStreamCaptor.enableLog();
assertThrows(IllegalStateException.class, () -> parseFile(file));
String log = SystemLambda.tapSystemErr(() -> {
assertThrows(IllegalStateException.class, () -> parseFile(file));
});
MatcherAssert.assertThat(errStreamCaptor.getLog(), containsString(" 8| <rule-property name='invalid_property'>foo</rule-property>\n"
+ " ^^^^ Unknown property, known property names are violationSuppressRegex, violationSuppressXPath\n"));
assertThat(log, containsString(" 8| <rule-property name='invalid_property'>foo</rule-property>\n"
+ " ^^^^ Unknown property, known property names are violationSuppressRegex, violationSuppressXPath\n"));
}
private RuleTestCollection parseFile(String file) throws IOException {