View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.google.common.collect.ImmutableList;
34  import java.nio.charset.StandardCharsets;
35  import java.util.Collections;
36  import org.apache.maven.project.MavenProject;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.jupiter.api.Test;
40  import org.mockito.Mockito;
41  
42  /**
43   * Test case for {@link DefaultMavenEnvironment} class.
44   * @since 0.8
45   */
46  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
47  final class DefaultMavenEnvironmentTest {
48  
49      /**
50       * DefaultMavenEnvironment can produce list of excludes.
51       */
52      @Test
53      void excludeAllFiles() {
54          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
55          env.setExcludes(Collections.singletonList("codenarc:**/*.groovy"));
56          MatcherAssert.assertThat(
57              "Excludes should be returned",
58              env.excludes("codenarc"),
59              Matchers.contains("**/*.groovy")
60          );
61      }
62  
63      /**
64       * DefaultMavenEnvironment can produce list of excludes from empty source.
65       */
66      @Test
67      void emptyExclude() {
68          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
69          env.setExcludes(Collections.<String>emptyList());
70          MatcherAssert.assertThat(
71              "Empty list should be returned",
72              env.excludes("codenarc").iterator().hasNext(),
73              Matchers.is(false)
74          );
75      }
76  
77      /**
78       * DefaultMavenEnvironment can produce list of excludes without excludes.
79       */
80      @Test
81      void noExclude() {
82          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
83          MatcherAssert.assertThat(
84              "Excludes should be empty list by default",
85              env.excludes("codenarc").iterator().hasNext(),
86              Matchers.is(false)
87          );
88      }
89  
90      /**
91       * DefaultMavenEnvironment can produce list of excludes.
92       */
93      @Test
94      void excludeSomeFiles() {
95          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
96          env.setExcludes(
97              ImmutableList.<String>builder()
98                  .add("codenarc:**/src/ex1/Main.groovy")
99                  .add("codenarc:**/src/ex2/Main2.groovy")
100                 .build()
101         );
102         MatcherAssert.assertThat(
103             "Excludes should be returned as list",
104             env.excludes("codenarc"),
105             Matchers.containsInAnyOrder(
106                 "**/src/ex1/Main.groovy",
107                 "**/src/ex2/Main2.groovy"
108             )
109         );
110     }
111 
112     /**
113      * DefaultMavenEnvironment can work with whitespaces in classpath.
114      * @throws Exception If something wrong happens inside
115      */
116     @Test
117     void passPathsWithWhitespaces()  throws Exception {
118         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
119         final MavenProject project = Mockito.mock(MavenProject.class);
120         Mockito.when(project.getRuntimeClasspathElements())
121             .thenReturn(
122                 Collections.singletonList("/Users/Carlos Miranda/git")
123             );
124         env.setProject(project);
125         MatcherAssert.assertThat(
126             "ClassPath should be returned",
127             env.classloader(),
128             Matchers.notNullValue()
129         );
130     }
131 
132     /**
133      * DefaultMavenEnvironment can produce empty collection when no matches
134      * with checker.
135      */
136     @Test
137     void producesEmptyExcludesWhenNoMatches() {
138         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
139         env.setExcludes(
140             ImmutableList.of(
141                 "checkstyle:**/src/ex1/Main.groovy",
142                 "pmd:**/src/ex2/Main2.groovy"
143             )
144         );
145         MatcherAssert.assertThat(
146             "Exclude dependencies should be empty",
147             env.excludes("dependencies"),
148             Matchers.empty()
149         );
150     }
151 
152     /**
153      * Default source files encoding should be UFT-8.
154      */
155     @Test
156     void defaultEncodingIsUtf() {
157         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
158         MatcherAssert.assertThat(
159             "Default encoding should be UTF-8",
160             env.encoding(),
161             Matchers.is(StandardCharsets.UTF_8)
162         );
163     }
164 }