Skip to content

Commit

Permalink
Issue checkstyle#6232: Load resources relative to resources root
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemaeyer committed Mar 20, 2019
1 parent 0bb9038 commit ced0377
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
Expand Up @@ -504,8 +504,9 @@ public static URI getUriByFilename(String filename) throws CheckstyleException {
else {
// check to see if the file is in the classpath
try {
final URL configUrl = CommonUtil.class
.getResource(filename);
final URL configUrl = filename.startsWith("/") ?
CommonUtil.class.getResource(filename) :
ClassLoader.getSystemResource(filename);
if (configUrl == null) {
throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename);
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
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.spy;
import static org.powermock.api.mockito.PowerMockito.when;

import java.net.URISyntaxException;
Expand All @@ -32,6 +33,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

Expand All @@ -49,7 +51,7 @@ public void testLoadSuppressionsUriSyntaxException() throws Exception {

when(configUrl.toURI()).thenThrow(URISyntaxException.class);
mockStatic(CommonUtil.class, Mockito.CALLS_REAL_METHODS);
final String fileName = "suppressions_none.xml";
final String fileName = "/suppressions_none.xml";
when(CommonUtil.class.getResource(fileName)).thenReturn(configUrl);

try {
Expand Down
Expand Up @@ -20,19 +20,29 @@
package com.puppycrawl.tools.checkstyle.utils;

import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URI;
import java.util.Dictionary;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
Expand All @@ -42,6 +52,8 @@ public class CommonUtilTest {

/** After appending to path produces equivalent, but denormalized path. */
private static final String PATH_DENORMALIZER = "/levelDown/.././";
private static final String PACKAGE_DIR_NAME = CommonUtilTest.class.getPackage().getName().replaceAll("\\.", "/");
private static final String SIMPLE_CLASS_NAME = CommonUtilTest.class.getSimpleName();

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
Expand Down Expand Up @@ -440,6 +452,30 @@ public void testIsIntNull() {
CommonUtil.isInt(null));
}

@Test
public void testGetUriByFilenameFindsAbsoluteResourceOnClasspath() throws Throwable {
URI uri = CommonUtil.getUriByFilename("/" + PACKAGE_DIR_NAME + "/commonutil/Input" + SIMPLE_CLASS_NAME + "_empty_checks.xml");
assertThat(uri, is(not(nullValue())));
}

@Test
public void testGetUriByFilenameFindsRelativeResourceOnClasspath() throws Throwable {
URI uri = CommonUtil.getUriByFilename(PACKAGE_DIR_NAME + "/commonutil/Input" + SIMPLE_CLASS_NAME + "_empty_checks.xml");
assertThat(uri, is(not(nullValue())));
}

@Test
public void testGetUriByFilenameFindsResourceRelativeToRootClasspath() throws Throwable {
URI uri = CommonUtil.getUriByFilename("commonutil/Input" + SIMPLE_CLASS_NAME + "_resource.txt");
assertThat(uri, is(not(nullValue())));
assertThat(uri.toString(), not(containsString(CommonUtil.class.getPackage().getName().replaceAll("\\.", "/"))));
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (InputStream in = uri.toURL().openStream()) {
IOUtils.copy(in, out);
}
assertThat(new String(out.toByteArray()), startsWith("good"));
}

private static class TestCloseable implements Closeable {

private boolean closed;
Expand Down
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">
<module name="Checker"/>
@@ -0,0 +1 @@
bad
@@ -0,0 +1 @@
good

0 comments on commit ced0377

Please sign in to comment.