100% code coverage for ConfigurationLoader. #1294

This commit is contained in:
Roman Ivanov 2015-07-30 20:25:43 -07:00
parent dcd7085ce2
commit 71c16c55c3
7 changed files with 233 additions and 20 deletions

View File

@ -1077,8 +1077,6 @@
<totalLineRate>95</totalLineRate>
<regexes>
<regex><pattern>.*.Checker</pattern><branchRate>79</branchRate><lineRate>84</lineRate></regex>
<regex><pattern>.*.ConfigurationLoader</pattern><branchRate>86</branchRate><lineRate>79</lineRate></regex>
<regex><pattern>.*.ConfigurationLoader\$.*</pattern><branchRate>65</branchRate><lineRate>84</lineRate></regex>
<regex><pattern>.*.DefaultLogger</pattern><branchRate>75</branchRate><lineRate>76</lineRate></regex>
<regex><pattern>.*.PackageNamesLoader</pattern><branchRate>78</branchRate><lineRate>72</lineRate></regex>
<regex><pattern>.*.TreeWalker</pattern><branchRate>94</branchRate><lineRate>91</lineRate></regex>

View File

@ -109,6 +109,8 @@ public final class ConfigurationLoader {
private static final String SEVERITY = "severity";
/** name of the message element */
private static final String MESSAGE = "message";
/** name of the message element */
private static final String METADATA = "metadata";
/** name of the key attribute */
private static final String KEY = "key";
@ -174,6 +176,11 @@ public final class ConfigurationLoader {
final DefaultConfiguration top = configStack.peek();
top.addMessage(key, value);
}
else {
if (!qName.equals(METADATA)) {
throw new IllegalStateException("Unknown name:" + qName + ".");
}
}
}
@Override
@ -300,13 +307,10 @@ public final class ConfigurationLoader {
final URL url = new URL(config);
uri = url.toURI();
}
catch (final MalformedURLException ex) {
uri = null;
}
catch (final URISyntaxException ex) {
// URL violating RFC 2396
catch (final URISyntaxException | MalformedURLException ex) {
uri = null;
}
if (uri == null) {
final File file = new File(config);
if (file.exists()) {
@ -323,7 +327,7 @@ public final class ConfigurationLoader {
uri = configUrl.toURI();
}
catch (final URISyntaxException e) {
throw new CheckstyleException("unable to find " + config);
throw new CheckstyleException("unable to find " + config, e);
}
}
}
@ -379,21 +383,13 @@ public final class ConfigurationLoader {
loader.parseInputSource(configSource);
return loader.getConfiguration();
}
catch (final ParserConfigurationException e) {
throw new CheckstyleException(
"unable to parse configuration stream", e);
}
catch (final SAXParseException e) {
throw new CheckstyleException("unable to parse configuration stream"
+ " - " + e.getMessage() + ":" + e.getLineNumber()
+ ":" + e.getColumnNumber(), e);
}
catch (final SAXException e) {
throw new CheckstyleException("unable to parse configuration stream"
+ " - " + e.getMessage(), e);
}
catch (final IOException e) {
throw new CheckstyleException("unable to read from stream", e);
catch (final ParserConfigurationException | IOException | SAXException e) {
throw new CheckstyleException("unable to parse configuration stream", e);
}
}

View File

@ -22,11 +22,23 @@ package com.puppycrawl.tools.checkstyle;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.util.Properties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.xml.sax.Attributes;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
@ -35,7 +47,10 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
* @author Rick Giles
* @author lkuehne
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ConfigurationLoader.class, ConfigurationLoaderTest.class })
public class ConfigurationLoaderTest {
private Configuration loadConfiguration(String name)
throws CheckstyleException {
return loadConfiguration(name, new Properties());
@ -47,7 +62,7 @@ public class ConfigurationLoaderTest {
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/" + name;
return ConfigurationLoader.loadConfiguration(
fName, new PropertiesExpander(props));
fName, new PropertiesExpander(props));
}
@ -331,7 +346,7 @@ public class ConfigurationLoaderTest {
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/subdir/including.xml");
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
file.toURI().toString(), new PropertiesExpander(props));
file.toURI().toString(), new PropertiesExpander(props));
final Properties atts = new Properties();
atts.put("tabWidth", "4");
@ -339,4 +354,175 @@ public class ConfigurationLoaderTest {
verifyConfigNode(config, "Checker", 2, atts);
}
@Test
public void testIncorrectTag() throws Exception {
try {
Class<?> aClassParent = ConfigurationLoader.class;
Constructor ctorParent = null;
Constructor[] parentConstructors = aClassParent.getDeclaredConstructors();
for (Constructor constr: parentConstructors) {
constr.setAccessible(true);
ctorParent = constr;
}
Object objParent = ctorParent.newInstance(null, true);
Class<?> aClass = Class.forName("com.puppycrawl.tools.checkstyle."
+ "ConfigurationLoader$InternalLoader");
Constructor constructor = null;
Constructor[] constructors = aClass.getDeclaredConstructors();
for (Constructor constr: constructors) {
constr.setAccessible(true);
constructor = constr;
}
Object obj = constructor.newInstance(objParent);
Class[] param = new Class[4];
param[0] = String.class;
param[1] = String.class;
param[2] = String.class;
param[3] = Attributes.class;
Method method = aClass.getDeclaredMethod("startElement", param);
method.invoke(obj, "", "", "hello", null);
fail("Exception is expected");
}
catch (InvocationTargetException e) {
assertTrue(e.getCause() instanceof IllegalStateException);
assertEquals("Unknown name:" + "hello" + ".", e.getCause().getMessage());
}
}
@Test
public void testNonExistingPropertyName() {
try {
loadConfiguration("config_nonexisting_property.xml");
fail("exception in expected");
}
catch (CheckstyleException ex) {
assertEquals("unable to parse configuration stream", ex.getMessage());
assertEquals("Property ${nonexisting} has not been set",
ex.getCause().getMessage());
}
}
@Test
public void testConfigWithIgnore() throws CheckstyleException {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml",
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
assertTrue(children[0].getChildren().length == 0);
}
@Test
public void testConfigCheckerWithIgnore() throws CheckstyleException {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_checker_ignore.xml",
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
assertTrue(children.length == 0);
}
@Test
public void testLoadConfiguration_WrongURL() throws CheckstyleException {
try {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
";config_with_ignore.xml",
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
assertTrue(children[0].getChildren().length == 0);
fail("Exception is expected");
}
catch (CheckstyleException ex) {
assertEquals("unable to find ;config_with_ignore.xml", ex.getMessage());
}
}
@Test
public void testLoadConfiguration_URISyntaxException() throws CheckstyleException {
mockStatic(ConfigurationLoader.class);
PropertiesExpander expander = new PropertiesExpander(new Properties());
when(ConfigurationLoader.class.getResource("config_with_ignore.xml"))
.thenThrow(URISyntaxException.class);
when(ConfigurationLoader.loadConfiguration("config_with_ignore.xml",
expander,
true))
.thenCallRealMethod();
try {
ConfigurationLoader.loadConfiguration(
"config_with_ignore.xml", expander, true);
fail("Exception is expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getCause() instanceof URISyntaxException);
assertEquals("unable to find config_with_ignore.xml", ex.getMessage());
}
}
@Test
public void testLoadConfiguration_Deprecated() throws CheckstyleException {
try {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
new FileInputStream(
"src/test/resources/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml"),
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
assertTrue(children[0].getChildren().length == 0);
}
catch (CheckstyleException ex) {
fail("unexpected exception");
}
catch (FileNotFoundException e) {
fail("unexpected exception");
}
}
@Test
public void testReplacePropertiesDefault() throws Exception {
final Properties props = new Properties();
String defaultValue = "defaultValue";
String value = ConfigurationLoader.replaceProperties("${checkstyle.basedir}",
new PropertiesExpander(props), defaultValue);
assertEquals(defaultValue, value);
}
@Test
public void testLoadConfigurationFromClassPath() throws CheckstyleException {
try {
final DefaultConfiguration config =
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
"/com/puppycrawl/tools/checkstyle/configs/"
+ "config_with_ignore.xml",
new PropertiesExpander(new Properties()), true);
final Configuration[] children = config.getChildren();
assertTrue(children[0].getChildren().length == 0);
}
catch (CheckstyleException ex) {
fail("unexpected exception");
}
}
}

View File

@ -5,6 +5,8 @@
"http://www.puppycrawl.com/dtds/configuration_1_1.dtd">
<module name="Checker">
<metadata name="any-possible-value" value="just smth"/>
<property name="tabWidth" value="4" />
<property name="basedir" value="${checkstyle.basedir}" />
<module name="TreeWalker">

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="tabWidth" value="${nonexisting}" />
</module>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="severity" value="ignore"/>
</module>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="MemberName">
<property name="severity" value="ignore"/>
</module>
</module>
</module>