PdfStructElemUnitTest.java
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itextpdf.kernel.pdf.tagging;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
import com.itextpdf.kernel.exceptions.PdfException;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.Pdf3DAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.test.ExtendedITextTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("UnitTest")
public class PdfStructElemUnitTest extends ExtendedITextTest {
@Test
public void noParentObjectTest() {
PdfDictionary parent = new PdfDictionary();
PdfArray kid = new PdfArray();
Exception exception = Assertions.assertThrows(PdfException.class,
() -> PdfStructElem.addKidObject(parent, 1, kid));
Assertions.assertEquals(KernelExceptionMessageConstant.STRUCTURE_ELEMENT_SHALL_CONTAIN_PARENT_OBJECT,
exception.getMessage());
}
@Test
public void annotationHasNoReferenceToPageTest() {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
PdfName pdfName = new PdfName("test");
PdfAnnotation annotation = new Pdf3DAnnotation(new Rectangle(100, 100), pdfName);
Exception exception = Assertions.assertThrows(PdfException.class,
() -> new PdfStructElem(pdfDoc, pdfName, annotation));
Assertions.assertEquals(KernelExceptionMessageConstant.ANNOTATION_SHALL_HAVE_REFERENCE_TO_PAGE,
exception.getMessage());
}
@Test
public void attributesAreNullTest() {
Map<PdfName, PdfObject> attributesMap = new HashMap<>();
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
List<PdfStructureAttributes> actualAttributesList = pdfStructElem.getAttributesList();
List<PdfStructureAttributes> expectedAttributesList = new ArrayList<>();
Assertions.assertEquals(actualAttributesList, expectedAttributesList);
}
@Test
public void attributesAreDictionaryTest() {
Map<PdfName, PdfObject> attributesMap = new HashMap<>();
Map<PdfName, PdfObject> dictionaryMap = new HashMap<>();
dictionaryMap.put(PdfName.A, new PdfName("value"));
attributesMap.put(PdfName.A, new PdfDictionary(dictionaryMap));
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
List<PdfStructureAttributes> actualAttributesList = pdfStructElem.getAttributesList();
List<PdfStructureAttributes> expectedAttributesList = new ArrayList<>();
expectedAttributesList.add(new PdfStructureAttributes(new PdfDictionary(dictionaryMap)));
Assertions.assertEquals(actualAttributesList.get(0).getPdfObject().get(PdfName.A),
expectedAttributesList.get(0).getPdfObject().get(PdfName.A));
}
@Test
public void attributesAreArrayTest() {
Map<PdfName, PdfObject> attributesMap = new HashMap<>();
Map<PdfName, PdfObject> dictionaryMap = new HashMap<>();
dictionaryMap.put(PdfName.A, new PdfName("value"));
PdfDictionary pdfDictionary = new PdfDictionary(dictionaryMap);
attributesMap.put(PdfName.A, new PdfArray(pdfDictionary));
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
List<PdfStructureAttributes> actualAttributesList = pdfStructElem.getAttributesList();
List<PdfStructureAttributes> expectedAttributesList = new ArrayList<>();
expectedAttributesList.add(new PdfStructureAttributes(new PdfDictionary(dictionaryMap)));
Assertions.assertEquals(actualAttributesList.get(0).getPdfObject().get(PdfName.A),
expectedAttributesList.get(0).getPdfObject().get(PdfName.A));
}
}