You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFParentTreeTestCase.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertTrue;
  23. import static org.mockito.Mockito.mock;
  24. /**
  25. * Tests that the nums array in the ParentTree dictionary is correctly being split into
  26. * separate arrays if the elements number exceeds the set limit.
  27. */
  28. public class PDFParentTreeTestCase {
  29. private PDFParentTree parentTree;
  30. @Before
  31. public void initializeStructureTree() {
  32. parentTree = new PDFParentTree();
  33. PDFDocument pdfDocument = new PDFDocument("test");
  34. pdfDocument.makeStructTreeRoot(parentTree);
  35. }
  36. /**
  37. * Adds less structured items than the imposed limit which should result
  38. * in only one nums array being created.
  39. * @throws Exception
  40. */
  41. @Test
  42. public void testNoSplit() throws Exception {
  43. assertEquals(getArrayNumber(45), 1);
  44. }
  45. /**
  46. * Adds more than the imposed array limit to test that it splits the
  47. * nums array into two objects.
  48. * @throws Exception
  49. */
  50. @Test
  51. public void testSingleSplit() throws Exception {
  52. assertEquals(getArrayNumber(70), 2);
  53. }
  54. /**
  55. * Adds items to the nums array to cause and test that multiple splits occur
  56. * @throws Exception
  57. */
  58. @Test
  59. public void testMultipleSplit() throws Exception {
  60. assertEquals(getArrayNumber(165), 4);
  61. }
  62. /**
  63. * Ensures that items added out of order get added to the correct nums array
  64. * @throws Exception
  65. */
  66. @Test
  67. public void testOutOfOrderSplit() throws Exception {
  68. PDFStructElem structElem = mock(PDFStructElem.class);
  69. for (int num = 50; num < 53; num++) {
  70. parentTree.addToNums(num, structElem);
  71. }
  72. assertEquals(getArrayNumber(50), 2);
  73. PDFNumberTreeNode treeNode = (PDFNumberTreeNode) parentTree.getKids().get(0);
  74. for (int num = 0; num < 50; num++) {
  75. assertTrue(treeNode.getNums().map.containsKey(num));
  76. }
  77. treeNode = (PDFNumberTreeNode) parentTree.getKids().get(1);
  78. for (int num = 50; num < 53; num++) {
  79. assertTrue(treeNode.getNums().map.containsKey(num));
  80. }
  81. }
  82. /**
  83. * Gets the number of arrays created for a given number of elements
  84. * @param elementNumber The number of elements to be added to the nums array
  85. * @return Returns the number of array objects
  86. * @throws Exception
  87. */
  88. private int getArrayNumber(int elementNumber) throws Exception {
  89. PDFStructElem structElem = mock(PDFStructElem.class);
  90. for (int structParent = 0; structParent < elementNumber; structParent++) {
  91. parentTree.addToNums(structParent, structElem);
  92. }
  93. return parentTree.getKids().length();
  94. }
  95. }