[test] Test built-in rulesets for warnings while loading

This commit is contained in:
Andreas Dangel
2023-12-07 18:44:41 +01:00
parent 9f9a8f7068
commit aa93a75009
2 changed files with 21 additions and 2 deletions

View File

@ -21,7 +21,7 @@ import net.sourceforge.pmd.util.log.MessageReporter;
*
* @author Clément Fournier
*/
abstract class MessageReporterBase implements MessageReporter {
public abstract class MessageReporterBase implements MessageReporter {
private int numErrors;
private @Nullable Level minLevel = Level.TRACE;

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -34,6 +36,7 @@ import javax.xml.parsers.SAXParserFactory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.event.Level;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
@ -45,6 +48,7 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.rule.RuleReference;
import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.util.log.internal.MessageReporterBase;
/**
* Base test class to verify the language's rulesets. This class should be
@ -344,7 +348,22 @@ public abstract class AbstractRuleSetFactoryTest {
}
private RuleSet loadRuleSetByFileName(String ruleSetFileName) {
return new RuleSetLoader().loadFromResource(ruleSetFileName);
final StringBuilder messages = new StringBuilder();
class Reporter extends MessageReporterBase {
@Override
protected void logImpl(Level level, String message) {
messages.append(message).append(System.lineSeparator());
}
}
RuleSet ruleSet = new RuleSetLoader()
.withReporter(new Reporter())
.loadFromResource(ruleSetFileName);
assertThat("There should be no warnings while loading the ruleset",
messages.toString(), emptyString());
return ruleSet;
}
private boolean validateAgainstSchema(String fileName) throws IOException, SAXException {