Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#4399: increase coverage of pitest-checkstyle main pr…
Browse files Browse the repository at this point in the history
…ofile to 100%
  • Loading branch information
Nimfadora committed Jul 27, 2017
1 parent a978ce5 commit b2f9cf1
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 44 deletions.
7 changes: 7 additions & 0 deletions config/pmd-main.xml
Expand Up @@ -19,4 +19,11 @@
| //MethodDeclaration[@Name='getHashCodeBasedOnObjectContent' and ../../..[@Image='PropertyCacheFile']]"/>
</properties>
</rule>
<rule ref="rulesets/java/basic.xml/CollapsibleIfStatements">
<properties>
<!-- till https://github.com/hcoles/pitest/issues/377 -->
<property name="violationSuppressXPath" value="//MethodDeclaration[@Name='main' and ../../..[@Image='Main']]"/>
</properties>
</rule>

</ruleset>
1 change: 1 addition & 0 deletions config/suppressions.xml
Expand Up @@ -49,6 +49,7 @@
<suppress checks="ExecutableStatementCount" files=".*GeneratedJavaTokenTypesTest\.java"/>
<suppress checks="JavaNCSS" files=".*GeneratedJavaTokenTypesTest\.java"/>
<suppress checks="ExecutableStatementCount" files=".*IllegalInstantiationCheckTest\.java"/>
<suppress checks="ExecutableStatementCount" files=".*Main\.java"/>

<suppress checks="." files=".*JavadocTokenTypes\.java"/>
<suppress checks="." files=".*ParseTreeBuilder\.java"/>
Expand Down
17 changes: 0 additions & 17 deletions pom.xml
Expand Up @@ -2091,23 +2091,6 @@
<targetTests>
<param>com.puppycrawl.tools.checkstyle.MainTest</param>
</targetTests>
<excludedMethods>
<!-- till https://github.com/checkstyle/checkstyle/issues/4399 -->
<param>main</param>
<!-- till https://github.com/checkstyle/checkstyle/issues/4399 -->
<param>createListener</param>
</excludedMethods>
<avoidCallsTo>
<!-- till https://github.com/checkstyle/checkstyle/issues/4399 -->
<avoidCallsTo>com.google.common.io.Closeables</avoidCallsTo>
<!-- till https://github.com/checkstyle/checkstyle/issues/4399 -->
<avoidCallsTo>com.puppycrawl.tools.checkstyle.utils.CommonUtils</avoidCallsTo>
<!-- till https://github.com/checkstyle/checkstyle/issues/4399 -->
<avoidCallsTo>com.puppycrawl.tools.checkstyle.api.RootModule</avoidCallsTo>
<!-- add default suppressions -->
<avoidCallsTo>org.apache.commons.logging</avoidCallsTo>
<avoidCallsTo>java.util.logging</avoidCallsTo>
</avoidCallsTo>
<mutationThreshold>100</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/puppycrawl/tools/checkstyle/Main.java
Expand Up @@ -226,11 +226,14 @@ public static void main(String... args) throws IOException {
}
finally {
// return exit code base on validation of Checker
if (errorCounter != 0 && !cliViolations) {
final LocalizedMessage errorCounterMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, ERROR_COUNTER,
new String[] {String.valueOf(errorCounter)}, null, Main.class, null);
System.out.println(errorCounterMessage.getMessage());
// two ifs exist till https://github.com/hcoles/pitest/issues/377
if (errorCounter != 0) {
if (!cliViolations) {
final LocalizedMessage errorCounterMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, ERROR_COUNTER,
new String[] {String.valueOf(errorCounter)}, null, Main.class, null);
System.out.println(errorCounterMessage.getMessage());
}
}
if (exitStatus != 0) {
System.exit(exitStatus);
Expand Down
Expand Up @@ -42,7 +42,7 @@ protected String getPackageLocation() {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(AstTreeStringPrinter.class);
assertUtilsClassHasPrivateConstructor(AstTreeStringPrinter.class, true);
}

@Test
Expand Down
Expand Up @@ -26,6 +26,6 @@
public class DefinitionsTest {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(Definitions.class);
assertUtilsClassHasPrivateConstructor(Definitions.class, true);
}
}
Expand Up @@ -45,7 +45,7 @@ private static String getPath(String filename) {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(DetailNodeTreeStringPrinter.class);
assertUtilsClassHasPrivateConstructor(DetailNodeTreeStringPrinter.class, true);
}

@Test
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java
Expand Up @@ -25,11 +25,18 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
Expand All @@ -52,11 +59,18 @@
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Main.class, CommonUtils.class, Closeables.class})
public class MainTest {
private static final String USAGE = String.format(Locale.ROOT,
"usage: java com.puppycrawl.tools.checkstyle.Main [options] -c <config.xml>"
Expand Down Expand Up @@ -148,7 +162,7 @@ public void setUp() {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(Main.class);
assertUtilsClassHasPrivateConstructor(Main.class, false);
}

@Test
Expand Down Expand Up @@ -387,8 +401,11 @@ public void testCreateNonExistingOutputFile() throws Exception {
}

@Test
public void testExistingTargetFilePlainOutputProperties()
throws Exception {
public void testExistingTargetFilePlainOutputProperties() throws Exception {
mockStatic(Closeables.class);
doNothing().when(Closeables.class);
Closeables.closeQuietly(any(InputStream.class));

//exit.expectSystemExitWithStatus(0);
exit.checkAssertionAfterwards(() -> {
assertEquals(auditStartMessage.getMessage() + EOL
Expand All @@ -398,6 +415,9 @@ public void testExistingTargetFilePlainOutputProperties()
Main.main("-c", getPath("InputMainConfig-classname-prop.xml"),
"-p", getPath("InputMainMycheckstyle.properties"),
getPath("InputMain.java"));

verifyStatic(times(2));
Closeables.closeQuietly(any(InputStream.class));
}

@Test
Expand Down Expand Up @@ -511,6 +531,10 @@ public void testCreateListenerIllegalStateException() throws Exception {

@Test
public void testCreateListenerWithLocationIllegalStateException() throws Exception {
mockStatic(CommonUtils.class);
doNothing().when(CommonUtils.class);
CommonUtils.close(any(OutputStream.class));

final Method method = Main.class.getDeclaredMethod("createListener", String.class,
String.class);
method.setAccessible(true);
Expand All @@ -530,6 +554,9 @@ public void testCreateListenerWithLocationIllegalStateException() throws Excepti
// method creates output folder
FileUtils.deleteQuietly(new File(outDir));
}

verifyStatic(times(1));
CommonUtils.close(any(OutputStream.class));
}

@Test
Expand Down Expand Up @@ -840,6 +867,7 @@ public void testCustomRootModule() throws Exception {
});
Main.main("-c", getPath("InputMainConfig-custom-root-module.xml"),
getPath("InputMain.java"));
assertTrue(TestRootModuleChecker.isDestroyed());
}

@Test
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void testParserConfiguratedSuccefully() throws Exception {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(
AbstractLoader.FeaturesForVerySecureJavaInstallations.class);
AbstractLoader.FeaturesForVerySecureJavaInstallations.class, true);
}

private static final class DummyLoader extends AbstractLoader {
Expand Down
Expand Up @@ -26,6 +26,6 @@
public class JavadocTokenTypesTest {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(JavadocTokenTypes.class);
assertUtilsClassHasPrivateConstructor(JavadocTokenTypes.class, true);
}
}
Expand Up @@ -36,6 +36,6 @@ public void testGetShortDescription() {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(TokenTypes.class);
assertUtilsClassHasPrivateConstructor(TokenTypes.class, true);
}
}
Expand Up @@ -168,7 +168,7 @@ public void testPositionOnlyComments()

@Test
public void testBlockCommentPositionHasPrivateConstr() throws Exception {
TestUtils.assertUtilsClassHasPrivateConstructor(BlockCommentPosition.class);
TestUtils.assertUtilsClassHasPrivateConstructor(BlockCommentPosition.class, true);
}

@Test
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class BlockTagUtilsTest {

@Test
public void testHasPrivateConstructor() throws Exception {
TestUtils.assertUtilsClassHasPrivateConstructor(BlockTagUtils.class);
TestUtils.assertUtilsClassHasPrivateConstructor(BlockTagUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class InlineTagUtilsTest {

@Test
public void testHasPrivateConstructor() throws Exception {
TestUtils.assertUtilsClassHasPrivateConstructor(InlineTagUtils.class);
TestUtils.assertUtilsClassHasPrivateConstructor(InlineTagUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -51,7 +51,7 @@ private static String getPath(String filename) {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(TokenTypesDoclet.class);
assertUtilsClassHasPrivateConstructor(TokenTypesDoclet.class, true);
}

@Test
Expand Down
Expand Up @@ -47,10 +47,11 @@ private TestUtils() {
/**
* Verifies that utils class has private constructor and invokes it to satisfy code coverage.
*/
public static void assertUtilsClassHasPrivateConstructor(final Class<?> utilClass)
public static void assertUtilsClassHasPrivateConstructor(final Class<?> utilClass,
boolean checkConstructorIsPrivate)
throws ReflectiveOperationException {
final Constructor<?> constructor = utilClass.getDeclaredConstructor();
if (!Modifier.isPrivate(constructor.getModifiers())) {
if (checkConstructorIsPrivate && !Modifier.isPrivate(constructor.getModifiers())) {
Assert.fail("Constructor is not private");
}
constructor.setAccessible(true);
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class AnnotationUtilityTest {
@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
try {
assertUtilsClassHasPrivateConstructor(AnnotationUtility.class);
assertUtilsClassHasPrivateConstructor(AnnotationUtility.class, true);
}
catch (InvocationTargetException ex) {
assertEquals("do not instantiate.", ex.getCause().getMessage());
Expand Down
Expand Up @@ -44,7 +44,7 @@ public class CheckUtilsTest {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(CheckUtils.class);
assertUtilsClassHasPrivateConstructor(CheckUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -54,7 +54,7 @@ public class CommonUtilsTest {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(CommonUtils.class);
assertUtilsClassHasPrivateConstructor(CommonUtils.class, true);
}

/**
Expand Down
Expand Up @@ -217,7 +217,7 @@ public void testEmptyJavadocCommentAst() {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(JavadocUtils.class);
assertUtilsClassHasPrivateConstructor(JavadocUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class ModuleReflectionUtilsTest {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(ModuleReflectionUtils.class);
assertUtilsClassHasPrivateConstructor(ModuleReflectionUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class ScopeUtilsTest {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(ScopeUtils.class);
assertUtilsClassHasPrivateConstructor(ScopeUtils.class, true);
}

@Test
Expand Down
Expand Up @@ -38,7 +38,7 @@ public class TokenUtilsTest {

@Test
public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(TokenUtils.class);
assertUtilsClassHasPrivateConstructor(TokenUtils.class, true);
}

@Test
Expand Down

0 comments on commit b2f9cf1

Please sign in to comment.