1 /* 2 * Copyright (c) 2011-2025 Yegor Bugayenko 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 1) Redistributions of source code must retain the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer. 2) Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 3) Neither the name of the Qulice.com nor 14 * the names of its contributors may be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 package com.qulice.pmd; 32 33 import com.qulice.spi.Environment; 34 import com.qulice.spi.Violation; 35 import java.io.File; 36 import java.util.Collection; 37 import java.util.Collections; 38 import org.cactoos.text.TextOf; 39 import org.hamcrest.Matcher; 40 import org.hamcrest.MatcherAssert; 41 42 /** 43 * PMD Validator assertions. 44 * @since 0.16 45 */ 46 final class PmdAssert { 47 /** 48 * File to validate. 49 */ 50 private final String file; 51 52 /** 53 * Expected build status, true means success. 54 */ 55 private final Matcher<Boolean> result; 56 57 /** 58 * Matcher that needs to match. 59 */ 60 private final Matcher<String> matcher; 61 62 /** 63 * Constructor. 64 * @param file File to validate. 65 * @param result Expected build status. 66 * @param matcher Matcher that needs to match. 67 */ 68 PmdAssert(final String file, final Matcher<Boolean> result, 69 final Matcher<String> matcher) { 70 this.file = file; 71 this.result = result; 72 this.matcher = matcher; 73 } 74 75 /** 76 * Validates given file against PMD. 77 * @throws Exception In case of error. 78 */ 79 public void validate() throws Exception { 80 final Environment.Mock mock = new Environment.Mock(); 81 final String name = String.format("src/main/java/foo/%s", this.file); 82 final Environment env = mock.withFile( 83 name, 84 new TextOf( 85 this.getClass().getResourceAsStream(this.file) 86 ).asString() 87 ); 88 final Collection<Violation> violations = new PmdValidator(env).validate( 89 Collections.singletonList(new File(env.basedir(), name)) 90 ); 91 MatcherAssert.assertThat(violations.isEmpty(), this.result); 92 final StringBuilder builder = new StringBuilder(); 93 for (final Violation violation : violations) { 94 builder.append( 95 String.format( 96 "PMD: %s[%s]: %s (%s)\n", 97 this.file, 98 violation.lines(), 99 violation.message(), 100 violation.name() 101 ) 102 ); 103 } 104 MatcherAssert.assertThat(builder.toString(), this.matcher); 105 } 106 }