DocumentUtil.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.XmlUtils;
4
import org.docx4j.wml.*;
5
import org.jvnet.jaxb2_commons.ppp.Child;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import pro.verron.officestamper.api.OfficeStamperException;
9
10
import static java.util.Collections.emptyList;
11
12
/// Utility class to retrieve elements from a document.
13
///
14
/// @author Joseph Verron
15
/// @author DallanMC
16
/// @version ${version}
17
/// @since 1.4.7
18
public class DocumentUtil {
19
20
    private static final Logger log = LoggerFactory.getLogger(DocumentUtil.class);
21
22
    private DocumentUtil() {
23
        throw new OfficeStamperException("Utility classes shouldn't be instantiated");
24
    }
25
26
    /// Finds the smallest common parent between two objects.
27
    ///
28
    /// @param o1 the first object
29
    /// @param o2 the second object
30
    ///
31
    /// @return the smallest common parent of the two objects
32
    ///
33
    /// @throws OfficeStamperException if there is an error finding the common parent
34
    public static ContentAccessor findSmallestCommonParent(Object o1, Object o2) {
35 2 1. findSmallestCommonParent : negated conditional → KILLED
2. findSmallestCommonParent : negated conditional → KILLED
        if (depthElementSearch(o1, o2) && o2 instanceof ContentAccessor contentAccessor)
36 1 1. findSmallestCommonParent : replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findSmallestCommonParent → KILLED
            return findInsertableParent(contentAccessor);
37 2 1. findSmallestCommonParent : replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findSmallestCommonParent → KILLED
2. findSmallestCommonParent : negated conditional → KILLED
        else if (o2 instanceof Child child) return findSmallestCommonParent(o1, child.getParent());
38
        else throw new OfficeStamperException();
39
    }
40
41
    /// Recursively searches for an element in a content tree.
42
    ///
43
    /// @param searchTarget the element to search for
44
    /// @param searchTree the content tree to search in
45
    ///
46
    /// @return true if the element is found, false otherwise
47
    public static boolean depthElementSearch(Object searchTarget, Object searchTree) {
48
        var element = XmlUtils.unwrap(searchTree);
49 2 1. depthElementSearch : replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED
2. depthElementSearch : negated conditional → KILLED
        if (searchTarget.equals(element)) return true;
50
51
        var contentContent = switch (element) {
52
            case ContentAccessor accessor -> accessor.getContent();
53
            case SdtRun sdtRun -> sdtRun.getSdtContent()
54
                                        .getContent();
55 8 1. depthElementSearch : negated conditional → SURVIVED
2. depthElementSearch : negated conditional → SURVIVED
3. depthElementSearch : negated conditional → SURVIVED
4. depthElementSearch : negated conditional → TIMED_OUT
5. depthElementSearch : negated conditional → TIMED_OUT
6. depthElementSearch : negated conditional → TIMED_OUT
7. depthElementSearch : negated conditional → TIMED_OUT
8. depthElementSearch : negated conditional → TIMED_OUT
            case ProofErr _, Text _, R.CommentReference _, CommentRangeEnd _, CommentRangeStart _, Br _,
56
                 R.LastRenderedPageBreak _, CTBookmark _ -> emptyList();
57
            default -> {
58
                log.warn("Element {} not recognized", element);
59
                yield emptyList();
60
            }
61
        };
62
63 2 1. depthElementSearch : replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED
2. depthElementSearch : replaced boolean return with true for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED
        return contentContent.stream()
64 2 1. lambda$depthElementSearch$0 : replaced boolean return with true for pro/verron/officestamper/core/DocumentUtil::lambda$depthElementSearch$0 → KILLED
2. lambda$depthElementSearch$0 : replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::lambda$depthElementSearch$0 → KILLED
                             .anyMatch(obj -> depthElementSearch(searchTarget, obj));
65
    }
66
67
    private static ContentAccessor findInsertableParent(Object searchFrom) {
68 1 1. findInsertableParent : replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findInsertableParent → KILLED
        return switch (searchFrom) {
69
            case Tc tc -> tc;
70
            case Body body -> body;
71
            case Child child -> findInsertableParent(child.getParent());
72
            default -> throw new OfficeStamperException("Unexpected parent " + searchFrom.getClass());
73
        };
74
    }
75
76
77
78
}

Mutations

35

1.1
Location : findSmallestCommonParent
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
negated conditional → KILLED

2.2
Location : findSmallestCommonParent
Killed by : pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatDocPart_BadPlaceholderTest]/[test-template:testBadExpressionShouldNotBlockCallerThread(pro.verron.officestamper.test.utils.ContextFactory)]/[test-template-invocation:#2]
negated conditional → KILLED

36

1.1
Location : findSmallestCommonParent
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findSmallestCommonParent → KILLED

37

1.1
Location : findSmallestCommonParent
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findSmallestCommonParent → KILLED

2.2
Location : findSmallestCommonParent
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
negated conditional → KILLED

49

1.1
Location : depthElementSearch
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED

2.2
Location : depthElementSearch
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
negated conditional → KILLED

55

1.1
Location : depthElementSearch
Killed by : none
negated conditional → SURVIVED
Covering tests

2.2
Location : depthElementSearch
Killed by : none
negated conditional → TIMED_OUT

3.3
Location : depthElementSearch
Killed by : none
negated conditional → SURVIVED Covering tests

4.4
Location : depthElementSearch
Killed by : none
negated conditional → SURVIVED Covering tests

5.5
Location : depthElementSearch
Killed by : none
negated conditional → TIMED_OUT

6.6
Location : depthElementSearch
Killed by : none
negated conditional → TIMED_OUT

7.7
Location : depthElementSearch
Killed by : none
negated conditional → TIMED_OUT

8.8
Location : depthElementSearch
Killed by : none
negated conditional → TIMED_OUT

63

1.1
Location : depthElementSearch
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED

2.2
Location : depthElementSearch
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with true for pro/verron/officestamper/core/DocumentUtil::depthElementSearch → KILLED

64

1.1
Location : lambda$depthElementSearch$0
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with true for pro/verron/officestamper/core/DocumentUtil::lambda$depthElementSearch$0 → KILLED

2.2
Location : lambda$depthElementSearch$0
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced boolean return with false for pro/verron/officestamper/core/DocumentUtil::lambda$depthElementSearch$0 → KILLED

68

1.1
Location : findInsertableParent
Killed by : pro.verron.officestamper.test.RegressionTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RegressionTests]/[test-template:test52(pro.verron.officestamper.test.RegressionTests$Conditions, java.lang.String)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/DocumentUtil::findInsertableParent → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0