Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<property name="message" value="Two consecutive empty lines"/>
</module>
<!--
Checks that the file is not binary
-->
<module name="RegexpMultiline">
<property name="format" value="[^\x00-\x7F]+"/>
<property name="fileExtensions" value="java"/>
<property name="id" value="BinaryContents"/>
<property name="message" value="Binary contents are not allowed"/>
</module>
<!--
JavaDoc regexp checks
-->
<module name="RegexpSingleline">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 java.util.Collection;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Test case for file metainfo validation.
* @since 0.3
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class CheckstyleMetainfoTest extends CheckstyleTestBase {

/**
* CheckstyleValidator can deny binary contents in java file.
* This is test for #1264.
* @throws Exception If something wrong happens inside
*/
@Test
void rejectsJavaFileWithBinaryContent() throws Exception {
final String message =
"Binary contents are not allowed";
final String file = "JavaFileWithBinaryContent.java";
final String name = "RegexpMultilineCheck";
final Collection<Violation> results = runValidation(file, false);
MatcherAssert.assertThat(
"Two violations with binary contents shoud be found",
results,
Matchers.hasItems(
new ViolationMatcher(
message, file, "13", name
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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;
import org.junit.jupiter.api.BeforeEach;

/**
* Base class for checkstyle tests.
* @since 0.3
*/
public class CheckstyleTestBase {

/**
* Name of property to set to change location of the license.
*/
public static final String LICENSE_PROP = "license";

/**
* Directory with classes.
*/
public static final String DIRECTORY = "src/main/java/foo";

/**
* License text.
*/
public static final String LICENSE = "Hello.";

/**
* License rule.
*/
private License rule;

/**
* Method to recet license rule before each test in inherited test classes.
*/
@BeforeEach
public void setRule() {
this.rule = new License();
}

/**
* 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 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")
public Collection<Violation> 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(), 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<Violation> results =
new CheckstyleValidator(env).validate(
env.files(file)
);
if (passes) {
MatcherAssert.assertThat(
results,
Matchers.<Violation>empty()
);
} else {
MatcherAssert.assertThat(
results,
Matchers.not(Matchers.<Violation>empty())
);
}
return results;
}

/**
* Method to access the rule from inherited classes.
* @return License rule
*/
protected License getRule() {
return this.rule;
}

/**
* Convert file name to URL.
* @param file The file
* @return The URL
*/
protected static String toUrl(final File file) {
return String.format("file:%s", file);
}

}
Loading