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

Mutations

39

1.1
Location : getPreviousSectionBreakIfPresent
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[method:shouldAcceptSet()]
Replaced integer subtraction with addition → KILLED

40

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

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

41

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

43

1.1
Location : getPreviousSectionBreakIfPresent
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
replaced return value with Optional.empty for pro/verron/officestamper/core/SectionUtil::getPreviousSectionBreakIfPresent → KILLED

59

1.1
Location : hasOddNumberOfSectionBreaks
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[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 boolean return with true for pro/verron/officestamper/core/SectionUtil::hasOddNumberOfSectionBreaks → KILLED

63

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.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#2]
negated conditional → KILLED

68

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

2.2
Location : hasSectionBreak
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[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 boolean return with true for pro/verron/officestamper/core/SectionUtil::hasSectionBreak → KILLED

70

1.1
Location : hasSectionBreak
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
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

79

1.1
Location : applySectionBreakToParagraph
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
removed call to org/docx4j/wml/PPr::setSectPr → KILLED

80

1.1
Location : applySectionBreakToParagraph
Killed by : pro.verron.officestamper.test.ProcessorRepeatParagraphTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.test.ProcessorRepeatParagraphTest]/[test-template:features(java.lang.String, pro.verron.officestamper.api.OfficeStamperConfiguration, java.lang.Object, java.io.InputStream, java.lang.String)]/[test-template-invocation:#3]
removed call to org/docx4j/wml/P::setPPr → KILLED

Active mutators

Tests examined


Report generated by PIT 1.21.0