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