Skip to content

Issue #13109: Kill mutation for RedundantImportCheck#13249

Merged
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:RDC
Jun 19, 2023
Merged

Issue #13109: Kill mutation for RedundantImportCheck#13249
romani merged 1 commit intocheckstyle:masterfrom
Kevin222004:RDC

Conversation

@Kevin222004
Copy link
Copy Markdown
Contributor

@Kevin222004 Kevin222004 commented Jun 17, 2023

Issue #13109: Kill mutation for RedundantImportCheck


check

https://checkstyle.org/config_imports.html#RedundantImport


Mutation

<mutation unstable="false">
<sourceFile>RedundantImportCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck</mutatedClass>
<mutatedMethod>beginTree</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator</mutator>
<description>Removed assignment to member variable pkgName</description>
<lineContent>pkgName = null;</lineContent>
</mutation>


Explaintaion

Test cases added

@Kevin222004 Kevin222004 changed the title Rdc Issue #13109: Kill mutation for RedundantImportCheck Jun 17, 2023
Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items


*/

import com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain why we need this import.
My concern is extension of suppression of id="noCheckstyleInInputs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For config.xml

kevin@inspiron-15-5510:~/Desktop/check_style$ cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>
    <property name="severity" value="warning"/>
    <property name="haltOnException" value="false"/>

    <module name="TreeWalker">
        <module name="RedundantImport"></module>
    </module>
</module>

If the import is from the same package and if the name of the package has not been mentioned then we don't log a violation.

kevin@inspiron-15-5510:~/Desktop/check_style$ cat InputRedundantImportWithoutPackage.java 
import com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*;
import java.io.*;
import java.lang.*; // violation 'Redundant import from the java.lang package - java.lang.*'
import java.lang.String; // violation 'Redundant import from the java.lang package - java.lang.String'

public class InputRedundantImportWithoutPackage {
}

-------------------------

kevin@inspiron-15-5510:~/Desktop/check_style$ java -jar /home/kevin/Downloads/checkstyle-10.12.0-all.jar -c config.xml InputRedundantImportWithoutPackage.java 
Starting audit...
[WARN] /home/kevin/Desktop/check_style/InputRedundantImportWithoutPackage.java:3:1: Redundant import from the java.lang package - java.lang.*. [RedundantImport]
[WARN] /home/kevin/Desktop/check_style/InputRedundantImportWithoutPackage.java:4:1: Redundant import from the java.lang package - java.lang.String. [RedundantImport]
Audit done.
kevin@inspiron-15-5510:~/Desktop/check_style$ 

If the import is from the same package and if the name of the package is mentioned in the java file, then only we log a violation.

kevin@inspiron-15-5510:~/Desktop/check_style$ cat InputRedundantImportWithoutPackage.java 
package com.puppycrawl.tools.checkstyle.checks.imports.redundantimport;

import com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*; // violation
import java.io.*;
import java.lang.*; // violation 'Redundant import from the java.lang package - java.lang.*'
import java.lang.String; // violation 'Redundant import from the java.lang package - java.lang.String'

public class InputRedundantImportWithoutPackage {
}

----------------

kevin@inspiron-15-5510:~/Desktop/check_style$ java -jar /home/kevin/Downloads/checkstyle-10.12.0-all.jar -c config.xml InputRedundantImportWithoutPackage.java 
Starting audit...
[WARN] /home/kevin/Desktop/check_style/InputRedundantImportWithoutPackage.java:3:1: Redundant import from the same package - com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.*. [RedundantImport]
[WARN] /home/kevin/Desktop/check_style/InputRedundantImportWithoutPackage.java:5:1: Redundant import from the java.lang package - java.lang.*. [RedundantImport]
[WARN] /home/kevin/Desktop/check_style/InputRedundantImportWithoutPackage.java:6:1: Redundant import from the java.lang package - java.lang.String. [RedundantImport]
Audit done.

know the code logic is

else if (pkgName != null && isFromPackage(importText, pkgName)) {
log(ast, MSG_SAME, importText);

over here if the package name is not null and the import text is similar to the package, then only it will log the violation when the package name is null then it won't execute

know in my test case the first file execute which has declared the name of the package, so the field pkgName will updated

/** Name of package in file. */
private String pkgName;

know when sequentially second file would be passed
where

public void beginTree(DetailAST aRootAST) {
pkgName = null;
imports.clear();
staticImports.clear();
}

will set to be null and in the second file I mentioned the import with a similar package name but I don't have set the name of the package, so the package is null there, so no violation is expected on that line as I mentioned above.

but if we remove pkgName = null from begin tree then the code logic will continue with previously set package name so here in this file even though there is no package name but import name is similar to the package name so it will cause a violation so here the usage of pkgName=null come in usage and mutation killed which is in begin tree so there is no other possible test case other then this.

so unfortunately I have to suppress the id="noCheckstyleInInputs

else we got error of

[ERROR] [checkstyle] [ERROR] /home/kevin/Desktop/check_style/checkstyle/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/redundantimport/InputRedundantImportWithoutPackage.java:7: Inputs should not reference checkstyles own files [noCheckstyleInInputs]
[INFO] ------------------------------------------------------------------------

Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants