Skip to content

Commit

Permalink
Issue #73: make tests to execute whole checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Jul 13, 2020
1 parent 48f835f commit 646103d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
files="(CheckerTest|AbstractModuleTestSupport|AbstractItModuleTestSupport|
|CheckstyleAntTaskTest|
|TranslationCheckTest|LocalizedMessageTest|AbstractFileSetCheckTest|
|AbstractCheckTest|AutomaticBeanTest|GeneratePatchFile)\.java"/>
|AbstractCheckTest|AutomaticBeanTest|GeneratePatchFile|
|SuppressionPatchFilterTest)\.java"/>
<suppress checks="MagicNumber"
files="GeneratePatchFileLauncher|GeneratePatchFileWithGitCommandLauncher\.java"/>
<suppress checks="UncommentedMain"
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected void finishLocalSetup() throws CheckstyleException {
}

private void loadPatchFile(String patchFileName) throws CheckstyleException {
System.out.print(new File(".").getAbsolutePath());
try {
final List<String> originPatch = Files.readAllLines(new File(patchFileName).toPath());
final List<List<String>> patchList = getPatchList(originPatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@

package com.puppycrawl.tools.checkstyle.filters;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Ignore;
import org.junit.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.ModuleFactory;
import com.puppycrawl.tools.checkstyle.PackageObjectFactory;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.RootModule;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.internal.utils.BriefUtLogger;

public class SuppressionPatchFilterTest extends AbstractModuleTestSupport {

Expand Down Expand Up @@ -98,4 +118,52 @@ public void testBoundaryOne() throws Exception {
suppressionPatchFilter.finishLocalSetup();
return suppressionPatchFilter;
}

@Ignore
@Test
public void testByConfig() throws Exception {
// we can add here any variable to provide path to patch name by PropertiesExpander
final Configuration config = ConfigurationLoader.loadConfiguration(
getPath("strategy/patchedline/InputRegexpSingleline/config.xml"),
new PropertiesExpander(System.getProperties()));
final ClassLoader moduleClassLoader = SuppressionPatchFilter.class.getClassLoader();
final ModuleFactory factory = new PackageObjectFactory(
SuppressionPatchFilter.class.getPackage().getName(), moduleClassLoader);

final RootModule rootModule = (RootModule) factory.createModule(config.getName());
rootModule.setModuleClassLoader(moduleClassLoader);
rootModule.configure(config);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
rootModule.addListener(new BriefUtLogger(stream));

// run RootModule
final String path = getPath("strategy/patchedline/InputRegexpSingleline/Input.java");
final List<File> files = Collections.singletonList(
new File(path));
final int errorCounter = rootModule.process(files);

final String[] expected = {
"3: Line matches the illegal pattern 'System.out.print'.",
"7: Line matches the illegal pattern 'System.out.print'.",
"12: Line matches the illegal pattern 'System.out.print'.",
};

// process each of the lines
try (ByteArrayInputStream inputStream =
new ByteArrayInputStream(stream.toByteArray());
LineNumberReader lnr = new LineNumberReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
final List<String> actuals = lnr.lines().limit(expected.length)
.sorted().collect(Collectors.toList());
Arrays.sort(expected);

for (int i = 0; i < expected.length; i++) {
final String expectedResult = path + ":" + expected[i];
assertEquals("error message " + i, expectedResult, actuals.get(i));
}

assertEquals("unexpected output: " + lnr.readLine(),
expected.length, errorCounter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<module name="RegexpSingleline">
<property name="format" value="System.out.print"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.filters.SuppressionPatchFilter">
<property name="file" value="src/test/resources/com/puppycrawl/tools/checkstyle/filters/strategy/patchedline/InputRegexpSingleline/config.xml" />
</module>
</module>

0 comments on commit 646103d

Please sign in to comment.