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.source;
26  
27  import java.io.IOException;
28  import java.security.NoSuchAlgorithmException;
29  
30  import org.eluder.coveralls.maven.plugin.ProcessingException;
31  import org.eluder.coveralls.maven.plugin.domain.Source;
32  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.extension.ExtendWith;
35  import org.mockito.ArgumentMatchers;
36  import org.mockito.Mock;
37  import org.mockito.Mockito;
38  import org.mockito.junit.jupiter.MockitoExtension;
39  
40  /**
41   * The Class UniqueSourceCallbackTest.
42   */
43  @ExtendWith(MockitoExtension.class)
44  class UniqueSourceCallbackTest {
45  
46      /** The source callback mock. */
47      @Mock
48      private SourceCallback sourceCallbackMock;
49  
50      /**
51       * On source with unique files.
52       *
53       * @throws NoSuchAlgorithmException
54       *             the no such algorithm exception
55       * @throws ProcessingException
56       *             the processing exception
57       * @throws IOException
58       *             Signals that an I/O exception has occurred.
59       */
60      @Test
61      void onSourceWithUniqueFiles() throws NoSuchAlgorithmException, ProcessingException, IOException {
62          final var s1 = this.createSource("Foo.java", "{\n  void();\n}\n", 2);
63          final var s2 = this.createSource("Bar.java", "{\n  bar();\n}\n", 2);
64  
65          final var cb = this.createUniqueSourceCallback();
66          cb.onBegin();
67          cb.onSource(s1);
68          cb.onSource(s2);
69          cb.onComplete();
70          Mockito.verify(this.sourceCallbackMock).onBegin();
71          Mockito.verify(this.sourceCallbackMock, Mockito.times(2)).onSource(ArgumentMatchers.any(Source.class));
72          Mockito.verify(this.sourceCallbackMock).onComplete();
73      }
74  
75      /**
76       * On source with duplicate sources.
77       *
78       * @throws NoSuchAlgorithmException
79       *             the no such algorithm exception
80       * @throws ProcessingException
81       *             the processing exception
82       * @throws IOException
83       *             Signals that an I/O exception has occurred.
84       */
85      @Test
86      void onSourceWithDuplicateSources() throws NoSuchAlgorithmException, ProcessingException, IOException {
87          final var s1 = this.createSource("Foo.java", "{\n  void();\n}\n", 2);
88          final var s2 = this.createSource("Foo.java", "{\n  void();\n}\n", 2);
89  
90          final var cb = this.createUniqueSourceCallback();
91          cb.onBegin();
92          cb.onSource(s1);
93          cb.onSource(s2);
94          cb.onComplete();
95          Mockito.verify(this.sourceCallbackMock).onBegin();
96          Mockito.verify(this.sourceCallbackMock, Mockito.times(1)).onSource(ArgumentMatchers.any(Source.class));
97          Mockito.verify(this.sourceCallbackMock).onComplete();
98      }
99  
100     /**
101      * On source with unique sources.
102      *
103      * @throws NoSuchAlgorithmException
104      *             the no such algorithm exception
105      * @throws ProcessingException
106      *             the processing exception
107      * @throws IOException
108      *             Signals that an I/O exception has occurred.
109      */
110     @Test
111     void onSourceWithUniqueSources() throws NoSuchAlgorithmException, ProcessingException, IOException {
112         final var s1 = this.createSource("Foo.java", "{\n  void();\n}\n", 2);
113         final var s2 = this.createSource("Foo.java", "{\n  void();\n  func();\n}\n", 2, 3);
114 
115         final var cb = this.createUniqueSourceCallback();
116         cb.onBegin();
117         cb.onSource(s1);
118         cb.onSource(s2);
119         cb.onComplete();
120         Mockito.verify(this.sourceCallbackMock).onBegin();
121         Mockito.verify(this.sourceCallbackMock, Mockito.times(2)).onSource(ArgumentMatchers.any(Source.class));
122         Mockito.verify(this.sourceCallbackMock).onComplete();
123     }
124 
125     /**
126      * Creates the unique source callback.
127      *
128      * @return the unique source callback
129      */
130     UniqueSourceCallback createUniqueSourceCallback() {
131         return new UniqueSourceCallback(this.sourceCallbackMock);
132     }
133 
134     /**
135      * Creates the source.
136      *
137      * @param name
138      *            the name
139      * @param source
140      *            the source
141      * @param relevant
142      *            the relevant
143      *
144      * @return the source
145      *
146      * @throws NoSuchAlgorithmException
147      *             the no such algorithm exception
148      */
149     Source createSource(final String name, final String source, final int... relevant) throws NoSuchAlgorithmException {
150         final var s = new Source(name, source, TestIoUtil.getSha512DigestHex(source));
151         for (final int i : relevant) {
152             s.addCoverage(i, 1);
153         }
154         return s;
155     }
156 
157 }