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.util;
26  
27  import java.io.IOException;
28  import java.nio.charset.Charset;
29  import java.nio.charset.StandardCharsets;
30  import java.nio.file.Files;
31  import java.nio.file.Path;
32  import java.util.Arrays;
33  import java.util.List;
34  
35  import org.apache.maven.project.MavenProject;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  import org.junit.jupiter.api.extension.ExtendWith;
40  import org.junit.jupiter.api.io.CleanupMode;
41  import org.junit.jupiter.api.io.TempDir;
42  import org.mockito.Mock;
43  import org.mockito.Mockito;
44  import org.mockito.junit.jupiter.MockitoExtension;
45  
46  /**
47   * The Class SourceLoaderFactoryTest.
48   */
49  @ExtendWith(MockitoExtension.class)
50  class SourceLoaderFactoryTest {
51  
52      /** The folder. */
53      @TempDir(cleanup = CleanupMode.ON_SUCCESS)
54      public Path folder;
55  
56      /** The root. */
57      @Mock
58      private MavenProject root;
59  
60      /** The m 1. */
61      @Mock
62      private MavenProject m1;
63  
64      /** The m 2. */
65      @Mock
66      private MavenProject m2;
67  
68      /** The root sources. */
69      private Path rootSources;
70  
71      /** The m 1 sources. */
72      private Path m1Sources;
73  
74      /** The m 2 sources. */
75      private Path m2Sources;
76  
77      /**
78       * Inits the Source Loader Factory.
79       *
80       * @throws IOException
81       *             Signals that an I/O exception has occurred.
82       */
83      @BeforeEach
84      void init() throws IOException {
85          this.rootSources = Files.createDirectory(this.folder.resolve("src"));
86          this.m1Sources = Files.createDirectory(this.rootSources.resolve("m1"));
87          this.m2Sources = Files.createDirectory(this.m1Sources.resolve("m2"));
88          Mockito.lenient().when(this.root.getCollectedProjects()).thenReturn(Arrays.asList(this.m1, this.m2));
89          Mockito.lenient().when(this.m1.getCollectedProjects()).thenReturn(List.of());
90          Mockito.lenient().when(this.m2.getCollectedProjects()).thenReturn(List.of());
91          Mockito.lenient().when(this.root.getCompileSourceRoots())
92                  .thenReturn(Arrays.asList(this.rootSources.toFile().getAbsolutePath()));
93          Mockito.lenient().when(this.m1.getCompileSourceRoots())
94                  .thenReturn(Arrays.asList(this.m1Sources.toFile().getAbsolutePath()));
95          Mockito.lenient().when(this.m2.getCompileSourceRoots())
96                  .thenReturn(Arrays.asList(this.m2Sources.toFile().getAbsolutePath()));
97      }
98  
99      /**
100      * Test create source loader.
101      */
102     @Test
103     void createSourceLoader() {
104         final var sourceLoader = this.createSourceLoaderFactory(StandardCharsets.UTF_8).createSourceLoader();
105         Assertions.assertNotNull(sourceLoader);
106     }
107 
108     /**
109      * Creates the source loader with additional source directories.
110      *
111      * @throws IOException
112      *             Signals that an I/O exception has occurred.
113      */
114     @Test
115     void createSourceLoaderWithAdditionalSourceDirectories() throws IOException {
116         final var s1 = Files.createDirectory(this.folder.resolve("s1"));
117         final var s2 = Files.createDirectory(this.folder.resolve("s2"));
118         final var sourceLoader = this.createSourceLoaderFactory(StandardCharsets.UTF_8)
119                 .withSourceDirectories(Arrays.asList(s1.toFile(), s2.toFile())).createSourceLoader();
120         Assertions.assertNotNull(sourceLoader);
121     }
122 
123     /**
124      * Creates the source loader with scan for sources.
125      */
126     @Test
127     void createSourceLoaderWithScanForSources() {
128         final var sourceLoader = this.createSourceLoaderFactory(StandardCharsets.UTF_8).withScanForSources(true)
129                 .createSourceLoader();
130         Assertions.assertNotNull(sourceLoader);
131     }
132 
133     /**
134      * Creates the source loader invalid directory.
135      *
136      * @throws IOException
137      *             Signals that an I/O exception has occurred.
138      */
139     @Test
140     void createSourceLoaderInvalidDirectory() throws IOException {
141         final var file = Files.createDirectory(this.folder.resolve("aFile")).toFile();
142         final var sourceLoader = this.createSourceLoaderFactory(StandardCharsets.UTF_8)
143                 .withSourceDirectories(Arrays.asList(file)).withScanForSources(true).createSourceLoader();
144         Assertions.assertNotNull(sourceLoader);
145     }
146 
147     /**
148      * Creates the source loader factory.
149      *
150      * @param sourceEncoding
151      *            the source encoding
152      *
153      * @return the source loader factory
154      */
155     private SourceLoaderFactory createSourceLoaderFactory(Charset sourceEncoding) {
156         return new SourceLoaderFactory(this.folder.toFile(), this.root, sourceEncoding);
157     }
158 
159 }