Skip to content

Commit 56af761

Browse files
Kevin222004romani
authored andcommitted
Issue #13672: Kill mutation using utility class
1 parent 351bbc1 commit 56af761

File tree

9 files changed

+33
-52
lines changed

9 files changed

+33
-52
lines changed

config/pitest-suppressions/pitest-api-suppressions.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99
<lineContent>charset = null;</lineContent>
1010
</mutation>
1111

12-
<mutation unstable="false">
13-
<sourceFile>Violation.java</sourceFile>
14-
<mutatedClass>com.puppycrawl.tools.checkstyle.api.Violation</mutatedClass>
15-
<mutatedMethod>&lt;init&gt;</mutatedMethod>
16-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
17-
<description>replaced call to java/util/Arrays::copyOf with argument</description>
18-
<lineContent>this.args = Arrays.copyOf(args, args.length);</lineContent>
19-
</mutation>
20-
2112
<mutation unstable="false">
2213
<sourceFile>Violation.java</sourceFile>
2314
<mutatedClass>com.puppycrawl.tools.checkstyle.api.Violation</mutatedClass>

config/pitest-suppressions/pitest-common-2-suppressions.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@
126126
<lineContent>final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH);</lineContent>
127127
</mutation>
128128

129-
<mutation unstable="false">
130-
<sourceFile>XmlLoader.java</sourceFile>
131-
<mutatedClass>com.puppycrawl.tools.checkstyle.XmlLoader</mutatedClass>
132-
<mutatedMethod>&lt;init&gt;</mutatedMethod>
133-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
134-
<description>replaced call to java/util/Map::copyOf with argument</description>
135-
<lineContent>this.publicIdToResourceNameMap = Map.copyOf(publicIdToResourceNameMap);</lineContent>
136-
</mutation>
137-
138129
<mutation unstable="false">
139130
<sourceFile>XmlLoader.java</sourceFile>
140131
<mutatedClass>com.puppycrawl.tools.checkstyle.XmlLoader</mutatedClass>

config/pitest-suppressions/pitest-common-suppressions.xml

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@
126126
<lineContent>final String value = attributes.getValue(VALUE);</lineContent>
127127
</mutation>
128128

129-
<mutation unstable="false">
130-
<sourceFile>LocalizedMessage.java</sourceFile>
131-
<mutatedClass>com.puppycrawl.tools.checkstyle.LocalizedMessage</mutatedClass>
132-
<mutatedMethod>&lt;init&gt;</mutatedMethod>
133-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
134-
<description>replaced call to java/util/Arrays::copyOf with argument</description>
135-
<lineContent>this.args = Arrays.copyOf(args, args.length);</lineContent>
136-
</mutation>
137-
138129
<mutation unstable="false">
139130
<sourceFile>LocalizedMessage.java</sourceFile>
140131
<mutatedClass>com.puppycrawl.tools.checkstyle.LocalizedMessage</mutatedClass>
@@ -278,22 +269,4 @@
278269
<description>replaced call to java/util/Collections::synchronizedList with argument</description>
279270
<lineContent>private final List&lt;Throwable&gt; exceptions = Collections.synchronizedList(new ArrayList&lt;&gt;());</lineContent>
280271
</mutation>
281-
282-
<mutation unstable="false">
283-
<sourceFile>XMLLogger.java</sourceFile>
284-
<mutatedClass>com.puppycrawl.tools.checkstyle.XMLLogger$FileMessages</mutatedClass>
285-
<mutatedMethod>getErrors</mutatedMethod>
286-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
287-
<description>replaced call to java/util/Collections::unmodifiableList with argument</description>
288-
<lineContent>return Collections.unmodifiableList(errors);</lineContent>
289-
</mutation>
290-
291-
<mutation unstable="false">
292-
<sourceFile>XMLLogger.java</sourceFile>
293-
<mutatedClass>com.puppycrawl.tools.checkstyle.XMLLogger$FileMessages</mutatedClass>
294-
<mutatedMethod>getExceptions</mutatedMethod>
295-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator</mutator>
296-
<description>replaced call to java/util/Collections::unmodifiableList with argument</description>
297-
<lineContent>return Collections.unmodifiableList(exceptions);</lineContent>
298-
</mutation>
299272
</suppressedMutations>

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,10 @@
43674367
<!-- kills mutation in ConfigurationLoader -->
43684368
<param>com.sun.checkstyle.test.chapter5comments.rule52documentationcomments.InvalidJavadocPositionTest</param>
43694369
</targetTests>
4370+
<avoidCallsTo>
4371+
<avoidCallsTo>org.apache.commons.logging</avoidCallsTo>
4372+
<avoidCallsTo>com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil</avoidCallsTo>
4373+
</avoidCallsTo>
43704374
<excludedMethods>
43714375
<!-- caching of search results, big performance optimization -->
43724376
<param>lazyLoad</param>
@@ -4692,6 +4696,9 @@
46924696
<param>com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheckTest</param>
46934697
<param>com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheckTest</param>
46944698
</targetTests>
4699+
<avoidCallsTo>
4700+
<avoidCallsTo>com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil</avoidCallsTo>
4701+
</avoidCallsTo>
46954702
<excludedTestClasses>
46964703
<param>*.Input*</param>
46974704
</excludedTestClasses>

src/main/java/com/puppycrawl/tools/checkstyle/LocalizedMessage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
import java.net.URLConnection;
2727
import java.nio.charset.StandardCharsets;
2828
import java.text.MessageFormat;
29-
import java.util.Arrays;
3029
import java.util.Locale;
3130
import java.util.MissingResourceException;
3231
import java.util.PropertyResourceBundle;
3332
import java.util.ResourceBundle;
3433
import java.util.ResourceBundle.Control;
3534

35+
import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
36+
3637
/**
3738
* Represents a message that can be localised. The translations come from
3839
* message.properties files. The underlying implementation uses
@@ -80,7 +81,7 @@ public LocalizedMessage(String bundle, Class<?> sourceClass, String key,
8081
this.args = null;
8182
}
8283
else {
83-
this.args = Arrays.copyOf(args, args.length);
84+
this.args = UnmodifiableCollectionUtil.copyOfArray(args, args.length);
8485
}
8586
}
8687

src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.puppycrawl.tools.checkstyle.api.AuditListener;
3535
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
3636
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
37+
import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
3738

3839
/**
3940
* Simple XML logger.
@@ -362,7 +363,7 @@ private static final class FileMessages {
362363
* @return the file error events.
363364
*/
364365
public List<AuditEvent> getErrors() {
365-
return Collections.unmodifiableList(errors);
366+
return UnmodifiableCollectionUtil.unmodifiableList(errors);
366367
}
367368

368369
/**
@@ -380,7 +381,7 @@ public void addError(AuditEvent event) {
380381
* @return the file exceptions.
381382
*/
382383
public List<Throwable> getExceptions() {
383-
return Collections.unmodifiableList(exceptions);
384+
return UnmodifiableCollectionUtil.unmodifiableList(exceptions);
384385
}
385386

386387
/**

src/main/java/com/puppycrawl/tools/checkstyle/XmlLoader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.xml.sax.XMLReader;
3333
import org.xml.sax.helpers.DefaultHandler;
3434

35+
import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
36+
3537
/**
3638
* Contains the common implementation of a loader, for loading a configuration
3739
* from an XML file.
@@ -65,7 +67,8 @@ public class XmlLoader
6567
*/
6668
protected XmlLoader(Map<String, String> publicIdToResourceNameMap)
6769
throws SAXException, ParserConfigurationException {
68-
this.publicIdToResourceNameMap = Map.copyOf(publicIdToResourceNameMap);
70+
this.publicIdToResourceNameMap =
71+
UnmodifiableCollectionUtil.copyOfMap(publicIdToResourceNameMap);
6972
parser = createXmlReader(this);
7073
}
7174

src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Objects;
2626

2727
import com.puppycrawl.tools.checkstyle.LocalizedMessage;
28+
import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
2829

2930
/**
3031
* Represents a violation that can be localised. The translations come from
@@ -111,7 +112,7 @@ public Violation(int lineNo,
111112
this.args = null;
112113
}
113114
else {
114-
this.args = Arrays.copyOf(args, args.length);
115+
this.args = UnmodifiableCollectionUtil.copyOfArray(args, args.length);
115116
}
116117
this.bundle = bundle;
117118
this.severityLevel = severityLevel;

src/main/java/com/puppycrawl/tools/checkstyle/utils/UnmodifiableCollectionUtil.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.Collections;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.Set;
2627

2728
/**
@@ -63,7 +64,7 @@ public static <T> List<T> unmodifiableList(List<T> collection) {
6364
}
6465

6566
/**
66-
* Creates an copy of array.
67+
* Creates a copy of array.
6768
*
6869
* @param array Array to create a copy of
6970
* @param length length of array
@@ -74,6 +75,18 @@ public static <T> T[] copyOfArray(T[] array, int length) {
7475
return Arrays.copyOf(array, length);
7576
}
7677

78+
/**
79+
* Creates a copy of Map.
80+
*
81+
* @param map map to create a copy of
82+
* @param <K> the type of keys in the map
83+
* @param <V> the type of values in the map
84+
* @return an immutable copy of the input map
85+
*/
86+
public static <K, V> Map<K, V> copyOfMap(Map<? extends K, ? extends V> map) {
87+
return Map.copyOf(map);
88+
}
89+
7790
/**
7891
* Returns an immutable set containing only the specified object.
7992
*

0 commit comments

Comments
 (0)