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