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