StandardComment.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.TextUtils;
4
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
5
import org.docx4j.wml.*;
6
import org.docx4j.wml.R.CommentReference;
7
import pro.verron.officestamper.api.Comment;
8
import pro.verron.officestamper.api.Placeholder;
9
10
import java.math.BigInteger;
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.stream.Collectors;
16
17
import static java.util.stream.Collectors.joining;
18
import static pro.verron.officestamper.utils.WmlFactory.*;
19
20
/**
21
 * <p>CommentWrapper class.</p>
22
 *
23
 * @author Joseph Verron
24
 * @author Tom Hombergs
25
 * @version ${version}
26
 * @since 1.0.2
27
 */
28
public class StandardComment
29
        implements Comment {
30
    private final Set<Comment> children = new HashSet<>();
31
    private final WordprocessingMLPackage document;
32
    private Comments.Comment comment;
33
    private CommentRangeStart commentRangeStart;
34
    private CommentRangeEnd commentRangeEnd;
35
    private CommentReference commentReference;
36
37
    /**
38
     * Constructs a new StandardComment object.
39
     *
40
     * @param document the WordprocessingMLPackage document instance
41
     */
42
    public StandardComment(WordprocessingMLPackage document) {
43
        this.document = document;
44
    }
45
46
    /**
47
     * Creates a new instance of a StandardComment and initializes its properties
48
     * including the comment, comment range start, comment range end, and comment reference.
49
     *
50
     * @param document    the WordprocessingMLPackage document where the comment will be created
51
     * @param parent      the parent element (P) to which the comment belongs
52
     * @param placeholder the placeholder containing the content for the comment
53
     * @param id          the unique identifier for the comment
54
     * @return a fully initialized StandardComment object
55
     */
56
    public static StandardComment create(
57
            WordprocessingMLPackage document,
58
            P parent,
59
            Placeholder placeholder,
60
            BigInteger id
61
    ) {
62
        var commentWrapper = new StandardComment(document);
63 1 1. create : removed call to pro/verron/officestamper/core/StandardComment::setComment → SURVIVED
        commentWrapper.setComment(newComment(id, placeholder.content()));
64 1 1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeStart → KILLED
        commentWrapper.setCommentRangeStart(newCommentRangeStart(id, parent));
65 1 1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeEnd → KILLED
        commentWrapper.setCommentRangeEnd(newCommentRangeEnd(id, parent));
66 1 1. create : removed call to pro/verron/officestamper/core/StandardComment::setCommentReference → SURVIVED
        commentWrapper.setCommentReference(newCommentReference(id, parent));
67 1 1. create : replaced return value with null for pro/verron/officestamper/core/StandardComment::create → KILLED
        return commentWrapper;
68
    }
69
70
    @Override public String toString() {
71 1 1. toString : replaced return value with "" for pro/verron/officestamper/core/StandardComment::toString → NO_COVERAGE
        return "StandardComment{comment={id=%s, content=%s, children=%s}}}".formatted(comment.getId(),
72
                comment.getContent()
73
                       .stream()
74
                       .map(TextUtils::getText)
75
                       .collect(Collectors.joining(",")),
76
                children.size());
77
    }
78
79
    @Override public Placeholder asPlaceholder() {
80
        String string = this.getComment()
81
                            .getContent()
82
                            .stream()
83
                            .filter(P.class::isInstance)
84
                            .map(P.class::cast)
85 1 1. lambda$asPlaceholder$0 : replaced return value with null for pro/verron/officestamper/core/StandardComment::lambda$asPlaceholder$0 → KILLED
                            .map(p -> StandardParagraph.from(new TextualDocxPart(document), p))
86
                            .map(StandardParagraph::asString)
87
                            .collect(joining());
88 1 1. asPlaceholder : replaced return value with null for pro/verron/officestamper/core/StandardComment::asPlaceholder → KILLED
        return Placeholders.raw(string);
89
    }
90
91
    @Override public ContentAccessor getParent() {
92 1 1. getParent : replaced return value with null for pro/verron/officestamper/core/StandardComment::getParent → KILLED
        return DocumentUtil.findSmallestCommonParent(getCommentRangeStart(), getCommentRangeEnd());
93
    }
94
95
    @Override public List<Object> getElements() {
96
        List<Object> elements = new ArrayList<>();
97
        boolean startFound = false;
98
        boolean endFound = false;
99
        var siblings = getParent().getContent();
100
        for (Object element : siblings) {
101 2 1. getElements : negated conditional → KILLED
2. getElements : negated conditional → KILLED
            startFound = startFound || DocumentUtil.depthElementSearch(getCommentRangeStart(), element);
102 2 1. getElements : negated conditional → KILLED
2. getElements : negated conditional → KILLED
            if (startFound && !endFound) elements.add(element);
103 2 1. getElements : negated conditional → KILLED
2. getElements : negated conditional → KILLED
            endFound = endFound || DocumentUtil.depthElementSearch(getCommentRangeEnd(), element);
104
        }
105 1 1. getElements : replaced return value with Collections.emptyList for pro/verron/officestamper/core/StandardComment::getElements → KILLED
        return elements;
106
    }
107
108
    @Override public CommentRangeEnd getCommentRangeEnd() {
109 1 1. getCommentRangeEnd : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeEnd → KILLED
        return commentRangeEnd;
110
    }
111
112
    /**
113
     * Sets the comment range end for the current comment.
114
     *
115
     * @param commentRangeEnd the {@link CommentRangeEnd} object representing the end of the comment range
116
     */
117
    public void setCommentRangeEnd(CommentRangeEnd commentRangeEnd) {
118
        this.commentRangeEnd = commentRangeEnd;
119
    }
120
121
    /**
122
     * <p>Getter for the field <code>commentRangeStart</code>.</p>
123
     *
124
     * @return a {@link CommentRangeStart} object
125
     */
126
    @Override public CommentRangeStart getCommentRangeStart() {
127 1 1. getCommentRangeStart : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeStart → KILLED
        return commentRangeStart;
128
    }
129
130
    /**
131
     * Sets the starting point of the comment range for the current comment.
132
     *
133
     * @param commentRangeStart the {@link CommentRangeStart} object representing the beginning of the comment range
134
     */
135
    public void setCommentRangeStart(CommentRangeStart commentRangeStart) {
136
        this.commentRangeStart = commentRangeStart;
137
    }
138
139
    @Override public CommentReference getCommentReference() {
140 1 1. getCommentReference : replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentReference → KILLED
        return commentReference;
141
    }
142
143
    /**
144
     * Sets the comment reference for the current comment.
145
     *
146
     * @param commentReference the {@link CommentReference} object to associate with this comment
147
     */
148
    public void setCommentReference(CommentReference commentReference) {
149
        this.commentReference = commentReference;
150
    }
151
152
    @Override public Set<Comment> getChildren() {
153 1 1. getChildren : replaced return value with Collections.emptySet for pro/verron/officestamper/core/StandardComment::getChildren → KILLED
        return children;
154
    }
155
156
    /**
157
     * Sets the children of the comment by adding all elements from the provided set
158
     * to the existing children set.
159
     *
160
     * @param children the set of {@link Comment} objects to be added as children
161
     */
162
    public void setChildren(Set<Comment> children) {
163
        this.children.addAll(children);
164
    }
165
166
    @Override public Comments.Comment getComment() {
167 1 1. getComment : replaced return value with null for pro/verron/officestamper/core/StandardComment::getComment → KILLED
        return comment;
168
    }
169
170
    /**
171
     * Sets the comment for the current StandardComment.
172
     *
173
     * @param comment the {@link Comments.Comment} object to associate with this StandardComment
174
     */
175
    public void setComment(Comments.Comment comment) {
176
        this.comment = comment;
177
    }
178
179
    @Override public WordprocessingMLPackage getDocument() {
180 1 1. getDocument : replaced return value with null for pro/verron/officestamper/core/StandardComment::getDocument → KILLED
        return document;
181
    }
182
183
}

Mutations

63

1.1
Location : create
Killed by : none
removed call to pro/verron/officestamper/core/StandardComment::setComment → SURVIVED
Covering tests

64

1.1
Location : create
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeStart → KILLED

65

1.1
Location : create
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
removed call to pro/verron/officestamper/core/StandardComment::setCommentRangeEnd → KILLED

66

1.1
Location : create
Killed by : none
removed call to pro/verron/officestamper/core/StandardComment::setCommentReference → SURVIVED
Covering tests

67

1.1
Location : create
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/core/StandardComment::create → KILLED

71

1.1
Location : toString
Killed by : none
replaced return value with "" for pro/verron/officestamper/core/StandardComment::toString → NO_COVERAGE

85

1.1
Location : lambda$asPlaceholder$0
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/StandardComment::lambda$asPlaceholder$0 → KILLED

88

1.1
Location : asPlaceholder
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/StandardComment::asPlaceholder → KILLED

92

1.1
Location : getParent
Killed by : pro.verron.officestamper.test.RepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatParagraphTest]/[method:shouldAcceptSet()]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getParent → KILLED

101

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

2.2
Location : getElements
Killed by : pro.verron.officestamper.test.RepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatParagraphTest]/[method:shouldAcceptSet()]
negated conditional → KILLED

102

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

2.2
Location : getElements
Killed by : pro.verron.officestamper.test.RepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatParagraphTest]/[method:shouldAcceptSet()]
negated conditional → KILLED

103

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

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

105

1.1
Location : getElements
Killed by : pro.verron.officestamper.test.RepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatParagraphTest]/[method:shouldAcceptSet()]
replaced return value with Collections.emptyList for pro/verron/officestamper/core/StandardComment::getElements → KILLED

109

1.1
Location : getCommentRangeEnd
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeEnd → KILLED

127

1.1
Location : getCommentRangeStart
Killed by : pro.verron.officestamper.test.SpelInjectionTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.SpelInjectionTest]/[test-template:spelInjectionTest(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#2]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentRangeStart → KILLED

140

1.1
Location : getCommentReference
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:#2]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getCommentReference → KILLED

153

1.1
Location : getChildren
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 Collections.emptySet for pro/verron/officestamper/core/StandardComment::getChildren → KILLED

167

1.1
Location : getComment
Killed by : pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.FailOnUnresolvedPlaceholderTest]/[test-template:fails(pro.verron.officestamper.test.ContextFactory)]/[test-template-invocation:#1]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getComment → KILLED

180

1.1
Location : getDocument
Killed by : pro.verron.officestamper.test.RepeatDocPartTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.RepeatDocPartTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#10]
replaced return value with null for pro/verron/officestamper/core/StandardComment::getDocument → KILLED

Active mutators

Tests examined


Report generated by PIT 1.20.0