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.domain;
26  
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Disabled;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * The Class SourceTest.
33   */
34  class SourceTest {
35  
36      /**
37       * Test add coverage.
38       */
39      @Test
40      void addCoverage() {
41          final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
42                  "E8BD88CF0BDB77A6408234FD91FD22C3");
43          source.addCoverage(1, 3);
44          source.addCoverage(3, 3);
45          Assertions.assertArrayEquals(new Integer[] { 3, null, 3, null }, source.getCoverage());
46      }
47  
48      /**
49       * Test add branch coverage.
50       */
51      @Test
52      void addBranchCoverage() {
53          final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
54                  "609BD24390ADB11D11536CA2ADD18BD0");
55          source.addBranchCoverage(2, 0, 0, 2);
56          source.addBranchCoverage(2, 0, 1, 3);
57          Assertions.assertArrayEquals(new Integer[] { 2, 0, 0, 2, 2, 0, 1, 3 }, source.getBranches());
58      }
59  
60      /**
61       * Adds the same branch replace existing one.
62       */
63      @Test
64      void addSameBranchReplaceExistingOne() {
65          final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
66                  "609BD24390ADB11D11536CA2ADD18BD0");
67          source.addBranchCoverage(2, 0, 0, 2);
68          source.addBranchCoverage(2, 0, 0, 3);
69          Assertions.assertArrayEquals(new Integer[] { 2, 0, 0, 3 }, source.getBranches());
70      }
71  
72      /**
73       * Adds the same branch do not keep ordering.
74       */
75      @Test
76      void addSameBranchDoNotKeepOrdering() {
77          final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
78                  "609BD24390ADB11D11536CA2ADD18BD0");
79          source.addBranchCoverage(2, 0, 0, 0);
80          source.addBranchCoverage(2, 0, 1, 0);
81          source.addBranchCoverage(2, 0, 0, 1);
82          Assertions.assertArrayEquals(new Integer[] { 2, 0, 1, 0, 2, 0, 0, 1 }, source.getBranches());
83      }
84  
85      /**
86       * Adds the coverage for source out of bounds.
87       */
88      @Test
89      void addCoverageForSourceOutOfBounds() {
90          final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
91                  "E8BD88CF0BDB77A6408234FD91FD22C3");
92          Assertions.assertThrows(IllegalArgumentException.class, () -> source.addCoverage(5, 1));
93      }
94  
95      /**
96       * Adds the branch coverage for source out of bounds.
97       */
98      @Test
99      void addBranchCoverageForSourceOutOfBounds() {
100         final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
101                 "609BD24390ADB11D11536CA2ADD18BD0");
102         Assertions.assertThrows(IllegalArgumentException.class, () -> source.addBranchCoverage(6, 0, 0, 2));
103     }
104 
105     /**
106      * Gets the name with classifier.
107      */
108     @Test
109     @Disabled("#45: https://github.com/trautonen/coveralls-maven-plugin/issues/45")
110     void nameWithClassifier() {
111         final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
112                 "E8BD88CF0BDB77A6408234FD91FD22C3");
113         source.setClassifier("Inner");
114         Assertions.assertEquals("src/main/java/Hello.java", source.getName());
115         Assertions.assertEquals("src/main/java/Hello.java#Inner", source.getFullName());
116     }
117 
118     /**
119      * Test merge.
120      */
121     @Test
122     void merge() {
123         final var source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
124                 "609BD24390ADB11D11536CA2ADD18BD0");
125         source1.addCoverage(1, 2);
126         source1.addCoverage(3, 4);
127         source1.addBranchCoverage(2, 0, 0, 1);
128         final var source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n",
129                 "609BD24390ADB11D11536CA2ADD18BD0");
130         source2.addCoverage(2, 1);
131         source2.addCoverage(3, 3);
132         source2.addBranchCoverage(2, 0, 0, 1);
133         source2.addBranchCoverage(2, 0, 1, 3);
134 
135         final var merged = source1.merge(source2);
136         org.assertj.core.api.Assertions.assertThat(source1).isNotSameAs(merged);
137         org.assertj.core.api.Assertions.assertThat(source2).isNotSameAs(merged);
138         Assertions.assertEquals(source1.getName(), merged.getName());
139         Assertions.assertEquals(source1.getDigest(), merged.getDigest());
140         Assertions.assertEquals(source1.getClassifier(), merged.getClassifier());
141         Assertions.assertEquals(Integer.valueOf(2), merged.getCoverage()[0]);
142         Assertions.assertEquals(Integer.valueOf(1), merged.getCoverage()[1]);
143         Assertions.assertEquals(Integer.valueOf(7), merged.getCoverage()[2]);
144         Assertions.assertNull(merged.getCoverage()[3]);
145         Assertions.assertEquals(Integer.valueOf(2), merged.getBranches()[0]);
146         Assertions.assertEquals(Integer.valueOf(0), merged.getBranches()[1]);
147         Assertions.assertEquals(Integer.valueOf(0), merged.getBranches()[2]);
148         Assertions.assertEquals(Integer.valueOf(2), merged.getBranches()[3]);
149         Assertions.assertEquals(Integer.valueOf(2), merged.getBranches()[4]);
150         Assertions.assertEquals(Integer.valueOf(0), merged.getBranches()[5]);
151         Assertions.assertEquals(Integer.valueOf(1), merged.getBranches()[6]);
152         Assertions.assertEquals(Integer.valueOf(3), merged.getBranches()[7]);
153     }
154 
155     /**
156      * Merge different.
157      */
158     @Test
159     void mergeDifferent() {
160         final var source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
161                 "E8BD88CF0BDB77A6408234FD91FD22C3");
162         source1.addCoverage(1, 3);
163         final var source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n",
164                 "CBA7831606B51D1499349451B70758E3");
165         source2.addCoverage(2, 4);
166         final var merged = source1.merge(source2);
167         org.assertj.core.api.Assertions.assertThat(source1).isNotSameAs(merged);
168         org.assertj.core.api.Assertions.assertThat(source2).isNotSameAs(merged);
169         Assertions.assertArrayEquals(source1.getCoverage(), merged.getCoverage());
170     }
171 
172     /**
173      * Equals for null.
174      */
175     @Test
176     void equalsForNull() {
177         final var source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
178                 "E8BD88CF0BDB77A6408234FD91FD22C3");
179         Assertions.assertNotNull(source);
180     }
181 
182     /**
183      * Equals for different sources.
184      */
185     @Test
186     void equalsForDifferentSources() {
187         final var source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
188                 "E8BD88CF0BDB77A6408234FD91FD22C3");
189         final var source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n",
190                 "CBA7831606B51D1499349451B70758E3");
191         Assertions.assertNotEquals(source1, source2);
192     }
193 
194     /**
195      * Test hash code.
196      */
197     @Test
198     void hashCodeVerification() {
199         final var source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
200                 "E8BD88CF0BDB77A6408234FD91FD22C3");
201         final var source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n",
202                 "E8BD88CF0BDB77A6408234FD91FD22C3");
203         final var source3 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n",
204                 "CBA7831606B51D1499349451B70758E3");
205         Assertions.assertEquals(source1.hashCode(), source2.hashCode());
206         Assertions.assertNotEquals(source1.hashCode(), source3.hashCode());
207         Assertions.assertNotEquals(source2.hashCode(), source3.hashCode());
208     }
209 
210 }