Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring out a test base for scaling tests #1277

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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