Fix test strings not being MessageFormat

This commit is contained in:
Clément Fournier committed 2022-04-16 17:53:04 +02:00
1 parent 1fd343619c
commit f9a1e840e0
3 files changed
+25 -1

No files matched your search

@@ -109,6 +109,7 @@ public enum RulePriority {
return null;
}
}
/**
* Returns the priority which corresponds to the given number as returned by
* {@link RulePriority#getPriority()}. If the number is an invalid value,
@@ -25,6 +25,9 @@ import net.sourceforge.pmd.annotation.InternalApi;
@InternalApi
public interface MessageReporter {
// todo change String to MessageFormat in those arg lists, it's too confusing
// where to apply MessageFormat otherwise...
boolean isLoggable(Level level);
default void log(Level level, String message, Object... formatArgs) {
@@ -13,6 +13,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.text.MessageFormat;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
@@ -56,7 +57,8 @@ public class RulesetFactoryTestBase {
return new Predicate<String>() {
@Override
public boolean test(String it) {
return it.contains(part);
String format = MessageFormat.format(it, new Object[0]);
return format.contains(part);
}
@Override
@@ -66,10 +68,16 @@ public class RulesetFactoryTestBase {
};
}
/**
* @param messageTest This is a MessageFormat string!
*/
protected void verifyFoundAWarningWithMessage(Predicate<String> messageTest) {
verifyFoundWarningWithMessage(times(1), messageTest);
}
/**
* @param messageTest This is a MessageFormat string!
*/
protected void verifyFoundWarningWithMessage(VerificationMode mode, Predicate<String> messageTest) {
verify(mockReporter, mode)
.logEx(eq(Level.WARN), argThat(messageTest::test), any(), any());
@@ -87,6 +95,11 @@ public class RulesetFactoryTestBase {
return loader.loadFromResource(resourceDir + "/" + ruleSetFilename);
}
/*
DSL to build a ruleset XML file with method calls.
*/
protected static @NonNull String rulesetXml(String... contents) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n"
+ "<ruleset name=\"Custom ruleset\" xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\"\n"
@@ -111,6 +124,13 @@ public class RulesetFactoryTestBase {
return rule(buildMap(dummyRuleDefAttrs(), attributes), body);
}
protected static @NonNull String dummyRule(String... body) {
return dummyRule(m -> { }, body);
}
/**
* Default attributes used by {@link #dummyRule(Consumer, String...)}.
*/
protected static Map<SchemaConstant, String> dummyRuleDefAttrs() {
return buildMap(
map -> {