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.source;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertNull;
28  import static org.junit.jupiter.api.Assertions.assertThrows;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.nio.file.Files;
33  import java.nio.file.Path;
34  
35  import org.eluder.coveralls.maven.plugin.domain.Source;
36  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.io.CleanupMode;
39  import org.junit.jupiter.api.io.TempDir;
40  
41  class DirectorySourceLoaderTest {
42  
43      @TempDir(cleanup = CleanupMode.ON_SUCCESS)
44      public Path folder;
45  
46      @Test
47      void testMissingSourceFileFromDirectory() throws IOException {
48          DirectorySourceLoader sourceLoader = new DirectorySourceLoader(folder.toFile(), folder.toFile(), "UTF-8");
49          assertNull(sourceLoader.load("Foo.java"));
50      }
51  
52      @Test
53      void testInvalidSourceFile() throws IOException {
54          String subFolder = Files.createDirectory(folder.resolve("subFolder")).toFile().getName();
55          DirectorySourceLoader sourceLoader = new DirectorySourceLoader(folder.toFile(), folder.toFile(), "UTF-8");
56          assertThrows(IllegalArgumentException.class, () -> {
57              sourceLoader.load(subFolder);
58          });
59      }
60  
61      @Test
62      void testLoadSource() throws IOException {
63          File file = Files.createFile(folder.resolve("newFile")).toFile(); 
64          TestIoUtil.writeFileContent("public class Foo {\r\n    \n}\r", file);
65          DirectorySourceLoader sourceLoader = new DirectorySourceLoader(folder.toFile(), folder.toFile(), "UTF-8");
66          Source source = sourceLoader.load(file.getName());
67          assertEquals(file.getName(), source.getName());
68          assertEquals("27F0B29785725F4946DBD05F7963E507B8DB735C2803BBB80C93ECB02291B2E2F9B03CBF27526DB68B6A862F1C6541275CD413A1CCD3E07209B9CAE0C04163C6", source.getDigest());
69          assertEquals(4, source.getCoverage().length);
70      }
71  }