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.qulice.checkstyle.CheckstyleValidator;
8   import com.qulice.pmd.PmdValidator;
9   import java.io.File;
10  import java.nio.charset.StandardCharsets;
11  import java.nio.file.Files;
12  import java.nio.file.Path;
13  import java.util.Arrays;
14  import java.util.Collections;
15  import java.util.List;
16  import org.apache.commons.io.FileUtils;
17  import org.apache.maven.model.Build;
18  import org.apache.maven.project.MavenProject;
19  import org.cactoos.io.ResourceOf;
20  import org.cactoos.text.TextOf;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.io.TempDir;
24  
25  /**
26   * Test case for {@link DefaultMavenEnvironment} class methods that
27   * exclude files from validation.
28   * @since 0.19
29   */
30  final class ValidationExclusionTest {
31  
32      /**
33       * Temporary directory for the project subfolder to be excluded.
34       */
35      private static final String TEMP_SUB = "excl";
36  
37      /**
38       * Java files extension.
39       */
40      private static final String JAVA_EXT = ".java";
41  
42      /**
43       * DefaultMavenEnvironment can exclude a path from PMD validation.
44       * @param dir Temporary directory
45       * @throws Exception If something wrong happens inside
46       */
47      @Test
48      void excludePathFromPmdValidation(@TempDir final Path dir) throws Exception {
49          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
50          final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
51          final MavenProject project = new MavenProject();
52          project.setFile(subdir.toFile());
53          env.setProject(project);
54          env.setExcludes(
55              Collections.singletonList(
56                  String.format("pmd:/%s/.*", subdir.getFileName())
57              )
58          );
59          Assertions.assertTrue(
60              new PmdValidator(env).getNonExcludedFiles(
61                  Collections.singletonList(
62                      ValidationExclusionTest.java(
63                          subdir,
64                          "com/qulice/maven/ValidationExclusion/PmdExample.txt"
65                      )
66                  )
67              ).isEmpty(),
68              "We expect no files to be returned for PMD validation"
69          );
70      }
71  
72      /**
73       * DefaultMavenEnvironment can exclude a path from Checkstyle validation.
74       * @param dir Temporary directory
75       * @throws Exception If something wrong happens inside
76       */
77      @Test
78      void excludePathFromCheckstyleValidation(@TempDir final Path dir) throws Exception {
79          final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
80          final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
81          final Build build = new Build();
82          build.setOutputDirectory(dir.toString());
83          final MavenProject project = new MavenProject();
84          project.setFile(subdir.toFile());
85          project.setBuild(build);
86          env.setProject(project);
87          env.setExcludes(
88              Collections.singletonList(
89                  String.format("checkstyle:/%s/.*", subdir.getFileName())
90              )
91          );
92          Assertions.assertTrue(
93              new CheckstyleValidator(env).getNonExcludedFiles(
94                  Collections.singletonList(
95                      ValidationExclusionTest.java(
96                          subdir,
97                          "com/qulice/maven/ValidationExclusion/CheckstyleExample.txt"
98                      )
99                  )
100             ).isEmpty(),
101             "We expect no files to be returned for Checkstyle validation"
102         );
103     }
104 
105     /**
106      * DefaultMavenEnvironment can exclude a path from entire validation.
107      * @param dir Temporary directory
108      * @throws Exception If something wrong happens inside
109      * @todo #1457:90min Add global exclusion support to qulice.
110      *  Currently, DefaultMavenEnvironment and validators support only
111      *  tool-specific exclusions. Once global exclusion support is added,
112      *  the {@link #excludePathFromEntireValidation}
113      *  should be enabled and verified to work as expected.
114      *  The original task comes from:
115      *  <a href="https://github.com/yegor256/qulice/issues/1457">#1457"</a>
116      */
117     @Test
118     void excludePathFromEntireValidation(@TempDir final Path dir) throws Exception {
119         final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
120         final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
121         final Build build = new Build();
122         build.setOutputDirectory(dir.toString());
123         final MavenProject project = new MavenProject();
124         project.setFile(subdir.toFile());
125         project.setBuild(build);
126         env.setProject(project);
127         env.setExcludes(
128             Collections.singletonList(
129                 String.format("*:/%s/.*", subdir.getFileName())
130             )
131         );
132         final List<File> included = Arrays.asList(
133             ValidationExclusionTest.java(
134                 subdir, "com/qulice/maven/ValidationExclusion/CheckstyleExample.txt"
135             ),
136             ValidationExclusionTest.java(
137                 subdir, "com/qulice/maven/ValidationExclusion/PmdExample.txt"
138             )
139         );
140         final int total = new PmdValidator(env).getNonExcludedFiles(included).size()
141             + new CheckstyleValidator(env).getNonExcludedFiles(included).size();
142         Assertions.assertEquals(
143             0,
144             total,
145             String.format(
146                 "We expect no files to be returned for entire validation, but found %d files",
147                 total
148             )
149         );
150     }
151 
152     /**
153      * Create a temporary Java file from resource.
154      * @param dir Directory to create the file in
155      * @param resource Resource to read the content from
156      * @return Created file with content from resource
157      * @throws Exception If something goes wrong
158      */
159     private static File java(final Path dir, final String resource) throws Exception {
160         final File file = File.createTempFile(
161             "Test", ValidationExclusionTest.JAVA_EXT, dir.toFile()
162         );
163         FileUtils.writeStringToFile(
164             file,
165             new TextOf(
166                 new ResourceOf(resource)
167             ).asString(),
168             StandardCharsets.UTF_8
169         );
170         return file;
171     }
172 }