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 Apr 27, 2019
1 parent 62f5bb1 commit a599dee
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ci/jsoref-spellchecker/whitelist.words
Expand Up @@ -250,6 +250,7 @@ columnlimit
Combobox
commentsindentation
commerical
commonutil
Compat
Compiletime
Comspace
Expand Down
2 changes: 0 additions & 2 deletions .ci/pitest.sh
Expand Up @@ -282,9 +282,7 @@ pitest-tree-walker)
pitest-utils)
mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage;
declare -a ignoredItems=(
"CommonUtil.java.html:<td class='uncovered'><pre><span class=''> uri = configUrl.toURI();</span></pre></td></tr>"
"CommonUtil.java.html:<td class='uncovered'><pre><span class=''> catch (final URISyntaxException ex) {</span></pre></td></tr>"
"CommonUtil.java.html:<td class='uncovered'><pre><span class=''> }</span></pre></td></tr>"
"CommonUtil.java.html:<td class='uncovered'><pre><span class='survived'> throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename, ex);</span></pre></td></tr>"
);
checkPitestReport "${ignoredItems[@]}"
Expand Down
Expand Up @@ -507,8 +507,13 @@ 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;
if (filename.charAt(0) == '/') {
configUrl = CommonUtil.class.getResource(filename);
}
else {
configUrl = ClassLoader.getSystemResource(filename);
}
if (configUrl == null) {
throw new CheckstyleException(UNABLE_TO_FIND_EXCEPTION_PREFIX + filename);
}
Expand Down
Expand Up @@ -49,7 +49,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,25 +20,37 @@
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.nio.charset.StandardCharsets;
import java.util.Dictionary;
import java.util.regex.Pattern;

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

import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class CommonUtilTest {
public class CommonUtilTest extends AbstractPathTestSupport {

/** After appending to path produces equivalent, but denormalized path. */
private static final String PATH_DENORMALIZER = "/levelDown/.././";
Expand Down Expand Up @@ -440,6 +452,42 @@ public void testIsIntNull() {
CommonUtil.isInt(null));
}

@Test
public void testGetUriByFilenameFindsAbsoluteResourceOnClasspath() throws Exception {
final String filename =
"/" + getPackageLocation() + "/InputCommonUtilTest_empty_checks.xml";
final URI uri = CommonUtil.getUriByFilename(filename);
assertThat("URI is null for: " + filename, uri, is(not(nullValue())));
}

@Test
public void testGetUriByFilenameFindsRelativeResourceOnClasspath() throws Exception {
final String filename =
getPackageLocation() + "/InputCommonUtilTest_empty_checks.xml";
final URI uri = CommonUtil.getUriByFilename(filename);
assertThat("URI is null for: " + filename, uri, is(not(nullValue())));
}

@Test
public void testGetUriByFilenameFindsResourceRelativeToRootClasspath() throws Exception {
final String filename = "commonutil/InputCommonUtilTest_resource.txt";
final URI uri = CommonUtil.getUriByFilename(filename);
assertThat("URI is null for: " + filename, uri, is(not(nullValue())));
assertThat("URI contains com/puppycrawl/tools/checkstyle/utils",
uri.toString(), not(containsString("com/puppycrawl/tools/checkstyle/utils")));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (InputStream in = uri.toURL().openStream()) {
IOUtils.copy(in, out);
}
assertThat("Content mismatches for: " + uri,
new String(out.toByteArray(), StandardCharsets.UTF_8), startsWith("good"));
}

@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/utils/commonutil";
}

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 a599dee

Please sign in to comment.