diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleTestBase.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleTestBase.java new file mode 100644 index 000000000..3580e358d --- /dev/null +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleTestBase.java @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2011-2024 Qulice.com + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the Qulice.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.qulice.checkstyle; + +import com.qulice.spi.Environment; +import com.qulice.spi.Violation; +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import org.cactoos.io.ResourceOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.IoCheckedText; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; + +/** + * Base class for checkstyle tests. + * @since 0.3 + */ +public final class CheckstyleTestBase { + + /** + * Directory with classes. + */ + public static final String DIRECTORY = "src/main/java/foo"; + + /** + * Name of property to set to change location of the license. + */ + public static final String LICENSE_PROP = "license"; + + /** + * License text. + */ + public static final String LICENSE = "Hello."; + + /** + * License rule. + */ + private static License rule; + + /** + * Empty constructor for utility class. + */ + private CheckstyleTestBase() { + } + + /** + * Validates that checkstyle reported given violation. + * @param file File to check. + * @param result Expected validation result. + * @param message Message to match + * @throws Exception In case of error + */ + @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") + public static void validate(final String file, final boolean result, + final String message) throws Exception { + MatcherAssert.assertThat( + CheckstyleTestBase.runValidation(file, result), + Matchers.hasItem( + new ViolationMatcher( + message, file + ) + ) + ); + } + + /** + * Returns string with Checkstyle validation results. + * @param file File to check. + * @param passes Whether validation is expected to pass. + * @return String containing validation results in textual form. + * @throws IOException In case of error + */ + @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") + public static Collection runValidation(final String file, + final boolean passes) throws IOException { + final Environment.Mock mock = new Environment.Mock(); + final File license = CheckstyleTestBase.rule.savePackageInfo( + new File(mock.basedir(), CheckstyleTestBase.DIRECTORY) + ).withLines(CheckstyleTestBase.LICENSE) + .withEol("\n").file(); + final Environment env = mock.withParam( + CheckstyleTestBase.LICENSE_PROP, + CheckstyleTestBase.toUrl(license) + ) + .withFile( + String.format("src/main/java/foo/%s", file), + new IoCheckedText( + new TextOf( + new ResourceOf( + new FormattedText("com/qulice/checkstyle/%s", file) + ) + ) + ).asString() + ); + final Collection results = + new CheckstyleValidator(env).validate( + env.files(file) + ); + if (passes) { + MatcherAssert.assertThat( + results, + Matchers.empty() + ); + } else { + MatcherAssert.assertThat( + results, + Matchers.not(Matchers.empty()) + ); + } + return results; + } + + /** + * Method to reset license rule before each test in inherited test classes. + */ + static void setRule() { + CheckstyleTestBase.rule = new License(); + } + + /** + * Method to access the rule from inherited classes. + * @return License rule + */ + static License getRule() { + return CheckstyleTestBase.rule; + } + + /** + * Convert file name to URL. + * @param file The file + * @return The URL + */ + static String toUrl(final File file) { + return String.format("file:%s", file); + } + +} diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java index 560218d22..46685df46 100644 --- a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/CheckstyleValidatorTest.java @@ -34,18 +34,11 @@ import com.qulice.spi.Environment; import com.qulice.spi.Violation; import java.io.File; -import java.io.IOException; import java.util.Collection; -import org.cactoos.io.ResourceOf; import org.cactoos.list.ListOf; -import org.cactoos.text.FormattedText; -import org.cactoos.text.IoCheckedText; import org.cactoos.text.Joined; -import org.cactoos.text.TextOf; -import org.hamcrest.Description; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.hamcrest.TypeSafeMatcher; import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -67,29 +60,9 @@ ) final class CheckstyleValidatorTest { - /** - * Name of property to set to change location of the license. - */ - private static final String LICENSE_PROP = "license"; - - /** - * Directory with classes. - */ - private static final String DIRECTORY = "src/main/java/foo"; - - /** - * License text. - */ - private static final String LICENSE = "Hello."; - - /** - * Rule for testing. - */ - private License rule; - @BeforeEach - public void setRule() { - this.rule = new License(); + public void updateRule() { + CheckstyleTestBase.setRule(); } /** @@ -99,8 +72,8 @@ public void setRule() { @Test void catchesCheckstyleViolationsInLicense() throws Exception { final Environment.Mock mock = new Environment.Mock(); - final File license = this.rule.savePackageInfo( - new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY) + final File license = CheckstyleTestBase.getRule().savePackageInfo( + new File(mock.basedir(), CheckstyleTestBase.DIRECTORY) ).withLines("License-1.", "", "License-2.") .withEol("\n") .file(); @@ -111,8 +84,8 @@ void catchesCheckstyleViolationsInLicense() throws Exception { + "public class Foo { }\n"; final String name = "Foo.java"; final Environment env = mock.withParam( - CheckstyleValidatorTest.LICENSE_PROP, - this.toUrl(license) + CheckstyleTestBase.LICENSE_PROP, + CheckstyleTestBase.toUrl(license) ).withFile(String.format("src/main/java/foo/%s", name), content); final Collection results = new CheckstyleValidator(env) @@ -134,7 +107,7 @@ void catchesCheckstyleViolationsInLicense() throws Exception { */ @Test void acceptsInstanceMethodReferences() throws Exception { - this.runValidation( + CheckstyleTestBase.runValidation( "InstanceMethodRef.java", true ); } @@ -147,7 +120,7 @@ void acceptsInstanceMethodReferences() throws Exception { @Test void reportsErrorWhenParameterObjectIsNotDocumented() throws Exception { - this.validate( + CheckstyleTestBase.validate( "ParametrizedClass.java", false, "Type Javadoc comment is missing @param tag." ); @@ -161,7 +134,7 @@ void reportsErrorWhenParameterObjectIsNotDocumented() @Test void reportsErrorWhenLineWrap() throws Exception { - this.validate( + CheckstyleTestBase.validate( "LineWrapPackage.java", false, "should not be line-wrapped" ); @@ -174,7 +147,7 @@ void reportsErrorWhenLineWrap() */ @Test void reportsErrorWhenIndentationIsIncorrect() throws Exception { - this.validate( + CheckstyleTestBase.validate( "InvalidIndentation.java", false, "Indentation (14) must be same or less than" @@ -188,7 +161,7 @@ void reportsErrorWhenIndentationIsIncorrect() throws Exception { */ @Test void doesNotReportErrorWhenMissingJavadocInTests() throws Exception { - this.runValidation("MissingJavadocTest.java", true); + CheckstyleTestBase.runValidation("MissingJavadocTest.java", true); } /** @@ -200,7 +173,7 @@ void doesNotReportErrorWhenMissingJavadocInTests() throws Exception { @SuppressWarnings("unchecked") void reportsErrorWhenCommentOrJavadocIsTooLong() throws Exception { final Collection results = - this.runValidation("TooLongLines.java", false); + CheckstyleTestBase.runValidation("TooLongLines.java", false); MatcherAssert.assertThat( "Two long lines should be found", results, @@ -226,7 +199,7 @@ void reportsAllCharEncodingUsages() throws Exception { final String message = "Use java.nio.charset.StandardCharsets instead"; final String file = "DoNotUseCharEncoding.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); final String name = "RegexpSinglelineCheck"; @@ -271,7 +244,7 @@ void reportsAllCharEncodingUsages() throws Exception { */ @Test void acceptsValidSingleLineComment() throws Exception { - this.runValidation( + CheckstyleTestBase.runValidation( "ValidSingleLineCommentCheck.java", true ); } @@ -283,7 +256,7 @@ void acceptsValidSingleLineComment() throws Exception { */ @Test void acceptsValidIndentation() throws Exception { - this.runValidation( + CheckstyleTestBase.runValidation( "ValidIndentation.java", true ); } @@ -295,7 +268,7 @@ void acceptsValidIndentation() throws Exception { */ @Test void reportsErrorOnMoreThanOneReturnStatement() throws Exception { - this.validate( + CheckstyleTestBase.validate( "ReturnCount.java", false, "Return count is 2 (max allowed for non-void methods/lambdas is 1)" ); @@ -307,7 +280,7 @@ void reportsErrorOnMoreThanOneReturnStatement() throws Exception { */ @Test void acceptsDefaultMethodsWithFinalModifiers() throws Exception { - this.runValidation( + CheckstyleTestBase.runValidation( "DefaultMethods.java", true ); } @@ -321,7 +294,7 @@ void acceptsDefaultMethodsWithFinalModifiers() throws Exception { */ @Test void acceptsConstantUsedInMethodAnnotation() throws Exception { - this.runValidation("AnnotationConstant.java", true); + CheckstyleTestBase.runValidation("AnnotationConstant.java", true); } /** @@ -333,7 +306,7 @@ void acceptsConstantUsedInMethodAnnotation() throws Exception { void acceptsConstructorParametersNamedJustLikeFields() throws Exception { final String file = "ConstructorParams.java"; - final Collection results = this.runValidation(file, false); + final Collection results = CheckstyleTestBase.runValidation(file, false); final String name = "HiddenFieldCheck"; MatcherAssert.assertThat( "Two hidden fields in ctor should be found", @@ -365,7 +338,7 @@ void acceptsConstructorParametersNamedJustLikeFields() @SuppressWarnings("unchecked") void allowsOnlyProperlyNamedLocalVariables() throws Exception { final String file = "LocalVariableNames.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); MatcherAssert.assertThat( @@ -429,7 +402,7 @@ void allowsOnlyProperlyNamedLocalVariables() throws Exception { @Test void allowsOnlyProperlyOrderedAtClauses() throws Exception { final String file = "AtClauseOrder.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); final String message = "tags have to appear in the order"; @@ -461,7 +434,7 @@ void allowsOnlyProperlyOrderedAtClauses() throws Exception { @Test void passesWindowsEndsOfLineWithoutException() throws Exception { final String file = "WindowsEol.java"; - final Collection results = this.runValidation(file, false); + final Collection results = CheckstyleTestBase.runValidation(file, false); MatcherAssert.assertThat( "violation should be reported correctly", results, @@ -484,7 +457,7 @@ void passesWindowsEndsOfLineWithoutException() throws Exception { @Test void testWindowsEndsOfLineWithLinuxSources() throws Exception { final String file = "WindowsEolLinux.java"; - final Collection results = this.runValidation(file, false); + final Collection results = CheckstyleTestBase.runValidation(file, false); MatcherAssert.assertThat( "violation should be reported correctly", results, @@ -505,7 +478,7 @@ void testWindowsEndsOfLineWithLinuxSources() throws Exception { */ @Test void allowsProperIndentationInAnnotations() throws Exception { - this.runValidation("AnnotationIndentation.java", true); + CheckstyleTestBase.runValidation("AnnotationIndentation.java", true); } /** @@ -515,7 +488,7 @@ void allowsProperIndentationInAnnotations() throws Exception { */ @Test void rejectsImproperIndentationInAnnotations() throws Exception { - this.runValidation("AnnotationIndentationNegative.java", false); + CheckstyleTestBase.runValidation("AnnotationIndentationNegative.java", false); } /** @@ -525,7 +498,7 @@ void rejectsImproperIndentationInAnnotations() throws Exception { */ @Test void testExtraSemicolonInTryWithResources() throws Exception { - this.validate( + CheckstyleTestBase.validate( "ExtraSemicolon.java", false, "Extra semicolon in the end of try-with-resources head." ); @@ -537,7 +510,7 @@ void testExtraSemicolonInTryWithResources() throws Exception { */ @Test void testSupportsRecordTypes() throws Exception { - this.runValidation("ValidRecord.java", true); + CheckstyleTestBase.runValidation("ValidRecord.java", true); } /** @@ -547,7 +520,7 @@ void testSupportsRecordTypes() throws Exception { */ @Test void acceptsTryWithResourcesWithoutSemicolon() throws Exception { - this.runValidation("ValidSemicolon.java", true); + CheckstyleTestBase.runValidation("ValidSemicolon.java", true); } /** @@ -557,7 +530,7 @@ void acceptsTryWithResourcesWithoutSemicolon() throws Exception { */ @Test void acceptsNonStaticMethodsInIt() throws Exception { - this.runValidation("ValidIT.java", true); + CheckstyleTestBase.runValidation("ValidIT.java", true); } /** @@ -567,7 +540,7 @@ void acceptsNonStaticMethodsInIt() throws Exception { */ @Test void acceptsNonStaticMethodsInItCases() throws Exception { - this.runValidation("ValidITCase.java", true); + CheckstyleTestBase.runValidation("ValidITCase.java", true); } /** @@ -579,8 +552,8 @@ void acceptsNonStaticMethodsInItCases() throws Exception { @Test void doesNotThrowExceptionIfImportsOnly() throws Exception { final Environment.Mock mock = new Environment.Mock(); - final File license = this.rule.savePackageInfo( - new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY) + final File license = CheckstyleTestBase.getRule().savePackageInfo( + new File(mock.basedir(), CheckstyleTestBase.DIRECTORY) ).withLines("License-1.", "", "License-2.") .withEol("\n") .file(); @@ -592,8 +565,8 @@ void doesNotThrowExceptionIfImportsOnly() throws Exception { ); final String name = "Foo.java"; final Environment env = mock.withParam( - CheckstyleValidatorTest.LICENSE_PROP, - this.toUrl(license) + CheckstyleTestBase.LICENSE_PROP, + CheckstyleTestBase.toUrl(license) ).withFile(String.format("src/main/java/foo/%s", name), content); final Collection results = new CheckstyleValidator(env).validate(env.files(name)); @@ -613,7 +586,7 @@ void doesNotThrowExceptionIfImportsOnly() throws Exception { @SuppressWarnings({"unchecked", "PMD.AvoidDuplicateLiterals"}) void distinguishesValidCatchParameterNames() throws Exception { final String file = "CatchParameterNames.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); MatcherAssert.assertThat( @@ -645,7 +618,7 @@ void distinguishesValidCatchParameterNames() throws Exception { */ @Test void doesNotRejectUrlsInLongLines() throws Exception { - this.runValidation("UrlInLongLine.java", true); + CheckstyleTestBase.runValidation("UrlInLongLine.java", true); } /** @@ -656,7 +629,7 @@ void doesNotRejectUrlsInLongLines() throws Exception { @Test void allowsSpacesBetweenMethodsOfAnonymousClasses() throws Exception { - this.runValidation("BlankLinesOutsideMethodsPass.java", true); + CheckstyleTestBase.runValidation("BlankLinesOutsideMethodsPass.java", true); } /** @@ -668,7 +641,7 @@ void allowsSpacesBetweenMethodsOfAnonymousClasses() @SuppressWarnings({"unchecked", "PMD.AvoidDuplicateLiterals"}) void rejectsSpacesInsideMethods() throws Exception { final String file = "BlankLinesInsideMethodsFail.java"; - final Collection result = this.runValidation( + final Collection result = CheckstyleTestBase.runValidation( file, false ); final String name = "EmptyLinesCheck"; @@ -703,7 +676,7 @@ void rejectsSpacesInsideMethods() throws Exception { @SuppressWarnings("unchecked") void rejectsUppercaseAbbreviations() throws Exception { final String file = "InvalidAbbreviationAsWordInNameXML.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); final String name = "AbbreviationAsWordInNameCheck"; @@ -733,7 +706,7 @@ void rejectsUppercaseAbbreviations() throws Exception { @Test void rejectsHiddenParameters() throws Exception { final String file = "HiddenParameter.java"; - final Collection results = this.runValidation( + final Collection results = CheckstyleTestBase.runValidation( file, false ); final String name = "HiddenFieldCheck"; @@ -756,7 +729,7 @@ void rejectsHiddenParameters() throws Exception { */ @Test void allowsITUppercaseAbbreviation() throws Exception { - this.runValidation("ValidAbbreviationAsWordInNameIT.java", true); + CheckstyleTestBase.runValidation("ValidAbbreviationAsWordInNameIT.java", true); } /** @@ -767,7 +740,7 @@ void allowsITUppercaseAbbreviation() throws Exception { */ @Test void allowsUppercaseAbbreviationExceptions() throws Exception { - this.runValidation("ValidAbbreviationAsWordInName.java", true); + CheckstyleTestBase.runValidation("ValidAbbreviationAsWordInName.java", true); } /** @@ -779,7 +752,7 @@ void allowsUppercaseAbbreviationExceptions() throws Exception { @Disabled @Test void checkLambdaAndGenericsAtEndOfLine() throws Exception { - this.runValidation("ValidLambdaAndGenericsAtEndOfLine.java", true); + CheckstyleTestBase.runValidation("ValidLambdaAndGenericsAtEndOfLine.java", true); } /** @@ -793,7 +766,7 @@ void rejectsNonDiamondOperatorUsage() throws Exception { final String message = "Use diamond operator"; MatcherAssert.assertThat( "Two diamond violations should be found", - this.runValidation(file, false), + CheckstyleTestBase.runValidation(file, false), Matchers.hasItems( new ViolationMatcher(message, file, "19", name), new ViolationMatcher(message, file, "29", name) @@ -807,7 +780,7 @@ void rejectsNonDiamondOperatorUsage() throws Exception { */ @Test void allowsDiamondOperatorUsage() throws Exception { - this.runValidation("ValidDiamondsUsage.java", true); + CheckstyleTestBase.runValidation("ValidDiamondsUsage.java", true); } /** @@ -817,7 +790,7 @@ void allowsDiamondOperatorUsage() throws Exception { */ @Test void allowsFullGenericOperatorUsage() throws Exception { - this.runValidation("DiamondUsageNotNeeded.java", true); + CheckstyleTestBase.runValidation("DiamondUsageNotNeeded.java", true); } /** @@ -829,172 +802,6 @@ void allowsFullGenericOperatorUsage() throws Exception { @Test void allowsStringLiteralsOnBothSideInComparisons() throws Exception { - this.runValidation("ValidLiteralComparisonCheck.java", true); - } - - /** - * Convert file name to URL. - * @param file The file - * @return The URL - */ - private String toUrl(final File file) { - return String.format("file:%s", file); + CheckstyleTestBase.runValidation("ValidLiteralComparisonCheck.java", true); } - - /** - * Validates that checkstyle reported given violation. - * @param file File to check. - * @param result Expected validation result. - * @param message Message to match - * @throws Exception In case of error - */ - @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") - private void validate(final String file, final boolean result, - final String message) throws Exception { - MatcherAssert.assertThat( - this.runValidation(file, result), - Matchers.hasItem( - new ViolationMatcher( - message, file - ) - ) - ); - } - - /** - * Returns string with Checkstyle validation results. - * @param file File to check. - * @param passes Whether validation is expected to pass. - * @return String containing validation results in textual form. - * @throws IOException In case of error - */ - @SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage") - private Collection runValidation(final String file, - final boolean passes) throws IOException { - final Environment.Mock mock = new Environment.Mock(); - final File license = this.rule.savePackageInfo( - new File(mock.basedir(), CheckstyleValidatorTest.DIRECTORY) - ).withLines(CheckstyleValidatorTest.LICENSE) - .withEol("\n").file(); - final Environment env = mock.withParam( - CheckstyleValidatorTest.LICENSE_PROP, - this.toUrl(license) - ) - .withFile( - String.format("src/main/java/foo/%s", file), - new IoCheckedText( - new TextOf( - new ResourceOf( - new FormattedText("com/qulice/checkstyle/%s", file) - ) - ) - ).asString() - ); - final Collection results = - new CheckstyleValidator(env).validate( - env.files(file) - ); - if (passes) { - MatcherAssert.assertThat( - results, - Matchers.empty() - ); - } else { - MatcherAssert.assertThat( - results, - Matchers.not(Matchers.empty()) - ); - } - return results; - } - - /** - * Validation results matcher. - * - * @since 0.1 - */ - private static final class ViolationMatcher extends - TypeSafeMatcher { - - /** - * Message to check. - */ - private final String message; - - /** - * File to check. - */ - private final String file; - - /** - * Expected line. - */ - private final String line; - - /** - * Check name. - */ - private final String check; - - /** - * Constructor. - * @param message Message to check - * @param file File to check - * @param line Line to check - * @param check Check name - * @checkstyle ParameterNumber (3 lines) - */ - ViolationMatcher(final String message, final String file, - final String line, final String check) { - super(); - this.message = message; - this.file = file; - this.line = line; - this.check = check; - } - - /** - * Constructor. - * @param message Message to check - * @param file File to check - */ - ViolationMatcher(final String message, final String file) { - this(message, file, "", ""); - } - - @Override - public boolean matchesSafely(final Violation item) { - return item.message().contains(this.message) - && item.file().endsWith(this.file) - && this.lineMatches(item) - && this.checkMatches(item); - } - - @Override - public void describeTo(final Description description) { - description.appendText("doesn't match"); - } - - /** - * Check name matches. - * @param item Item to check. - * @return True if check name matches. - */ - private boolean checkMatches(final Violation item) { - return this.check.isEmpty() - || !this.check.isEmpty() && item.name().equals(this.check); - } - - /** - * Check that given line matches. - * @param item Item to check. - * @return True if line matches. - */ - private boolean lineMatches(final Violation item) { - return this.line.isEmpty() - || !this.line.isEmpty() && item.lines().equals(this.line); - } - - } - } diff --git a/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ViolationMatcher.java b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ViolationMatcher.java new file mode 100644 index 000000000..22c0e32c2 --- /dev/null +++ b/qulice-checkstyle/src/test/java/com/qulice/checkstyle/ViolationMatcher.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2011-2024 Qulice.com + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the Qulice.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.qulice.checkstyle; + +import com.qulice.spi.Violation; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * Validation results matcher. + * + * @since 0.1 + */ +final class ViolationMatcher extends + TypeSafeMatcher { + + /** + * Message to check. + */ + private final String message; + + /** + * File to check. + */ + private final String file; + + /** + * Expected line. + */ + private final String line; + + /** + * Check name. + */ + private final String check; + + /** + * Constructor. + * @param message Message to check + * @param file File to check + * @param line Line to check + * @param check Check name + * @checkstyle ParameterNumber (3 lines) + */ + ViolationMatcher(final String message, final String file, + final String line, final String check) { + super(); + this.message = message; + this.file = file; + this.line = line; + this.check = check; + } + + /** + * Constructor. + * @param message Message to check + * @param file File to check + */ + ViolationMatcher(final String message, final String file) { + this(message, file, "", ""); + } + + @Override + public boolean matchesSafely(final Violation item) { + return item.message().contains(this.message) + && item.file().endsWith(this.file) + && this.lineMatches(item) + && this.checkMatches(item); + } + + @Override + public void describeTo(final Description description) { + description.appendText("doesn't match"); + } + + /** + * Check name matches. + * @param item Item to check. + * @return True if check name matches. + */ + private boolean checkMatches(final Violation item) { + return this.check.isEmpty() + || !this.check.isEmpty() && item.name().equals(this.check); + } + + /** + * Check that given line matches. + * @param item Item to check. + * @return True if line matches. + */ + private boolean lineMatches(final Violation item) { + return this.line.isEmpty() + || !this.line.isEmpty() && item.lines().equals(this.line); + } + +}