Skip to content

Commit

Permalink
Issue #6320: added REMOVE_CONDITIONALS mutator for misc
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Jan 3, 2019
1 parent 42640b0 commit a262bad
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 41 deletions.
4 changes: 4 additions & 0 deletions config/checkstyle_resources_suppressions.xml
Expand Up @@ -47,7 +47,11 @@
files="linelength[\\/]InputLineLengthLongPackageStatement\.java"/> files="linelength[\\/]InputLineLengthLongPackageStatement\.java"/>


<!-- intentional problem for testing --> <!-- intentional problem for testing -->
<suppress checks="OuterTypeFilename"
files="outertypefilename[\\/]InputOuterTypeFilename1a\.java"/>
<suppress checks="OuterTypeFilename" <suppress checks="OuterTypeFilename"
files="outertypefilename[\\/]InputOuterTypeFilename5\.java"/> files="outertypefilename[\\/]InputOuterTypeFilename5\.java"/>
<suppress checks="OuterTypeFilename"
files="outertypefilename[\\/]InputOuterTypeFilenameNoPublic\.java"/>


</suppressions> </suppressions>
3 changes: 2 additions & 1 deletion config/suppressions.xml
Expand Up @@ -88,7 +88,8 @@
<suppress checks="ClassDataAbstractionCoupling" <suppress checks="ClassDataAbstractionCoupling"
files="(Checker|Main|CheckstyleAntTask|JavadocDetailNodeParser)\.java"/> files="(Checker|Main|CheckstyleAntTask|JavadocDetailNodeParser)\.java"/>
<suppress checks="ClassDataAbstractionCoupling" <suppress checks="ClassDataAbstractionCoupling"
files="(CheckerTest|AbstractModuleTestSupport|CheckstyleAntTaskTest)\.java"/> files="(CheckerTest|AbstractModuleTestSupport|CheckstyleAntTaskTest|
|TranslationCheckTest)\.java"/>
<suppress checks="ClassDataAbstractionCoupling" files="PropertyCacheFileTest\.java"/> <suppress checks="ClassDataAbstractionCoupling" files="PropertyCacheFileTest\.java"/>
<suppress checks="ClassDataAbstractionCoupling" <suppress checks="ClassDataAbstractionCoupling"
files="XpathFileGeneratorAuditListenerTest\.java"/> files="XpathFileGeneratorAuditListenerTest\.java"/>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -1784,6 +1784,7 @@
<mutator>INVERT_NEGS</mutator> <mutator>INVERT_NEGS</mutator>
<mutator>MATH</mutator> <mutator>MATH</mutator>
<mutator>NEGATE_CONDITIONALS</mutator> <mutator>NEGATE_CONDITIONALS</mutator>
<mutator>REMOVE_CONDITIONALS</mutator>
<mutator>RETURN_VALS</mutator> <mutator>RETURN_VALS</mutator>
<mutator>TRUE_RETURNS</mutator> <mutator>TRUE_RETURNS</mutator>
<mutator>VOID_METHOD_CALLS</mutator> <mutator>VOID_METHOD_CALLS</mutator>
Expand Down
Expand Up @@ -137,14 +137,10 @@ else if (ast.getType() == TokenTypes.FOR_EACH_CLAUSE) {
private void visitMethod(final DetailAST method) { private void visitMethod(final DetailAST method) {
final DetailAST modifiers = final DetailAST modifiers =
method.findFirstToken(TokenTypes.MODIFIERS); method.findFirstToken(TokenTypes.MODIFIERS);
// exit on fast lane if there is nothing to check here


if (method.findFirstToken(TokenTypes.PARAMETERS) // ignore abstract and native methods
.findFirstToken(TokenTypes.PARAMETER_DEF) != null if (modifiers.findFirstToken(TokenTypes.ABSTRACT) == null
// ignore abstract and native methods
&& modifiers.findFirstToken(TokenTypes.ABSTRACT) == null
&& modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) { && modifiers.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) {
// we can now be sure that there is at least one parameter
final DetailAST parameters = final DetailAST parameters =
method.findFirstToken(TokenTypes.PARAMETERS); method.findFirstToken(TokenTypes.PARAMETERS);
DetailAST child = parameters.getFirstChild(); DetailAST child = parameters.getFirstChild();
Expand Down
Expand Up @@ -67,10 +67,9 @@ public enum LineSeparatorOption {
public boolean matches(byte... bytes) { public boolean matches(byte... bytes) {
final boolean result; final boolean result;
if (this == LF_CR_CRLF) { if (this == LF_CR_CRLF) {
// this silently assumes CRLF and ANY have the same length // this silently assumes LF and CR are of length 1
// and LF and CR are of length 1 // CRLF always matches LF, so CRLF isn't tested
result = CRLF.matches(bytes) result = LF.matches(Arrays.copyOfRange(bytes, 1, 2))
|| LF.matches(Arrays.copyOfRange(bytes, 1, 2))
|| CR.matches(Arrays.copyOfRange(bytes, 1, 2)); || CR.matches(Arrays.copyOfRange(bytes, 1, 2));
} }
else { else {
Expand Down
Expand Up @@ -51,9 +51,6 @@ public class OuterTypeFilenameCheck extends AbstractCheck {
/** If file has public type. */ /** If file has public type. */
private boolean hasPublic; private boolean hasPublic;


/** If first type has has same name as file. */
private boolean validFirst;

/** Outer type with mismatched file name. */ /** Outer type with mismatched file name. */
private DetailAST wrongType; private DetailAST wrongType;


Expand Down Expand Up @@ -81,7 +78,6 @@ public int[] getRequiredTokens() {
public void beginTree(DetailAST rootAST) { public void beginTree(DetailAST rootAST) {
fileName = getFileName(); fileName = getFileName();
seenFirstToken = false; seenFirstToken = false;
validFirst = false;
hasPublic = false; hasPublic = false;
wrongType = null; wrongType = null;
} }
Expand All @@ -98,10 +94,7 @@ public void visitToken(DetailAST ast) {
else { else {
final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText(); final String outerTypeName = ast.findFirstToken(TokenTypes.IDENT).getText();


if (fileName.equals(outerTypeName)) { if (!fileName.equals(outerTypeName)) {
validFirst = true;
}
else {
wrongType = ast; wrongType = ast;
} }
} }
Expand All @@ -110,7 +103,7 @@ public void visitToken(DetailAST ast) {


@Override @Override
public void finishTree(DetailAST rootAST) { public void finishTree(DetailAST rootAST) {
if (!validFirst && !hasPublic && wrongType != null) { if (!hasPublic && wrongType != null) {
log(wrongType.getLineNo(), MSG_KEY); log(wrongType.getLineNo(), MSG_KEY);
} }
} }
Expand Down
Expand Up @@ -435,17 +435,15 @@ private static String getPath(String fileNameWithPath) {
*/ */
private void checkTranslationKeys(ResourceBundle bundle) { private void checkTranslationKeys(ResourceBundle bundle) {
final Set<File> filesInBundle = bundle.getFiles(); final Set<File> filesInBundle = bundle.getFiles();
if (filesInBundle.size() >= 2) { // build a map from files to the keys they contain
// build a map from files to the keys they contain final Set<String> allTranslationKeys = new HashSet<>();
final Set<String> allTranslationKeys = new HashSet<>(); final Map<File, Set<String>> filesAssociatedWithKeys = new HashMap<>();
final Map<File, Set<String>> filesAssociatedWithKeys = new HashMap<>(); for (File currentFile : filesInBundle) {
for (File currentFile : filesInBundle) { final Set<String> keysInCurrentFile = getTranslationKeys(currentFile);
final Set<String> keysInCurrentFile = getTranslationKeys(currentFile); allTranslationKeys.addAll(keysInCurrentFile);
allTranslationKeys.addAll(keysInCurrentFile); filesAssociatedWithKeys.put(currentFile, keysInCurrentFile);
filesAssociatedWithKeys.put(currentFile, keysInCurrentFile);
}
checkFilesForConsistencyRegardingTheirKeys(filesAssociatedWithKeys, allTranslationKeys);
} }
checkFilesForConsistencyRegardingTheirKeys(filesAssociatedWithKeys, allTranslationKeys);
} }


/** /**
Expand All @@ -463,10 +461,8 @@ private void checkFilesForConsistencyRegardingTheirKeys(Map<File, Set<String>> f
final Set<String> currentFileKeys = fileKey.getValue(); final Set<String> currentFileKeys = fileKey.getValue();
final Set<String> missingKeys = keysThatMustExist.stream() final Set<String> missingKeys = keysThatMustExist.stream()
.filter(key -> !currentFileKeys.contains(key)).collect(Collectors.toSet()); .filter(key -> !currentFileKeys.contains(key)).collect(Collectors.toSet());
if (!missingKeys.isEmpty()) { for (Object key : missingKeys) {
for (Object key : missingKeys) { log(1, MSG_KEY, key);
log(1, MSG_KEY, key);
}
} }
fireErrors(path); fireErrors(path);
dispatcher.fireFileFinished(path); dispatcher.fireFileFinished(path);
Expand Down
Expand Up @@ -127,6 +127,29 @@ public void testIgnorePrimitiveTypesParameters() throws Exception {
verify(checkConfig, getPath("InputFinalParametersPrimitiveTypes.java"), expected); verify(checkConfig, getPath("InputFinalParametersPrimitiveTypes.java"), expected);
} }


@Test
public void testPrimitiveTypesParameters() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(FinalParametersCheck.class);
final String[] expected = {
"5:14: " + getCheckMessage(MSG_KEY, "i"),
"6:15: " + getCheckMessage(MSG_KEY, "i"),
"6:22: " + getCheckMessage(MSG_KEY, "k"),
"6:32: " + getCheckMessage(MSG_KEY, "s"),
"7:15: " + getCheckMessage(MSG_KEY, "s"),
"7:25: " + getCheckMessage(MSG_KEY, "o"),
"7:35: " + getCheckMessage(MSG_KEY, "l"),
"8:15: " + getCheckMessage(MSG_KEY, "array"),
"9:15: " + getCheckMessage(MSG_KEY, "i"),
"9:22: " + getCheckMessage(MSG_KEY, "x"),
"9:31: " + getCheckMessage(MSG_KEY, "s"),
"10:15: " + getCheckMessage(MSG_KEY, "x"),
"10:22: " + getCheckMessage(MSG_KEY, "l"),
"10:32: " + getCheckMessage(MSG_KEY, "s"),
};
verify(checkConfig, getPath("InputFinalParametersPrimitiveTypes.java"), expected);
}

@Test @Test
public void testReceiverParameters() throws Exception { public void testReceiverParameters() throws Exception {
final DefaultConfiguration checkConfig = final DefaultConfiguration checkConfig =
Expand Down
Expand Up @@ -86,6 +86,15 @@ public void testNestedClass() throws Exception {
verify(checkConfig, getPath("InputOuterTypeFilename1.java"), expected); verify(checkConfig, getPath("InputOuterTypeFilename1.java"), expected);
} }


@Test
public void testNestedClass2() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
final String[] expected = {
"3: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputOuterTypeFilename1a.java"), expected);
}

@Test @Test
public void testFinePublic() throws Exception { public void testFinePublic() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class); final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
Expand All @@ -100,6 +109,15 @@ public void testPublicClassIsNotFirst() throws Exception {
verify(checkConfig, getPath("InputOuterTypeFilenameCheckPublic.java"), expected); verify(checkConfig, getPath("InputOuterTypeFilenameCheckPublic.java"), expected);
} }


@Test
public void testNoPublicClasses() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
final String[] expected = {
"3: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputOuterTypeFilenameNoPublic.java"), expected);
}

@Test @Test
public void testFineDefault() throws Exception { public void testFineDefault() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class); final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
Expand Down
Expand Up @@ -199,6 +199,14 @@ public void testIsSuppressedAfterEventEnd() throws Exception {
assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event)); assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
} }


@Test
public void testIsSuppressedAfterEventEnd2() throws Exception {
createHolder("check", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("check", 400, 10);

assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}

@Test @Test
public void testIsSuppressedAfterEventStart() throws Exception { public void testIsSuppressedAfterEventStart() throws Exception {
createHolder("check", 100, 100, 350, 350); createHolder("check", 100, 100, 350, 350);
Expand Down
Expand Up @@ -62,6 +62,8 @@ public void testDefaults() throws Exception {
"18: " + getCheckMessage(MSG_KEY), "18: " + getCheckMessage(MSG_KEY),
"19: " + getCheckMessage(MSG_KEY), "19: " + getCheckMessage(MSG_KEY),
"29: " + getCheckMessage(MSG_KEY), "29: " + getCheckMessage(MSG_KEY),
"30: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
}; };
verify(checkConfig, getPath("InputTrailingComment.java"), expected); verify(checkConfig, getPath("InputTrailingComment.java"), expected);
} }
Expand All @@ -76,6 +78,7 @@ public void testLegalComment() throws Exception {
"8: " + getCheckMessage(MSG_KEY), "8: " + getCheckMessage(MSG_KEY),
"18: " + getCheckMessage(MSG_KEY), "18: " + getCheckMessage(MSG_KEY),
"19: " + getCheckMessage(MSG_KEY), "19: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
}; };
verify(checkConfig, getPath("InputTrailingComment.java"), expected); verify(checkConfig, getPath("InputTrailingComment.java"), expected);
} }
Expand All @@ -97,6 +100,8 @@ public void testFormat() throws Exception {
"19: " + getCheckMessage(MSG_KEY), "19: " + getCheckMessage(MSG_KEY),
"26: " + getCheckMessage(MSG_KEY), "26: " + getCheckMessage(MSG_KEY),
"29: " + getCheckMessage(MSG_KEY), "29: " + getCheckMessage(MSG_KEY),
"30: " + getCheckMessage(MSG_KEY),
"31: " + getCheckMessage(MSG_KEY),
}; };
verify(checkConfig, getPath("InputTrailingComment.java"), expected); verify(checkConfig, getPath("InputTrailingComment.java"), expected);
} }
Expand Down

0 comments on commit a262bad

Please sign in to comment.