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.qulice.checkstyle.CheckstyleValidator;
34  import com.qulice.pmd.PmdValidator;
35  import java.io.File;
36  import java.nio.charset.StandardCharsets;
37  import java.nio.file.Files;
38  import java.nio.file.Path;
39  import java.util.Collection;
40  import java.util.Collections;
41  import java.util.List;
42  import net.sourceforge.pmd.util.datasource.DataSource;
43  import org.apache.commons.io.FileUtils;
44  import org.apache.maven.model.Build;
45  import org.apache.maven.project.MavenProject;
46  import org.cactoos.io.ResourceOf;
47  import org.cactoos.text.TextOf;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  import org.mockito.Mockito;
51  
52  /**
53   * Test case for {@link DefaultMavenEnvironment} class methods that
54   * exclude files from validation.
55   * @since 0.19
56   */
57  final class ValidationExclusionTest {
58      /**
59       * Temporary directory for the project source folder.
60       */
61      private static final String TEMP_DIR = "src";
62  
63      /**
64       * Temporary directory for the project subfolder to be excluded.
65       */
66      private static final String TEMP_SUB = "excl";
67  
68      /**
69       * Java files extension.
70       */
71      private static final String JAVA_EXT = ".java";
72  
73      /**
74       * DefaultMavenEnvironment can exclude a path from PMD validation.
75       * @throws Exception If something wrong happens inside
76       */
77      @Test
78      void excludePathFromPmdValidation() throws Exception {
79          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
80          final MavenProject project = Mockito.mock(MavenProject.class);
81          final Path dir = Files.createTempDirectory(ValidationExclusionTest.TEMP_DIR);
82          final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
83          final File file = File.createTempFile(
84              "PmdExample", ValidationExclusionTest.JAVA_EXT,
85              subdir.toFile()
86          );
87          Mockito.when(project.getBasedir())
88              .thenReturn(
89                  dir.toFile()
90              );
91          env.setProject(project);
92          Assertions.assertNotNull(project.getBasedir());
93          final String source = new TextOf(
94              new ResourceOf("com/qulice/maven/ValidationExclusion/PmdExample.txt")
95          ).asString();
96          FileUtils.forceDeleteOnExit(file);
97          FileUtils.writeStringToFile(
98              file,
99              source,
100             StandardCharsets.UTF_8
101         );
102         env.setExcludes(
103             Collections.singletonList(
104                 String.format("pmd:/%s/.*", subdir.getFileName())
105             )
106         );
107         final PmdValidator validator = new PmdValidator(env);
108         final Collection<DataSource> files = validator.getNonExcludedFiles(
109             Collections.singletonList(file)
110         );
111         Assertions.assertTrue(files.isEmpty());
112     }
113 
114     /**
115      * DefaultMavenEnvironment can exclude a path from Checkstyle validation.
116      * @throws Exception If something wrong happens inside
117      */
118     @Test
119     void excludePathFromCheckstyleValidation() throws Exception {
120         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
121         final MavenProject project = Mockito.mock(MavenProject.class);
122         final Path dir = Files.createTempDirectory(ValidationExclusionTest.TEMP_DIR);
123         final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
124         final File file = File.createTempFile(
125             "CheckstyleExample", ValidationExclusionTest.JAVA_EXT,
126             subdir.toFile()
127         );
128         env.setProject(project);
129         Mockito.when(project.getBasedir()).thenReturn(dir.toFile());
130         final Build build = new Build();
131         build.setOutputDirectory(dir.toString());
132         Mockito.when(project.getBuild()).thenReturn(build);
133         Assertions.assertNotNull(project.getBasedir());
134         Assertions.assertNotNull(env.tempdir());
135         final String source = new TextOf(
136             new ResourceOf("com/qulice/maven/ValidationExclusion/CheckstyleExample.txt")
137         ).asString();
138         FileUtils.forceDeleteOnExit(file);
139         FileUtils.writeStringToFile(
140             file,
141             source,
142             StandardCharsets.UTF_8
143         );
144         env.setExcludes(
145             Collections.singletonList(
146                 String.format("checkstyle:/%s/.*", subdir.getFileName())
147             )
148         );
149         final CheckstyleValidator validator = new CheckstyleValidator(env);
150         final List<File> files = validator.getNonExcludedFiles(Collections.singletonList(file));
151         Assertions.assertTrue(files.isEmpty());
152     }
153 }