diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java b/src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java index ae92be6f288..f2d0d8dabca 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java @@ -19,13 +19,9 @@ package com.puppycrawl.tools.checkstyle; -import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; @@ -47,6 +43,7 @@ import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** * Loads a configuration from a standard configuration XML file. @@ -89,9 +86,6 @@ public final class ConfigurationLoader { private static final String DTD_RESOURCE_NAME_1_3 = "com/puppycrawl/tools/checkstyle/configuration_1_3.dtd"; - /** Prefix for the exception when unable to find resource. */ - private static final String UNABLE_TO_FIND_EXCEPTION_PREFIX = "unable to find "; - /** Prefix for the exception when unable to parse resource. */ private static final String UNABLE_TO_PARSE_EXCEPTION_PREFIX = "unable to parse" + " configuration stream"; @@ -182,35 +176,7 @@ public static Configuration loadConfiguration(String config, PropertyResolver overridePropsResolver, boolean omitIgnoredModules) throws CheckstyleException { // figure out if this is a File or a URL - URI uri; - try { - final URL url = new URL(config); - uri = url.toURI(); - } - catch (final URISyntaxException | MalformedURLException ignored) { - uri = null; - } - - if (uri == null) { - final File file = new File(config); - if (file.exists()) { - uri = file.toURI(); - } - else { - // check to see if the file is in the classpath - try { - final URL configUrl = ConfigurationLoader.class - .getResource(config); - if (configUrl == null) { - throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + config); - } - uri = configUrl.toURI(); - } - catch (final URISyntaxException e) { - throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + config, e); - } - } - } + final URI uri = CommonUtils.getUriByFilename(config); final InputSource source = new InputSource(uri.toString()); return loadConfiguration(source, overridePropsResolver, omitIgnoredModules); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java index 4168dbcfcbf..83381fc18b5 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java @@ -20,18 +20,13 @@ package com.puppycrawl.tools.checkstyle.checks.header; import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Reader; import java.io.StringReader; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.nio.charset.Charset; import java.util.List; import java.util.regex.Pattern; @@ -44,6 +39,7 @@ import com.google.common.io.Closeables; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** * Abstract super class for header checks. @@ -106,7 +102,7 @@ private void loadHeaderFile() throws CheckstyleException { checkHeaderNotInitialized(); Reader headerReader = null; try { - final URI uri = resolveHeaderFile(); + final URI uri = CommonUtils.getUriByFilename(filename); headerReader = new InputStreamReader(new BufferedInputStream( uri.toURL().openStream()), charset); loadHeader(headerReader); @@ -120,45 +116,6 @@ private void loadHeaderFile() throws CheckstyleException { } } - /** - * Resolve the specified filename param to a URI. - * @return resolved header file URI - * @throws IOException on failure - */ - private URI resolveHeaderFile() throws IOException { - // figure out if this is a File or a URL - URI uri; - try { - final URL url = new URL(filename); - uri = url.toURI(); - } - catch (final MalformedURLException | URISyntaxException ignored) { - // URL violating RFC 2396 - uri = null; - } - if (uri == null) { - final File file = new File(filename); - if (file.exists()) { - uri = file.toURI(); - } - else { - // check to see if the file is in the classpath - try { - final URL configUrl = AbstractHeaderCheck.class - .getResource(filename); - if (configUrl == null) { - throw new FileNotFoundException(filename); - } - uri = configUrl.toURI(); - } - catch (final URISyntaxException ignored) { - throw new FileNotFoundException(filename); - } - } - } - return uri; - } - /** * Called before initializing the header. * @throws ConversionException if header has already been set diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java index 39c2bca3782..ddf52c55406 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java @@ -19,13 +19,9 @@ package com.puppycrawl.tools.checkstyle.filters; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.util.Map; import java.util.regex.PatternSyntaxException; @@ -39,6 +35,7 @@ import com.puppycrawl.tools.checkstyle.api.AbstractLoader; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.FilterSet; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** * Loads a filter chain of suppressions. @@ -125,35 +122,7 @@ public void startElement(String namespaceURI, public static FilterSet loadSuppressions(String filename) throws CheckstyleException { // figure out if this is a File or a URL - URI uri; - try { - final URL url = new URL(filename); - uri = url.toURI(); - } - catch (final MalformedURLException | URISyntaxException ignored) { - // URL violating RFC 2396 - uri = null; - } - if (uri == null) { - final File file = new File(filename); - if (file.exists()) { - uri = file.toURI(); - } - else { - // check to see if the file is in the classpath - try { - final URL configUrl = SuppressionsLoader.class - .getResource(filename); - if (configUrl == null) { - throw new CheckstyleException(UNABLE_TO_FIND_ERROR_MESSAGE + filename); - } - uri = configUrl.toURI(); - } - catch (final URISyntaxException e) { - throw new CheckstyleException(UNABLE_TO_FIND_ERROR_MESSAGE + filename, e); - } - } - } + final URI uri = CommonUtils.getUriByFilename(filename); final InputSource source = new InputSource(uri.toString()); return loadSuppressions(source, filename); } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java index 78f00bdb001..a121317d88f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java @@ -24,6 +24,10 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Pattern; @@ -31,6 +35,8 @@ import org.apache.commons.beanutils.ConversionException; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; + /** * Contains utility methods. * @@ -38,6 +44,9 @@ */ public final class CommonUtils { + /** Prefix for the exception when unable to find resource. */ + private static final String UNABLE_TO_FIND_EXCEPTION_PREFIX = "Unable to find: "; + /** Stop instances being created. **/ private CommonUtils() { @@ -312,4 +321,45 @@ public static void close(Closeable closeable) { throw new IllegalStateException("Cannot close the stream", e); } } + + /** + * Resolve the specified filename to a URI. + * @param filename name os the file + * @return resolved header file URI + * @throws CheckstyleException on failure + */ + public static URI getUriByFilename(String filename) throws CheckstyleException { + // figure out if this is a File or a URL + URI uri; + try { + final URL url = new URL(filename); + uri = url.toURI(); + } + catch (final URISyntaxException | MalformedURLException ignored) { + uri = null; + } + + if (uri == null) { + final File file = new File(filename); + if (file.exists()) { + uri = file.toURI(); + } + else { + // check to see if the file is in the classpath + try { + final URL configUrl = CommonUtils.class + .getResource(filename); + if (configUrl == null) { + throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename); + } + uri = configUrl.toURI(); + } + catch (final URISyntaxException e) { + throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename, e); + } + } + } + + return uri; + } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java index 121d0c59eb6..a1433708acd 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java @@ -22,8 +22,6 @@ 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; @@ -31,14 +29,9 @@ 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.mockito.Mockito; -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; @@ -49,8 +42,6 @@ * @author Rick Giles * @author lkuehne */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({ ConfigurationLoader.class, ConfigurationLoaderTest.class }) public class ConfigurationLoaderTest { private static Configuration loadConfiguration(String name) @@ -445,29 +436,7 @@ public void testLoadConfigurationWrongURL() throws CheckstyleException { fail("Exception is expected"); } catch (CheckstyleException ex) { - assertEquals("unable to find ;config_with_ignore.xml", ex.getMessage()); - } - } - - @Test - @SuppressWarnings("unchecked") - public void testLoadConfigurationURISyntaxException() throws CheckstyleException { - mockStatic(ConfigurationLoader.class, Mockito.CALLS_REAL_METHODS); - - PropertiesExpander expander = new PropertiesExpander(new Properties()); - - when(ConfigurationLoader.class.getResource("config_with_ignore.xml")) - .thenThrow(URISyntaxException.class); - - 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()); + assertEquals("Unable to find: ;config_with_ignore.xml", ex.getMessage()); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java index a490d483bf0..f35d46bd0c8 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java @@ -135,7 +135,8 @@ public void testNonExistingConfigFile() exit.checkAssertionAfterwards(new Assertion() { @Override public void checkAssertion() { - assertEquals(String.format("unable to find src/main/resources/non_existing_config.xml%n" + assertEquals( + String.format("Unable to find: src/main/resources/non_existing_config.xml%n" + "Checkstyle ends with 1 errors.%n"), systemOut.getLog()); assertEquals("", systemErr.getLog()); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java index 3916d01e206..86174a9cfba 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java @@ -21,15 +21,10 @@ 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.mock; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.when; import java.io.IOException; import java.lang.reflect.Method; import java.net.HttpURLConnection; -import java.net.URISyntaxException; import java.net.URL; import org.junit.Assume; @@ -37,7 +32,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.xml.sax.InputSource; @@ -265,23 +259,4 @@ public void testLoadFromClasspath() assertEquals(fc, fc2); } - @Test - @SuppressWarnings("unchecked") - public void testLoadSuppressionsURISyntaxException() throws Exception { - URL configUrl = mock(URL.class); - - when(configUrl.toURI()).thenThrow(URISyntaxException.class); - mockStatic(SuppressionsLoader.class, Mockito.CALLS_REAL_METHODS); - String fileName = "suppressions_none.xml"; - when(SuppressionsLoader.class.getResource(fileName)).thenReturn(configUrl); - - try { - SuppressionsLoader.loadSuppressions(fileName); - fail("Exception is expected"); - } - catch (CheckstyleException ex) { - assertTrue(ex.getCause() instanceof URISyntaxException); - assertEquals("Unable to find: " + fileName, ex.getMessage()); - } - } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java index 4b29b30338a..6d96b8f44e0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java @@ -25,16 +25,28 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Dictionary; import org.apache.commons.beanutils.ConversionException; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; + +@RunWith(PowerMockRunner.class) public class CommonUtilsTest { /** After appending to path produces equivalent, but denormalized path. */ @@ -189,6 +201,27 @@ public void close() throws IOException { }); } + @Test + @PrepareForTest({ CommonUtils.class, CommonUtilsTest.class }) + @SuppressWarnings("unchecked") + public void testLoadSuppressionsURISyntaxException() throws Exception { + URL configUrl = mock(URL.class); + + when(configUrl.toURI()).thenThrow(URISyntaxException.class); + mockStatic(CommonUtils.class, Mockito.CALLS_REAL_METHODS); + String fileName = "suppressions_none.xml"; + when(CommonUtils.class.getResource(fileName)).thenReturn(configUrl); + + try { + CommonUtils.getUriByFilename(fileName); + fail("Exception is expected"); + } + catch (CheckstyleException ex) { + assertTrue(ex.getCause() instanceof URISyntaxException); + assertEquals("Unable to find: " + fileName, ex.getMessage()); + } + } + private static class TestCloseable implements Closeable { private boolean closed;