forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#6207: Add xpath regression test for AvoidNestedBlocks
- Loading branch information
Showing
6 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionAvoidNestedBlocksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2019 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package org.checkstyle.suppressionxpathfilter; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck; | ||
|
||
public class XpathRegressionAvoidNestedBlocksTest extends AbstractXpathTestSupport { | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return AvoidNestedBlocksCheck.class.getSimpleName(); | ||
} | ||
|
||
@Test | ||
public void testEmpty() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionAvoidNestedBlocksEmpty.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(AvoidNestedBlocksCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"6:9: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksEmpty']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='empty']]/SLIST/SLIST" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, | ||
expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testVariableAssignment() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionAvoidNestedBlocksVariable.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(AvoidNestedBlocksCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"7:9: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksVariable']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='varAssign']]/SLIST/SLIST" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, | ||
expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testSwitchAllowInSwitchCaseFalse() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionAvoidNestedBlocksSwitch1.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(AvoidNestedBlocksCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"9:21: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
"16:13: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
"20:21: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Arrays.asList( | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksSwitch1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH" | ||
+ "/CASE_GROUP/SLIST", | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksSwitch1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH" | ||
+ "/CASE_GROUP/SLIST/SLIST" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, | ||
expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testSwitchAllowInSwitchCaseTrue() throws Exception { | ||
final File fileToProcess = new File( | ||
getPath("SuppressionXpathRegressionAvoidNestedBlocksSwitch2.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(AvoidNestedBlocksCheck.class); | ||
moduleConfig.addAttribute("allowInSwitchCase", "true"); | ||
|
||
final String[] expectedViolation = { | ||
"9:21: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
"16:13: " + getCheckMessage(AvoidNestedBlocksCheck.class, | ||
AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Arrays.asList( | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksSwitch2']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH" | ||
+ "/CASE_GROUP/SLIST", | ||
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAvoidNestedBlocksSwitch2']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH" | ||
+ "/CASE_GROUP/SLIST/SLIST" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, | ||
expectedXpathQueries); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...essionxpathfilter/avoidnestedblocks/SuppressionXpathRegressionAvoidNestedBlocksEmpty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.checkstyle.suppressionxpathfilter.avoidnestedblocks; | ||
|
||
public class SuppressionXpathRegressionAvoidNestedBlocksEmpty { | ||
|
||
void empty() { | ||
{} // warn | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...sionxpathfilter/avoidnestedblocks/SuppressionXpathRegressionAvoidNestedBlocksSwitch1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.checkstyle.suppressionxpathfilter.avoidnestedblocks; | ||
|
||
public class SuppressionXpathRegressionAvoidNestedBlocksSwitch1 { | ||
|
||
int s(int a) { | ||
int x; | ||
int y; | ||
switch (a) { | ||
case 0: { // warn: break outside block | ||
x = 0; | ||
y = 0; | ||
} | ||
break; | ||
case 1: | ||
x = 1; | ||
{ // warn: statement outside block | ||
y = -1; | ||
break; | ||
} | ||
case 2: { // warn: allowInSwitchCase=false | ||
x = 2; | ||
y = -2; | ||
break; | ||
} | ||
default: | ||
x = 3; | ||
y = -3; | ||
} | ||
return x + y; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...sionxpathfilter/avoidnestedblocks/SuppressionXpathRegressionAvoidNestedBlocksSwitch2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.checkstyle.suppressionxpathfilter.avoidnestedblocks; | ||
|
||
public class SuppressionXpathRegressionAvoidNestedBlocksSwitch2 { | ||
|
||
int s(int a) { | ||
int x; | ||
int y; | ||
switch (a) { | ||
case 0: { // warn: break outside block | ||
x = 0; | ||
y = 0; | ||
} | ||
break; | ||
case 1: | ||
x = 1; | ||
{ // warn: statement outside block | ||
y = -1; | ||
break; | ||
} | ||
case 2: { // ok: allowInSwitchCase=true | ||
x = 2; | ||
y = -2; | ||
break; | ||
} | ||
default: | ||
x = 3; | ||
y = -3; | ||
} | ||
return x + y; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ionxpathfilter/avoidnestedblocks/SuppressionXpathRegressionAvoidNestedBlocksVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.checkstyle.suppressionxpathfilter.avoidnestedblocks; | ||
|
||
public class SuppressionXpathRegressionAvoidNestedBlocksVariable { | ||
|
||
void varAssign() { | ||
int whichIsWhich = 0; | ||
{ // warn | ||
whichIsWhich = 2; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters