SectionUtil.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.XmlUtils;
4
import org.docx4j.wml.ContentAccessor;
5
import org.docx4j.wml.P;
6
import org.docx4j.wml.PPr;
7
import org.docx4j.wml.SectPr;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import pro.verron.officestamper.api.OfficeStamperException;
11
import pro.verron.officestamper.utils.WmlFactory;
12
13
import java.util.List;
14
import java.util.Optional;
15
16
import static java.util.Optional.ofNullable;
17
18
/**
19
 * Utility class to handle section breaks in paragraphs.
20
 *
21
 * @author Joseph Verron
22
 * @version ${version}
23
 * @since 1.6.2
24
 */
25
public class SectionUtil {
26
    private static final Logger log = LoggerFactory.getLogger(SectionUtil.class);
27
28
    private SectionUtil() {
29
        throw new OfficeStamperException("Utility class shouldn't be instantiated");
30
    }
31
32
    /**
33
     * Creates a new section break object.
34
     *
35
     * @param firstObject a {@link Object} object
36
	 * @param parent      a {@link ContentAccessor} object
37
     * @return a new section break object.
38
     */
39
    public static Optional<SectPr> getPreviousSectionBreakIfPresent(Object firstObject, ContentAccessor parent) {
40
        List<Object> parentContent = parent.getContent();
41
        int pIndex = parentContent.indexOf(firstObject);
42
43 1 1. getPreviousSectionBreakIfPresent : Replaced integer subtraction with addition → KILLED
        int i = pIndex - 1;
44 2 1. getPreviousSectionBreakIfPresent : changed conditional boundary → SURVIVED
2. getPreviousSectionBreakIfPresent : negated conditional → KILLED
        while (i >= 0) {
45 1 1. getPreviousSectionBreakIfPresent : negated conditional → KILLED
            if (parentContent.get(i) instanceof P prevParagraph) {
46
                // the first P preceding the object is the one potentially carrying a section break
47 1 1. getPreviousSectionBreakIfPresent : replaced return value with Optional.empty for pro/verron/officestamper/core/SectionUtil::getPreviousSectionBreakIfPresent → KILLED
                return ofNullable(prevParagraph.getPPr())
48
                        .map(PPr::getSectPr);
49
            }
50
            else log.debug("The previous sibling was not a P, continuing search");
51
            i--;
52
        }
53
        log.info("No previous section break found from : {}, first object index={}", parent, pIndex);
54
        return Optional.empty();
55
    }
56
57
    /**
58
     * Creates a new section break object.
59
     *
60
     * @param objects a {@link List} object
61
     *
62
     * @return a new section break object.
63
     */
64
    public static boolean hasOddNumberOfSectionBreaks(List<Object> objects) {
65 1 1. hasOddNumberOfSectionBreaks : replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasOddNumberOfSectionBreaks → KILLED
        return objects.stream()
66
                      .filter(P.class::isInstance)
67
                      .map(P.class::cast)
68
                      .filter(SectionUtil::hasSectionBreak)
69 2 1. hasOddNumberOfSectionBreaks : Replaced long modulus with multiplication → SURVIVED
2. hasOddNumberOfSectionBreaks : negated conditional → KILLED
                      .count() % 2 != 0;
70
    }
71
72
    private static boolean hasSectionBreak(P p) {
73
        var pPPr = p.getPPr();
74 2 1. hasSectionBreak : negated conditional → KILLED
2. hasSectionBreak : replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasSectionBreak → KILLED
        if (pPPr == null) return false;
75
        var pPPrSectPr = pPPr.getSectPr();
76 2 1. hasSectionBreak : replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasSectionBreak → SURVIVED
2. hasSectionBreak : negated conditional → KILLED
        return pPPrSectPr != null;
77
    }
78
79
    /**
80
     * Creates a new section break object.
81
     *
82
     * @param sectPr    a {@link SectPr} object
83
     * @param paragraph a {@link P} object
84
     */
85
    public static void applySectionBreakToParagraph(SectPr sectPr, P paragraph) {
86
        PPr nextPPr = ofNullable(paragraph.getPPr()).orElseGet(WmlFactory::newPPr);
87 1 1. applySectionBreakToParagraph : removed call to org/docx4j/wml/PPr::setSectPr → KILLED
        nextPPr.setSectPr(XmlUtils.deepCopy(sectPr));
88 1 1. applySectionBreakToParagraph : removed call to org/docx4j/wml/P::setPPr → KILLED
        paragraph.setPPr(nextPPr);
89
    }
90
}

Mutations

43

1.1
Location : getPreviousSectionBreakIfPresent
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:#14]
Replaced integer subtraction with addition → KILLED

44

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

2.2
Location : getPreviousSectionBreakIfPresent
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

45

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

47

1.1
Location : getPreviousSectionBreakIfPresent
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:#14]
replaced return value with Optional.empty for pro/verron/officestamper/core/SectionUtil::getPreviousSectionBreakIfPresent → KILLED

65

1.1
Location : hasOddNumberOfSectionBreaks
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:#13]
replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasOddNumberOfSectionBreaks → KILLED

69

1.1
Location : hasOddNumberOfSectionBreaks
Killed by : none
Replaced long modulus with multiplication → SURVIVED
Covering tests

2.2
Location : hasOddNumberOfSectionBreaks
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:#14]
negated conditional → KILLED

74

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

2.2
Location : hasSectionBreak
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:#14]
replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasSectionBreak → KILLED

76

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

2.2
Location : hasSectionBreak
Killed by : none
replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::hasSectionBreak → SURVIVED
Covering tests

87

1.1
Location : applySectionBreakToParagraph
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:#14]
removed call to org/docx4j/wml/PPr::setSectPr → KILLED

88

1.1
Location : applySectionBreakToParagraph
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:#14]
removed call to org/docx4j/wml/P::setPPr → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.1