Skip to content

Commit

Permalink
Improve checkAndAddPage error message
Browse files Browse the repository at this point in the history
Do use the number of the page instead of its object.

DEVSIX-5146
  • Loading branch information
ars18wrw committed Jul 26, 2021
1 parent 97afe9f commit 6b13c28
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ public final class KernelExceptionMessageConstant {
public static final String ONLY_BMP_CAN_BE_WRAPPED_IN_WMF = "Only BMP can be wrapped in WMF.";
public static final String OPERATOR_EI_NOT_FOUND_AFTER_END_OF_IMAGE_DATA = "Operator EI not found after the end "
+ "of image data.";
public static final String PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT = "Page {0} "
+ "cannot be added to document {1}, because it belongs to document {2}.";
public static final String PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT =
"The passed page belongs to document {0} (page {1} of the document) and therefore cannot be added "
+ "to this document ({2}).";
public static final String PAGE_IS_NOT_SET_FOR_THE_PDF_TAG_STRUCTURE = "Page is not set for the pdf tag structure.";
public static final String PAGE_ALREADY_FLUSHED = "The page has been already flushed.";
public static final String PDF_ENCRYPTION = "PdfEncryption exception.";
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ protected void checkAndAddPage(int index, PdfPage page) {
if (page.getDocument() != null && this != page.getDocument()) {
throw new PdfException(
KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT
).setMessageParams(page, this, page.getDocument());
).setMessageParams(page.getDocument(), page.getDocument().getPageNumber(page), this);
}
catalog.getPageTree().addPage(index, page);
}
Expand All @@ -2168,7 +2168,7 @@ protected void checkAndAddPage(PdfPage page) {
if (page.getDocument() != null && this != page.getDocument())
throw new PdfException(
KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT)
.setMessageParams(page, this, page.getDocument());
.setMessageParams(page.getDocument(), page.getDocument().getPageNumber(page), this);
catalog.getPageTree().addPage(page);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,11 @@ public void cannotAddPageToAnotherDocumentTest01() {
pdfDoc1.addNewPage(1);

junitExpectedException.expect(PdfException.class);
junitExpectedException.expectMessage(
MessageFormatUtil
.format(KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT,
pdfDoc1.getPage(1), pdfDoc2, pdfDoc1));
junitExpectedException.expectMessage(MessageFormatUtil.format(
KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT,
pdfDoc1,
1,
pdfDoc2));

pdfDoc2.checkAndAddPage(1, pdfDoc1.getPage(1));
}
Expand All @@ -430,10 +431,11 @@ public void cannotAddPageToAnotherDocumentTest02() {
pdfDoc1.addNewPage(1);

junitExpectedException.expect(PdfException.class);
junitExpectedException.expectMessage(
MessageFormatUtil
.format(KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT,
pdfDoc1.getPage(1), pdfDoc2, pdfDoc1));
junitExpectedException.expectMessage(MessageFormatUtil.format(
KernelExceptionMessageConstant.PAGE_CANNOT_BE_ADDED_TO_DOCUMENT_BECAUSE_IT_BELONGS_TO_ANOTHER_DOCUMENT,
pdfDoc1,
1,
pdfDoc2));

pdfDoc2.checkAndAddPage(pdfDoc1.getPage(1));
}
Expand Down

0 comments on commit 6b13c28

Please sign in to comment.