Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually parse the Ant configUrl field into a URL #4463

Merged
merged 1 commit into from Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,7 +24,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -93,7 +92,7 @@ public class CheckstyleAntTask extends Task {
private String fileName;

/** Config file containing configuration. */
private String configLocation;
private String config;

/** Whether to fail build on violations. */
private boolean failOnViolation = true;
Expand Down Expand Up @@ -230,42 +229,13 @@ public void setFile(File file) {

/**
* Sets configuration file.
* @param file the configuration file to use
* @param configuration the configuration file, URL, or resource to use
*/
public void setConfig(File file) {
setConfigLocation(file.getAbsolutePath());
}

/**
* Sets URL to the configuration.
* @param url the URL of the configuration to use
* @deprecated please use setConfigUrl instead
*/
// -@cs[AbbreviationAsWordInName] Should be removed at 7.0 version,
// we keep for some time to avoid braking compatibility.
@Deprecated
public void setConfigURL(URL url) {
setConfigUrl(url);
}

/**
* Sets URL to the configuration.
* @param url the URL of the configuration to use
*/
public void setConfigUrl(URL url) {
setConfigLocation(url.toExternalForm());
}

/**
* Sets the location of the configuration.
* @param location the location, which is either a
*/
private void setConfigLocation(String location) {
if (configLocation != null) {
throw new BuildException("Attributes 'config' and 'configURL' "
+ "must not be set at the same time");
public void setConfig(String configuration) {
if (config != null) {
throw new BuildException("Attribute 'config' has already been set");
}
configLocation = location;
config = configuration;
}

/**
Expand Down Expand Up @@ -316,7 +286,7 @@ public void execute() {
"Must specify at least one of 'file' or nested 'fileset' or 'path'.",
getLocation());
}
if (configLocation == null) {
if (config == null) {
throw new BuildException("Must specify 'config'.", getLocation());
}
realExecute(version);
Expand Down Expand Up @@ -381,7 +351,7 @@ private void processFiles(RootModule rootModule, final SeverityLevelCounter warn

log("Running Checkstyle " + checkstyleVersion + " on " + files.size()
+ " files", Project.MSG_INFO);
log("Using configuration " + configLocation, Project.MSG_VERBOSE);
log("Using configuration " + config, Project.MSG_VERBOSE);

final int numErrs;

Expand Down Expand Up @@ -421,9 +391,9 @@ private RootModule createRootModule() {
final RootModule rootModule;
try {
final Properties props = createOverridingProperties();
final Configuration config =
final Configuration configuration =
ConfigurationLoader.loadConfiguration(
configLocation,
config,
new PropertiesExpander(props),
!executeIgnoredModules);

Expand All @@ -433,7 +403,7 @@ private RootModule createRootModule() {
final ModuleFactory factory = new PackageObjectFactory(
Checker.class.getPackage().getName() + ".", moduleClassLoader);

rootModule = (RootModule) factory.createModule(config.getName());
rootModule = (RootModule) factory.createModule(configuration.getName());
rootModule.setModuleClassLoader(moduleClassLoader);

if (rootModule instanceof Checker) {
Expand All @@ -443,11 +413,11 @@ private RootModule createRootModule() {
((Checker) rootModule).setClassLoader(loader);
}

rootModule.configure(config);
rootModule.configure(configuration);
}
catch (final CheckstyleException ex) {
throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: "
+ "configLocation {%s}, classpath {%s}.", configLocation, classpath), ex);
+ "config {%s}, classpath {%s}.", config, classpath), ex);
}
return rootModule;
}
Expand Down
Expand Up @@ -73,6 +73,7 @@ public class CheckstyleAntTaskTest extends BaseCheckTestSupport {
private static final String WARNING_INPUT =
"ant/checkstyleanttask/InputCheckstyleAntTaskWarning.java";
private static final String CONFIG_FILE = "ant/ant_task_test_checks.xml";
private static final String CONFIG_RESOURCE = "/com/puppycrawl/tools/checkstyle/" + CONFIG_FILE;
private static final String CUSTOM_ROOT_CONFIG_FILE = "config-custom-root-module.xml";
private static final String NOT_EXISTING_FILE = "target/not_existing.xml";
private static final String FAILURE_PROPERTY_VALUE = "myValue";
Expand All @@ -83,7 +84,7 @@ private CheckstyleAntTask getCheckstyleAntTask() throws IOException {

private CheckstyleAntTask getCheckstyleAntTask(String configFile) throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(new File(getPath(configFile)));
antTask.setConfig(getPath(configFile));
antTask.setProject(new Project());
return antTask;
}
Expand Down Expand Up @@ -184,7 +185,7 @@ public final void testNoConfigFile() throws IOException {
@Test
public final void testNonExistingConfig() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(new File(getPath(NOT_EXISTING_FILE)));
antTask.setConfig(getPath(NOT_EXISTING_FILE));
antTask.setProject(new Project());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
try {
Expand All @@ -193,14 +194,14 @@ public final void testNonExistingConfig() throws IOException {
}
catch (BuildException ex) {
assertTrue("Error message is unexpected",
ex.getMessage().startsWith("Unable to create Root Module: configLocation"));
ex.getMessage().startsWith("Unable to create Root Module: config"));
}
}

@Test
public final void testEmptyConfigFile() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(new File(getPath("ant/empty_config.xml")));
antTask.setConfig(getPath("ant/empty_config.xml"));
antTask.setProject(new Project());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
try {
Expand All @@ -209,7 +210,7 @@ public final void testEmptyConfigFile() throws IOException {
}
catch (BuildException ex) {
assertTrue("Error message is unexpected",
ex.getMessage().startsWith("Unable to create Root Module: configLocation"));
ex.getMessage().startsWith("Unable to create Root Module: config"));
}
}

Expand Down Expand Up @@ -253,7 +254,7 @@ public final void testMaxErrors() throws IOException {
@Test
public final void testFailureProperty() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(new File(getPath(CONFIG_FILE)));
antTask.setConfig(getPath(CONFIG_FILE));
antTask.setFile(new File(getPath(VIOLATED_INPUT)));

final Project project = new Project();
Expand Down Expand Up @@ -323,32 +324,29 @@ public final void testConfigurationByUrl() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setProject(new Project());
final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
antTask.setConfigURL(url);
antTask.setConfig(url.toString());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
antTask.execute();
}

@Test
public final void testConfigurationByResource() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setProject(new Project());
antTask.setConfig(CONFIG_RESOURCE);
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));
antTask.execute();
}

@Test
public final void testSimultaneousConfiguration() throws IOException {
CheckstyleAntTask antTask;
final File file = new File(getPath(CONFIG_FILE));
final URL url = file.toURI().toURL();
final String expected =
"Attributes 'config' and 'configURL' must not be set at the same time";
try {
antTask = new CheckstyleAntTask();
antTask.setConfigUrl(url);
antTask.setConfig(file);
fail("Exception is expected");
}
catch (BuildException ex) {
assertEquals("Error message is unexpected",
expected, ex.getMessage());
}
final String expected = "Attribute 'config' has already been set";
try {
antTask = new CheckstyleAntTask();
antTask.setConfig(file);
antTask.setConfigUrl(url);
final CheckstyleAntTask antTask = new CheckstyleAntTask();
antTask.setConfig(url.toString());
antTask.setConfig(file.toString());
fail("Exception is expected");
}
catch (BuildException ex) {
Expand Down Expand Up @@ -515,7 +513,7 @@ public void testSetClasspathRef() {
@Test
public void testCheckerException() throws IOException {
final CheckstyleAntTask antTask = new CheckstyleAntTaskStub();
antTask.setConfig(new File(getPath(CONFIG_FILE)));
antTask.setConfig(getPath(CONFIG_FILE));
antTask.setProject(new Project());
antTask.setFile(new File(""));
try {
Expand All @@ -533,7 +531,7 @@ public final void testExecuteLogOutput() throws Exception {
final CheckstyleAntTaskLogStub antTask = new CheckstyleAntTaskLogStub();
final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL();
antTask.setProject(new Project());
antTask.setConfigURL(url);
antTask.setConfig(url.toString());
antTask.setFile(new File(getPath(FLAWLESS_INPUT)));

mockStatic(System.class);
Expand Down
14 changes: 2 additions & 12 deletions src/xdocs/anttask.xml.vm
Expand Up @@ -135,23 +135,13 @@
<tr>
<td>config</td>
<td>
Specifies the location of the file that defines the configuration
Specifies the location of the file, URL, or Java resource that defines the configuration
modules.
<br/>
<a href="config.html">See here</a> for a description of how to
define a configuration.
</td>
<td>Exactly one config file</td>
</tr>

<tr>
<td>configURL</td>
<td>
Specifies a URL or name passed to the ClassLoader.getResource() method that defines the configuration modules. <a
href="config.html">See here</a> for a description of how to define
a configuration.
</td>
<td>Exactly one of config or configURL</td>
<td>Exactly one config location</td>
</tr>

<tr>
Expand Down