SectionUtil.java

1
package pro.verron.officestamper.core;
2
3
import org.docx4j.XmlUtils;
4
import org.docx4j.jaxb.Context;
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.List;
12
13
import static java.util.Optional.ofNullable;
14
15
/**
16
 * Utility class to handle section breaks in paragraphs.
17
 *
18
 * @author Joseph Verron
19
 * @version ${version}
20
 * @since 1.6.2
21
 */
22
public class SectionUtil {
23
	private static final Logger log = LoggerFactory.getLogger(SectionUtil.class);
24
25
    private SectionUtil() {
26
		throw new OfficeStamperException("Utility class shouldn't be instantiated");
27
    }
28
29
	private static final ObjectFactory factory = Context.getWmlObjectFactory();
30
31
	/**
32
	 * Creates a new section break object.
33
	 *
34
	 * @param firstObject a {@link Object} object
35
	 * @param parent      a {@link ContentAccessor} object
36
	 * @return a new section break object.
37
	 */
38
	@Nullable
39
	public static 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
				PPr pPr = prevParagraph.getPPr();
48 2 1. getPreviousSectionBreakIfPresent : negated conditional → KILLED
2. getPreviousSectionBreakIfPresent : negated conditional → KILLED
				if (pPr != null && pPr.getSectPr() != null) {
49 1 1. getPreviousSectionBreakIfPresent : replaced return value with null for pro/verron/officestamper/core/SectionUtil::getPreviousSectionBreakIfPresent → KILLED
					return pPr.getSectPr();
50
				} else return null;
51
			}
52
			i--;
53
		}
54
		log.info("No previous section break found from : {}, first object index={}", parent, pIndex);
55
		return null;
56
	}
57
58
	/**
59
	 * Creates a new section break object.
60
	 *
61
	 * @return a new section break object.
62
	 * @param p a {@link P} object
63
	 */
64
	@Nullable
65
	public static SectPr getParagraphSectionBreak(P p) {
66 2 1. getParagraphSectionBreak : negated conditional → SURVIVED
2. getParagraphSectionBreak : negated conditional → KILLED
		return p.getPPr() != null && p.getPPr().getSectPr() != null
67
				? p.getPPr().getSectPr()
68
				: null;
69
	}
70
71
	/**
72
	 * Creates a new section break object.
73
	 *
74
	 * @return a new section break object.
75
	 * @param objects a {@link List} object
76
	 */
77
	public static boolean isOddNumberOfSectionBreaks(List<Object> objects) {
78
		long count = objects.stream()
79
				.filter(P.class::isInstance)
80
				.map(P.class::cast)
81 3 1. lambda$isOddNumberOfSectionBreaks$0 : negated conditional → KILLED
2. lambda$isOddNumberOfSectionBreaks$0 : negated conditional → KILLED
3. lambda$isOddNumberOfSectionBreaks$0 : replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::lambda$isOddNumberOfSectionBreaks$0 → KILLED
							.filter(p -> p.getPPr() != null && p.getPPr().getSectPr() != null)
82
							.count();
83 3 1. isOddNumberOfSectionBreaks : Replaced long modulus with multiplication → SURVIVED
2. isOddNumberOfSectionBreaks : replaced boolean return with true for pro/verron/officestamper/core/SectionUtil::isOddNumberOfSectionBreaks → KILLED
3. isOddNumberOfSectionBreaks : negated conditional → KILLED
		return count % 2 != 0;
84
	}
85
86
	/**
87
	 * Creates a new section break object.
88
	 *
89
	 * @param sectPr a {@link SectPr} object
90
	 * @param paragraph a {@link P} object
91
	 */
92
	public static void applySectionBreakToParagraph(SectPr sectPr, P paragraph) {
93
		PPr nextPPr = ofNullable(paragraph.getPPr())
94
				.orElseGet(factory::createPPr);
95 1 1. applySectionBreakToParagraph : removed call to org/docx4j/wml/PPr::setSectPr → KILLED
		nextPPr.setSectPr(XmlUtils.deepCopy(sectPr));
96 1 1. applySectionBreakToParagraph : removed call to org/docx4j/wml/P::setPPr → KILLED
		paragraph.setPPr(nextPPr);
97
	}
98
99
}

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

48

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 : 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

49

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 null for pro/verron/officestamper/core/SectionUtil::getPreviousSectionBreakIfPresent → KILLED

66

1.1
Location : getParagraphSectionBreak
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 : getParagraphSectionBreak
Killed by : none
negated conditional → SURVIVED
Covering tests

81

1.1
Location : lambda$isOddNumberOfSectionBreaks$0
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 : lambda$isOddNumberOfSectionBreaks$0
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

3.3
Location : lambda$isOddNumberOfSectionBreaks$0
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::lambda$isOddNumberOfSectionBreaks$0 → KILLED

83

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

2.2
Location : isOddNumberOfSectionBreaks
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::isOddNumberOfSectionBreaks → KILLED

3.3
Location : isOddNumberOfSectionBreaks
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

95

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

96

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.0