Skip to content

minor: Kill Mutation for AbstractHeaderCheck#13268

Closed
Kevin222004 wants to merge 1 commit intocheckstyle:masterfrom
Kevin222004:ABC2
Closed

minor: Kill Mutation for AbstractHeaderCheck#13268
Kevin222004 wants to merge 1 commit intocheckstyle:masterfrom
Kevin222004:ABC2

Conversation

@Kevin222004
Copy link
Copy Markdown
Contributor

@Kevin222004 Kevin222004 commented Jun 20, 2023

minor: Kill Mutation for AbstractHeaderCheck

for #13269

@Kevin222004 Kevin222004 changed the title minor: Kill Mutation for HeaderRegexp minor: Kill Mutation for AbstractHeaderCheck Jun 20, 2023
checker.setCharset("US-ASCII");
final String[] expected = {
"1: " + getCheckMessage(MSG_HEADER_MISMATCH, "// some.class.тест.passed"),
};
Copy link
Copy Markdown
Contributor Author

@Kevin222004 Kevin222004 Jun 20, 2023

Choose a reason for hiding this comment

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

@romani in the same file I have created 2 test cases with name testCharsetProperty and testCharsetProperty2
To show that the setter in AbstractHeaderCheck is useless.

In first test case testCharsetProperty I have set the property charset in the parent module checker.
hence you can observe the violation
"1: " + getCheckMessage(MSG_HEADER_MISMATCH, "// some.class.тест.passed"),

and in second Test case testCharsetProperty2 where property is setted in check it self
and the message we got
"1: " + getCheckMessage(MSG_HEADER_MISMATCH, "// some.class.��������.passed"),

so I feel that we are not setting any property in check it is been setting up in checker only.

In the main pr at #13269
you can see I have removed that.

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.

I am not sure about this that why this is happening, I have currently removed the setter from the main pr.

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.

@romani please give a look

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 create same testing by CLI, to make sure we do all as users and system works as expected.
in junit we always need to be accurate as we might miss some actions that actual system is doing.
We did propogation of such properties to all children modules.

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.

kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat config.header 
// some.class.тест.passed
kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat header.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="US-ASCII" />
        <module name="RegexpHeader">
                <property name="headerFile" value="config.header"/>
        </module>
</module>
kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat Test2.java 
// some.class

public class Test2 {
}
kevin@inspiron-15-5510:~/Desktop/check_style/header$ 
kevin@inspiron-15-5510:~/Desktop/check_style/header$ java -jar /home/kevin/Downloads/checkstyle-10.12.1-all.jar -c header.xml Test2.java
Starting audit...
[ERROR] /home/kevin/Desktop/check_style/header/Test2.java:1: Line does not match expected header line of '// some.class.��������.passed'. [RegexpHeader]
Audit done.
Checkstyle ends with 1 errors.

kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat config.header 
// some.class.тест.passed
kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat Test2.java 
// some.class

public class Test2 {
}
kevin@inspiron-15-5510:~/Desktop/check_style/header$ cat header2.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="US-ASCII" /> -->
        <module name="RegexpHeader">
                <property name="charset" value="US-ASCII" />
                <property name="headerFile" value="config.header"/>
        </module>
</module>

kevin@inspiron-15-5510:~/Desktop/check_style/header$ java -jar /home/kevin/Downloads/checkstyle-10.12.1-all.jar -c header2.xml Test2.java
Starting audit...
[ERROR] /home/kevin/Desktop/check_style/header/Test2.java:1: Line does not match expected header line of '// some.class.��������.passed'. [RegexpHeader]
Audit done.
Checkstyle ends with 1 errors.
kevin@inspiron-15-5510:~/Desktop/check_style/header$ 

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.

@romani looks like with cli non of them is working

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.

If we drop non ASCII symbols, expected match is // some.class..passed
Please create Input based test method to see what is actual string for match.

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.

@Kevin222004 , ping

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.

@romani I have some misconceptions on this pr.

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.

@Kevin222004 , please update this PR to have mutation removal, I lost a point of this PR.

@Kevin222004
Copy link
Copy Markdown
Contributor Author

I have updated the pr and updated the test as well and they are working as expected. тест this text will be accepted by charset UTF_8 and not expected by US-ASCII

Just for mutation purpose the mutation on setCharset is killed

<mutation unstable="false">
<sourceFile>AbstractHeaderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck</mutatedClass>
<mutatedMethod>setCharset</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator</mutator>
<description>Removed assignment to member variable charset</description>
<lineContent>this.charset = createCharset(charset);</lineContent>
</mutation>

But still I believe that, the actuall testing of this property is been done when the issue #13392
will be resolved and we set some different charset in checker and some other charset in our check.

@Kevin222004
Copy link
Copy Markdown
Contributor Author

Kevin222004 commented Jul 21, 2023

Rest all the mutations

<suppressedMutations>
<mutation unstable="false">
<sourceFile>AbstractHeaderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck</mutatedClass>
<mutatedMethod>&lt;init&gt;</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to java/nio/charset/Charset::name</description>
<lineContent>StandardCharsets.UTF_8.name()));</lineContent>
</mutation>
<mutation unstable="false">
<sourceFile>AbstractHeaderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck</mutatedClass>
<mutatedMethod>&lt;init&gt;</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck::createCharset</description>
<lineContent>private Charset charset = createCharset(System.getProperty(&quot;file.encoding&quot;,</lineContent>
</mutation>
<mutation unstable="false">
<sourceFile>AbstractHeaderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck</mutatedClass>
<mutatedMethod>&lt;init&gt;</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
<description>replaced call to java/lang/System::getProperty with argument</description>
<lineContent>private Charset charset = createCharset(System.getProperty(&quot;file.encoding&quot;,</lineContent>
</mutation>
<mutation unstable="false">
<sourceFile>AbstractHeaderCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.header.AbstractHeaderCheck</mutatedClass>
<mutatedMethod>&lt;init&gt;</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator</mutator>
<description>Removed assignment to member variable charset</description>
<lineContent>private Charset charset = createCharset(System.getProperty(&quot;file.encoding&quot;,</lineContent>
</mutation>

are on the line

private Charset charset = createCharset(System.getProperty("file.encoding",
StandardCharsets.UTF_8.name()));

In my opinion this is by default UTF_8 so we can simple Change this as
private Charset charset;

Also suggested at #13269 (comment)

@Kevin222004
Copy link
Copy Markdown
Contributor Author

@romani
Copy link
Copy Markdown
Member

romani commented Jul 30, 2023

I confirm that
initialization

private Charset charset = createCharset(System.getProperty("file.encoding",
StandardCharsets.UTF_8.name()));
should be removed as we set all charsets from Checker:
private String charset = StandardCharsets.UTF_8.name();

and populated further to all modules by context:
final DefaultContext context = new DefaultContext();
context.add("charset", charset);
context.add("moduleFactory", moduleFactory);
context.add("severity", severity.getName());
context.add("basedir", basedir);
context.add("tabWidth", String.valueOf(tabWidth));
childContext = context;
}

so after module instantiation, this Check use charset from its itialization, but it instantly changed by setter to what ever is Checker/context.

here is stacktrace (I just executed HeaderCheckTest.testInvalidCharset):
image

setCharset is required, but initialzation in class field is not.

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