1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
54
55
56
57 final class ValidationExclusionTest {
58
59
60
61 private static final String TEMP_DIR = "src";
62
63
64
65
66 private static final String TEMP_SUB = "excl";
67
68
69
70
71 private static final String JAVA_EXT = ".java";
72
73
74
75
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
116
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 }