View Javadoc
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 java.util.Arrays;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import org.apache.maven.plugin.logging.Log;
32  import org.eluder.coveralls.maven.plugin.service.ServiceSetup;
33  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.extension.ExtendWith;
38  import org.mockito.Mock;
39  import org.mockito.Mockito;
40  import org.mockito.junit.jupiter.MockitoExtension;
41  
42  /**
43   * The Class EnvironmentTest.
44   */
45  @ExtendWith(MockitoExtension.class)
46  class EnvironmentTest {
47  
48      /** The mojo. */
49      CoverallsReportMojo mojo;
50  
51      /** The coverage parser mock. */
52      @Mock
53      CoverageParser coverageParserMock;
54  
55      /** The log mock. */
56      @Mock
57      Log logMock;
58  
59      /** The service mock. */
60      @Mock
61      ServiceSetup serviceMock;
62  
63      /**
64       * Inits the Environment.
65       */
66      @BeforeEach
67      void init() {
68          this.mojo = new CoverallsReportMojo() {
69              @Override
70              protected List<CoverageParser> createCoverageParsers(SourceLoader sourceLoader) {
71                  return Arrays.asList(EnvironmentTest.this.coverageParserMock);
72              }
73  
74              @Override
75              public Log getLog() {
76                  return EnvironmentTest.this.logMock;
77              }
78          };
79          this.mojo.serviceName = "service";
80          this.mojo.sourceEncoding = "UTF-8";
81          Mockito.lenient().when(this.serviceMock.isSelected()).thenReturn(true);
82      }
83  
84      /**
85       * Missing mojo.
86       */
87      @Test
88      void missingMojo() {
89          final var mockList = Arrays.asList(this.serviceMock);
90          Assertions.assertThrows(IllegalArgumentException.class, () -> new Environment(null, mockList));
91      }
92  
93      /**
94       * Missing services.
95       */
96      @Test
97      void missingServices() {
98          Assertions.assertThrows(IllegalArgumentException.class, () -> new Environment(this.mojo, null));
99      }
100 
101     /**
102      * Setup without services.
103      */
104     @Test
105     void setupWithoutServices() {
106         this.create(List.of()).setup();
107         Assertions.assertEquals("service", this.mojo.serviceName);
108     }
109 
110     /**
111      * Setup without source encoding.
112      */
113     @Test
114     void setupWithoutSourceEncoding() {
115         this.mojo.sourceEncoding = null;
116         final var list = Arrays.asList(this.serviceMock);
117         final var environment = this.create(list);
118         Assertions.assertThrows(IllegalArgumentException.class, environment::setup);
119     }
120 
121     /**
122      * Setup with incomplete job.
123      */
124     @Test
125     void setupWithIncompleteJob() {
126         Mockito.when(this.serviceMock.getJobId()).thenReturn("");
127         Mockito.when(this.serviceMock.getBuildUrl()).thenReturn("  ");
128 
129         this.create(Arrays.asList(this.serviceMock)).setup();
130         Assertions.assertEquals("service", this.mojo.serviceName);
131         Assertions.assertNull(this.mojo.serviceJobId);
132         Assertions.assertNull(this.mojo.serviceBuildNumber);
133         Assertions.assertNull(this.mojo.serviceBuildUrl);
134         Assertions.assertNull(this.mojo.branch);
135         Assertions.assertNull(this.mojo.pullRequest);
136         Assertions.assertNull(this.mojo.serviceEnvironment);
137     }
138 
139     /**
140      * Setup with complete job.
141      */
142     @Test
143     void setupWithCompleteJob() {
144         this.mojo.serviceName = null;
145         final var environment = new Properties();
146         environment.setProperty("env", "true");
147         Mockito.when(this.serviceMock.getName()).thenReturn("defined service");
148         Mockito.when(this.serviceMock.getJobId()).thenReturn("123");
149         Mockito.when(this.serviceMock.getBuildNumber()).thenReturn("456");
150         Mockito.when(this.serviceMock.getBuildUrl()).thenReturn("https://ci.com/project");
151         Mockito.when(this.serviceMock.getBranch()).thenReturn("master");
152         Mockito.when(this.serviceMock.getPullRequest()).thenReturn("111");
153         Mockito.when(this.serviceMock.getEnvironment()).thenReturn(environment);
154 
155         this.create(Arrays.asList(Mockito.mock(ServiceSetup.class), this.serviceMock)).setup();
156         Assertions.assertEquals("defined service", this.mojo.serviceName);
157         Assertions.assertEquals("123", this.mojo.serviceJobId);
158         Assertions.assertEquals("456", this.mojo.serviceBuildNumber);
159         Assertions.assertEquals("https://ci.com/project", this.mojo.serviceBuildUrl);
160         Assertions.assertEquals("master", this.mojo.branch);
161         Assertions.assertEquals("111", this.mojo.pullRequest);
162         Assertions.assertEquals("true", this.mojo.serviceEnvironment.get("env"));
163     }
164 
165     /**
166      * Setup without job override.
167      */
168     @Test
169     void setupWithoutJobOverride() {
170         final var environment = new Properties();
171         environment.setProperty("env", "true");
172         final var serviceEnvironment = new Properties();
173         serviceEnvironment.setProperty("env", "setProperty");
174         Mockito.when(this.serviceMock.getName()).thenReturn("defined service");
175         Mockito.when(this.serviceMock.getJobId()).thenReturn("123");
176         Mockito.when(this.serviceMock.getBuildNumber()).thenReturn("456");
177         Mockito.when(this.serviceMock.getBuildUrl()).thenReturn("https://ci.com/project");
178         Mockito.when(this.serviceMock.getBranch()).thenReturn("master");
179         Mockito.when(this.serviceMock.getPullRequest()).thenReturn("111");
180         Mockito.when(this.serviceMock.getEnvironment()).thenReturn(environment);
181         this.mojo.serviceJobId = "setJobId";
182         this.mojo.serviceBuildNumber = "setBuildNumber";
183         this.mojo.serviceBuildUrl = "setBuildUrl";
184         this.mojo.serviceEnvironment = serviceEnvironment;
185         this.mojo.branch = "setBranch";
186         this.mojo.pullRequest = "setPullRequest";
187 
188         this.create(Arrays.asList(this.serviceMock)).setup();
189 
190         Assertions.assertEquals("service", this.mojo.serviceName);
191         Assertions.assertEquals("setJobId", this.mojo.serviceJobId);
192         Assertions.assertEquals("setBuildNumber", this.mojo.serviceBuildNumber);
193         Assertions.assertEquals("setBuildUrl", this.mojo.serviceBuildUrl);
194         Assertions.assertEquals("setBranch", this.mojo.branch);
195         Assertions.assertEquals("setPullRequest", this.mojo.pullRequest);
196         Assertions.assertEquals("setProperty", this.mojo.serviceEnvironment.get("env"));
197     }
198 
199     /**
200      * Creates the.
201      *
202      * @param services
203      *            the services
204      *
205      * @return the environment
206      */
207     Environment create(final Iterable<ServiceSetup> services) {
208         return new Environment(this.mojo, services);
209     }
210 
211 }