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.domain;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.*;
29  
30  import org.junit.jupiter.api.Disabled;
31  
32  class SourceTest {
33  
34      @Test
35      void testAddCoverage() {
36          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
37          source.addCoverage(1, 3);
38          source.addCoverage(3, 3);
39          assertArrayEquals(new Integer[] { 3, null, 3, null }, source.getCoverage());
40      }
41  
42      @Test
43      void testAddBranchCoverage() {
44          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
45          source.addBranchCoverage(2, 0, 0, 2);
46          source.addBranchCoverage(2, 0, 1, 3);
47          assertArrayEquals(new Integer[] { 2, 0, 0, 2, 2, 0, 1, 3 }, source.getBranches());
48      }
49  
50      @Test
51      void testAddSameBranchReplaceExistingOne() {
52          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
53          source.addBranchCoverage(2, 0, 0, 2);
54          source.addBranchCoverage(2, 0, 0, 3);
55          assertArrayEquals(new Integer[] { 2, 0, 0, 3 }, source.getBranches());
56      }
57  
58      @Test
59      void testAddSameBranchDoNotKeepOrdering() {
60          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
61          source.addBranchCoverage(2, 0, 0, 0);
62          source.addBranchCoverage(2, 0, 1, 0);
63          source.addBranchCoverage(2, 0, 0, 1);
64          assertArrayEquals(new Integer[] { 2, 0, 1, 0, 2, 0, 0, 1 }, source.getBranches());
65      }
66  
67      @Test
68      void testAddCoverageForSourceOutOfBounds() {
69          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
70          assertThrows(IllegalArgumentException.class, () -> {
71              source.addCoverage(5, 1);
72          });
73      }
74  
75      @Test
76      void testAddBranchCoverageForSourceOutOfBounds() {
77          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
78          assertThrows(IllegalArgumentException.class, () -> {
79              source.addBranchCoverage(6, 0, 0, 2);
80          });
81      }
82  
83      @Test
84      @Disabled("#45: https://github.com/trautonen/coveralls-maven-plugin/issues/45")
85      void testGetNameWithClassifier() {
86          Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
87          source.setClassifier("Inner");
88          assertEquals("src/main/java/Hello.java", source.getName());
89          assertEquals("src/main/java/Hello.java#Inner", source.getFullName());
90      }
91  
92      @Test
93      void testMerge() {
94          Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
95          source1.addCoverage(1, 2);
96          source1.addCoverage(3, 4);
97          source1.addBranchCoverage(2, 0, 0, 1);
98          Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  if(true) {\n  }\n}\n", "609BD24390ADB11D11536CA2ADD18BD0");
99          source2.addCoverage(2, 1);
100         source2.addCoverage(3, 3);
101         source2.addBranchCoverage(2, 0, 0, 1);
102         source2.addBranchCoverage(2, 0, 1, 3);
103 
104         Source merged = source1.merge(source2);
105         assertFalse(source1 == merged);
106         assertFalse(source2 == merged);
107         assertEquals(source1.getName(), merged.getName());
108         assertEquals(source1.getDigest(), merged.getDigest());
109         assertEquals(source1.getClassifier(), merged.getClassifier());
110         assertEquals(Integer.valueOf(2), merged.getCoverage()[0]);
111         assertEquals(Integer.valueOf(1), merged.getCoverage()[1]);
112         assertEquals(Integer.valueOf(7), merged.getCoverage()[2]);
113         assertNull(merged.getCoverage()[3]);
114         assertEquals(Integer.valueOf(2), merged.getBranches()[0]);
115         assertEquals(Integer.valueOf(0), merged.getBranches()[1]);
116         assertEquals(Integer.valueOf(0), merged.getBranches()[2]);
117         assertEquals(Integer.valueOf(2), merged.getBranches()[3]);
118         assertEquals(Integer.valueOf(2), merged.getBranches()[4]);
119         assertEquals(Integer.valueOf(0), merged.getBranches()[5]);
120         assertEquals(Integer.valueOf(1), merged.getBranches()[6]);
121         assertEquals(Integer.valueOf(3), merged.getBranches()[7]);
122     }
123 
124     @Test
125     void testMergeDifferent() {
126         Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
127         source1.addCoverage(1, 3);
128         Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n", "CBA7831606B51D1499349451B70758E3");
129         source2.addCoverage(2, 4);
130         Source merged = source1.merge(source2);
131         assertFalse(source1 == merged);
132         assertFalse(source2 == merged);
133         assertArrayEquals(source1.getCoverage(), merged.getCoverage());
134     }
135 
136     @Test
137     void testEqualsForNull() {
138         Source source = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
139         assertNotNull(source);
140     }
141 
142     @Test
143     void testEqualsForDifferentSources() {
144         Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
145         Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n", "CBA7831606B51D1499349451B70758E3");
146         assertNotEquals(source1, source2);
147     }
148 
149     @Test
150     void testHashCode() {
151         Source source1 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
152         Source source2 = new Source("src/main/java/Hello.java", "public class Hello {\n  \n}\n", "E8BD88CF0BDB77A6408234FD91FD22C3");
153         Source source3 = new Source("src/main/java/Hello.java", "public class Hello {\n  void();\n}\n", "CBA7831606B51D1499349451B70758E3");
154         assertEquals(source1.hashCode(), source2.hashCode());
155         assertNotEquals(source1.hashCode(), source3.hashCode());
156         assertNotEquals(source2.hashCode(), source3.hashCode());
157     }
158 }