Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ under the License.
<artifactId>commons-lang3</artifactId>
<version>3.19.0</version>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Abstract class to test reports generation.
Expand All @@ -51,8 +54,8 @@ public abstract class AbstractCheckstyleTestCase extends AbstractMojoTestCase {
*/
private MavenProject testMavenProject;

@Override
protected void setUp() throws Exception {
@BeforeEach
public void setUp() throws Exception {
// required for mojo lookups to work
super.setUp();

Expand Down Expand Up @@ -104,7 +107,7 @@ protected File generateReport(String goal, String pluginXml) throws Exception {

protected CheckstyleReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
CheckstyleReport mojo = (CheckstyleReport) lookupMojo(goal, pluginXmlFile);
assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

LegacySupport legacySupport = lookup(LegacySupport.class);
legacySupport.setSession(newMavenSession(new MavenProjectStub()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,54 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.doxia.tools.SiteTool;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author Edwin Punzalan
*/
public class CheckstyleReportTest extends AbstractCheckstyleTestCase {
@Test
public void testNoSource() throws Exception {
File generatedReport = generateReport(getGoal(), "no-source-plugin-config.xml");
assertFalse(new File(generatedReport.getAbsolutePath()).exists());
}

@Test
public void testMinConfiguration() throws Exception {
generateReport("min-plugin-config.xml");
}

@Test
public void testCustomConfiguration() throws Exception {
generateReport("custom-plugin-config.xml");
}

@Test
public void testUseFile() throws Exception {
generateReport("useFile-plugin-config.xml");
}

@Test
public void testNoRulesSummary() throws Exception {
generateReport("no-rules-plugin-config.xml");
}

@Test
public void testNoSeveritySummary() throws Exception {
generateReport("no-severity-plugin-config.xml");
}

@Test
public void testNoFilesSummary() throws Exception {
generateReport("no-files-plugin-config.xml");
}

@Test
public void testFailOnError() {
try {
generateReport("fail-on-error-plugin-config.xml");
Expand All @@ -70,6 +84,7 @@ public void testFailOnError() {
}
}

@Test
public void testDependencyResolutionException() {
try {
generateReport("dep-resolution-exception-plugin-config.xml");
Expand All @@ -84,6 +99,7 @@ public void testDependencyResolutionException() {
}
}

@Test
public void testTestSourceDirectory() throws Exception {
generateReport("test-source-directory-plugin-config.xml");
}
Expand All @@ -104,43 +120,43 @@ private void generateReport(String pluginXml) throws Exception {
assertTrue(new File(generatedReport.getAbsolutePath()).exists());

File outputFile = (File) getVariableValueFromObject(mojo, "outputFile");
assertNotNull("Test output file", outputFile);
assertTrue("Test output file exists", outputFile.exists());
assertNotNull(outputFile, "Test output file");
assertTrue(outputFile.exists(), "Test output file exists");

String cacheFile = (String) getVariableValueFromObject(mojo, "cacheFile");
if (cacheFile != null) {
assertTrue("Test cache file exists", new File(cacheFile).exists());
assertTrue(new File(cacheFile).exists(), "Test cache file exists");
}

File useFile = (File) getVariableValueFromObject(mojo, "useFile");
if (useFile != null) {
assertTrue("Test useFile exists", useFile.exists());
assertTrue(useFile.exists(), "Test useFile exists");
}

String str = new String(Files.readAllBytes(generatedReport.toPath()), StandardCharsets.UTF_8);

boolean searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.rules")));
Boolean rules = (Boolean) getVariableValueFromObject(mojo, "enableRulesSummary");
if (rules) {
assertTrue("Test for Rules Summary", searchHeaderFound);
assertTrue(searchHeaderFound, "Test for Rules Summary");
} else {
assertFalse("Test for Rules Summary", searchHeaderFound);
assertFalse(searchHeaderFound, "Test for Rules Summary");
}

searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.summary")));
Boolean severity = (Boolean) getVariableValueFromObject(mojo, "enableSeveritySummary");
if (severity) {
assertTrue("Test for Severity Summary", searchHeaderFound);
assertTrue(searchHeaderFound, "Test for Severity Summary");
} else {
assertFalse("Test for Severity Summary", searchHeaderFound);
assertFalse(searchHeaderFound, "Test for Severity Summary");
}

searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.files")));
Boolean files = (Boolean) getVariableValueFromObject(mojo, "enableFilesSummary");
if (files) {
assertTrue("Test for Files Summary", searchHeaderFound);
assertTrue(searchHeaderFound, "Test for Files Summary");
} else {
assertFalse("Test for Files Summary", searchHeaderFound);
assertFalse(searchHeaderFound, "Test for Files Summary");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author Edwin Punzalan
*
*/
public class CheckstyleViolationCheckMojoTest extends AbstractMojoTestCase {
@Test
public void testDefaultConfig() throws Exception {
File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");

CheckstyleViolationCheckMojo mojo = (CheckstyleViolationCheckMojo) lookupMojo("check", pluginXmlFile);

mojoSetup(mojo);

assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

assertNotNull("project null.", mojo.project);
assertNotNull(mojo.project, "project null.");

try {
mojo.execute();
Expand All @@ -54,12 +59,13 @@ public void testDefaultConfig() throws Exception {
}
}

@Test
public void testInvalidFormatWithSkipExec() throws Exception {
File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");

Mojo mojo = lookupMojo("check", pluginXmlFile);

assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

mojoSetup(mojo);

Expand All @@ -74,12 +80,13 @@ public void testInvalidFormatWithSkipExec() throws Exception {
}
}

@Test
public void testNoOutputFile() throws Exception {
File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");

Mojo mojo = lookupMojo("check", pluginXmlFile);

assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

mojoSetup(mojo);

Expand All @@ -93,7 +100,7 @@ private void doTestPlainOutputFile(boolean failsOnError) throws Exception {

Mojo mojo = lookupMojo("check", pluginXmlFile);

assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

PluginDescriptor descriptorStub = new PluginDescriptor();
descriptorStub.setGroupId("org.apache.maven.plugins");
Expand All @@ -105,6 +112,7 @@ private void doTestPlainOutputFile(boolean failsOnError) throws Exception {
mojo.execute();
}

@Test
public void testPlainOutputFileFailOnError() throws Exception {
try {
doTestPlainOutputFile(true);
Expand All @@ -115,16 +123,18 @@ public void testPlainOutputFileFailOnError() throws Exception {
}
}

@Test
public void testPlainOutputFile() throws Exception {
doTestPlainOutputFile(false);
}

@Test
public void testNoFail() throws Exception {
File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");

Mojo mojo = lookupMojo("check", pluginXmlFile);

assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

mojoSetup(mojo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@

import java.util.List;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;

public class RuleUtilTest extends TestCase {
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RuleUtilTest {
private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";

@Test
public void testGetName() {
assertEquals("FinalParameters", RuleUtil.getName(CHECKSTYLE_PACKAGE + ".FinalParameters"));
assertEquals("FinalParameters", RuleUtil.getName(CHECKSTYLE_PACKAGE + ".FinalParametersCheck"));
assertNull(RuleUtil.getName((String) null));
}

@Test
public void testGetCategory() {
assertEquals("misc", RuleUtil.getCategory(CHECKSTYLE_PACKAGE + ".FinalParametersCheck"));
assertEquals("test", RuleUtil.getCategory(CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck"));
Expand All @@ -39,6 +45,7 @@ public void testGetCategory() {
assertNull(RuleUtil.getCategory((String) null));
}

@Test
public void testMatcher() {
String[] specs = ("misc, test, extension, Header, " + CHECKSTYLE_PACKAGE + ".test2").split(",");
String[] eventSrcNames = new String[] {
Expand All @@ -57,13 +64,14 @@ public void testMatcher() {
for (int j = 0; j < matchers.size(); j++) {
String eventSrcName = eventSrcNames[j];
assertEquals(
spec + " should" + ((i == j) ? " " : " not ") + "match " + eventSrcName,
i == j,
matcher.match(eventSrcName));
matcher.match(eventSrcName),
spec + " should" + ((i == j) ? " " : " not ") + "match " + eventSrcName);
}
}
}

@Test
public void testMatcherWithBlankStrings() {
String[] specs = (" ,,foo, ").split(",");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/
package org.apache.maven.plugins.checkstyle;

import junit.framework.TestCase;
import org.junit.jupiter.api.Test;

public class ViolationTest extends TestCase {
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ViolationTest {
@Test
public void testEquals() {
Violation v1 = new Violation("", null, null, "", "", "", "");
Violation v2 = new Violation("", null, null, "", "", "", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
package org.apache.maven.plugins.checkstyle.exec;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class CheckstyleExecutorTest extends AbstractMojoTestCase {
@Test
public void testDefaultConfig() throws Exception {
CheckstyleExecutor executor = (CheckstyleExecutor) lookup(CheckstyleExecutor.class);
assertNotNull("project null.", executor);
assertNotNull(executor, "project null.");
}
}
Loading
Loading