Skip to content

Commit

Permalink
Issue checkstyle#13809: Kill mutation in PackageObjectFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin222004 committed Nov 3, 2023
1 parent 1545427 commit 171c0d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
18 changes: 0 additions & 18 deletions config/pitest-suppressions/pitest-common-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@
<lineContent>this.args = null;</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>PackageObjectFactory.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.PackageObjectFactory</mutatedClass>
<mutatedMethod>createModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to java/lang/String::contains</description>
<lineContent>if (!name.contains(PACKAGE_SEPARATOR)) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>PackageObjectFactory.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.PackageObjectFactory</mutatedClass>
<mutatedMethod>createModule</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
<description>removed conditional - replaced equality check with true</description>
<lineContent>if (!name.contains(PACKAGE_SEPARATOR)) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>PackageObjectFactory.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.PackageObjectFactory</mutatedClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,35 @@ public void testCreateObjectWithNameContainingPackageSeparator() throws Exceptio
.isInstanceOf(MockClass.class);
}

/**
* This test case is designed to verify the behavior of the PackageObjectFactory's
* createModule method when it is provided with a fully qualified class name
* (containing a package separator).
* It ensures that ModuleReflectionUtil.getCheckstyleModules is not executed in this case.
*/
@Test
public void testCreateObjectWithNameContainingPackageSeparatorWithoutSearch() throws Exception {
final ClassLoader classLoader = ClassLoader.getSystemClassLoader();
final Set<String> packages = Collections.singleton(BASE_PACKAGE);
final PackageObjectFactory objectFactory =
new PackageObjectFactory(packages, classLoader, TRY_IN_ALL_REGISTERED_PACKAGES);

try (MockedStatic<ModuleReflectionUtil> utilities =
mockStatic(ModuleReflectionUtil.class)) {
utilities.when(() -> ModuleReflectionUtil.getCheckstyleModules(packages, classLoader))
.thenThrow(new IllegalStateException("creation of objects by fully qualified"
+ " class names should not result in search of modules in classpath"));

final String fullyQualifiedName = MockClass.class.getName();
assertWithMessage("class name is not in expected format")
.that(fullyQualifiedName).contains(".");
final Object object = objectFactory.createModule(fullyQualifiedName);
assertWithMessage("Object should be an instance of MockClass")
.that(object)
.isInstanceOf(MockClass.class);
}
}

@Test
public void testCreateModuleWithTryInAllRegisteredPackages() {
final ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Expand Down

0 comments on commit 171c0d1

Please sign in to comment.