Fix tests

This commit is contained in:
Clément Fournier
2020-12-12 19:31:00 +01:00
parent 3cf619e8e4
commit 789e4aa730
9 changed files with 52 additions and 36 deletions

View File

@ -15,7 +15,6 @@ import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleSetLoader;
public class DefaultRulesetTest {
@ -30,15 +29,15 @@ public class DefaultRulesetTest {
@After
public void cleanup() {
Handler[] handlers = Logger.getLogger(RuleSetFactory.class.getName()).getHandlers();
Handler[] handlers = Logger.getLogger(RuleSetLoader.class.getName()).getHandlers();
for (Handler handler : handlers) {
Logger.getLogger(RuleSetFactory.class.getName()).removeHandler(handler);
Logger.getLogger(RuleSetLoader.class.getName()).removeHandler(handler);
}
}
@Test
public void loadQuickstartRuleset() {
Logger.getLogger(RuleSetFactory.class.getName()).addHandler(new Handler() {
Logger.getLogger(RuleSetLoader.class.getName()).addHandler(new Handler() {
@Override
public void publish(LogRecord record) {
Assert.fail("No Logging expected: " + record.getMessage());

View File

@ -278,7 +278,8 @@ public class PMD {
ruleSets = factory.loadFromResources(rulesetPaths);
printRuleNamesInDebug(ruleSets);
if (isEmpty(ruleSets)) {
String msg = "No rules found. Maybe you misspelled a rule name? (" + rulesetPaths + ')';
String msg = "No rules found. Maybe you misspelled a rule name? ("
+ String.join(",", rulesetPaths) + ')';
LOG.log(Level.SEVERE, msg);
throw new IllegalArgumentException(msg);
}

View File

@ -402,16 +402,25 @@ public class RuleSetReferenceId {
public InputStream getInputStream(final ResourceLoader rl) throws IOException {
if (externalRuleSetReferenceId == null) {
if (StringUtils.isBlank(ruleSetFileName)) {
throw new FileNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName
+ "'" + ". Make sure the resource is a valid file or URL and is on the CLASSPATH. "
+ "Here's the current classpath: " + System.getProperty("java.class.path"));
throw notFoundException();
}
try {
return rl.loadResourceAsStream(ruleSetFileName);
} catch (FileNotFoundException ignored) {
throw notFoundException();
}
return rl.loadResourceAsStream(ruleSetFileName);
} else {
return externalRuleSetReferenceId.getInputStream(rl);
}
}
private FileNotFoundException notFoundException() {
return new FileNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName
+ "'" + ". Make sure the resource is a valid file or URL and is on the classpath. "
+ "Here's the current classpath: "
+ System.getProperty("java.class.path"));
}
/**
* Return the String form of this Rule reference.
*

View File

@ -14,6 +14,9 @@ import java.net.URLConnection;
import java.nio.file.Files;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.annotation.InternalApi;
@ -61,7 +64,7 @@ public class ResourceLoader {
*
* @return InputStream
*/
public InputStream loadResourceAsStream(final String name) throws IOException {
public @NonNull InputStream loadResourceAsStream(final String name) throws IOException {
// Search file locations first
final File file = new File(name);
if (file.exists()) {
@ -79,18 +82,18 @@ public class ResourceLoader {
final HttpURLConnection connection = (HttpURLConnection) new URL(name).openConnection();
connection.setConnectTimeout(TIMEOUT);
connection.setReadTimeout(TIMEOUT);
return connection.getInputStream();
} catch (final Exception e) {
try {
return loadClassPathResourceAsStream(name);
} catch (final IOException ioe) {
throw new IOException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH", ioe);
InputStream is = connection.getInputStream();
if (is != null) {
return is;
}
} catch (final Exception e) {
return loadClassPathResourceAsStreamOrThrow(name);
}
throw new IOException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the classpath");
}
public InputStream loadClassPathResourceAsStream(final String name) throws IOException {
public @Nullable InputStream loadClassPathResourceAsStream(final String name) throws IOException {
/*
* Don't use getResourceAsStream to avoid reusing connections between threads
* See https://github.com/pmd/pmd/issues/234
@ -109,7 +112,7 @@ public class ResourceLoader {
}
}
public InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws IOException {
public @NonNull InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws IOException {
InputStream is = null;
try {
is = loadClassPathResourceAsStream(name);
@ -119,7 +122,7 @@ public class ResourceLoader {
if (is == null) {
throw new FileNotFoundException("Can't find resource " + name
+ ". Make sure the resource is on the CLASSPATH");
+ ". Make sure the resource is on the classpath");
}
return is;

View File

@ -59,7 +59,7 @@ public class RuleDocGeneratorTest {
public void testSingleRuleset() throws IOException {
RuleDocGenerator generator = new RuleDocGenerator(writer, root);
RuleSetLoader rsf = new RuleSetLoader();
RuleSetLoader rsf = new RuleSetLoader().includeDeprecatedRuleReferences(true);
RuleSet ruleset = rsf.loadFromResource("rulesets/ruledoctest/sample.xml");
generator.generate(Arrays.asList(ruleset),

View File

@ -4,9 +4,10 @@
package net.sourceforge.pmd.docs;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -17,11 +18,9 @@ import net.sourceforge.pmd.RuleSetLoader;
public class RuleSetResolverTest {
private static final List<String> excludedRulesets = new ArrayList<>();
static {
excludedRulesets.add(FilenameUtils.normalize("pmd-test/src/main/resources/rulesets/dummy/basic.xml"));
}
private static final List<String> EXCLUDED_RULESETS = listOf(
FilenameUtils.normalize("pmd-test/src/main/resources/rulesets/dummy/basic.xml")
);
@Test
public void resolveAllRulesets() {
@ -39,7 +38,7 @@ public class RuleSetResolverTest {
Iterator<String> it = additionalRulesets.iterator();
while (it.hasNext()) {
String filename = it.next();
for (String exclusion : excludedRulesets) {
for (String exclusion : EXCLUDED_RULESETS) {
if (filename.endsWith(exclusion)) {
it.remove();
break;

View File

@ -7,7 +7,6 @@ package net.sourceforge.pmd.cli;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import org.junit.Assert;
@ -39,7 +38,7 @@ public class CLITest extends BaseCLITest {
}
@Test
public void changeJavaVersion() throws IOException {
public void changeJavaVersion() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml", "-version", "1.5", "-language",
"java", "-debug", };
String resultFilename = runTest(args, "chgJavaVersion");
@ -54,14 +53,14 @@ public class CLITest extends BaseCLITest {
}
@Test
public void exitStatusWithViolations() throws IOException {
public void exitStatusWithViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", };
String resultFilename = runTest(args, "exitStatusWithViolations", 4);
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
}
@Test
public void exitStatusWithViolationsAndWithoutFailOnViolations() throws IOException {
public void exitStatusWithViolationsAndWithoutFailOnViolations() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/errorprone.xml", "-failOnViolation", "false", };
String resultFilename = runTest(args, "exitStatusWithViolationsAndWithoutFailOnViolations", 0);
assertTrue(FileUtil.findPatternInFile(new File(resultFilename), "Avoid empty if"));
@ -71,7 +70,7 @@ public class CLITest extends BaseCLITest {
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRuleset() throws Exception {
public void testWrongRuleset() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);
@ -85,7 +84,7 @@ public class CLITest extends BaseCLITest {
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRulesetWithRulename() throws Exception {
public void testWrongRulesetWithRulename() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/designn.xml/UseCollectionIsEmpty", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);
@ -99,7 +98,7 @@ public class CLITest extends BaseCLITest {
* See https://sourceforge.net/p/pmd/bugs/1231/
*/
@Test
public void testWrongRulename() throws Exception {
public void testWrongRulename() {
String[] args = { "-d", SOURCE_FOLDER, "-f", "text", "-R", "category/java/design.xml/ThisRuleDoesNotExist", };
String filename = TEST_OUPUT_DIRECTORY + "testWrongRuleset.txt";
createTestOutputFile(filename);

View File

@ -101,8 +101,11 @@ public class AbstractLanguageVersionTest {
}
Properties props = new Properties();
String rulesetsProperties = "category/" + simpleTerseName + "/categories.properties";
String rulesetsProperties = "/category/" + simpleTerseName + "/categories.properties";
try (InputStream inputStream = getClass().getResourceAsStream(rulesetsProperties)) {
if (inputStream == null) {
throw new IOException();
}
props.load(inputStream);
}
assertRulesetsAndCategoriesProperties(props);
@ -122,7 +125,7 @@ public class AbstractLanguageVersionTest {
}
Properties props = new Properties();
String rulesetsProperties = "rulesets/" + simpleTerseName + "/rulesets.properties";
String rulesetsProperties = "/rulesets/" + simpleTerseName + "/rulesets.properties";
InputStream inputStream = getClass().getResourceAsStream(rulesetsProperties);
if (inputStream != null) {
// rulesets.properties file exists

View File

@ -18,6 +18,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -285,11 +286,13 @@ public abstract class AbstractRuleSetFactoryTest {
private List<String> getRuleSetFileNames(String language) throws IOException {
List<String> ruleSetFileNames = new ArrayList<>();
Properties properties = new Properties();
@SuppressWarnings("PMD.CloseResource")
InputStream input = getClass().getResourceAsStream("rulesets/" + language + "/rulesets.properties");
if (input == null) {
// this might happen if a language is only support by CPD, but not
// by PMD
System.err.println("No ruleset found for language " + language);
return Collections.emptyList();
}
try (InputStream is = input) {
properties.load(is);