[bug-66081] fix editing of added abstract numberings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-05-22 10:23:59 +00:00
parent b9d8bb01ec
commit 435dc86896
2 changed files with 47 additions and 3 deletions

View File

@ -220,12 +220,15 @@ public class XWPFNumbering extends POIXMLDocumentPart {
public BigInteger addAbstractNum(XWPFAbstractNum abstractNum) {
int pos = abstractNums.size();
if (abstractNum.getAbstractNum() != null) { // Use the current CTAbstractNum if it exists
ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
CTAbstractNum ctAbstractNum = ctNumbering.addNewAbstractNum();
ctAbstractNum.set(abstractNum.getAbstractNum());
abstractNum.setCtAbstractNum(ctAbstractNum);
} else {
abstractNum.setCtAbstractNum(ctNumbering.addNewAbstractNum());
BigInteger id = findNextAbstractNumberingId();
abstractNum.getAbstractNum().setAbstractNumId(id);
ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
abstractNum.setCtAbstractNum(ctNumbering.getAbstractNumArray(pos));
}
abstractNums.add(abstractNum);
return abstractNum.getCTAbstractNum().getAbstractNumId();

View File

@ -214,8 +214,7 @@ class TestXWPFBugs {
// we added one level for numbering with id "1"
if (id == 1) {
//TODO remaining issue from https://bz.apache.org/bugzilla/show_bug.cgi?id=66079
//assertEquals(1, abstractNum.getAbstractNum().getLvlList().size());
assertEquals(1, abstractNum.getAbstractNum().getLvlList().size());
} else {
assertEquals(0, abstractNum.getAbstractNum().getLvlList().size());
}
@ -288,6 +287,48 @@ class TestXWPFBugs {
}
}
@Test
void testEditNumberings() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWithOutOfOrderId.docx")) {
XWPFNumbering numbering = doc.createNumbering();
// Abstract numbering with id = 1 already exists in the file, and has 9 levels
XWPFAbstractNum abstractNum1 = numbering.getAbstractNum(BigInteger.ONE);
assertEquals(9, abstractNum1.getAbstractNum().getLvlList().size());
// Remove all levels from this numbering
while (abstractNum1.getAbstractNum().getLvlList().size() > 0) {
abstractNum1.getAbstractNum().removeLvl(0);
}
// Add abstract numbering with id = 0
addNumberingWithAbstractId(numbering, 0);
// Add abstract numbering with auto-generated id, this should be 2
numbering.addAbstractNum(new XWPFAbstractNum());
// Add (id + 1) levels to each abstract numbering
for (int id = 0; id < 3; id++) {
XWPFAbstractNum num = numbering.getAbstractNum(BigInteger.valueOf(id));
for (int j = 0; j < id + 1; j++) {
num.getAbstractNum().addNewLvl();
}
}
// Check that all levels added successfully
for (int id = 0; id < 3; id++) {
assertEquals(id + 1, numbering.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
}
XWPFDocument docReloaded = writeOutAndReadBack(doc);
XWPFNumbering numberingReloaded = docReloaded.getNumbering();
// Check that all added levels persisted after document reload
for (int id = 0; id < 3; id++) {
assertEquals(id + 1, numberingReloaded.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
}
}
}
private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){
// create a numbering scheme
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();