Skip to content

Commit

Permalink
Issue checkstyle#4592: Added AbstractPathTestSupport and CheckstyleAn…
Browse files Browse the repository at this point in the history
…tTaskTest, SuppressionsLoaderTest now extends from it
  • Loading branch information
subkrish committed Jul 12, 2017
1 parent 689790e commit c247fe9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
@@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle;

import java.io.File;
import java.io.IOException;

public abstract class AbstractPathTestSupport {
/**
* Returns the exact location for the package where the file is present.
* @return path for the package name for the file.
*/
protected abstract String getPackageLocation();

/**
* Returns canonical path for the file with the given file name.
* The path is formed base on the root location.
* This implementation uses 'src/test/resources/'
* as a root location.
* @param filename file name.
* @return canonical path for the file name.
* @throws IOException if I/O exception occurs while forming the path.
*/
protected String getPath(String filename) throws IOException {
return new File("src/test/resources/" + getPackageLocation() + "/" + filename)
.getCanonicalPath();
}
}
Expand Up @@ -58,7 +58,7 @@
import org.powermock.modules.junit4.PowerMockRunner;

import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
import com.puppycrawl.tools.checkstyle.CheckerStub;
import com.puppycrawl.tools.checkstyle.DefaultLogger;
import com.puppycrawl.tools.checkstyle.Definitions;
Expand All @@ -70,22 +70,28 @@

@RunWith(PowerMockRunner.class)
@PrepareForTest({CheckstyleAntTask.class, Closeables.class})
public class CheckstyleAntTaskTest extends BaseCheckTestSupport {
public class CheckstyleAntTaskTest extends AbstractPathTestSupport {

private static final String FLAWLESS_INPUT =
"/ant/checkstyleanttask/InputCheckstyleAntTaskFlawless.java";
"checkstyleanttask/InputCheckstyleAntTaskFlawless.java";
private static final String VIOLATED_INPUT =
"ant/checkstyleanttask/InputCheckstyleAntTaskError.java";
"checkstyleanttask/InputCheckstyleAntTaskError.java";
private static final String WARNING_INPUT =
"ant/checkstyleanttask/InputCheckstyleAntTaskWarning.java";
"checkstyleanttask/InputCheckstyleAntTaskWarning.java";
private static final String CONFIG_FILE =
"ant/checkstyleanttask/InputCheckstyleAntTaskTestChecks.xml";
private static final String CONFIG_RESOURCE = "/com/puppycrawl/tools/checkstyle/" + CONFIG_FILE;
"checkstyleanttask/InputCheckstyleAntTaskTestChecks.xml";
private static final String CONFIG_RESOURCE =
"/com/puppycrawl/tools/checkstyle/ant/" + CONFIG_FILE;
private static final String CUSTOM_ROOT_CONFIG_FILE =
"ant/checkstyleanttask/InputCheckstyleAntTaskConfigCustomRootModule.xml";
"checkstyleanttask/InputCheckstyleAntTaskConfigCustomRootModule.xml";
private static final String NOT_EXISTING_FILE = "target/not_existing.xml";
private static final String FAILURE_PROPERTY_VALUE = "myValue";

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

private CheckstyleAntTask getCheckstyleAntTask() throws IOException {
return getCheckstyleAntTask(CONFIG_FILE);
}
Expand Down Expand Up @@ -181,7 +187,7 @@ public final void testPathsDirectoryWithNestedFile() throws IOException {
antTask.setProject(new Project());

final FileResource fileResource = new FileResource(
antTask.getProject(), getPath("ant/checkstyleanttask/"));
antTask.getProject(), getPath("checkstyleanttask/"));
final Path sourcePath = new Path(antTask.getProject());
sourcePath.add(fileResource);
antTask.addPath(sourcePath);
Expand Down Expand Up @@ -477,7 +483,7 @@ public final void testSetPropertiesFile() throws IOException {

final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
antTask.setFile(new File(getPath(VIOLATED_INPUT)));
antTask.setProperties(new File(getPath("ant/checkstyleanttask/"
antTask.setProperties(new File(getPath("checkstyleanttask/"
+ "InputCheckstyleAntTaskCheckstyleAntTest.properties")));
antTask.execute();

Expand Down Expand Up @@ -517,7 +523,7 @@ public final void testXmlOutput() throws IOException {
antTask.execute();

final List<String> expected = FileUtils.readLines(
new File(getPath("ant/checkstyleanttask/InputCheckstyleAntTaskXmlOutput.xml")));
new File(getPath("checkstyleanttask/InputCheckstyleAntTaskXmlOutput.xml")));
final List<String> actual = FileUtils.readLines(outputFile);
for (int i = 0; i < expected.size(); i++) {
final String line = expected.get(i);
Expand Down Expand Up @@ -727,7 +733,7 @@ public void testClassloaderInRootModule() throws IOException {
CheckerStub.reset();

final CheckstyleAntTask antTask =
getCheckstyleAntTask("ant/checkstyleanttask/"
getCheckstyleAntTask("checkstyleanttask/"
+ "InputCheckstyleAntTaskConfigCustomCheckerRootModule.xml");
antTask.setFile(new File(getPath(VIOLATED_INPUT)));

Expand Down
Expand Up @@ -36,7 +36,7 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.xml.sax.InputSource;

import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FilterSet;

Expand All @@ -47,18 +47,18 @@
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ SuppressionsLoader.class, SuppressionsLoaderTest.class })
public class SuppressionsLoaderTest extends BaseCheckTestSupport {
public class SuppressionsLoaderTest extends AbstractPathTestSupport {
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Override
protected String getPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/filters/" + filename;
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/filters";
}

@Test
public void testNoSuppressions()
throws CheckstyleException {
throws IOException, CheckstyleException {
final FilterSet fc =
SuppressionsLoader.loadSuppressions(getPath("suppressions_none.xml"));
final FilterSet fc2 = new FilterSet();
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testLoadFromNonExistingUrl() {

@Test
public void testMultipleSuppression()
throws CheckstyleException {
throws IOException, CheckstyleException {
final FilterSet fc =
SuppressionsLoader.loadSuppressions(getPath("suppressions_multiple.xml"));
final FilterSet fc2 = new FilterSet();
Expand All @@ -139,7 +139,7 @@ public void testMultipleSuppression()
}

@Test
public void testNoFile() {
public void testNoFile() throws IOException {
final String fn = getPath("suppressions_no_file.xml");
try {
SuppressionsLoader.loadSuppressions(fn);
Expand All @@ -156,7 +156,7 @@ public void testNoFile() {
}

@Test
public void testNoCheck() {
public void testNoCheck() throws IOException {
final String fn = getPath("suppressions_no_check.xml");
try {
SuppressionsLoader.loadSuppressions(fn);
Expand All @@ -173,7 +173,7 @@ public void testNoCheck() {
}

@Test
public void testBadInt() {
public void testBadInt() throws IOException {
final String fn = getPath("suppressions_bad_int.xml");
try {
SuppressionsLoader.loadSuppressions(fn);
Expand Down Expand Up @@ -259,7 +259,7 @@ public void testUnableToReadSuppressions() throws Exception {
}

@Test
public void testNoCheckNoId() {
public void testNoCheckNoId() throws IOException {
final String fn = getPath("suppressions_no_check_and_id.xml");
try {
SuppressionsLoader.loadSuppressions(fn);
Expand All @@ -280,7 +280,7 @@ public void testNoCheckYesId() throws Exception {
}

@Test
public void testInvalidFileFormat() {
public void testInvalidFileFormat() throws IOException {
final String fn = getPath("suppressions_invalid_file.xml");
try {
SuppressionsLoader.loadSuppressions(fn);
Expand All @@ -294,7 +294,7 @@ public void testInvalidFileFormat() {

@Test
public void testLoadFromClasspath()
throws CheckstyleException {
throws IOException, CheckstyleException {
final FilterSet fc =
SuppressionsLoader.loadSuppressions(getPath("suppressions_none.xml"));
final FilterSet fc2 = new FilterSet();
Expand Down

0 comments on commit c247fe9

Please sign in to comment.