RunUtil.java

1
package pro.verron.officestamper.core;
2
3
import jakarta.xml.bind.JAXBElement;
4
import org.docx4j.model.styles.StyleUtil;
5
import org.docx4j.wml.*;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.lang.Nullable;
9
import pro.verron.officestamper.api.OfficeStamperException;
10
11
import java.util.Objects;
12
13
import static java.util.stream.Collectors.joining;
14
import static pro.verron.officestamper.utils.WmlFactory.*;
15
16
/**
17
 * Utility class to handle runs.
18
 *
19
 * @author Joseph Verron
20
 * @author Tom Hombergs
21
 * @version ${version}
22
 * @since 1.0.0
23
 */
24
public class RunUtil {
25
26
27
    private static final String PRESERVE = "preserve";
28
    private static final Logger log = LoggerFactory.getLogger(RunUtil.class);
29
30
    private RunUtil() {
31
        throw new OfficeStamperException("Utility class shouldn't be instantiated");
32
    }
33
34
    /**
35
     * Returns the text string of a run.
36
     *
37
     * @param run the run whose text to get.
38
     *
39
     * @return {@link String} representation of the run.
40
     */
41
    public static String getText(R run) {
42 1 1. getText : replaced return value with "" for pro/verron/officestamper/core/RunUtil::getText → KILLED
        return run.getContent()
43
                  .stream()
44
                  .map(RunUtil::getText)
45
                  .collect(joining());
46
    }
47
48
    /**
49
     * Returns the textual representation of a run child
50
     *
51
     * @param content the run child to represent textually
52
     *
53
     * @return {@link String} representation of run child
54
     */
55
    public static CharSequence getText(Object content) {
56 1 1. getText : replaced return value with null for pro/verron/officestamper/core/RunUtil::getText → KILLED
        return switch (content) {
57
            case JAXBElement<?> jaxbElement -> getText(jaxbElement.getValue());
58
            case Text text -> getText(text);
59
            case R.Tab ignored -> "\t";
60
            case R.Cr ignored -> "\n";
61 1 1. getText : negated conditional → KILLED
            case Br br when br.getType() == null -> "\n";
62 1 1. getText : negated conditional → SURVIVED
            case Br br when br.getType() == STBrType.TEXT_WRAPPING -> "\n";
63 1 1. getText : negated conditional → SURVIVED
            case Br br when br.getType() == STBrType.PAGE -> "\n";
64 1 1. getText : negated conditional → NO_COVERAGE
            case Br br when br.getType() == STBrType.COLUMN -> "\n";
65
            case R.NoBreakHyphen ignored -> "‑";
66
            case R.SoftHyphen ignored -> "\u00AD";
67
            case R.LastRenderedPageBreak ignored -> "";
68
            case R.AnnotationRef ignored -> "";
69
            case R.CommentReference ignored -> "";
70
            case Drawing ignored -> "";
71
            case R.Sym sym -> "<sym(%s, %s)>".formatted(sym.getFont(), sym.getChar());
72
            default -> {
73
                log.debug("Unhandled object type: {}", content.getClass());
74
                yield "";
75
            }
76
        };
77
    }
78
79
    private static CharSequence getText(Text text) {
80
        String value = text.getValue();
81
        String space = text.getSpace();
82 2 1. getText : negated conditional → KILLED
2. getText : replaced return value with null for pro/verron/officestamper/core/RunUtil::getText → KILLED
        return Objects.equals(space, PRESERVE)
83
                ? value // keeps spaces if spaces are to be preserved (LibreOffice seems to ignore the "space" property)
84
                : value.trim(); // trimming value if spaces are not to be preserved (simulates behavior of Word;)
85
    }
86
87
    /**
88
     * Creates a new run with the specified text and inherits the style of the parent paragraph.
89
     *
90
     * @param text the initial text of the run.
91
     *
92
     * @return the newly created run.
93
     */
94
    public static R create(String text, PPr paragraphPr) {
95
        R run = newRun(text);
96 1 1. create : removed call to pro/verron/officestamper/core/RunUtil::applyParagraphStyle → NO_COVERAGE
        applyParagraphStyle(run, paragraphPr);
97 1 1. create : replaced return value with null for pro/verron/officestamper/core/RunUtil::create → NO_COVERAGE
        return run;
98
    }
99
100
    /**
101
     * Applies the style of the given paragraph to the given content object (if the content object is a Run).
102
     *
103
     * @param run the Run to which the style should be applied.
104
     */
105
    public static void applyParagraphStyle(R run, @Nullable PPr paragraphPr) {
106 1 1. applyParagraphStyle : negated conditional → NO_COVERAGE
        if (paragraphPr == null) return;
107
        var runPr = paragraphPr.getRPr();
108 1 1. applyParagraphStyle : negated conditional → NO_COVERAGE
        if (runPr == null) return;
109
        RPr runProperties = new RPr();
110
        StyleUtil.apply(runPr, runProperties);
111 1 1. applyParagraphStyle : removed call to org/docx4j/wml/R::setRPr → NO_COVERAGE
        run.setRPr(runProperties);
112
    }
113
114
    /**
115
     * Sets the text of the given run to the given value.
116
     *
117
     * @param run  the run whose text to change.
118
     * @param text the text to set.
119
     */
120
    public static void setText(R run, String text) {
121
        run.getContent()
122 1 1. setText : removed call to java/util/List::clear → KILLED
           .clear();
123
        Text textObj = newText(text);
124
        run.getContent()
125
           .add(textObj);
126
    }
127
128
    static int getLength(R run) {
129 1 1. getLength : replaced int return with 0 for pro/verron/officestamper/core/RunUtil::getLength → KILLED
        return getText(run)
130
                .length();
131
    }
132
133
    static String getSubstring(R run, int beginIndex) {
134 1 1. getSubstring : replaced return value with "" for pro/verron/officestamper/core/RunUtil::getSubstring → KILLED
        return getText(run)
135
                .substring(beginIndex);
136
    }
137
138
    static String getSubstring(R run, int beginIndex, int endIndex) {
139 1 1. getSubstring : replaced return value with "" for pro/verron/officestamper/core/RunUtil::getSubstring → KILLED
        return getText(run)
140
                .substring(beginIndex, endIndex);
141
    }
142
143
    static R create(String text, RPr rPr) {
144
        R newStartRun = newRun(text);
145 1 1. create : removed call to org/docx4j/wml/R::setRPr → SURVIVED
        newStartRun.setRPr(rPr);
146 1 1. create : replaced return value with null for pro/verron/officestamper/core/RunUtil::create → KILLED
        return newStartRun;
147
    }
148
}

Mutations

42

1.1
Location : getText
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:#30]
replaced return value with "" for pro/verron/officestamper/core/RunUtil::getText → KILLED

56

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

61

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

62

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

63

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

64

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

82

1.1
Location : getText
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:#29]
negated conditional → KILLED

2.2
Location : getText
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:#30]
replaced return value with null for pro/verron/officestamper/core/RunUtil::getText → KILLED

96

1.1
Location : create
Killed by : none
removed call to pro/verron/officestamper/core/RunUtil::applyParagraphStyle → NO_COVERAGE

97

1.1
Location : create
Killed by : none
replaced return value with null for pro/verron/officestamper/core/RunUtil::create → NO_COVERAGE

106

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

108

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

111

1.1
Location : applyParagraphStyle
Killed by : none
removed call to org/docx4j/wml/R::setRPr → NO_COVERAGE

122

1.1
Location : setText
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:#28]
removed call to java/util/List::clear → KILLED

129

1.1
Location : getLength
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:#28]
replaced int return with 0 for pro/verron/officestamper/core/RunUtil::getLength → KILLED

134

1.1
Location : getSubstring
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:#16]
replaced return value with "" for pro/verron/officestamper/core/RunUtil::getSubstring → KILLED

139

1.1
Location : getSubstring
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:#37]
replaced return value with "" for pro/verron/officestamper/core/RunUtil::getSubstring → KILLED

145

1.1
Location : create
Killed by : none
removed call to org/docx4j/wml/R::setRPr → SURVIVED
Covering tests

146

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

Active mutators

Tests examined


Report generated by PIT 1.17.1