From 7927e7e6a421ba81ae3d42593f30dfbbc18ed00c Mon Sep 17 00:00:00 2001 From: Eyal Kaspi Date: Wed, 7 Feb 2018 20:41:40 +0100 Subject: [PATCH] Allow custom separator for years in license header --- CHANGES.md | 2 + .../spotless/generic/LicenseHeaderStep.java | 20 +++-- plugin-gradle/CHANGES.md | 2 + plugin-gradle/README.md | 14 +++- .../gradle/spotless/FormatExtension.java | 82 +++++++++++++++++-- .../gradle/spotless/GroovyExtension.java | 8 +- .../gradle/spotless/JavaExtension.java | 8 +- .../gradle/spotless/KotlinExtension.java | 8 +- .../gradle/spotless/KotlinExtensionTest.java | 64 +++++++++++++++ .../KotlinCodeWithMultiYearHeader.test | 5 ++ .../KotlinCodeWithMultiYearHeader2.test | 5 ++ .../generic/LicenseHeaderStepTest.java | 50 +++++++++-- 12 files changed, 230 insertions(+), 38 deletions(-) create mode 100644 plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test create mode 100644 plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test diff --git a/CHANGES.md b/CHANGES.md index 6ba54eeee4..4effe38a64 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ You might be looking for: ### Version 1.10.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) +* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199)) + ### Version 1.9.0 - February 5th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.9.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.9.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) * Updated default ktlint from 0.6.1 to 0.14.0 diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index 817bb3bc1c..5ea10fa34d 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -43,11 +43,13 @@ public final class LicenseHeaderStep implements Serializable { private String licenseHeaderWithYearTokenReplaced; /** Creates a FormatterStep which forces the start of each file to match a license header. */ - public static FormatterStep createFromHeader(String licenseHeader, String delimiter) { + public static FormatterStep createFromHeader(String licenseHeader, String delimiter, + String yearSeparator) { Objects.requireNonNull(licenseHeader, "licenseHeader"); Objects.requireNonNull(delimiter, "delimiter"); + Objects.requireNonNull(yearSeparator, "yearSeparator"); return FormatterStep.create(LicenseHeaderStep.NAME, - new LicenseHeaderStep(licenseHeader, delimiter), + new LicenseHeaderStep(licenseHeader, delimiter, yearSeparator), step -> step::format); } @@ -55,12 +57,14 @@ public static FormatterStep createFromHeader(String licenseHeader, String delimi * Creates a FormatterStep which forces the start of each file to match the license header * contained in the given file. */ - public static FormatterStep createFromFile(File licenseHeaderFile, Charset encoding, String delimiter) { + public static FormatterStep createFromFile(File licenseHeaderFile, Charset encoding, String delimiter, + String yearSeparator) { Objects.requireNonNull(licenseHeaderFile, "licenseHeaderFile"); Objects.requireNonNull(encoding, "encoding"); Objects.requireNonNull(delimiter, "delimiter"); + Objects.requireNonNull(yearSeparator, "yearSeparator"); return FormatterStep.createLazy(LicenseHeaderStep.NAME, - () -> new LicenseHeaderStep(licenseHeaderFile, encoding, delimiter), + () -> new LicenseHeaderStep(licenseHeaderFile, encoding, delimiter, yearSeparator), step -> step::format); } @@ -69,7 +73,7 @@ public static String name() { } /** The license that we'd like enforced. */ - private LicenseHeaderStep(String licenseHeader, String delimiter) { + private LicenseHeaderStep(String licenseHeader, String delimiter, String yearSeparator) { if (delimiter.contains("\n")) { throw new IllegalArgumentException("The delimiter must not contain any newlines."); } @@ -86,13 +90,13 @@ private LicenseHeaderStep(String licenseHeader, String delimiter) { this.licenseHeaderBeforeYearToken = licenseHeader.substring(0, yearTokenIndex); this.licenseHeaderAfterYearToken = licenseHeader.substring(yearTokenIndex + 5, licenseHeader.length()); this.licenseHeaderWithYearTokenReplaced = licenseHeader.replace("$YEAR", String.valueOf(YearMonth.now().getYear())); - this.yearMatcherPattern = Pattern.compile("[0-9]{4}(-[0-9]{4})?"); + this.yearMatcherPattern = Pattern.compile("[0-9]{4}(" + Pattern.quote(yearSeparator) + "[0-9]{4})?"); } } /** Reads the license file from the given file. */ - private LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter) throws IOException { - this(new String(Files.readAllBytes(licenseFile.toPath()), encoding), delimiter); + private LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter, String yearSeparator) throws IOException { + this(new String(Files.readAllBytes(licenseFile.toPath()), encoding), delimiter, yearSeparator); } /** Formats the given string. */ diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index fd32bb85c3..9fb2639787 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -2,6 +2,8 @@ ### Version 3.10.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/)) +* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199) + ### Version 3.9.0 - February 5th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.9.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.9.0)) * Updated default ktlint from 0.6.1 to 0.14.0 diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 29d8159c19..185e5df421 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -307,10 +307,22 @@ The `licenseHeader` and `licenseHeaderFile` steps will generate license headers * A generated license header will _not_ be updated when * a single year is already present, e.g. `/* Licensed under Apache-2.0 1990. */` - * a hyphen-separated year range is already present, e.g. + * a year range is already present, e.g. `/* Licensed under Apache-2.0 1990-2003. */` * the `$YEAR` token is otherwise missing +The separator for the year range defaults to the hyphen character, e.g `1990-2003`, but can be customized with the `yearSeparator` property. + +For instance, the following configuration treats `1990, 2003` as a valid year range. + +```gradle +spotless { + format java { + licenseHeader(''Licensed under Apache-2.0 $YEAR').yearSeparator(', ') + } +} +``` + diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 8856199c46..fc3e2e2deb 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -345,26 +345,92 @@ public void indentWithTabs() { indentWithTabs(4); } + abstract class LicenseHeaderConfig { + + String delimiter; + + static final String DEFAULT_LICENSE_YEAR_DELIMITER = "-"; + String yearSeparator = DEFAULT_LICENSE_YEAR_DELIMITER; + + public LicenseHeaderConfig(String delimiter) { + this.delimiter = Objects.requireNonNull(delimiter, "delimiter"); + } + + /** + * @param delimiter + * Spotless will look for a line that starts with this to know what the "top" is. + */ + public LicenseHeaderConfig delimiter(String delimiter) { + this.delimiter = Objects.requireNonNull(delimiter, "delimiter"); + replaceStep(createStep()); + return this; + } + + /** + * @param yearSeparator + * The characters used to separate the first and last years in multi years patterns. + */ + public LicenseHeaderConfig yearSeparator(String yearSeparator) { + this.yearSeparator = Objects.requireNonNull(yearSeparator, "yearSeparator"); + replaceStep(createStep()); + return this; + } + + abstract FormatterStep createStep(); + } + + class LicenseStringHeaderConfig extends LicenseHeaderConfig { + + private String header; + + LicenseStringHeaderConfig(String delimiter, String header) { + super(delimiter); + this.header = Objects.requireNonNull(header, "header"); + } + + FormatterStep createStep() { + return LicenseHeaderStep.createFromHeader(header, delimiter, yearSeparator); + } + } + + class LicenseFileHeaderConfig extends LicenseHeaderConfig { + + private Object headerFile; + + LicenseFileHeaderConfig(String delimiter, Object headerFile) { + super(delimiter); + this.headerFile = Objects.requireNonNull(headerFile, "headerFile"); + } + + FormatterStep createStep() { + return LicenseHeaderStep + .createFromFile(getProject().file(headerFile), getEncoding(), delimiter, + yearSeparator); + } + } + /** * @param licenseHeader - * Content that should be at the top of every file + * Content that should be at the top of every file. * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void licenseHeader(String licenseHeader, String delimiter) { - addStep(LicenseHeaderStep.createFromHeader(licenseHeader, delimiter)); + public LicenseHeaderConfig licenseHeader(String licenseHeader, String delimiter) { + LicenseHeaderConfig config = new LicenseStringHeaderConfig(delimiter, licenseHeader); + addStep(config.createStep()); + return config; } /** * @param licenseHeaderFile - * Content that should be at the top of every file + * Content that should be at the top of every file. * @param delimiter * Spotless will look for a line that starts with this to know what the "top" is. */ - public void licenseHeaderFile(Object licenseHeaderFile, String delimiter) { - Objects.requireNonNull(licenseHeaderFile, "licenseHeaderFile"); - Objects.requireNonNull(delimiter, "delimiter"); - addStep(LicenseHeaderStep.createFromFile(getProject().file(licenseHeaderFile), getEncoding(), delimiter)); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile, String delimiter) { + LicenseHeaderConfig config = new LicenseFileHeaderConfig(delimiter, licenseHeaderFile); + addStep(config.createStep()); + return config; } /** Sets up a format task according to the values in this extension. */ diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java index 838b2b7fa2..f297e901d5 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java @@ -55,12 +55,12 @@ public void excludeJava(boolean excludeJava) { this.excludeJava = excludeJava; } - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, JavaExtension.LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, JavaExtension.LICENSE_HEADER_DELIMITER); } - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER); } /** Method interface has been changed to diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 8d553bdaab..5831612ce7 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -46,12 +46,12 @@ public JavaExtension(SpotlessExtension rootExtension) { // testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java as well static final String LICENSE_HEADER_DELIMITER = "package "; - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); } - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); } /** Method interface has been changed to diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 886dfb0f59..45a81a2630 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -33,12 +33,12 @@ public KotlinExtension(SpotlessExtension rootExtension) { super(rootExtension); } - public void licenseHeader(String licenseHeader) { - licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeader(String licenseHeader) { + return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER); } - public void licenseHeaderFile(Object licenseHeaderFile) { - licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); + public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { + return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER); } /** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */ diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index 96c06f78d0..e8a1cc5add 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; +import java.time.YearMonth; import org.assertj.core.api.Assertions; import org.junit.Assert; @@ -24,6 +25,7 @@ public class KotlinExtensionTest extends GradleIntegrationTest { private static final String HEADER = "// License Header"; + private static final String HEADER_WITH_YEAR = "// License Header $YEAR"; @Test public void integration() throws IOException { @@ -72,4 +74,66 @@ public void testWithHeader() throws IOException { // Make sure that no additional stuff got added to the file. .contains(HEADER + '\n' + original); } + + @Test + public void testWithCustomHeaderSeparator() throws IOException { + write("build.gradle", + "plugins {", + " id 'nebula.kotlin' version '1.0.6'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " licenseHeader ('" + HEADER + "', '@file')", + " ktlint()", + " }", + "}"); + final File testFile = write("src/main/kotlin/test.kt", getTestResource("kotlin/licenseheader/KotlinCodeWithoutHeader.test")); + final String original = read(testFile.toPath()); + gradleRunner().withArguments("spotlessApply").build(); + final String result = read(testFile.toPath()); + Assertions + .assertThat(result) + // Make sure the header gets added. + .startsWith(HEADER) + // Make sure that the rest of the file is still there with nothing removed. + .endsWith(original) + // Make sure that no additional stuff got added to the file. + .contains(HEADER + '\n' + original); + } + + @Test + public void testWithNonStandardYearSeparator() throws IOException { + write("build.gradle", + "plugins {", + " id 'nebula.kotlin' version '1.0.6'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlin {", + " licenseHeader('" + HEADER_WITH_YEAR + "').yearSeparator(', ')", + " ktlint()", + " }", + "}"); + + final File testFile = write("src/main/kotlin/test.kt", getTestResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test")); + final String original = read(testFile.toPath()); + final File testFile2 = write("src/main/kotlin/test2.kt", getTestResource("kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test")); + final String original2 = read(testFile.toPath()); + gradleRunner().withArguments("spotlessApply").build(); + final String result = read(testFile.toPath()); + final String result2 = read(testFile2.toPath()); + + Assertions + .assertThat(result) + // Make sure the a "valid" header isn't changed + .contains("// License Header 2012, 2014"); + + Assertions + .assertThat(result2) + // Make sure that an "invalid" header is rewritten + .startsWith(HEADER_WITH_YEAR.replace("$YEAR", String.valueOf(YearMonth.now().getYear()))); + } } diff --git a/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test b/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test new file mode 100644 index 0000000000..c8890b5f34 --- /dev/null +++ b/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader.test @@ -0,0 +1,5 @@ +// License Header 2012, 2014 +@file:JvmName("SomeFileName") +package my.test + +object AnObject diff --git a/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test b/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test new file mode 100644 index 0000000000..70707e6d6a --- /dev/null +++ b/plugin-gradle/src/test/resources/kotlin/licenseheader/KotlinCodeWithMultiYearHeader2.test @@ -0,0 +1,5 @@ +// License Header 2012-2014 +@file:JvmName("SomeFileName") +package my.test + +object AnObject diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 2f57af6212..164d927a94 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -38,25 +38,56 @@ public class LicenseHeaderStepTest extends ResourceHarness { private static final String KEY_FILE_WITHOUT_LICENSE = "license/FileWithoutLicenseHeader.test"; private static final String KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER = "license/FileWithLicenseHeaderAndPlaceholder.test"; - // If this constant changes, don't forget to change the similarly-named one in - // plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java as well + // If these constants change, don't forget to change the similarly-named ones in + // plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java and TODO as well private static final String LICENSE_HEADER_DELIMITER = "package "; + private static final String LICENSE_YEAR_DELIMITER = "-"; @Test public void fromHeader() throws Throwable { - FormatterStep step = LicenseHeaderStep.createFromHeader(getTestResource(KEY_LICENSE), LICENSE_HEADER_DELIMITER); + FormatterStep step = LicenseHeaderStep.createFromHeader(getTestResource(KEY_LICENSE), LICENSE_HEADER_DELIMITER, LICENSE_YEAR_DELIMITER); assertOnResources(step, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED); } @Test public void fromFile() throws Throwable { - FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER); + FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER, LICENSE_YEAR_DELIMITER); assertOnResources(step, KEY_FILE_NOTAPPLIED, KEY_FILE_APPLIED); } @Test public void should_apply_license_containing_YEAR_token() throws Throwable { - FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE_WITH_YEAR_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER); + FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE_WITH_YEAR_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER, LICENSE_YEAR_DELIMITER); + + StepHarness.forStep(step) + .test(getTestResource(KEY_FILE_WITHOUT_LICENSE), fileWithPlaceholderContaining(currentYear())) + .testUnaffected(fileWithPlaceholderContaining(currentYear())) + .testUnaffected(fileWithPlaceholderContaining("2003")) + .testUnaffected(fileWithPlaceholderContaining("1990-2015")) + .test(fileWithPlaceholderContaining("not a year"), fileWithPlaceholderContaining(currentYear())); + } + + @Test + public void should_apply_license_containing_YEAR_token_with_non_default_year_delimiter() throws Throwable { + FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE_WITH_YEAR_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER, ", "); + + StepHarness.forStep(step) + .testUnaffected(fileWithPlaceholderContaining("1990, 2015")) + .test(fileWithPlaceholderContaining("1990-2015"), fileWithPlaceholderContaining(currentYear())); + } + + @Test + public void should_apply_license_containing_YEAR_token_with_special_character_in_year_delimiter() throws Throwable { + FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE_WITH_YEAR_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER, "("); + + StepHarness.forStep(step) + .testUnaffected(fileWithPlaceholderContaining("1990(2015")) + .test(fileWithPlaceholderContaining("1990-2015"), fileWithPlaceholderContaining(currentYear())); + } + + @Test + public void should_apply_license_containing_YEAR_token_with_custom_separator() throws Throwable { + FormatterStep step = LicenseHeaderStep.createFromFile(createTestFile(KEY_LICENSE_WITH_YEAR_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER, LICENSE_YEAR_DELIMITER); StepHarness.forStep(step) .test(getTestResource(KEY_FILE_WITHOUT_LICENSE), fileWithPlaceholderContaining(currentYear())) @@ -76,7 +107,7 @@ private String currentYear() { @Test public void efficient() throws Throwable { - FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n", "contentstart"); + FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n", "contentstart", LICENSE_YEAR_DELIMITER); String alreadyCorrect = "LicenseHeader\ncontentstart"; Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File(""))); // If no change is required, it should return the exact same string for efficiency reasons @@ -86,7 +117,7 @@ public void efficient() throws Throwable { @Test public void sanitized() throws Throwable { // The sanitizer should add a \n - FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader", "contentstart"); + FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader", "contentstart", LICENSE_YEAR_DELIMITER); String alreadyCorrect = "LicenseHeader\ncontentstart"; Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File(""))); Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect, new File(""))); @@ -95,7 +126,7 @@ public void sanitized() throws Throwable { @Test public void sanitizerDoesntGoTooFar() throws Throwable { // if the user wants extra lines after the header, we shouldn't clobber them - FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n\n", "contentstart"); + FormatterStep step = LicenseHeaderStep.createFromHeader("LicenseHeader\n\n", "contentstart", LICENSE_YEAR_DELIMITER); String alreadyCorrect = "LicenseHeader\n\ncontentstart"; Assert.assertEquals(alreadyCorrect, step.format(alreadyCorrect, new File(""))); Assert.assertSame(alreadyCorrect, step.format(alreadyCorrect, new File(""))); @@ -106,6 +137,7 @@ public void equality() { new SerializableEqualityTester() { String header = "LICENSE"; String delimiter = "package"; + String yearSeparator = "-"; @Override protected void setupTest(API api) { @@ -123,7 +155,7 @@ protected void setupTest(API api) { @Override protected FormatterStep create() { - return LicenseHeaderStep.createFromHeader(header, delimiter); + return LicenseHeaderStep.createFromHeader(header, delimiter, yearSeparator); } }.testEquals(); }