forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
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 IT Regression test for SuperFinalize
- Loading branch information
Showing
7 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionSuperFinalizeTest.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,129 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code and other text files for adherence to a set of rules. | ||
// Copyright (C) 2001-2024 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.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.coding.SuperFinalizeCheck; | ||
import static com.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck.MSG_KEY; | ||
|
||
public class XpathRegressionSuperFinalizeTest extends AbstractXpathTestSupport { | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return SuperFinalizeCheck.class.getSimpleName(); | ||
} | ||
|
||
@Test | ||
public void testSuperFinalizeNoSuperFinalize() throws Exception { | ||
final String fileName = "SuppressionXpathRegressionSuperFinalizeNoSuperFinalize.java"; | ||
final File fileToProcess = new File(getPath(fileName)); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(SuperFinalizeCheck.class); | ||
|
||
final String[] expectedViolations = { | ||
"6:17: " + | ||
getCheckMessage(SuperFinalizeCheck.class, MSG_KEY), | ||
}; | ||
|
||
final List<String> expectedXpathQuery = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" + | ||
"[@text='SuppressionXpathRegressionSuperFinalizeNoSuperFinalize']]" + | ||
"/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, | ||
expectedViolations, expectedXpathQuery); | ||
} | ||
|
||
@Test | ||
public void testSuperFinalizeInnerFinalize() throws Exception { | ||
final String fileName = "SuppressionXpathRegressionSuperFinalizeInnerFinalize.java"; | ||
final File fileToProcess = new File(getPath(fileName)); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(SuperFinalizeCheck.class); | ||
|
||
final String[] expectedViolations = { | ||
"4:17: " + | ||
getCheckMessage(SuperFinalizeCheck.class, MSG_KEY), | ||
}; | ||
|
||
final List<String> expectedXpathQuery = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" + | ||
"[@text='SuppressionXpathRegressionSuperFinalizeInnerFinalize']]" + | ||
"/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, | ||
expectedViolations, expectedXpathQuery); | ||
} | ||
|
||
@Test | ||
public void testSuperFinalizeOverrideClass() throws Exception { | ||
final String fileName = "SuppressionXpathRegressionSuperFinalizeOverrideClass.java"; | ||
final File fileToProcess = new File(getPath(fileName)); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(SuperFinalizeCheck.class); | ||
|
||
final String[] expectedViolations = { | ||
"5:20: " + | ||
getCheckMessage(SuperFinalizeCheck.class, MSG_KEY), | ||
}; | ||
|
||
final List<String> expectedXpathQuery = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" + | ||
"[@text='SuppressionXpathRegressionSuperFinalizeOverrideClass']]" + | ||
"/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, | ||
expectedViolations, expectedXpathQuery); | ||
} | ||
|
||
@Test | ||
public void testSuperFinalizeMethodReference() throws Exception { | ||
final String fileName = "SuppressionXpathRegressionSuperFinalizeMethodReference.java"; | ||
final File fileToProcess = new File(getPath(fileName)); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(SuperFinalizeCheck.class); | ||
|
||
final String[] expectedViolations = { | ||
"17:20:" + | ||
getCheckMessage(SuperFinalizeCheck.class, MSG_KEY), | ||
}; | ||
|
||
final List<String> expectedXpathQuery = Arrays.asList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='ClassWithFinalizer']]" + | ||
"/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, | ||
expectedViolations, expectedXpathQuery); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...essionxpathfilter/superfinalize/SuppressionXpathRegressionSuperFinalizeInnerFinalize.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,14 @@ | ||
package org.checkstyle.suppressionxpathfilter.superfinalize; | ||
|
||
public class SuppressionXpathRegressionSuperFinalizeInnerFinalize { | ||
public void finalize() // violation | ||
{ | ||
class Inner | ||
{ | ||
public void finalize() throws Throwable | ||
{ | ||
super.finalize(); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...sionxpathfilter/superfinalize/SuppressionXpathRegressionSuperFinalizeMethodReference.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,19 @@ | ||
package org.checkstyle.suppressionxpathfilter.superfinalize; | ||
|
||
public class SuppressionXpathRegressionSuperFinalizeMethodReference extends ClassWithFinalizer { | ||
|
||
public interface CheckedConsumer<E extends Throwable> { | ||
void get() throws E; | ||
} | ||
|
||
@Override | ||
protected void finalize() throws Throwable { | ||
CheckedConsumer<Throwable> r = super::finalize; | ||
r.get(); | ||
} | ||
} | ||
|
||
class ClassWithFinalizer { | ||
protected void finalize() throws Throwable { // violation | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...sionxpathfilter/superfinalize/SuppressionXpathRegressionSuperFinalizeNoSuperFinalize.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,58 @@ | ||
package org.checkstyle.suppressionxpathfilter.superfinalize; | ||
|
||
|
||
public class SuppressionXpathRegressionSuperFinalizeNoSuperFinalize | ||
{ | ||
public void finalize() // violation | ||
{ | ||
} | ||
} | ||
|
||
class InputSuperFinalizeVariations | ||
{ | ||
public InputSuperFinalizeVariations() throws Throwable | ||
{ | ||
super.equals(new String()); | ||
super.finalize(); | ||
} | ||
|
||
public void finalize() /* comment test */ throws Throwable | ||
{ | ||
super.finalize(); | ||
} | ||
|
||
public void method() throws Throwable | ||
{ | ||
super.finalize(); | ||
} | ||
|
||
{ | ||
super.finalize(); | ||
} | ||
} | ||
|
||
//Check that super keyword isn't snagged here | ||
class MyClassWithGenericSuperMethod1 | ||
{ | ||
void someMethod(java.util.List<? super java.util.Map> l) | ||
{ | ||
|
||
} | ||
} | ||
|
||
class TestNative { | ||
public native void finalize(); | ||
} | ||
|
||
class InputOneMore { | ||
|
||
public void doSmt() throws Throwable { | ||
{ | ||
{ | ||
super.finalize(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
12 changes: 12 additions & 0 deletions
12
...essionxpathfilter/superfinalize/SuppressionXpathRegressionSuperFinalizeOverrideClass.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,12 @@ | ||
package org.checkstyle.suppressionxpathfilter.superfinalize; | ||
|
||
public class SuppressionXpathRegressionSuperFinalizeOverrideClass extends FinalizeWithArgs { | ||
@Override | ||
protected void finalize() throws Throwable { // violation | ||
super.finalize(new Object()); | ||
} | ||
} | ||
|
||
class FinalizeWithArgs { | ||
public void finalize(Object a) {}; | ||
} |
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
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