From 44476a029b2ff7f48d8dd436e55fc49f174f8eb0 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sat, 18 Sep 2021 23:00:53 +0200 Subject: [PATCH] Fix #191 --- .../plugin/license/AbstractLicenseMojo.java | 2 +- .../maven/plugin/license/XmlPerLineTest.java | 156 ++++++++++++++++++ .../resources/issues/issue-191/expected1.xml | 21 +++ .../resources/issues/issue-191/expected2.xml | 19 +++ .../test/resources/issues/issue-191/file.xml | 4 + .../resources/issues/issue-191/header1.txt | 15 ++ .../resources/issues/issue-191/header2.txt | 13 ++ 7 files changed, 229 insertions(+), 1 deletion(-) create mode 100755 license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/XmlPerLineTest.java create mode 100644 license-maven-plugin/src/test/resources/issues/issue-191/expected1.xml create mode 100644 license-maven-plugin/src/test/resources/issues/issue-191/expected2.xml create mode 100644 license-maven-plugin/src/test/resources/issues/issue-191/file.xml create mode 100755 license-maven-plugin/src/test/resources/issues/issue-191/header1.txt create mode 100755 license-maven-plugin/src/test/resources/issues/issue-191/header2.txt diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java index 5fbbe0fc1..ecd745750 100755 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java @@ -242,7 +242,7 @@ public abstract class AbstractLicenseMojo extends AbstractMojo { * support, and the value is the name of the comment type to use. */ @Parameter - public LinkedHashMap mapping = new LinkedHashMap(); + public Map mapping = new LinkedHashMap(); /** * Whether to use the default mapping between file extensions and comment diff --git a/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/XmlPerLineTest.java b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/XmlPerLineTest.java new file mode 100755 index 000000000..bffb001ed --- /dev/null +++ b/license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/XmlPerLineTest.java @@ -0,0 +1,156 @@ +/** + * Copyright (C) 2008 Mycila (mathieu.carbou@gmail.com) + *

+ * 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.mycila.maven.plugin.license; + +import com.mycila.maven.plugin.license.util.FileUtils; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.junit.Test; + +import java.io.File; +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Mathieu Carbou (mathieu.carbou@gmail.com) + */ +public final class XmlPerLineTest { + + @Test + public void test_add() throws Exception { + File tmp = new File("target/test/issues/issue-191/test_add"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-191"), tmp); + + AbstractLicenseMojo plugin = new LicenseFormatMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + String processed = FileUtils.read(new File(tmp, "file.xml"), System.getProperty("file.encoding")); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-191/expected1.xml"), System.getProperty("file.encoding")); + assertThat(processed, is(equalTo(expected))); + } + + @Test + public void test_update() throws Exception { + File tmp = new File("target/test/issues/issue-191/test_update"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-191"), tmp); + + AbstractLicenseMojo plugin = new LicenseFormatMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + plugin = new LicenseFormatMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header2.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + String processed = FileUtils.read(new File(tmp, "file.xml"), System.getProperty("file.encoding")); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-191/expected2.xml"), System.getProperty("file.encoding")); + assertThat(processed, is(equalTo(expected))); + } + + @Test + public void test_remove() throws Exception { + File tmp = new File("target/test/issues/issue-191/test_remove"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-191"), tmp); + + AbstractLicenseMojo plugin = new LicenseFormatMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + plugin = new LicenseRemoveMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + String processed = FileUtils.read(new File(tmp, "file.xml"), System.getProperty("file.encoding")); + String expected = FileUtils.read(new File("src/test/resources/issues/issue-191/file.xml"), System.getProperty("file.encoding")); + assertThat(processed, is(equalTo(expected))); + } + + @Test + public void test_check_failed() throws Exception { + File tmp = new File("target/test/issues/issue-191/test_check_failed"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-191"), tmp); + + AbstractLicenseMojo plugin = new LicenseCheckMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + + try { + plugin.execute(); + fail(); + } catch (MojoExecutionException e) { + assertEquals("Some files do not have the expected license header", e.getMessage()); + } + } + + @Test + public void test_check_success() throws Exception { + File tmp = new File("target/test/issues/issue-191/test_check_success"); + FileUtils.copyFilesToFolder(new File("src/test/resources/issues/issue-191"), tmp); + + AbstractLicenseMojo plugin = new LicenseFormatMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + + plugin = new LicenseCheckMojo(); + plugin.project = new MavenProjectStub(); + plugin.defaultBasedir = tmp; + plugin.legacyConfigHeader = "src/test/resources/issues/issue-191/header1.txt"; + plugin.legacyConfigIncludes = new String[]{"file.xml"}; + plugin.mapping = Collections.singletonMap("xml", "XML_PER_LINE"); + + plugin.execute(); + } +} diff --git a/license-maven-plugin/src/test/resources/issues/issue-191/expected1.xml b/license-maven-plugin/src/test/resources/issues/issue-191/expected1.xml new file mode 100644 index 000000000..a4ffd9bdd --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-191/expected1.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + com.mycila + \ No newline at end of file diff --git a/license-maven-plugin/src/test/resources/issues/issue-191/expected2.xml b/license-maven-plugin/src/test/resources/issues/issue-191/expected2.xml new file mode 100644 index 000000000..4c23cfef8 --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-191/expected2.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + com.mycila + \ No newline at end of file diff --git a/license-maven-plugin/src/test/resources/issues/issue-191/file.xml b/license-maven-plugin/src/test/resources/issues/issue-191/file.xml new file mode 100644 index 000000000..c360b10a9 --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-191/file.xml @@ -0,0 +1,4 @@ + + + com.mycila + \ No newline at end of file diff --git a/license-maven-plugin/src/test/resources/issues/issue-191/header1.txt b/license-maven-plugin/src/test/resources/issues/issue-191/header1.txt new file mode 100755 index 000000000..4571b87ff --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-191/header1.txt @@ -0,0 +1,15 @@ +Copyright (c) 2006-2009 + +All rights reserved. + +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. \ No newline at end of file diff --git a/license-maven-plugin/src/test/resources/issues/issue-191/header2.txt b/license-maven-plugin/src/test/resources/issues/issue-191/header2.txt new file mode 100755 index 000000000..4abff6064 --- /dev/null +++ b/license-maven-plugin/src/test/resources/issues/issue-191/header2.txt @@ -0,0 +1,13 @@ +Copyright (c) 2006-2009 + +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. \ No newline at end of file