Issue #2161: unify test input locations for checkstyle package

This commit is contained in:
rnveach 2015-10-20 11:32:01 -04:00 committed by Roman Ivanov
parent 5cdff34f23
commit 2fe5ade8b3
4 changed files with 72 additions and 69 deletions

View File

@ -58,8 +58,7 @@ public class AllChecksTest extends BaseCheckTestSupport {
public void testAllChecksWithDefaultConfiguration() throws Exception {
final Set<Class<?>> checkstyleChecks = getCheckstyleChecks();
final String inputFilePath = "src/test/resources-noncompilable/"
+ "com/puppycrawl/tools/checkstyle/InputDefaultConfig.java";
final String inputFilePath = getNonCompilablePath("InputDefaultConfig.java");
final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
for (Class<?> check : checkstyleChecks) {
@ -69,8 +68,7 @@ public class AllChecksTest extends BaseCheckTestSupport {
// Checks which have Check as a parent.
if (check.equals(ImportControlCheck.class)) {
// ImportControlCheck must have the import control configuration file to avoid violation.
checkConfig.addAttribute("file",
"src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/import-control_complete.xml");
checkConfig.addAttribute("file", getPath("import-control_complete.xml"));
}
checker = createChecker(checkConfig);
}

View File

@ -44,6 +44,9 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
* @author lkuehne
*/
public class ConfigurationLoaderTest {
private static String getConfigPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/configs/" + filename;
}
private static Configuration loadConfiguration(String name)
throws CheckstyleException {
@ -52,8 +55,7 @@ public class ConfigurationLoaderTest {
private static Configuration loadConfiguration(
String name, Properties props) throws CheckstyleException {
final String fName =
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/" + name;
final String fName = getConfigPath(name);
return ConfigurationLoader.loadConfiguration(
fName, new PropertiesExpander(props));
@ -66,7 +68,7 @@ public class ConfigurationLoaderTest {
// load config that's only found in the classpath
final DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/checkstyle_checks.xml", new PropertiesExpander(props));
getConfigPath("checkstyle_checks.xml"), new PropertiesExpander(props));
//verify the root, and property substitution
final Properties attributes = new Properties();
@ -334,8 +336,7 @@ public class ConfigurationLoaderTest {
final Properties props = new Properties();
props.setProperty("checkstyle.basedir", "basedir");
final File file = new File(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/subdir/including.xml");
final File file = new File(getConfigPath("subdir/including.xml"));
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
file.toURI().toString(), new PropertiesExpander(props));
@ -403,8 +404,7 @@ public class ConfigurationLoaderTest {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml",
getConfigPath("config_with_ignore.xml"),
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
@ -416,8 +416,7 @@ public class ConfigurationLoaderTest {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(new InputSource(
new File("src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml").toURI().toString()),
new File(getConfigPath("config_with_ignore.xml")).toURI().toString()),
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
@ -429,8 +428,7 @@ public class ConfigurationLoaderTest {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_checker_ignore.xml",
getConfigPath("config_with_checker_ignore.xml"),
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
@ -460,9 +458,7 @@ public class ConfigurationLoaderTest {
@SuppressWarnings("deprecation")
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
new FileInputStream(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml"),
new FileInputStream(getConfigPath("config_with_ignore.xml")),
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();

View File

@ -55,6 +55,14 @@ public class MainTest {
@Rule
public final SystemOutRule systemOut = new SystemOutRule().enableLog().mute();
private static String getPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/" + filename;
}
private static String getFilePath(String filename) throws IOException {
return new File(getPath(filename)).getCanonicalPath();
}
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(Main.class);
@ -110,7 +118,7 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main(getPath("InputMain.java"));
}
@Test
@ -143,7 +151,7 @@ public class MainTest {
}
});
Main.main("-c", "src/main/resources/non_existing_config.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -158,7 +166,7 @@ public class MainTest {
}
});
Main.main("-c", "/google_checks.xml", "-f", "xmlp",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -198,8 +206,8 @@ public class MainTest {
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-non-existing-classname.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main("-c", getPath("config-non-existing-classname.xml"),
getPath("InputMain.java"));
}
@Test
@ -213,8 +221,8 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main("-c", getPath("config-classname.xml"),
getPath("InputMain.java"));
}
@Test
@ -223,10 +231,7 @@ public class MainTest {
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() throws IOException {
String currentPath = new File(".").getCanonicalPath();
String expectedPath = currentPath
+ "/src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java"
.replace("/", File.separator);
String expectedPath = getFilePath("InputMain.java");
final ResourceBundle compilationProperties =
ResourceBundle.getBundle("checkstylecompilation");
String version = compilationProperties.getString("checkstyle.compile.version");
@ -239,9 +244,9 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -255,9 +260,9 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "plain",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -266,10 +271,7 @@ public class MainTest {
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() throws IOException {
String currentPath = new File(".").getCanonicalPath();
String expectedPath = currentPath
+ "/src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java"
.replace("/", File.separator);
String expectedPath = getFilePath("InputMain.java");
assertEquals(String.format(Locale.ROOT, "Starting audit...%n"
+ "%1$s:3:14: "
+ "warning: Name 'InputMain' must match pattern '^[a-z0-9]*$'.%n"
@ -280,8 +282,8 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname2.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main("-c", getPath("config-classname2.xml"),
getPath("InputMain.java"));
}
@Test
@ -291,10 +293,7 @@ public class MainTest {
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() throws IOException {
String currentPath = new File(".").getCanonicalPath();
String expectedPath = currentPath
+ "/src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java"
.replace("/", File.separator);
String expectedPath = getFilePath("InputMain.java");
assertEquals(String.format(Locale.ROOT, "Starting audit...%n"
+ "%1$s:3:14: error: "
+ "Name 'InputMain' must match pattern '^[a-z0-9]*$'.%n"
@ -306,8 +305,8 @@ public class MainTest {
}
});
Main.main("-c",
"src/test/resources/com/puppycrawl/tools/checkstyle/config-classname2-error.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("config-classname2-error.xml"),
getPath("InputMain.java"));
}
@Test
@ -321,10 +320,10 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "plain",
"-o", temporaryFolder.getRoot() + "/output.txt",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -338,10 +337,10 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "plain",
"-o", file.getCanonicalPath(),
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -359,10 +358,10 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "plain",
"-o", file.getCanonicalPath(),
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -384,10 +383,10 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-classname.xml",
Main.main("-c", getPath("config-classname.xml"),
"-f", "plain",
"-o", file.getCanonicalPath(),
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -402,10 +401,9 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/"
+ "config-classname-prop.xml",
"-p", "src/test/resources/com/puppycrawl/tools/checkstyle/mycheckstyle.properties",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main("-c", getPath("config-classname-prop.xml"),
"-p", getPath("mycheckstyle.properties"),
getPath("InputMain.java"));
}
@Test
@ -420,10 +418,9 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/"
+ "config-classname-prop.xml",
Main.main("-c", getPath("config-classname-prop.xml"),
"-p", "nonexisting.properties",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
getPath("InputMain.java"));
}
@Test
@ -443,9 +440,8 @@ public class MainTest {
assertEquals("", systemErr.getLog());
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/"
+ "config-Incorrect.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/InputMain.java");
Main.main("-c", getPath("config-Incorrect.xml"),
getPath("InputMain.java"));
}
@Test
@ -522,10 +518,7 @@ public class MainTest {
exit.checkAssertionAfterwards(new Assertion() {
@Override public void checkAssertion() throws IOException {
String currentPath = new File(".").getCanonicalPath();
String expectedPath = currentPath
+ "/src/test/resources/com/puppycrawl/tools/checkstyle/checks/metrics/"
.replace("/", File.separator);
String expectedPath = getFilePath("checks/metrics") + File.separator;
StringBuilder sb = new StringBuilder();
sb.append("Starting audit...").append(System.getProperty("line.separator"));
String format = "%s.java:%s: warning: File length is %s lines (max allowed is 170).";
@ -541,8 +534,8 @@ public class MainTest {
}
});
Main.main("-c", "src/test/resources/com/puppycrawl/tools/checkstyle/config-filelength.xml",
"src/test/resources/com/puppycrawl/tools/checkstyle/checks/metrics/");
Main.main("-c", getPath("config-filelength.xml"),
getPath("checks/metrics"));
}
@Test

View File

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!DOCTYPE import-control PUBLIC
"-//Puppy Crawl//DTD Import Control 1.0//EN"
"http://www.puppycrawl.com/dtds/import_control_1_0.dtd">
<import-control pkg="com">
<allow class="some.class"/>
<disallow class="another.class" local-only="true"/>
<allow pkg="some.pkg"/>
<disallow pkg="another.pkg" local-only="true"/>
<disallow pkg="and.another.pkg" exact-match="true"/>
<subpackage name="puppycrawl">
<disallow pkg="some.pkg"/>
<disallow class="some.class"/>
</subpackage>
</import-control>