From 6b13c28ba12b2e3b82f7a5d686035e649e8f813d Mon Sep 17 00:00:00 2001 From: Uladzimir Asipchuk Date: Fri, 19 Mar 2021 15:19:30 +0300 Subject: [PATCH] Improve checkAndAddPage error message Do use the number of the page instead of its object. DEVSIX-5146 --- .../KernelExceptionMessageConstant.java | 5 +++-- .../com/itextpdf/kernel/pdf/PdfDocument.java | 4 ++-- .../kernel/pdf/PdfDocumentUnitTest.java | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java b/kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java index c9bdb4781c..78dd53c037 100644 --- a/kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java +++ b/kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java @@ -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."; diff --git a/kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java b/kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java index f99f2e93d6..600cf1380a 100644 --- a/kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java +++ b/kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java @@ -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); } @@ -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); } diff --git a/kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentUnitTest.java b/kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentUnitTest.java index 9e8047a1d0..0a138b4c81 100644 --- a/kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentUnitTest.java +++ b/kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentUnitTest.java @@ -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)); } @@ -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)); }