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.File;
28  import java.io.IOException;
29  import java.nio.charset.StandardCharsets;
30  import java.nio.file.Files;
31  import java.nio.file.Path;
32  
33  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.api.io.CleanupMode;
37  import org.junit.jupiter.api.io.TempDir;
38  
39  /**
40   * The Class ScanSourceLoaderTest.
41   */
42  class ScanSourceLoaderTest {
43  
44      /** The folder. */
45      @TempDir(cleanup = CleanupMode.ON_SUCCESS)
46      Path folder;
47  
48      /**
49       * Missing source file from directory.
50       *
51       * @throws IOException
52       *             Signals that an I/O exception has occurred.
53       */
54      @Test
55      void missingSourceFileFromDirectory() throws IOException {
56          final var sourceLoader = new ScanSourceLoader(this.folder.toFile(), this.folder.toFile(),
57                  StandardCharsets.UTF_8);
58          Assertions.assertNull(sourceLoader.load("Foo.java"));
59      }
60  
61      /**
62       * Invalid source file.
63       *
64       * @throws IOException
65       *             Signals that an I/O exception has occurred.
66       */
67      @Test
68      void invalidSourceFile() throws IOException {
69          final var subFolder = Files.createDirectory(this.folder.resolve("subFolder")).toFile();
70          final var sourceLoader = new ScanSourceLoader(this.folder.toFile(), this.folder.toFile(),
71                  StandardCharsets.UTF_8);
72          final var subFolderName = subFolder.getName();
73          Assertions.assertThrows(IllegalArgumentException.class, () -> sourceLoader.load(subFolderName));
74      }
75  
76      /**
77       * Load source.
78       *
79       * @throws IOException
80       *             Signals that an I/O exception has occurred.
81       */
82      @Test
83      void loadSource() throws IOException {
84          final var level1 = Files.createDirectory(this.folder.resolve("level1"));
85          final var level2 = Files.createDirectory(level1.resolve("level2"));
86          final var level3 = Files.createDirectory(level2.resolve("level3"));
87          final var fileA = Files.createFile(level3.resolve("AFile.java")).toFile();
88          final var fileB = Files.createFile(level3.resolve("BFile.java")).toFile();
89          TestIoUtil.writeFileContent("public class Foo {\r\n    \n}\r", fileA);
90          TestIoUtil.writeFileContent("public class Foo {\r\n    \n}\r", fileB);
91          final var sourceLoader = new ScanSourceLoader(this.folder.toFile(), this.folder.toFile(),
92                  StandardCharsets.UTF_8);
93          final var sourceA = sourceLoader.load(fileA.getName());
94          Assertions.assertEquals(
95                  "level1" + File.separator + "level2" + File.separator + "level3" + File.separator + "AFile.java",
96                  sourceA.getName());
97          Assertions.assertEquals(
98                  "27F0B29785725F4946DBD05F7963E507B8DB735C2803BBB80C93ECB02291B2E2F9B03CBF27526DB68B6A862F1C6541275CD413A1CCD3E07209B9CAE0C04163C6",
99                  sourceA.getDigest());
100         Assertions.assertEquals(4, sourceA.getCoverage().length);
101         final var sourceB = sourceLoader.load(fileB.getName());
102         Assertions.assertEquals(
103                 "level1" + File.separator + "level2" + File.separator + "level3" + File.separator + "BFile.java",
104                 sourceB.getName());
105         Assertions.assertEquals(
106                 "27F0B29785725F4946DBD05F7963E507B8DB735C2803BBB80C93ECB02291B2E2F9B03CBF27526DB68B6A862F1C6541275CD413A1CCD3E07209B9CAE0C04163C6",
107                 sourceB.getDigest());
108         Assertions.assertEquals(4, sourceB.getCoverage().length);
109     }
110 
111 }