<commit_before>/*
 * Copyright 2016-2020 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless.generic;

import java.io.File;
import java.io.IOException;
import java.time.YearMonth;

import org.junit.Assert;
import org.junit.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode;

public class LicenseHeaderStepTest extends ResourceHarness {
	private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test";
	private static final String package_ = "package ";
	private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp.";

	@Test
	public void fromHeader() throws Throwable {
		FormatterStep step = LicenseHeaderStep.headerDelimiter(getTestResource("license/TestLicense"), package_).build();
		StepHarness.forStep(step)
				.testResource("license/MissingLicense.test", "license/HasLicense.test");
	}

	@Test
	public void should_apply_license_containing_YEAR_token() throws Throwable {
		StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR), package_).build())
				.test(getTestResource(FILE_NO_LICENSE), hasHeaderYear(currentYear()))
				.testUnaffected(hasHeaderYear(currentYear()))
				.testUnaffected(hasHeaderYear("2003"))
				.testUnaffected(hasHeaderYear("1990-2015"))
				.test(hasHeaderYear("Something before license.*/\n/* \n * " + HEADER_WITH_$YEAR, "2003"), hasHeaderYear(currentYear()))
				.test(hasHeaderYear(HEADER_WITH_$YEAR + "\n **/\n/* Something after license.", "2003"), hasHeaderYear("2003"))
				.test(hasHeaderYear("not a year"), hasHeaderYear(currentYear()));
		// Check with variant
		String otherFakeLicense = "This is a fake license. Copyright $YEAR ACME corp.";
		StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(otherFakeLicense), package_).build())<commit_msg>Add a test about parsing an existing year.<commit_after>/*
 * Copyright 2016-2020 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.spotless.generic;

import java.io.File;
import java.io.IOException;
import java.time.YearMonth;

import org.junit.Assert;
import org.junit.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode;

public class LicenseHeaderStepTest extends ResourceHarness {
	private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test";
	private static final String package_ = "package ";
	private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp.";

	@Test
	public void parseExistingYear() throws Exception {
		StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR), package_).build())
				// has existing
				.test(hasHeader("This is a fake license, 2007. ACME corp."), hasHeader("This is a fake license, 2007. ACME corp."))
				// if prefix changes, the year will get set to today
				.test(hasHeader("This is a license, 2007. ACME corp."), hasHeader("This is a fake license, " + currentYear() + ". ACME corp."))
				// if suffix changes, the year will get set to today
				.test(hasHeader("This is a fake license, 2007. Other corp."), hasHeader("This is a fake license, " + currentYear() + ". ACME corp."));
	}

	@Test
	public void fromHeader() throws Throwable {
		FormatterStep step = LicenseHeaderStep.headerDelimiter(getTestResource("license/TestLicense"), package_).build();
		StepHarness.forStep(step)
				.testResource("license/MissingLicense.test", "license/HasLicense.test");
	}

	@Test
	public void should_apply_license_containing_YEAR_token() throws Throwable {
		StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR), package_).build())
				.test(getTestResource(FILE_NO_LICENSE), hasHeaderYear(currentYear()))
				.testUnaffected(hasHeaderYear(currentYear()))
				.testUnaffected(hasHeaderYear("2003"))
				.testUnaffected(hasHeaderYear("1990-2015"))
				.test(hasHeaderYear("Something before license.*/\n/* \n * " + HEADER_WITH_$YEAR, "2003"), hasHeaderYear(currentYear()))
				.test(hasHeaderYear(HEADER_WITH_$YEAR + "\n **/\n/* Something after license.", "2003"), hasHeaderYear("2003"))
				.test(hasHeaderYear("not a year"), hasHeaderYear(currentYear()));
		// Check with variant
		String otherFakeLicense = "This is a fake license. Copyright $YEAR ACME corp.";
		StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(otherFakeLicense), package_).build())
{'fraction_non_alphanumeric': 0.1108999827258594, 'fraction_numerical': 0.015201243738124029, 'mean_word_length': 5.273022751895991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7252842', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1645, 'n_words': 473}