Skip to content

Commit

Permalink
Issue #46: blocks of code should not be duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaka authored and romani committed Sep 11, 2015
1 parent 0cf971e commit 526c9d0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 172 deletions.
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
Expand Up @@ -24,20 +24,29 @@
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;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.beanutils.ConversionException;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;

/**
* Contains utility methods.
*
* @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
*/
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() {

Expand Down Expand Up @@ -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;
}
}
Expand Up @@ -22,23 +22,16 @@
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.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;
Expand All @@ -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)
Expand Down Expand Up @@ -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());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java
Expand Up @@ -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());
Expand Down

0 comments on commit 526c9d0

Please sign in to comment.