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.util;
25  
26  import static org.junit.jupiter.api.Assertions.*;
27  import static org.mockito.Mockito.lenient;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.nio.file.Files;
32  import java.nio.file.Path;
33  import java.util.Arrays;
34  import java.util.Collections;
35  
36  import org.apache.maven.project.MavenProject;
37  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.Test;
40  import org.junit.jupiter.api.extension.ExtendWith;
41  import org.junit.jupiter.api.io.CleanupMode;
42  import org.junit.jupiter.api.io.TempDir;
43  import org.mockito.Mock;
44  import org.mockito.junit.jupiter.MockitoExtension;
45  
46  @ExtendWith(MockitoExtension.class)
47  class SourceLoaderFactoryTest {
48  
49      @TempDir(cleanup = CleanupMode.ON_SUCCESS)
50      public Path folder;
51  
52      @Mock
53      private MavenProject root;
54  
55      @Mock
56      private MavenProject m1;
57  
58      @Mock
59      private MavenProject m2;
60  
61      private Path rootSources;
62      private Path m1Sources;
63      private Path m2Sources;
64  
65      @BeforeEach
66      void init() throws IOException {
67          rootSources = Files.createDirectory(folder.resolve("src")); 
68          m1Sources = Files.createDirectory(rootSources.resolve("m1"));
69          m2Sources = Files.createDirectory(m1Sources.resolve("m2"));
70          lenient().when(root.getCollectedProjects()).thenReturn(Arrays.asList(m1, m2));
71          lenient().when(m1.getCollectedProjects()).thenReturn(Collections.<MavenProject>emptyList());
72          lenient().when(m2.getCollectedProjects()).thenReturn(Collections.<MavenProject>emptyList());
73          lenient().when(root.getCompileSourceRoots()).thenReturn(Arrays.asList(rootSources.toFile().getAbsolutePath()));
74          lenient().when(m1.getCompileSourceRoots()).thenReturn(Arrays.asList(m1Sources.toFile().getAbsolutePath()));
75          lenient().when(m2.getCompileSourceRoots()).thenReturn(Arrays.asList(m2Sources.toFile().getAbsolutePath()));
76      }
77  
78      @Test
79      void testCreateSourceLoader() {
80          SourceLoader sourceLoader = createSourceLoaderFactory("UTF-8").createSourceLoader();
81          assertNotNull(sourceLoader);
82      }
83  
84      @Test
85      void testCreateSourceLoaderWithAdditionalSourceDirectories() throws IOException {
86          Path s1 = Files.createDirectory(folder.resolve("s1"));
87          Path s2 = Files.createDirectory(folder.resolve("s2"));
88          SourceLoader sourceLoader = createSourceLoaderFactory("UTF-8")
89                  .withSourceDirectories(Arrays.asList(s1.toFile(), s2.toFile()))
90                  .createSourceLoader();
91          assertNotNull(sourceLoader);
92      }
93  
94      @Test
95      void testCreateSourceLoaderWithScanForSources() {
96          SourceLoader sourceLoader = createSourceLoaderFactory("UTF-8")
97                  .withScanForSources( true )
98                  .createSourceLoader();
99          assertNotNull(sourceLoader);
100     }
101 
102     @Test
103     void testCreateSourceLoaderInvalidDirectory() throws IOException {
104         File file = Files.createDirectory(folder.resolve("aFile")).toFile();
105         SourceLoader sourceLoader = createSourceLoaderFactory("UTF-8")
106                 .withSourceDirectories(Arrays.asList(file))
107                 .withScanForSources(true)
108                 .createSourceLoader();
109         assertNotNull(sourceLoader);
110     }
111 
112     private SourceLoaderFactory createSourceLoaderFactory(String sourceEncoding) {
113         return new SourceLoaderFactory(folder.toFile(), root, sourceEncoding);
114     }
115 }