|
| 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 | +} |
0 commit comments