From c247fe9c128a7b85bb68baf4d93f4d6775827468 Mon Sep 17 00:00:00 2001 From: Subbu Dantu Date: Wed, 12 Jul 2017 10:30:50 -0500 Subject: [PATCH] Issue #4592: Added AbstractPathTestSupport and CheckstyleAntTaskTest, SuppressionsLoaderTest now extends from it --- .../checkstyle/AbstractPathTestSupport.java | 45 +++++++++++++++++++ .../checkstyle/ant/CheckstyleAntTaskTest.java | 30 ++++++++----- .../filters/SuppressionsLoaderTest.java | 24 +++++----- 3 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java new file mode 100644 index 000000000000..ad8f16785244 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java @@ -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(); + } +} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java index fa40d3efeb02..7ad8f6f6b119 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java @@ -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; @@ -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); } @@ -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); @@ -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(); @@ -517,7 +523,7 @@ public final void testXmlOutput() throws IOException { antTask.execute(); final List expected = FileUtils.readLines( - new File(getPath("ant/checkstyleanttask/InputCheckstyleAntTaskXmlOutput.xml"))); + new File(getPath("checkstyleanttask/InputCheckstyleAntTaskXmlOutput.xml"))); final List actual = FileUtils.readLines(outputFile); for (int i = 0; i < expected.size(); i++) { final String line = expected.get(i); @@ -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))); 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 8b6e127ca1ee..e6b97cf3365b 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java @@ -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; @@ -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(); @@ -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(); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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();