Skip to content

Commit 75abdf5

Browse files
author
Polina Volkhontseva
committedApr 25, 2022
Added tests for files validation exclusion
1 parent fd964ae commit 75abdf5

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
 

‎qulice-maven-plugin/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
4040
<name>qulice-maven-plugin</name>
4141
<properties>
4242
<maven.version>3.0.5</maven.version>
43+
<pmd.version>6.10.0</pmd.version>
4344
</properties>
4445
<dependencies>
4546
<dependency>
@@ -224,6 +225,11 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
224225
<version>5.0.0.Final</version>
225226
<scope>runtime</scope>
226227
</dependency>
228+
<dependency>
229+
<groupId>net.sourceforge.pmd</groupId>
230+
<artifactId>pmd-core</artifactId>
231+
<version>${pmd.version}</version>
232+
</dependency>
227233
</dependencies>
228234
<build>
229235
<plugins>
@@ -343,6 +349,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
343349
<exclude>findbugs:com.qulice.maven.HelpMojo</exclude>
344350
<exclude>checkstyle:/src/it/.*</exclude>
345351
<exclude>pmd:.*/src/it/.*</exclude>
352+
<exclude>pmd:.*/src/test/resources/com/qulice/maven/ValidationExclusion/.*</exclude>
346353
<exclude>xml:/src/it/.*</exclude>
347354
<exclude>duplicatefinder:.*</exclude>
348355
</excludes>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright (c) 2011-2022 Qulice.com
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met: 1) Redistributions of source code must retain the above
8+
* copyright notice, this list of conditions and the following
9+
* disclaimer. 2) Redistributions in binary form must reproduce the above
10+
* copyright notice, this list of conditions and the following
11+
* disclaimer in the documentation and/or other materials provided
12+
* with the distribution. 3) Neither the name of the Qulice.com nor
13+
* the names of its contributors may be used to endorse or promote
14+
* products derived from this software without specific prior written
15+
* permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19+
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
package com.qulice.maven;
31+
32+
import com.qulice.checkstyle.CheckstyleValidator;
33+
import com.qulice.pmd.PmdValidator;
34+
import java.io.File;
35+
import java.nio.charset.StandardCharsets;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.util.Collection;
39+
import java.util.Collections;
40+
import java.util.List;
41+
import net.sourceforge.pmd.util.datasource.DataSource;
42+
import org.apache.commons.io.FileUtils;
43+
import org.apache.maven.model.Build;
44+
import org.apache.maven.project.MavenProject;
45+
import org.cactoos.io.ResourceOf;
46+
import org.cactoos.text.TextOf;
47+
import org.junit.jupiter.api.Assertions;
48+
import org.junit.jupiter.api.Test;
49+
import org.mockito.Mockito;
50+
51+
/**
52+
* Test case for {@link DefaultMavenEnvironment} class methods that
53+
* exclude files from validation.
54+
* @since 0.19
55+
*/
56+
public class ValidationExclusionTest {
57+
/**
58+
* Temporary directory for the project source folder.
59+
*/
60+
private static final String TEMP_DIR = "src";
61+
62+
/**
63+
* Temporary directory for the project subfolder to be excluded.
64+
*/
65+
private static final String TEMP_SUB = "excl";
66+
67+
/**
68+
* Java files extension.
69+
*/
70+
private static final String JAVA_EXT = ".java";
71+
72+
/**
73+
* DefaultMavenEnvironment can exclude a path from PMD validation.
74+
* @throws Exception If something wrong happens inside
75+
*/
76+
@Test
77+
public final void excludePathFromPmdValidation() throws Exception {
78+
final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
79+
final MavenProject project = Mockito.mock(MavenProject.class);
80+
final Path dir = Files.createTempDirectory(ValidationExclusionTest.TEMP_DIR);
81+
final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
82+
final File file = File.createTempFile(
83+
"PmdExample", ValidationExclusionTest.JAVA_EXT,
84+
subdir.toFile()
85+
);
86+
Mockito.when(project.getBasedir())
87+
.thenReturn(
88+
dir.toFile()
89+
);
90+
env.setProject(project);
91+
Assertions.assertNotNull(project.getBasedir());
92+
final String source = new TextOf(
93+
new ResourceOf("com/qulice/maven/ValidationExclusion/PmdExample.txt")
94+
).asString();
95+
FileUtils.forceDeleteOnExit(file);
96+
FileUtils.writeStringToFile(
97+
file,
98+
source,
99+
StandardCharsets.UTF_8
100+
);
101+
env.setExcludes(
102+
Collections.singletonList(
103+
String.format("pmd:/%s/.*", subdir.getFileName())
104+
)
105+
);
106+
final PmdValidator validator = new PmdValidator(env);
107+
final Collection<DataSource> files = validator.getNonExcludedFiles(
108+
Collections.singletonList(file)
109+
);
110+
Assertions.assertTrue(files.isEmpty());
111+
}
112+
113+
/**
114+
* DefaultMavenEnvironment can exclude a path from Checkstyle validation.
115+
* @throws Exception If something wrong happens inside
116+
*/
117+
@Test
118+
public final void excludePathFromCheckstyleValidation() throws Exception {
119+
final DefaultMavenEnvironment env = new DefaultMavenEnvironment();
120+
final MavenProject project = Mockito.mock(MavenProject.class);
121+
final Path dir = Files.createTempDirectory(ValidationExclusionTest.TEMP_DIR);
122+
final Path subdir = Files.createTempDirectory(dir, ValidationExclusionTest.TEMP_SUB);
123+
final File file = File.createTempFile(
124+
"CheckstyleExample", ValidationExclusionTest.JAVA_EXT,
125+
subdir.toFile()
126+
);
127+
env.setProject(project);
128+
Mockito.when(project.getBasedir()).thenReturn(dir.toFile());
129+
final Build build = new Build();
130+
build.setOutputDirectory(dir.toString());
131+
Mockito.when(project.getBuild()).thenReturn(build);
132+
Assertions.assertNotNull(project.getBasedir());
133+
Assertions.assertNotNull(env.tempdir());
134+
final String source = new TextOf(
135+
new ResourceOf("com/qulice/maven/ValidationExclusion/CheckstyleExample.txt")
136+
).asString();
137+
FileUtils.forceDeleteOnExit(file);
138+
FileUtils.writeStringToFile(
139+
file,
140+
source,
141+
StandardCharsets.UTF_8
142+
);
143+
env.setExcludes(
144+
Collections.singletonList(
145+
String.format("checkstyle:/%s/.*", subdir.getFileName())
146+
)
147+
);
148+
final CheckstyleValidator validator = new CheckstyleValidator(env);
149+
final List<File> files = validator.getNonExcludedFiles(Collections.singletonList(file));
150+
Assertions.assertTrue(files.isEmpty());
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Hello.
3+
*/
4+
package foo;
5+
6+
/**
7+
* Simple.
8+
* @since 1.0
9+
*/
10+
public class MissingJavadoc {
11+
public String testSomething() {
12+
return "something";
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo;
2+
3+
public final class LocalVariableCouldBeFinal {
4+
5+
public int method() {
6+
int nonfinal = 0;
7+
return nonfinal;
8+
}
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.