CommentCollectorWalker.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.TextUtils;
4
import org.docx4j.wml.CommentRangeEnd;
5
import org.docx4j.wml.CommentRangeStart;
6
import org.docx4j.wml.R;
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import pro.verron.officestamper.api.Comment;
10
import pro.verron.officestamper.api.DocxPart;
11
import pro.verron.officestamper.api.OfficeStamperException;
12
13
import java.math.BigInteger;
14
import java.util.*;
15
16
import static java.util.stream.Collectors.joining;
17
import static java.util.stream.Collectors.toSet;
18
19
class CommentCollectorWalker
20
        extends BaseDocumentWalker {
21
    private static final Logger logger = LoggerFactory.getLogger(CommentCollectorWalker.class);
22
    private final DocxPart document;
23
    private final Map<BigInteger, Comment> allComments;
24
    private final Queue<Comment> stack;
25
    private final Map<BigInteger, Comment> rootComments;
26
27
    private CommentCollectorWalker(
28
            DocxPart document,
29
            Map<BigInteger, Comment> rootComments,
30
            Map<BigInteger, Comment> allComments
31
    ) {
32
        super(document);
33
        this.document = document;
34
        this.allComments = allComments;
35
        this.stack = Collections.asLifoQueue(new ArrayDeque<>());
36
        this.rootComments = rootComments;
37
    }
38
39
    static Map<BigInteger, Comment> collectComments(DocxPart docxPart) {
40
        var rootComments = new HashMap<BigInteger, Comment>();
41
        var allComments = new HashMap<BigInteger, Comment>();
42 1 1. collectComments : removed call to pro/verron/officestamper/core/CommentCollectorWalker::walk → KILLED
        new CommentCollectorWalker(docxPart, rootComments, allComments).walk();
43
44
        var commentsPart = docxPart.commentsPart();
45 1 1. collectComments : negated conditional → KILLED
        if (commentsPart == null)
46 1 1. collectComments : replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::collectComments → SURVIVED
            return rootComments;
47
        var comments = CommentUtil.getComments(commentsPart);
48
49
        for (var comment : comments) {
50
            var commentWrapper = allComments.get(comment.getId());
51 1 1. collectComments : negated conditional → KILLED
            if (commentWrapper != null)
52 1 1. collectComments : removed call to pro/verron/officestamper/api/Comment::setComment → KILLED
                commentWrapper.setComment(comment);
53
        }
54 1 1. collectComments : replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::collectComments → KILLED
        return cleanMalformedComments(rootComments);
55
    }
56
57
    private static Map<BigInteger, Comment> cleanMalformedComments(Map<BigInteger, Comment> rootComments) {
58
        Map<BigInteger, Comment> filteredCommentEntries = new HashMap<>();
59
60 1 1. cleanMalformedComments : removed call to java/util/Map::forEach → KILLED
        rootComments.forEach((key, comment) -> {
61 1 1. lambda$cleanMalformedComments$0 : negated conditional → KILLED
            if (isCommentMalformed(comment)) {
62
                var commentContent = getCommentContent(comment);
63
                logger.error("Skipping malformed comment, missing range start and/or range end : {}", commentContent);
64
            }
65
            else {
66
                filteredCommentEntries.put(key, comment);
67 1 1. lambda$cleanMalformedComments$0 : removed call to pro/verron/officestamper/api/Comment::setChildren → SURVIVED
                comment.setChildren(cleanMalformedComments(comment.getChildren()));
68
            }
69
        });
70 1 1. cleanMalformedComments : replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::cleanMalformedComments → KILLED
        return filteredCommentEntries;
71
    }
72
73
    private static Set<Comment> cleanMalformedComments(Set<Comment> children) {
74 1 1. cleanMalformedComments : replaced return value with Collections.emptySet for pro/verron/officestamper/core/CommentCollectorWalker::cleanMalformedComments → SURVIVED
        return children
75
                .stream()
76
                .filter(comment -> {
77 1 1. lambda$cleanMalformedComments$1 : negated conditional → SURVIVED
                    if (isCommentMalformed(comment)) {
78
                        var commentContent = getCommentContent(comment);
79
                        logger.error("Skipping malformed comment, missing range start and/or range end : {}",
80
                                commentContent);
81 1 1. lambda$cleanMalformedComments$1 : replaced boolean return with true for pro/verron/officestamper/core/CommentCollectorWalker::lambda$cleanMalformedComments$1 → NO_COVERAGE
                        return false;
82
                    }
83 1 1. lambda$cleanMalformedComments$1 : removed call to pro/verron/officestamper/api/Comment::setChildren → SURVIVED
                    comment.setChildren(cleanMalformedComments(comment.getChildren()));
84 1 1. lambda$cleanMalformedComments$1 : replaced boolean return with false for pro/verron/officestamper/core/CommentCollectorWalker::lambda$cleanMalformedComments$1 → SURVIVED
                    return true;
85
                })
86
                .collect(toSet());
87
    }
88
89
    private static boolean isCommentMalformed(Comment comment) {
90 2 1. isCommentMalformed : negated conditional → KILLED
2. isCommentMalformed : replaced boolean return with true for pro/verron/officestamper/core/CommentCollectorWalker::isCommentMalformed → KILLED
        return comment.getCommentRangeStart() == null
91 1 1. isCommentMalformed : negated conditional → KILLED
               || comment.getCommentRangeEnd() == null
92 1 1. isCommentMalformed : negated conditional → KILLED
               || comment.getComment() == null;
93
    }
94
95
    private static String getCommentContent(Comment comment) {
96 2 1. getCommentContent : negated conditional → NO_COVERAGE
2. getCommentContent : replaced return value with "" for pro/verron/officestamper/core/CommentCollectorWalker::getCommentContent → NO_COVERAGE
        return comment.getComment() == null
97
                ? "<no content>"
98
                : comment.getComment()
99
                         .getContent()
100
                         .stream()
101
                         .map(TextUtils::getText)
102
                         .collect(joining(""));
103
    }
104
105
    @Override
106
    protected void onCommentRangeStart(CommentRangeStart commentRangeStart) {
107
        Comment comment = allComments.get(commentRangeStart.getId());
108 1 1. onCommentRangeStart : negated conditional → KILLED
        if (comment == null) {
109
            comment = new StandardComment(document.document());
110
            allComments.put(commentRangeStart.getId(), comment);
111 1 1. onCommentRangeStart : negated conditional → KILLED
            if (stack.isEmpty()) {
112
                rootComments.put(commentRangeStart.getId(), comment);
113
            }
114
            else {
115
                stack.peek()
116
                     .getChildren()
117
                     .add(comment);
118
            }
119
        }
120 1 1. onCommentRangeStart : removed call to pro/verron/officestamper/api/Comment::setCommentRangeStart → KILLED
        comment.setCommentRangeStart(commentRangeStart);
121
        stack.add(comment);
122
    }
123
124
    @Override
125
    protected void onCommentRangeEnd(CommentRangeEnd commentRangeEnd) {
126
        Comment comment = allComments.get(commentRangeEnd.getId());
127 1 1. onCommentRangeEnd : negated conditional → KILLED
        if (comment == null)
128
            throw new OfficeStamperException("Found a comment range end before the comment range start !");
129
130 1 1. onCommentRangeEnd : removed call to pro/verron/officestamper/api/Comment::setCommentRangeEnd → KILLED
        comment.setCommentRangeEnd(commentRangeEnd);
131
132 1 1. onCommentRangeEnd : negated conditional → KILLED
        if (stack.isEmpty()) return;
133
134
        var peek = stack.peek();
135 1 1. onCommentRangeEnd : negated conditional → KILLED
        if (peek.equals(comment))
136
            stack.remove();
137
        else throw new OfficeStamperException("Cannot figure which comment contains the other !");
138
    }
139
140
    @Override
141
    protected void onCommentReference(R.CommentReference commentReference) {
142
        Comment comment = allComments.get(commentReference.getId());
143 1 1. onCommentReference : negated conditional → KILLED
        if (comment == null) {
144
            comment = new StandardComment(document.document());
145
            allComments.put(commentReference.getId(), comment);
146
        }
147 1 1. onCommentReference : removed call to pro/verron/officestamper/api/Comment::setCommentReference → KILLED
        comment.setCommentReference(commentReference);
148
    }
149
}

Mutations

42

1.1
Location : collectComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
removed call to pro/verron/officestamper/core/CommentCollectorWalker::walk → KILLED

45

1.1
Location : collectComments
Killed by : pro.verron.officestamper.test.ResolutionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ResolutionTest]/[test-template:testCustomResolution(java.lang.String, boolean, boolean, boolean, boolean, boolean, java.lang.String, boolean, java.lang.String)]/[test-template-invocation:#17]
negated conditional → KILLED

46

1.1
Location : collectComments
Killed by : none
replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::collectComments → SURVIVED
Covering tests

51

1.1
Location : collectComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

52

1.1
Location : collectComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
removed call to pro/verron/officestamper/api/Comment::setComment → KILLED

54

1.1
Location : collectComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::collectComments → KILLED

60

1.1
Location : cleanMalformedComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
removed call to java/util/Map::forEach → KILLED

61

1.1
Location : lambda$cleanMalformedComments$0
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

67

1.1
Location : lambda$cleanMalformedComments$0
Killed by : none
removed call to pro/verron/officestamper/api/Comment::setChildren → SURVIVED
Covering tests

70

1.1
Location : cleanMalformedComments
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
replaced return value with Collections.emptyMap for pro/verron/officestamper/core/CommentCollectorWalker::cleanMalformedComments → KILLED

74

1.1
Location : cleanMalformedComments
Killed by : none
replaced return value with Collections.emptySet for pro/verron/officestamper/core/CommentCollectorWalker::cleanMalformedComments → SURVIVED
Covering tests

77

1.1
Location : lambda$cleanMalformedComments$1
Killed by : none
negated conditional → SURVIVED
Covering tests

81

1.1
Location : lambda$cleanMalformedComments$1
Killed by : none
replaced boolean return with true for pro/verron/officestamper/core/CommentCollectorWalker::lambda$cleanMalformedComments$1 → NO_COVERAGE

83

1.1
Location : lambda$cleanMalformedComments$1
Killed by : none
removed call to pro/verron/officestamper/api/Comment::setChildren → SURVIVED
Covering tests

84

1.1
Location : lambda$cleanMalformedComments$1
Killed by : none
replaced boolean return with false for pro/verron/officestamper/core/CommentCollectorWalker::lambda$cleanMalformedComments$1 → SURVIVED
Covering tests

90

1.1
Location : isCommentMalformed
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

2.2
Location : isCommentMalformed
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
replaced boolean return with true for pro/verron/officestamper/core/CommentCollectorWalker::isCommentMalformed → KILLED

91

1.1
Location : isCommentMalformed
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

92

1.1
Location : isCommentMalformed
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

96

1.1
Location : getCommentContent
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : getCommentContent
Killed by : none
replaced return value with "" for pro/verron/officestamper/core/CommentCollectorWalker::getCommentContent → NO_COVERAGE

108

1.1
Location : onCommentRangeStart
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

111

1.1
Location : onCommentRangeStart
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

120

1.1
Location : onCommentRangeStart
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
removed call to pro/verron/officestamper/api/Comment::setCommentRangeStart → KILLED

127

1.1
Location : onCommentRangeEnd
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:doesNotFail()]
negated conditional → KILLED

130

1.1
Location : onCommentRangeEnd
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
removed call to pro/verron/officestamper/api/Comment::setCommentRangeEnd → KILLED

132

1.1
Location : onCommentRangeEnd
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#4]
negated conditional → KILLED

135

1.1
Location : onCommentRangeEnd
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:doesNotFail()]
negated conditional → KILLED

143

1.1
Location : onCommentReference
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[method:fails()]
negated conditional → KILLED

147

1.1
Location : onCommentReference
Killed by : pro.verron.officestamper.test.DefaultTests.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.DefaultTests]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#30]
removed call to pro/verron/officestamper/api/Comment::setCommentReference → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0