Skip to content

Commit

Permalink
Merge branch '#54.configuration.cache'
Browse files Browse the repository at this point in the history
* #54.configuration.cache:
  make encoding a property (#54)
  • Loading branch information
aaschmid committed Mar 27, 2021
2 parents cdcc1ed + b3805f7 commit 9c4e69f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
29 changes: 17 additions & 12 deletions src/main/java/de/aaschmid/gradle/plugins/cpd/Cpd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.reporting.Reporting;
import org.gradle.api.reporting.SingleFileReport;
import org.gradle.api.tasks.CacheableTask;
Expand Down Expand Up @@ -72,9 +75,10 @@
public class Cpd extends SourceTask implements VerificationTask, Reporting<CpdReports> {

private final WorkerExecutor workerExecutor;
private ProviderFactory providerFactory;
private final CpdReportsImpl reports;

private String encoding;
private final Property<String> encoding;
private boolean ignoreAnnotations;
private boolean ignoreFailures;
private boolean ignoreIdentifiers;
Expand All @@ -89,9 +93,12 @@ public class Cpd extends SourceTask implements VerificationTask, Reporting<CpdRe


@Inject
public Cpd(CollectionCallbackActionDecorator callbackActionDecorator, Instantiator instantiator, WorkerExecutor workerExecutor) {
public Cpd(CollectionCallbackActionDecorator callbackActionDecorator, Instantiator instantiator, WorkerExecutor workerExecutor, ObjectFactory objectFactory, ProviderFactory providerFactory) {
this.reports = instantiator.newInstance(CpdReportsImpl.class, this, callbackActionDecorator);
this.workerExecutor = workerExecutor;
this.providerFactory = providerFactory;

this.encoding = objectFactory.property(String.class);
}

@TaskAction
Expand All @@ -104,8 +111,10 @@ public void run() {
}

private void checkTaskState() {
if (getEncoding() == null) {
throw new InvalidUserDataException(String.format("Task '%s' requires 'encoding' but was: %s.", getName(), getEncoding()));
if (!getEncoding().isPresent()) {
throw new InvalidUserDataException(String.format(
"Task '%s' requires 'encoding' but was not set and default could not be retrieved.",
getName()));
}
if (getMinimumTokenCount() <= 0) {
throw new InvalidUserDataException(String.format("Task '%s' requires 'minimumTokenCount' to be greater than zero.", getName()));
Expand Down Expand Up @@ -167,11 +176,11 @@ private List<Report> createReportParameters(CpdReports reports) {
// VisibleForTesting
String getXmlRendererEncoding(CpdXmlFileReport report) {
String encoding = report.getEncoding();
if (encoding == null) {
encoding = getEncoding();
if (encoding == null && getEncoding().isPresent()) {
encoding = getEncoding().get();
}
if (encoding == null) {
encoding = System.getProperty("file.encoding");
encoding = providerFactory.provider(() -> System.getProperty("file.encoding")).get();
}
return encoding;
}
Expand Down Expand Up @@ -211,14 +220,10 @@ public CpdReports reports(Action<? super CpdReports> action) {
* @return the charset encoding
*/
@Input
public String getEncoding() {
public Property<String> getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}

/**
* Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very redundant and
* causes false positives.
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/de/aaschmid/gradle/plugins/cpd/CpdExtension.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package de.aaschmid.gradle.plugins.cpd;

import org.gradle.api.plugins.quality.CodeQualityExtension;
import javax.inject.Inject;

import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.quality.CodeQualityExtension;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;

/**
* Configuration options for the CPD plugin.
Expand All @@ -23,7 +28,7 @@
*/
public class CpdExtension extends CodeQualityExtension {

private String encoding = System.getProperty("file.encoding");
private final Property<String> encoding;
private boolean ignoreAnnotations = false;
private boolean ignoreIdentifiers = false;
private boolean ignoreLiterals = false;
Expand All @@ -34,6 +39,11 @@ public class CpdExtension extends CodeQualityExtension {
private boolean skipBlocks = true;
private String skipBlocksPattern = "#if 0|#endif";

@Inject
public CpdExtension(ObjectFactory objectFactory, ProviderFactory providerFactory) {
encoding = objectFactory.property(String.class).value(providerFactory.provider(() -> System.getProperty("file.encoding")));
}

/**
* The character set encoding (e.g., UTF-8) to use when reading the source code files but also when producing the report; defaults to
* {@code System.getProperty("file.encoding")}.
Expand All @@ -42,14 +52,10 @@ public class CpdExtension extends CodeQualityExtension {
*
* @return the charset encoding
*/
public String getEncoding() {
public Property<String> getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}

/**
* Ignore annotations because more and more modern frameworks use annotations on classes and methods which can be very redundant and
* causes false positives; defaults to {@code false}.
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/de/aaschmid/gradle/plugins/cpd/CpdPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void CpdPlugin_shouldApplyRequiredReportingBasePlugin(Project project) {

@Test
void CpdPlugin_shouldCreateAndConfigureCpdExtension(CpdExtension cpd) {
assertThat(cpd.getEncoding()).isEqualTo(System.getProperty("file.encoding"));
assertThat(cpd.getEncoding().get()).isEqualTo(System.getProperty("file.encoding"));
assertThat(cpd.isIgnoreAnnotations()).isFalse();
assertThat(cpd.isIgnoreIdentifiers()).isFalse();
assertThat(cpd.isIgnoreFailures()).isFalse();
Expand Down Expand Up @@ -80,7 +80,7 @@ void CpdPlugin_shouldCreateAndConfigureCpdCheckTaskWithCorrectDefaultValues(Proj
assertThat(t.getDescription()).isEqualTo("Run CPD analysis for all sources");
assertThat(t.getGroup()).isNull();

assertThat(t.getEncoding()).isEqualTo(System.getProperty("file.encoding"));
assertThat(t.getEncoding().get()).isEqualTo(System.getProperty("file.encoding"));
assertThat(t.getIgnoreAnnotations()).isFalse();
assertThat(t.getIgnoreFailures()).isFalse();
assertThat(t.getIgnoreIdentifiers()).isFalse();
Expand Down Expand Up @@ -116,7 +116,7 @@ void CpdPlugin_shouldConfigureProperDefaultsForAdditionalCpdTask(Project project
assertThat(t.getDescription()).isNull();
assertThat(t.getGroup()).isNull();

assertThat(t.getEncoding()).isEqualTo(System.getProperty("file.encoding"));
assertThat(t.getEncoding().get()).isEqualTo(System.getProperty("file.encoding"));
assertThat(t.getIgnoreAnnotations()).isFalse();
assertThat(t.getIgnoreFailures()).isFalse();
assertThat(t.getIgnoreIdentifiers()).isFalse();
Expand Down Expand Up @@ -273,7 +273,7 @@ void CpdPlugin_shouldAllowConfigureToolDependenciesExplicitlyViaConfiguration(Pr
@Test
void CpdPlugin_shouldAllowConfigureCpdCheckTaskViaCpdExtension(Project project, CpdExtension cpd, TaskProvider<Cpd> cpdCheck) {
// Given:
cpd.setEncoding("UTF-8");
cpd.getEncoding().set("UTF-8");
cpd.setIgnoreAnnotations(true);
cpd.setIgnoreFailures(true);
cpd.setIgnoreIdentifiers(true);
Expand All @@ -290,7 +290,7 @@ void CpdPlugin_shouldAllowConfigureCpdCheckTaskViaCpdExtension(Project project,
Cpd task = cpdCheck.get();

// Then:
assertThat(task.getEncoding()).isEqualTo("UTF-8");
assertThat(task.getEncoding().get()).isEqualTo("UTF-8");
assertThat(task.getIgnoreAnnotations()).isTrue();
assertThat(task.getIgnoreFailures()).isTrue();
assertThat(task.getIgnoreIdentifiers()).isTrue();
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/de/aaschmid/gradle/plugins/cpd/CpdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Cpd_shouldAllowConfigurationOfCpdTaskProperties(Project project, TaskProvid
task.setDescription("Execute me!");
task.setGroup("check");

task.setEncoding("ISO-8859-1");
task.getEncoding().set("ISO-8859-1");
task.setIgnoreAnnotations(true);
task.setIgnoreFailures(true);
task.setIgnoreIdentifiers(true);
Expand All @@ -95,7 +95,7 @@ void Cpd_shouldAllowConfigurationOfCpdTaskProperties(Project project, TaskProvid

// Then:
Cpd actual = cpdCheck.get();
assertThat(actual.getEncoding()).isEqualTo("ISO-8859-1");
assertThat(actual.getEncoding().get()).isEqualTo("ISO-8859-1");
assertThat(actual.getIgnoreAnnotations()).isTrue();
assertThat(actual.getIgnoreFailures()).isTrue();
assertThat(actual.getIgnoreIdentifiers()).isTrue();
Expand Down Expand Up @@ -192,13 +192,13 @@ void Cpd_shouldHaveCorrectTaskOutputs(Project project, TaskProvider<Cpd> cpdChec
@Test
void Cpd_shouldThrowInvalidUserDataExceptionIfEncodingIsNull(TaskProvider<Cpd> cpdCheck) {
// Given:
cpdCheck.configure(task -> task.setEncoding(null));
cpdCheck.configure(task -> task.getEncoding().set((String) null));
Cpd actual = cpdCheck.get();

// Expect:
assertThatThrownBy(() -> actual.getActions().forEach(a -> a.execute(actual)))
.isInstanceOf(InvalidUserDataException.class)
.hasMessage("Task 'cpdCheck' requires 'encoding' but was: null.");
.hasMessage("Task 'cpdCheck' requires 'encoding' but was not set and default could not be retrieved.");
}

@Test
Expand Down Expand Up @@ -246,7 +246,7 @@ static Arguments[] getXmlRendererEncoding() {
@MethodSource
void getXmlRendererEncoding(String taskEncoding, String reportEncoding, String expected, TaskProvider<Cpd> cpdCheck) {
// Given:
cpdCheck.configure(task -> task.setEncoding(taskEncoding));
cpdCheck.configure(task -> task.getEncoding().set(taskEncoding));

CpdXmlFileReportImpl report = new CpdXmlFileReportImpl("xml", cpdCheck.get());
report.setEncoding(reportEncoding);
Expand Down

0 comments on commit 9c4e69f

Please sign in to comment.