Skip to content

Commit

Permalink
Issue #5007: increase coverage of pitest-checks-design to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimfadora authored and romani committed Aug 31, 2017
1 parent fc44df8 commit d97519f
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@
<targetTests>
<param>com.puppycrawl.tools.checkstyle.checks.design.*</param>
</targetTests>
<mutationThreshold>98</mutationThreshold>
<mutationThreshold>100</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
4 changes: 2 additions & 2 deletions shippable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ env:
- PROFILE="-Ppitest-checks-javadoc,no-validations"
- PROFILE="-Ppitest-checks-imports,no-validations"
- PROFILE="-Ppitest-checks-metrics,no-validations"
- PROFILE="-Ppitest-checks-regexp,no-validations"
- PROFILE="-Ppitest-checks-regexp,no-validations"; POST_ACTION=check_survived
- PROFILE="-Ppitest-checks-sizes,no-validations"; POST_ACTION=check_survived
- PROFILE="-Ppitest-checks-whitespace,no-validations"
- PROFILE="-Ppitest-checks-misc,no-validations"
- PROFILE="-Ppitest-checks-blocks,no-validations"
- PROFILE="-Ppitest-checks-coding,no-validations"
- PROFILE="-Ppitest-checks-design,no-validations"
- PROFILE="-Ppitest-checks-design,no-validations"; POST_ACTION=check_survived
- PROFILE="-Ppitest-checks-annotation,no-validations"
- PROFILE="-Ppitest-checks-header,no-validations"; POST_ACTION=check_survived
- PROFILE="-Ppitest-checks-modifier,no-validations"; POST_ACTION=check_survived
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class InnerTypeLastCheckTest extends AbstractModuleTestSupport {
@Override
Expand Down Expand Up @@ -56,6 +57,14 @@ public void testMembersBeforeInner() throws Exception {
verify(checkConfig, getPath("InputInnerTypeLastClass.java"), expected);
}

@Test
public void testIfRootClassChecked() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(InnerTypeLastCheck.class);
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputInnerTypeLastClassRootClass.java"), expected);
}

@Test
public void testGetAcceptableTokens() {
final InnerTypeLastCheck obj = new InnerTypeLastCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import antlr.CommonHiddenStreamToken;
import com.google.common.collect.ImmutableMap;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
Expand Down Expand Up @@ -61,6 +66,26 @@ public void testDefault() throws Exception {
verify(checkConfig, getPath("InputMutableException.java"), expected);
}

@Test
public void testMultipleInputs() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
final String filePath1 = getPath("InputMutableException.java");
final String filePath2 = getPath("InputMutableExceptionMultipleInputs.java");

final List<String> expected1 = Arrays.asList(
"6:9: " + getCheckMessage(MSG_KEY, "errorCode"),
"23:9: " + getCheckMessage(MSG_KEY, "errorCode"),
"46:9: " + getCheckMessage(MSG_KEY, "errorCode"));
final List<String> expected2 = Arrays.asList(
"6:9: " + getCheckMessage(MSG_KEY, "errorCode"),
"10:9: " + getCheckMessage(MSG_KEY, "errorCode"));

final File[] inputs = {new File(filePath1), new File(filePath2)};

verify(createChecker(checkConfig), inputs,
ImmutableMap.of(filePath1, expected1, filePath2, expected2));
}

@Test
public void testFormat() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(MutableExceptionCheck.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
import static com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck.MSG_KEY;
import static org.junit.Assert.assertArrayEquals;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.collect.ImmutableMap;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
Expand All @@ -42,6 +48,29 @@ public void testGetRequiredTokens() {
CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
}

@Test
public void testClearState() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(OneTopLevelClassCheck.class);
final String firstInputFilePath = getPath("InputOneTopLevelClassDeclarationOrder.java");
final String secondInputFilePath = getPath("InputOneTopLevelInterface2.java");

final File[] inputs = {
new File(firstInputFilePath),
new File(secondInputFilePath),
};

final List<String> expectedFirstInput = Collections.singletonList(
"10: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"));
final List<String> expectedSecondInput = Arrays.asList(
"3: " + getCheckMessage(MSG_KEY, "InputOneTopLevelInterface2inner1"),
"11: " + getCheckMessage(MSG_KEY, "InputOneTopLevelInterface2inner2"));

verify(createChecker(checkConfig), inputs,
ImmutableMap.of(firstInputFilePath, expectedFirstInput,
secondInputFilePath, expectedSecondInput));
}

@Test
public void testAcceptableTokens() {
final OneTopLevelClassCheck check = new OneTopLevelClassCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,13 @@ public void testNotIgnorePrivateMethod() throws Exception {
};
verify(checkConfig, getPath("InputThrowsCount.java"), expected);
}

@Test
public void testMethodWithAnnotation() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(ThrowsCountCheck.class);
final String[] expected = {
"18:26: " + getCheckMessage(MSG_KEY, 5, 4),
};
verify(checkConfig, getPath("InputThrowsCountMwthodWithAnnotation.java"), expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@
import static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.lang.reflect.Method;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

import antlr.CommonHiddenStreamToken;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.internal.TestUtils;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class VisibilityModifierCheckTest
Expand Down Expand Up @@ -435,4 +441,25 @@ public void testVisibilityModifiersOfGenericFields() throws Exception {
};
verify(checkConfig, getPath("InputVisibilityModifierGenerics.java"), expected);
}

/**
* We can not cover this mutation because it force all imports to be non static,
* but static imports are ignored, so we will not see any affect on validation.
* We could remove this method at all, and it will work correctly as we can not use
* class with name "", but in this case internal collection will have short names
* as "" that will not make problems, but will be weird in debug.
*
* @throws Exception when exception occured during execution.
*/
@Test
public void testIsStarImportNullAst() throws Exception {
final DetailAST importAst = TestUtils.parseFile(new File(getPath(
"InputVisibilityModifierIsStarImport.java"))).getNextSibling();
final VisibilityModifierCheck check = new VisibilityModifierCheck();
final Method isStarImport = Whitebox.getMethod(VisibilityModifierCheck.class,
"isStarImport", DetailAST.class);

assertTrue("Should return true when star import is passed",
(boolean) isStarImport.invoke(check, importAst));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static boolean isStatefulFieldClearedDuringBeginTree(AbstractCheck check,
String fieldName,
Predicate<Object> isClear)
throws NoSuchFieldException, IllegalAccessException {
check.beginTree(null);
check.beginTree(astToVisit);
check.visitToken(astToVisit);
check.beginTree(null);
final Field field = check.getClass().getDeclaredField(fieldName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.puppycrawl.tools.checkstyle.checks.design.innertypelast;

public enum InputInnerTypeLastClassRootClass {

ALWAYS(Bits.YES), NEVER(Bits.NO);

private interface Bits {
public static final int YES = 1;

public static final int NO = 4;
}

private final int bits;

private InputInnerTypeLastClassRootClass(int bits) {
this.bits = bits;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.puppycrawl.tools.checkstyle.checks.design.mutableexception;

public class InputMutableExceptionMultipleInputs {

public class BarError extends Throwable {
private int errorCode;
}

class CustomMutableException extends java.lang.Exception {
int errorCode;
final int errorCount = 6;
}

private String variable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.puppycrawl.tools.checkstyle.checks.design.throwscount;

import java.awt.AWTException;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.nio.file.FileAlreadyExistsException;
import java.sql.SQLException;

public class InputThrowsCountMwthodWithAnnotation extends ParentClass {
@Override
public void method() throws AWTException, SQLException, FileNotFoundException,
EOFException, FileAlreadyExistsException {
super.method();
}
}

class ParentClass {
public void method() throws AWTException, SQLException,
FileNotFoundException, EOFException, FileAlreadyExistsException {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.puppycrawl.tools.checkstyle.checks.design.visibilitymodifier;

import com.puppycrawl.*;

public class InputVisibilityModifierIsStarImport {
}

0 comments on commit d97519f

Please sign in to comment.