[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> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit</artifactId> <artifactId>junit-jupiter</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.stefanbirkner</groupId> <groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId> <artifactId>system-lambda</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

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