Skip to content

Commit

Permalink
Issue checkstyle#6207: Add XPath IT Regression test for SuperFinalize
Browse files Browse the repository at this point in the history
  • Loading branch information
CubeTures committed Mar 5, 2024
1 parent 2cc3449 commit 4953591
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 1 deletion.
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);
}
}
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();
}
}
}
}
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
}
}
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();
}
}
}
}


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) {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
@StatelessCheck
public class SuperFinalizeCheck extends AbstractSuperCheck {

public static final String MSG_KEY = "missing.super.call";

@Override
protected String getMethodName() {
return "finalize";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
"ParameterAssignment",
"RedundantModifier",
"SeparatorWrap",
"SuperFinalize",
"SuppressWarnings",
"VisibilityModifier"
);
Expand Down

0 comments on commit 4953591

Please sign in to comment.