Skip to content

Commit

Permalink
Issue checkstyle#13809: Kill mutation in PackageObjectFactory-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin222004 committed Nov 5, 2023
1 parent 6de140e commit 53898c3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
4 changes: 4 additions & 0 deletions config/checkstyle-input-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
files="[\\/]packageobjectfactory[\\/]foo[\\/]FooCheck.java"/>
<suppress id="noCheckstyleInInputs"
files="[\\/]packageobjectfactory[\\/]foo[\\/]FooCheck.java"/>
<suppress id="noCheckstyleInInputs"
files="[\\/]packageobjectfactory[\\/]abc[\\/]FooCheck.java"/>
<suppress id="noCheckstyleInInputs"
files="[\\/]packageobjectfactory[\\/]zoo[\\/]FooCheck.java"/>
<suppress id="noCheckstyleInInputs"
files="[\\/]meta[\\/]javadocmetadatascraper[\\/]InputJavadocMetadataScraperAbstractSuperCheck.java"/>
<suppress id="noCheckstyleInInputs"
Expand Down
9 changes: 0 additions & 9 deletions config/pitest-suppressions/pitest-common-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@
<lineContent>this.args = null;</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>PackageObjectFactory.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.PackageObjectFactory</mutatedClass>
<mutatedMethod>createObjectFromFullModuleNames</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator</mutator>
<description>replaced call to java/util/stream/Stream::sorted with receiver</description>
<lineContent>.sorted()</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>SarifLogger.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.SarifLogger</mutatedClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,35 @@ public void testCreateModuleWithTryInAllRegisteredPackages() {

}

@Test
public void testExceptionMessage() {
final String barPackage = BASE_PACKAGE + ".packageobjectfactory.bar";
final String fooPackage = BASE_PACKAGE + ".packageobjectfactory.foo";
final String zooPackage = BASE_PACKAGE + ".packageobjectfactory.zoo";
final String abcPackage = BASE_PACKAGE + ".packageobjectfactory.abc";
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final PackageObjectFactory objectFactory = new PackageObjectFactory(
new HashSet<>(Arrays.asList(abcPackage, barPackage,
fooPackage, zooPackage)), classLoader);
final String name = "FooCheck";
try {
objectFactory.createModule(name);
assertWithMessage("Exception is expected").fail();
}
catch (CheckstyleException ex) {
final String optionalNames = abcPackage + PACKAGE_SEPARATOR + name
+ STRING_SEPARATOR + barPackage + PACKAGE_SEPARATOR + name
+ STRING_SEPARATOR + fooPackage + PACKAGE_SEPARATOR + name
+ STRING_SEPARATOR + zooPackage + PACKAGE_SEPARATOR + name;
final LocalizedMessage exceptionMessage = new LocalizedMessage(
Definitions.CHECKSTYLE_BUNDLE, getClass(),
AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, name, optionalNames);
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo(exceptionMessage.getMessage());
}
}

private static final class FailConstructorFileSet extends AbstractFileSetCheck {

private FailConstructorFileSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ private static void verifyInputFile(Map<String, List<String>> allTests, File fil
}

// until https://github.com/checkstyle/checkstyle/issues/5105
if (!path.contains(File.separatorChar + "grammar" + File.separatorChar)
&& !path.contains(File.separatorChar + "foo" + File.separatorChar)
&& !path.contains(File.separatorChar + "bar" + File.separatorChar)) {
if (shouldSkipFileProcessing(path)) {
String fileName = file.getName();
final boolean skipFileNaming = shouldSkipInputFileNameCheck(path, fileName);

Expand Down Expand Up @@ -204,6 +202,20 @@ && checkInputMatchCorrectFileStructure(classes, folderPath, skipFileNaming,
.isTrue();
}

/**
* Checks if the file processing should be skipped based on the path.
*
* @param path The path to check for skip conditions.
* @return true if file processing should be skipped, false otherwise.
*/
private static boolean shouldSkipFileProcessing(String path) {
return !path.contains(File.separatorChar + "grammar" + File.separatorChar)
&& !path.contains(File.separatorChar + "foo" + File.separatorChar)
&& !path.contains(File.separatorChar + "bar" + File.separatorChar)
&& !path.contains(File.separator + "abc" + File.separatorChar)
&& !path.contains(File.separator + "zoo" + File.separatorChar);
}

private static void verifyHasProductionFile(Map<String, List<String>> allTests, File file) {
if (file.isFile()) {
final String fileName = file.getName().replace("Test.java", ".java");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.puppycrawl.tools.checkstyle.packageobjectfactory.abc;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;

public class FooCheck extends AbstractCheck {
@Override
public int[] getDefaultTokens() {
return new int[] {0};
}

@Override
public int[] getAcceptableTokens() {
return getDefaultTokens();
}

@Override
public int[] getRequiredTokens() {
return getDefaultTokens();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.puppycrawl.tools.checkstyle.packageobjectfactory.zoo;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;

public class FooCheck extends AbstractCheck {
@Override
public int[] getDefaultTokens() {
return new int[] {0};
}

@Override
public int[] getAcceptableTokens() {
return getDefaultTokens();
}

@Override
public int[] getRequiredTokens() {
return getDefaultTokens();
}
}

0 comments on commit 53898c3

Please sign in to comment.