This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-changes-plugin.git
commit bcef34866f8342c1cee8362cf1da65da57b08307 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Thu Nov 26 05:53:15 2026 +0000 Add unit tests for changes-check, changes-validate, report links and custom announcement template --- .../plugins/changes/ChangesCheckMojoTest.java | 51 ++++++++++++ .../plugins/changes/ChangesReportRendererTest.java | 92 ++++++++++++++++++++++ .../plugins/changes/ChangesValidatorMojoTest.java | 30 +++++++ .../changes/announcement/AnnouncementMojoTest.java | 23 ++++++ src/test/unit/announce-custom/test-announce.vm | 20 +++++ src/test/unit/check/pending-changes.xml | 28 +++++++ src/test/unit/check/pending-release-changes.xml | 28 +++++++ src/test/unit/check/valid-changes.xml | 28 +++++++ src/test/unit/report/system-changes.xml | 53 +++++++++++++ src/test/unit/validate/schema-invalid-changes.xml | 27 +++++++ 10 files changed, 380 insertions(+) diff --git a/src/test/java/org/apache/maven/plugins/changes/ChangesCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/changes/ChangesCheckMojoTest.java index e954b58..9f853ac 100644 --- a/src/test/java/org/apache/maven/plugins/changes/ChangesCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/changes/ChangesCheckMojoTest.java @@ -18,9 +18,14 @@ */ package org.apache.maven.plugins.changes; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.plugin.MojoExecutionException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -29,6 +34,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; * @version $Id$ * @since 2.4 */ +@MojoTest public class ChangesCheckMojoTest { @Test public void testIsValidDate() { @@ -137,4 +143,49 @@ public class ChangesCheckMojoTest { } } } + + private static final String POM = "src/test/unit/plugin-config.xml"; + + @InjectMojo(goal = "changes-check", pom = POM) + @MojoParameter(name = "xmlPath", value = "src/test/unit/check/pending-release-changes.xml") + @MojoParameter(name = "version", value = "1.0") + @Test + public void testPendingReleaseDateFails(ChangesCheckMojo mojo) { + MojoExecutionException e = assertThrows(MojoExecutionException.class, mojo::execute); + assertTrue(e.getMessage().endsWith("pending-release-changes.xml has an invalid release date.")); + } + + @InjectMojo(goal = "changes-check", pom = POM) + @MojoParameter(name = "xmlPath", value = "src/test/unit/check/valid-changes.xml") + @MojoParameter(name = "version", value = "1.0") + @Test + public void testValidReleaseDatePasses(ChangesCheckMojo mojo) throws Exception { + mojo.execute(); + } + + @InjectMojo(goal = "changes-check", pom = POM) + @MojoParameter(name = "xmlPath", value = "src/test/unit/check/pending-changes.xml") + @MojoParameter(name = "version", value = "1.0-SNAPSHOT") + @MojoParameter(name = "skipSnapshots", value = "true") + @Test + public void testSkipSnapshotsSkipsInvalidDate(ChangesCheckMojo mojo) throws Exception { + mojo.execute(); + } + + @InjectMojo(goal = "changes-check", pom = POM) + @MojoParameter(name = "xmlPath", value = "src/test/unit/check/pending-changes.xml") + @MojoParameter(name = "version", value = "1.0-SNAPSHOT") + @MojoParameter(name = "skipSnapshots", value = "false") + @Test + public void testSnapshotIsCheckedWhenNotSkipped(ChangesCheckMojo mojo) { + assertThrows(MojoExecutionException.class, mojo::execute); + } + + @InjectMojo(goal = "changes-check", pom = POM) + @MojoParameter(name = "xmlPath", value = "src/test/unit/check/does-not-exist.xml") + @MojoParameter(name = "version", value = "1.0") + @Test + public void testMissingFileOnlyWarns(ChangesCheckMojo mojo) throws Exception { + mojo.execute(); + } } diff --git a/src/test/java/org/apache/maven/plugins/changes/ChangesReportRendererTest.java b/src/test/java/org/apache/maven/plugins/changes/ChangesReportRendererTest.java new file mode 100644 index 0000000..d45c9d0 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/changes/ChangesReportRendererTest.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.maven.plugins.changes; + +import java.io.File; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; + +import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Renders the changes report to a Doxia sink and checks the generated markup. + */ +public class ChangesReportRendererTest { + + private static String render(String changesXml, String system, String url, Map<String, String> links) { + StringWriter out = new StringWriter(); + Xhtml5BaseSink sink = new Xhtml5BaseSink(out); + ChangesXML xml = new ChangesXML(new File(changesXml), new SystemStreamLog()); + ChangesReportRenderer renderer = + new ChangesReportRenderer(sink, ResourceBundle.getBundle("changes-report", Locale.ENGLISH), xml); + renderer.setIssueLinksPerSystem(links); + renderer.setSystem(system); + renderer.setUrl(url); + renderer.setTeam("team.html"); + renderer.setAddActionDate(true); + renderer.setEscapeText(true); + renderer.render(); + return out.toString(); + } + + @Test + public void testIssueLinksPerSystemWithConfiguredSystem() { + Map<String, String> links = new HashMap<>(); + links.put("jira", "http://myjira/browse/%ISSUE%"); + links.put("qc", "http://localhost/%ISSUE%"); + + String html = render("src/test/unit/report/system-changes.xml", "jira", "http://localhost", links); + + assertTrue(html.contains("href=\"http://myjira/browse/MCHANGES-88\""), html); + assertTrue(html.contains("href=\"http://localhost/1212\""), html); + assertTrue(html.contains("2008-01-01"), "action date missing"); + } + + @Test + public void testIssueLinksPerSystemWithoutIssueManagement() { + Map<String, String> links = new HashMap<>(); + links.put("default", "http://myjira/browse/%ISSUE%"); + links.put("qc", "http://localhost/%ISSUE%"); + links.put("redmine", "http://www.redmine.org/show/issue/%ISSUE%"); + + String html = render("src/test/unit/report/system-changes.xml", null, null, links); + + assertTrue(html.contains("href=\"http://myjira/browse/MCHANGES-88\""), html); + assertTrue(html.contains("href=\"http://localhost/1212\""), html); + assertTrue(html.contains("href=\"http://www.redmine.org/show/issue/88\""), html); + assertTrue(html.contains("2008-01-01"), "action date missing"); + } + + @Test + public void testNoLinkForSystemWithoutTemplate() { + String html = render("src/test/unit/report/system-changes.xml", null, null, new HashMap<>()); + + assertFalse(html.contains("href=\"http://myjira"), html); + assertFalse(html.contains("href=\"http://localhost/1212\""), html); + assertTrue(html.contains("MCHANGES-88"), html); + } +} diff --git a/src/test/java/org/apache/maven/plugins/changes/ChangesValidatorMojoTest.java b/src/test/java/org/apache/maven/plugins/changes/ChangesValidatorMojoTest.java index 7bc3030..7fc6077 100644 --- a/src/test/java/org/apache/maven/plugins/changes/ChangesValidatorMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/changes/ChangesValidatorMojoTest.java @@ -22,9 +22,15 @@ import org.apache.maven.api.plugin.testing.InjectMojo; import org.apache.maven.api.plugin.testing.MojoParameter; import org.apache.maven.api.plugin.testing.MojoTest; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; /** * @author Olivier Lamy @@ -65,4 +71,28 @@ public class ChangesValidatorMojoTest { public void testValidationFailedWithNoMojoFailure(ChangesValidatorMojo mojo) throws Exception { mojo.execute(); } + + @InjectMojo(goal = "changes-validate", pom = "src/test/unit/plugin-config.xml") + @MojoParameter(name = "xmlPath", value = "src/test/unit/validate/schema-invalid-changes.xml") + @MojoParameter(name = "failOnError", value = "true") + @Test + public void testSchemaExceptionMessageNamesFile(ChangesValidatorMojo mojo) { + MojoExecutionException e = assertThrows(MojoExecutionException.class, mojo::execute); + assertTrue(e.getMessage().contains("changes.xml file is not valid"), e.getMessage()); + assertTrue(e.getMessage().contains("schema-invalid-changes.xml"), e.getMessage()); + } + + @InjectMojo(goal = "changes-validate", pom = "src/test/unit/plugin-config.xml") + @MojoParameter(name = "xmlPath", value = "src/test/unit/validate/schema-invalid-changes.xml") + @MojoParameter(name = "failOnError", value = "false") + @Test + public void testInvalidFileOnlyWarnsWhenNotFailing(ChangesValidatorMojo mojo) throws Exception { + Log log = mock(Log.class); + mojo.setLog(log); + + mojo.execute(); + + verify(log).warn("validation errors: "); + verify(log).info(contains("skip previous validation errors due to failOnError=false.")); + } } diff --git a/src/test/java/org/apache/maven/plugins/changes/announcement/AnnouncementMojoTest.java b/src/test/java/org/apache/maven/plugins/changes/announcement/AnnouncementMojoTest.java index 4aff4a9..0b0695f 100644 --- a/src/test/java/org/apache/maven/plugins/changes/announcement/AnnouncementMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/changes/announcement/AnnouncementMojoTest.java @@ -29,6 +29,7 @@ import org.codehaus.plexus.util.FileUtils; import org.junit.jupiter.api.Test; import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -68,6 +69,28 @@ public class AnnouncementMojoTest { assertContains("Deleted the erroneous code.", result); } + @InjectMojo(goal = "announcement-generate", pom = "src/test/unit/plugin-config.xml") + @MojoParameter(name = "xmlPath", value = "src/test/unit/announce-changes.xml") + @MojoParameter(name = "announcementDirectory", value = "target/test-custom-template") + @MojoParameter(name = "version", value = "1.1") + @MojoParameter(name = "finalName", value = "maven-changes-plugin-test-1.1") + @MojoParameter(name = "template", value = "test-announce.vm") + @MojoParameter(name = "templateDirectory", value = "src/test/unit/announce-custom") + @MojoParameter(name = "introduction", value = "Test Announcement Custom template") + @Test + public void testCustomTemplate(AnnouncementMojo mojo) throws Exception { + File dir = new File(getBasedir(), "target/test-custom-template"); + FileUtils.deleteDirectory(dir); + dir.mkdirs(); + + mojo.execute(); + + String result = new String(Files.readAllBytes(dir.toPath().resolve("test-announce.vm")), "UTF-8"); + assertTrue(result.contains("maven-changes-plugin-test-1.1 release"), result); + assertTrue(result.contains("Test Announcement Custom template"), result); + assertFalse(result.contains("$"), "unresolved velocity reference in: " + result); + } + private File prepareAnnouncementDirectory() throws IOException { File announcementDirectory = new File(getBasedir(), "target/test"); diff --git a/src/test/unit/announce-custom/test-announce.vm b/src/test/unit/announce-custom/test-announce.vm new file mode 100644 index 0000000..f692651 --- /dev/null +++ b/src/test/unit/announce-custom/test-announce.vm @@ -0,0 +1,20 @@ +## Licensed to the Apache Software Foundation (ASF) under one +## or more contributor license agreements. See the NOTICE file +## distributed with this work for additional information +## regarding copyright ownership. The ASF licenses this file +## to you 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. +The ${developmentTeam} is pleased to announce the ${finalName} release! + +${introduction} + diff --git a/src/test/unit/check/pending-changes.xml b/src/test/unit/check/pending-changes.xml new file mode 100644 index 0000000..8b19e78 --- /dev/null +++ b/src/test/unit/check/pending-changes.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +--> +<document xmlns="http://maven.apache.org/changes/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/changes/2.0.0 http://maven.apache.org/xsd/changes-2.0.0.xsd"> + <body> + <release version="1.0-SNAPSHOT" date="pending" description="Release"> + <action dev="me" type="add">Something new.</action> + </release> + </body> +</document> diff --git a/src/test/unit/check/pending-release-changes.xml b/src/test/unit/check/pending-release-changes.xml new file mode 100644 index 0000000..f13d7fb --- /dev/null +++ b/src/test/unit/check/pending-release-changes.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +--> +<document xmlns="http://maven.apache.org/changes/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/changes/2.0.0 http://maven.apache.org/xsd/changes-2.0.0.xsd"> + <body> + <release version="1.0" date="pending" description="Release"> + <action dev="me" type="add">Something new.</action> + </release> + </body> +</document> diff --git a/src/test/unit/check/valid-changes.xml b/src/test/unit/check/valid-changes.xml new file mode 100644 index 0000000..4fe5aa4 --- /dev/null +++ b/src/test/unit/check/valid-changes.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +--> +<document xmlns="http://maven.apache.org/changes/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/changes/2.0.0 http://maven.apache.org/xsd/changes-2.0.0.xsd"> + <body> + <release version="1.0" date="2010-12-16" description="Release"> + <action dev="me" type="add">Something new.</action> + </release> + </body> +</document> diff --git a/src/test/unit/report/system-changes.xml b/src/test/unit/report/system-changes.xml new file mode 100644 index 0000000..32e2eff --- /dev/null +++ b/src/test/unit/report/system-changes.xml @@ -0,0 +1,53 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +--> + +<document xmlns="http://maven.apache.org/changes/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/changes/2.0.0 http://maven.apache.org/xsd/changes-2.0.0.xsd"> + <properties> + <title>Changes report Project</title> + <author email="[email protected]">Mr Zloug</author> + </properties> + <body> + <release version="1.1" date="2005-03-01" description="Subsequent release"> + <action dev="me" type="add"> + Added additional documentation on how to configure the plugin. + </action> + <action dev="me" type="fix" issue="MCHANGES-88"> + Enable retrieving component-specific issues. + </action> + <action dev="me" type="fix" issue="1212" system="qc"> + Enable retrieving component-specific issues. + </action> + <action dev="TheOneAndOnly" type="update" issue="88" system="redmine"> + Updated library dependency. + </action> + <action dev="jruiz" type="remove" due-to="others" due-to-email="[email protected]"> + The element type " link " must be terminated by the matching end-tag. + Deleted the erroneous code. + </action> + </release> + + <release version="1.0" date="2005-01-01" description="First release"> + <action dev="me" type="update" date="2008-01-01"> + Uploaded documentation on how to use the plugin. + </action> + </release> + </body> +</document> diff --git a/src/test/unit/validate/schema-invalid-changes.xml b/src/test/unit/validate/schema-invalid-changes.xml new file mode 100644 index 0000000..d9ead51 --- /dev/null +++ b/src/test/unit/validate/schema-invalid-changes.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. +--> + +<document xmlns="http://maven.apache.org/changes/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/changes/2.0.0 http://maven.apache.org/xsd/changes-2.0.0.xsd"> + <properties> + <atitle>Changes report Project</atitle> + </properties> +</document>
