Skip to content

Commit

Permalink
Fix #191
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Sep 18, 2021
1 parent 54e69d9 commit 44476a0
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> mapping = new LinkedHashMap<String, String>();
public Map<String, String> mapping = new LinkedHashMap<String, String>();

/**
* Whether to use the default mapping between file extensions and comment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Copyright (C) 2008 Mycila (mathieu.carbou@gmail.com)
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- 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. -->

<plugin>
<groupId>com.mycila</groupId>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- 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. -->

<plugin>
<groupId>com.mycila</groupId>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<groupId>com.mycila</groupId>
</plugin>
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 44476a0

Please sign in to comment.