View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.google.common.collect.ImmutableList;
8   import java.nio.charset.StandardCharsets;
9   import java.util.Collections;
10  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link DefaultMavenEnvironment} class.
17   * @since 0.8
18   */
19  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
20  final class DefaultMavenEnvironmentTest {
21  
22      /**
23       * DefaultMavenEnvironment can produce list of excludes.
24       */
25      @Test
26      void excludeAllFiles() {
27          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
28          env.setExcludes(Collections.singletonList("codenarc:**/*.groovy"));
29          MatcherAssert.assertThat(
30              "Excludes should be returned",
31              env.excludes("codenarc"),
32              Matchers.contains("**/*.groovy")
33          );
34      }
35  
36      /**
37       * DefaultMavenEnvironment can produce list of excludes from empty source.
38       */
39      @Test
40      void emptyExclude() {
41          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
42          env.setExcludes(Collections.<String>emptyList());
43          MatcherAssert.assertThat(
44              "Empty list should be returned",
45              env.excludes("codenarc").iterator().hasNext(),
46              Matchers.is(false)
47          );
48      }
49  
50      /**
51       * DefaultMavenEnvironment can produce list of excludes without excludes.
52       */
53      @Test
54      void noExclude() {
55          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
56          MatcherAssert.assertThat(
57              "Excludes should be empty list by default",
58              env.excludes("codenarc").iterator().hasNext(),
59              Matchers.is(false)
60          );
61      }
62  
63      /**
64       * DefaultMavenEnvironment can produce list of excludes.
65       */
66      @Test
67      void excludeSomeFiles() {
68          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
69          env.setExcludes(
70              ImmutableList.<String>builder()
71                  .add("codenarc:**/src/ex1/Main.groovy")
72                  .add("codenarc:**/src/ex2/Main2.groovy")
73                  .build()
74          );
75          MatcherAssert.assertThat(
76              "Excludes should be returned as list",
77              env.excludes("codenarc"),
78              Matchers.containsInAnyOrder(
79                  "**/src/ex1/Main.groovy",
80                  "**/src/ex2/Main2.groovy"
81              )
82          );
83      }
84  
85      /**
86       * DefaultMavenEnvironment can work with whitespaces in classpath.
87       * @throws Exception If something wrong happens inside
88       */
89      @Test
90      void passPathsWithWhitespaces()  throws Exception {
91          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
92          final MavenProjectStub project = new MavenProjectStub();
93          project.setRuntimeClasspathElements(
94              Collections.singletonList("/Users/Carlos Miranda/git")
95          );
96          project.setDependencyArtifacts(Collections.emptySet());
97          env.setProject(project);
98          MatcherAssert.assertThat(
99              "ClassPath should be returned",
100             env.classloader(),
101             Matchers.notNullValue()
102         );
103     }
104 
105     /**
106      * DefaultMavenEnvironment can produce empty collection when no matches
107      * with checker.
108      */
109     @Test
110     void producesEmptyExcludesWhenNoMatches() {
111         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
112         env.setExcludes(
113             ImmutableList.of(
114                 "checkstyle:**/src/ex1/Main.groovy",
115                 "pmd:**/src/ex2/Main2.groovy"
116             )
117         );
118         MatcherAssert.assertThat(
119             "Exclude dependencies should be empty",
120             env.excludes("dependencies"),
121             Matchers.empty()
122         );
123     }
124 
125     /**
126      * Default source files encoding should be UFT-8.
127      */
128     @Test
129     void defaultEncodingIsUtf() {
130         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
131         MatcherAssert.assertThat(
132             "Default encoding should be UTF-8",
133             env.encoding(),
134             Matchers.is(StandardCharsets.UTF_8)
135         );
136     }
137 }