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.parser;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import org.assertj.core.api.Assertions;
33  import org.eluder.coveralls.maven.plugin.CoverageFixture;
34  import org.eluder.coveralls.maven.plugin.CoverageParser;
35  import org.eluder.coveralls.maven.plugin.ProcessingException;
36  import org.eluder.coveralls.maven.plugin.domain.Source;
37  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
38  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
39  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
40  import org.junit.jupiter.api.Test;
41  import org.mockito.ArgumentCaptor;
42  import org.mockito.Mockito;
43  
44  /**
45   * The Class CoberturaParserTest.
46   */
47  class CoberturaParserTest extends AbstractCoverageParserTest {
48  
49      @Override
50      protected CoverageParser createCoverageParser(final File coverageFile, final SourceLoader sourceLoader) {
51          return new CoberturaParser(coverageFile, sourceLoader);
52      }
53  
54      @Override
55      protected List<String> getCoverageResources() {
56          return Arrays.asList("cobertura.xml");
57      }
58  
59      @Override
60      protected List<List<String>> getCoverageFixture() {
61          return CoverageFixture.JAVA_FILES;
62      }
63  
64      /**
65       * Tests that a branch="true" line without a condition-coverage attribute causes an early return with no branches
66       * added to the source (covers the null condition-coverage guard).
67       *
68       * @throws ProcessingException
69       *             the processing exception
70       * @throws IOException
71       *             Signals that an I/O exception has occurred.
72       */
73      @Test
74      void parseBranchLineWithNullConditionCoverage() throws ProcessingException, IOException {
75          final var content = TestIoUtil.readFileContent(TestIoUtil.getFile("SimpleCoverage.java"));
76          final var sourceLoader = Mockito.mock(SourceLoader.class);
77          Mockito.when(sourceLoader.load("org/eluder/coverage/sample/SimpleCoverage.java"))
78                  .thenAnswer(invocation -> new Source("org/eluder/coverage/sample/SimpleCoverage.java", content,
79                          TestIoUtil.getSha512DigestHex(content)));
80  
81          final SourceCallback callback = Mockito.mock(SourceCallback.class);
82  
83          final var parser = new CoberturaParser(TestIoUtil.getFile("cobertura-branch-null.xml"), sourceLoader);
84          parser.parse(callback);
85  
86          final ArgumentCaptor<Source> captor = ArgumentCaptor.forClass(Source.class);
87          Mockito.verify(callback).onSource(captor.capture());
88  
89          final var source = captor.getValue();
90          // The branch=true line with no condition-coverage should NOT add any branches
91          Assertions.assertThat(source.getBranchesList()).isEmpty();
92      }
93  
94  }