1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5 * https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package org.eluder.coveralls.maven.plugin;
26
27 import org.apache.maven.api.plugin.testing.InjectMojo;
28 import org.apache.maven.api.plugin.testing.MojoExtension;
29 import org.apache.maven.api.plugin.testing.MojoParameter;
30 import org.apache.maven.api.plugin.testing.MojoTest;
31 import org.assertj.core.api.Assertions;
32 import org.junit.jupiter.api.Test;
33
34 /**
35 * Tests for {@link CoverallsReportMojo} using the maven-plugin-testing-harness.
36 * <p>
37 * This provides more realistic testing by using actual Maven infrastructure to look up and configure the Mojo, rather
38 * than relying solely on mocks. The harness reads the generated plugin descriptor and configures the Mojo using Plexus
39 * dependency injection, closely simulating what Maven does at runtime.
40 */
41 @MojoTest
42 class CoverallsReportMojoHarnessTest {
43
44 /** The test POM path, relative to the project basedir. */
45 private static final String TEST_POM = "src/test/resources/unit/test-pom.xml";
46
47 /**
48 * Verify the mojo can be looked up by goal name using the testing harness.
49 * <p>
50 * This confirms the plugin descriptor is correctly generated and the mojo is properly registered as a Plexus
51 * component.
52 *
53 * @param mojo
54 * the injected mojo
55 */
56 @Test
57 void testMojoLookupWithHarness(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo) {
58 Assertions.assertThat(mojo).isNotNull();
59 }
60
61 /**
62 * Verify that the repoToken parameter is correctly injected from the test POM configuration.
63 *
64 * @param mojo
65 * the injected mojo
66 *
67 * @throws IllegalAccessException
68 * if field access via reflection fails
69 */
70 @Test
71 void testRepoTokenConfigured(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
72 throws IllegalAccessException {
73 Assertions.assertThat((String) MojoExtension.getVariableValueFromObject(mojo, "repoToken"))
74 .isEqualTo("test-token");
75 }
76
77 /**
78 * Verify that the dryRun parameter is correctly injected from the test POM configuration.
79 *
80 * @param mojo
81 * the injected mojo
82 *
83 * @throws IllegalAccessException
84 * if field access via reflection fails
85 */
86 @Test
87 void testDryRunConfigured(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
88 throws IllegalAccessException {
89 Assertions.assertThat((Boolean) MojoExtension.getVariableValueFromObject(mojo, "dryRun"))
90 .isEqualTo(Boolean.TRUE);
91 }
92
93 /**
94 * Verify that the sourceEncoding parameter is correctly injected from the test POM configuration.
95 *
96 * @param mojo
97 * the injected mojo
98 *
99 * @throws IllegalAccessException
100 * if field access via reflection fails
101 */
102 @Test
103 void testSourceEncodingConfigured(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
104 throws IllegalAccessException {
105 Assertions.assertThat((String) MojoExtension.getVariableValueFromObject(mojo, "sourceEncoding"))
106 .isEqualTo("UTF-8");
107 }
108
109 /**
110 * Verify that the serviceName parameter is correctly injected from the test POM configuration.
111 *
112 * @param mojo
113 * the injected mojo
114 *
115 * @throws IllegalAccessException
116 * if field access via reflection fails
117 */
118 @Test
119 void testServiceNameConfigured(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
120 throws IllegalAccessException {
121 Assertions.assertThat((String) MojoExtension.getVariableValueFromObject(mojo, "serviceName"))
122 .isEqualTo("test-ci-service");
123 }
124
125 /**
126 * Verify that the failOnServiceError parameter has the correct default value of {@code true} as declared in the
127 * plugin descriptor.
128 *
129 * @param mojo
130 * the injected mojo
131 *
132 * @throws IllegalAccessException
133 * if field access via reflection fails
134 */
135 @Test
136 void testDefaultFailOnServiceError(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
137 throws IllegalAccessException {
138 Assertions.assertThat((Boolean) MojoExtension.getVariableValueFromObject(mojo, "failOnServiceError"))
139 .isEqualTo(Boolean.TRUE);
140 }
141
142 /**
143 * Verify that the skip parameter has the correct default value of {@code false} as declared in the plugin
144 * descriptor.
145 *
146 * @param mojo
147 * the injected mojo
148 *
149 * @throws IllegalAccessException
150 * if field access via reflection fails
151 */
152 @Test
153 void testDefaultSkipIsFalse(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
154 throws IllegalAccessException {
155 Assertions.assertThat((Boolean) MojoExtension.getVariableValueFromObject(mojo, "skip"))
156 .isEqualTo(Boolean.FALSE);
157 }
158
159 /**
160 * Verify that the {@code @MojoParameter} annotation can be used to inject individual mojo parameters without
161 * specifying them in the test POM.
162 *
163 * @param mojo
164 * the injected mojo
165 *
166 * @throws IllegalAccessException
167 * if field access via reflection fails
168 */
169 @Test
170 @MojoParameter(name = "serviceJobId", value = "build-42")
171 void testMojoParameterAnnotationOverride(@InjectMojo(goal = "report", pom = TEST_POM) CoverallsReportMojo mojo)
172 throws IllegalAccessException {
173 Assertions.assertThat((String) MojoExtension.getVariableValueFromObject(mojo, "serviceJobId"))
174 .isEqualTo("build-42");
175 }
176
177 }