View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin;
25  
26  import static org.junit.jupiter.api.Assertions.*;
27  import static org.mockito.Mockito.lenient;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.when;
30  
31  import java.util.Arrays;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Properties;
35  
36  import org.apache.maven.plugin.logging.Log;
37  import org.eluder.coveralls.maven.plugin.service.ServiceSetup;
38  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.extension.ExtendWith;
42  import org.mockito.Mock;
43  import org.mockito.junit.jupiter.MockitoExtension;
44  
45  @ExtendWith(MockitoExtension.class)
46  class EnvironmentTest {
47  
48      private CoverallsReportMojo mojo;
49  
50      @Mock
51      private CoverageParser coverageParserMock;
52  
53      @Mock
54      private Log logMock;
55  
56      @Mock
57      private ServiceSetup serviceMock;
58  
59      @BeforeEach
60      void init() {
61          mojo = new CoverallsReportMojo() {
62              @Override
63              protected List<CoverageParser> createCoverageParsers(SourceLoader sourceLoader) {
64                  return Arrays.asList(coverageParserMock);
65              }
66  
67              @Override
68              public Log getLog() {
69                  return logMock;
70              }
71          };
72          mojo.serviceName = "service";
73          mojo.sourceEncoding = "UTF-8";
74          lenient().when(serviceMock.isSelected()).thenReturn(true);
75      }
76  
77      @Test
78      void testMissingMojo() {
79          assertThrows(IllegalArgumentException.class, () -> {
80              new Environment(null, Arrays.asList(serviceMock));
81          });
82      }
83  
84      @Test
85      void testMissingServices() {
86          assertThrows(IllegalArgumentException.class, () -> {
87              new Environment(mojo, null);
88          });
89      }
90  
91      @Test
92      void testSetupWithoutServices() {
93          create(Collections.<ServiceSetup>emptyList()).setup();
94          assertEquals("service", mojo.serviceName);
95      }
96  
97      @Test
98      void testSetupWithoutSourceEncoding() {
99          mojo.sourceEncoding = null;
100         assertThrows(IllegalArgumentException.class, () -> {
101             create(Arrays.asList(serviceMock)).setup();
102         });
103     }
104 
105     @Test
106     void testSetupWithIncompleteJob() {
107         when(serviceMock.getJobId()).thenReturn("");
108         when(serviceMock.getBuildUrl()).thenReturn("  ");
109 
110         create(Arrays.asList(serviceMock)).setup();
111         assertEquals("service", mojo.serviceName);
112         assertNull(mojo.serviceJobId);
113         assertNull(mojo.serviceBuildNumber);
114         assertNull(mojo.serviceBuildUrl);
115         assertNull(mojo.branch);
116         assertNull(mojo.pullRequest);
117         assertNull(mojo.serviceEnvironment);
118     }
119 
120     @Test
121     void testSetupWithCompleteJob() {
122         mojo.serviceName = null;
123         Properties environment = new Properties();
124         environment.setProperty("env", "true");
125         when(serviceMock.getName()).thenReturn("defined service");
126         when(serviceMock.getJobId()).thenReturn("123");
127         when(serviceMock.getBuildNumber()).thenReturn("456");
128         when(serviceMock.getBuildUrl()).thenReturn("http://ci.com/project");
129         when(serviceMock.getBranch()).thenReturn("master");
130         when(serviceMock.getPullRequest()).thenReturn("111");
131         when(serviceMock.getEnvironment()).thenReturn(environment);
132 
133         create(Arrays.asList(mock(ServiceSetup.class), serviceMock)).setup();
134         assertEquals("defined service", mojo.serviceName);
135         assertEquals("123", mojo.serviceJobId);
136         assertEquals("456", mojo.serviceBuildNumber);
137         assertEquals("http://ci.com/project", mojo.serviceBuildUrl);
138         assertEquals("master", mojo.branch);
139         assertEquals("111", mojo.pullRequest);
140         assertEquals("true", mojo.serviceEnvironment.get("env"));
141     }
142 
143     @Test
144     void testSetupWithoutJobOverride() {
145         Properties environment = new Properties();
146         environment.setProperty("env", "true");
147         Properties serviceEnvironment = new Properties();
148         serviceEnvironment.setProperty("env", "setProperty");
149         when(serviceMock.getName()).thenReturn("defined service");
150         when(serviceMock.getJobId()).thenReturn("123");
151         when(serviceMock.getBuildNumber()).thenReturn("456");
152         when(serviceMock.getBuildUrl()).thenReturn("http://ci.com/project");
153         when(serviceMock.getBranch()).thenReturn("master");
154         when(serviceMock.getPullRequest()).thenReturn("111");
155         when(serviceMock.getEnvironment()).thenReturn(environment);
156         mojo.serviceJobId = "setJobId";
157         mojo.serviceBuildNumber = "setBuildNumber";
158         mojo.serviceBuildUrl = "setBuildUrl";
159         mojo.serviceEnvironment = serviceEnvironment;
160         mojo.branch = "setBranch";
161         mojo.pullRequest = "setPullRequest";
162 
163         create(Arrays.asList(serviceMock)).setup();
164 
165         assertEquals("service", mojo.serviceName);
166         assertEquals("setJobId", mojo.serviceJobId);
167         assertEquals("setBuildNumber", mojo.serviceBuildNumber);
168         assertEquals("setBuildUrl", mojo.serviceBuildUrl);
169         assertEquals("setBranch", mojo.branch);
170         assertEquals("setPullRequest", mojo.pullRequest);
171         assertEquals("setProperty", mojo.serviceEnvironment.get("env"));
172     }
173 
174     private Environment create(final Iterable<ServiceSetup> services) {
175         return new Environment(mojo, services);
176     }
177 }