ObjectDeleter.java

1
package pro.verron.officestamper.core;
2
3
import jakarta.xml.bind.JAXBElement;
4
import org.docx4j.wml.*;
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7
import pro.verron.officestamper.api.OfficeStamperException;
8
9
import java.util.Iterator;
10
11
/**
12
 * Utility class for deleting objects from a {@link Document}.
13
 *
14
 * @author Joseph Verron
15
 * @author Tom Hombergs
16
 * @version ${version}
17
 * @since 1.0.0
18
 */
19
public class ObjectDeleter {
20
	private static final Logger log = LoggerFactory.getLogger(ObjectDeleter.class);
21
22
    private ObjectDeleter() {
23
		throw new OfficeStamperException("Utility class shouldn't be instantiated");
24
	}
25
26
    /**
27
     * Deletes the given paragraph from the document.
28
     *
29
     * @param paragraph the paragraph to delete.
30
     */
31
    public static void deleteParagraph(P paragraph) {
32 1 1. deleteParagraph : negated conditional → KILLED
		if (paragraph.getParent() instanceof Tc parentCell) {
33
			// paragraph within a table cell
34 1 1. deleteParagraph : removed call to pro/verron/officestamper/core/ObjectDeleter::deleteFromCell → KILLED
            ObjectDeleter.deleteFromCell(parentCell, paragraph);
35
		} else {
36
			((ContentAccessor) paragraph.getParent()).getContent().remove(paragraph);
37
        }
38
    }
39
40
    /**
41
     * Deletes the given table from the document.
42
     *
43
     * @param table the table to delete.
44
     */
45
    public static void deleteTable(Tbl table) {
46 1 1. deleteTable : negated conditional → KILLED
		if (table.getParent() instanceof Tc parentCell) {
47
			// nested table within a table cell
48 1 1. deleteTable : removed call to pro/verron/officestamper/core/ObjectDeleter::deleteFromCell → KILLED
            ObjectDeleter.deleteFromCell(parentCell, table);
49
		} else {
50
			// global table
51
			((ContentAccessor) table.getParent()).getContent().remove(table.getParent());
52
			// iterate through the containing list to find the jaxb element that contains the table.
53
			for (Iterator<Object> iterator = ((ContentAccessor) table.getParent()).getContent()
54 1 1. deleteTable : negated conditional → KILLED
																				  .listIterator(); iterator.hasNext(); ) {
55
				Object next = iterator.next();
56 2 1. deleteTable : negated conditional → KILLED
2. deleteTable : negated conditional → KILLED
				if (next instanceof JAXBElement<?> element && element.getValue().equals(table)) {
57 1 1. deleteTable : removed call to java/util/Iterator::remove → KILLED
					iterator.remove();
58
					break;
59
                }
60
            }
61
        }
62
    }
63
64
    private static void deleteFromCell(Tc cell, P paragraph) {
65
		cell.getContent().remove(paragraph);
66 1 1. deleteFromCell : negated conditional → SURVIVED
		if (TableCellUtil.hasNoParagraphOrTable(cell)) {
67 1 1. deleteFromCell : removed call to pro/verron/officestamper/core/TableCellUtil::addEmptyParagraph → SURVIVED
			TableCellUtil.addEmptyParagraph(cell);
68
		}
69
		// TODO: find out why border lines are removed in some cells after having deleted a paragraph
70
    }
71
72
    private static void deleteFromCell(Tc cell, Object obj) {
73 2 1. deleteFromCell : negated conditional → NO_COVERAGE
2. deleteFromCell : negated conditional → KILLED
		if (!(obj instanceof Tbl || obj instanceof P)) {
74
			throw new AssertionError("Only delete Tables or Paragraphs with this method.");
75
		}
76
		cell.getContent().remove(obj);
77 1 1. deleteFromCell : negated conditional → SURVIVED
		if (TableCellUtil.hasNoParagraphOrTable(cell)) {
78 1 1. deleteFromCell : removed call to pro/verron/officestamper/core/TableCellUtil::addEmptyParagraph → NO_COVERAGE
			TableCellUtil.addEmptyParagraph(cell);
79
		}
80
		// TODO: find out why border lines are removed in some cells after having deleted a paragraph
81
    }
82
83
    /**
84
     * Deletes the given table row from the document.
85
     *
86
     * @param tableRow the table row to delete.
87
     */
88
    public static void deleteTableRow(Tr tableRow) {
89 1 1. deleteTableRow : negated conditional → KILLED
		if (tableRow.getParent() instanceof Tbl table) {
90
            table.getContent().remove(tableRow);
91
        } else {
92
            log.error("Table row is not contained within a table. Unable to remove");
93
		}
94
	}
95
96
}

Mutations

32

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

34

1.1
Location : deleteParagraph
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:#20]
removed call to pro/verron/officestamper/core/ObjectDeleter::deleteFromCell → KILLED

46

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

48

1.1
Location : deleteTable
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:#23]
removed call to pro/verron/officestamper/core/ObjectDeleter::deleteFromCell → KILLED

54

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

56

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

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

57

1.1
Location : deleteTable
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:#23]
removed call to java/util/Iterator::remove → KILLED

66

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

67

1.1
Location : deleteFromCell
Killed by : none
removed call to pro/verron/officestamper/core/TableCellUtil::addEmptyParagraph → SURVIVED
Covering tests

73

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

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

77

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

78

1.1
Location : deleteFromCell
Killed by : none
removed call to pro/verron/officestamper/core/TableCellUtil::addEmptyParagraph → NO_COVERAGE

89

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

Active mutators

Tests examined


Report generated by PIT 1.17.1