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.

PDFArrayTestCase.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertFalse;
  26. import static org.junit.Assert.assertTrue;
  27. import static org.junit.Assert.fail;
  28. /**
  29. * Test case for {@link PDFArray}.
  30. */
  31. public class PDFArrayTestCase extends PDFObjectTestCase {
  32. private PDFArray intArray;
  33. private String intArrayOutput;
  34. private PDFArray doubleArray;
  35. private String doubleArrayOutput;
  36. private PDFArray collectionArray;
  37. private String collectionArrayOutput;
  38. private PDFArray objArray;
  39. private String objArrayOutput;
  40. /** A PDF object used solely for testing */
  41. private PDFNumber num;
  42. @Before
  43. public void setUp() {
  44. intArray = new PDFArray(parent, new int[] {1, 2, 3, 4, 5});
  45. intArrayOutput = "[1 2 3 4 5]";
  46. doubleArray = new PDFArray(parent, new double[] {1.1, 2.2, 3.3, 4.4, 5.5});
  47. doubleArrayOutput = "[1.1 2.2 3.3 4.4 5.5]";
  48. List<Object> strList = new ArrayList<Object>();
  49. strList.add("one");
  50. strList.add("two");
  51. strList.add("three");
  52. collectionArray = new PDFArray(parent, strList);
  53. collectionArrayOutput = "[(one) (two) (three)]";
  54. // Set arbitrary values here
  55. num = new PDFNumber();
  56. num.setNumber(20);
  57. num.setObjectNumber(4);
  58. objArray = new PDFArray(parent, new Object[] {"one", 2, 3.0f, num});
  59. objArrayOutput = "[(one) 2 3 4 0 R]";
  60. // set the document
  61. intArray.setDocument(doc);
  62. doubleArray.setDocument(doc);
  63. collectionArray.setDocument(doc);
  64. objArray.setDocument(doc);
  65. // Test the progenitor in the inheritance stack
  66. objArray.setParent(parent);
  67. pdfObjectUnderTest = objArray;
  68. }
  69. private void intArrayContainsTests() {
  70. for (int i = 1; i <= 5; i++) {
  71. assertTrue(intArray.contains(i));
  72. }
  73. assertFalse(intArray.contains(6));
  74. assertFalse(intArray.contains(0));
  75. }
  76. private void doubleArrayContainsTests() {
  77. assertTrue(doubleArray.contains(1.1));
  78. assertTrue(doubleArray.contains(2.2));
  79. assertTrue(doubleArray.contains(3.3));
  80. assertTrue(doubleArray.contains(4.4));
  81. assertTrue(doubleArray.contains(5.5));
  82. assertFalse(doubleArray.contains(10.0));
  83. assertFalse(doubleArray.contains(0.0));
  84. }
  85. private void collectionArrayContainsTests() {
  86. assertTrue(collectionArray.contains("one"));
  87. assertTrue(collectionArray.contains("two"));
  88. assertTrue(collectionArray.contains("three"));
  89. assertFalse(collectionArray.contains("zero"));
  90. assertFalse(collectionArray.contains("four"));
  91. }
  92. private void objectArrayContainsTests() {
  93. assertTrue(objArray.contains("one"));
  94. assertTrue(objArray.contains(2));
  95. assertTrue(objArray.contains(3.0f));
  96. assertTrue(objArray.contains(num));
  97. assertFalse(objArray.contains("four"));
  98. assertFalse(objArray.contains(0.0));
  99. }
  100. /**
  101. * Test contains() - test whether this PDFArray contains an object.
  102. */
  103. @Test
  104. public void testContains() {
  105. // Test some arbitrary values
  106. intArrayContainsTests();
  107. doubleArrayContainsTests();
  108. collectionArrayContainsTests();
  109. objectArrayContainsTests();
  110. }
  111. /**
  112. * Test length() - tests the length of an array.
  113. */
  114. @Test
  115. public void testLength() {
  116. assertEquals(5, intArray.length());
  117. assertEquals(5, doubleArray.length());
  118. assertEquals(3, collectionArray.length());
  119. assertEquals(4, objArray.length());
  120. // Test the count is incremented when an object is added (this only
  121. // needs to be tested once)
  122. intArray.add(6);
  123. assertEquals(6, intArray.length());
  124. }
  125. /**
  126. * Test set() - tests that a particular point has been properly set.
  127. */
  128. @Test
  129. public void testSet() {
  130. PDFName name = new PDFName("zero test");
  131. objArray.set(0, name);
  132. assertEquals(name, objArray.get(0));
  133. objArray.set(1, "test");
  134. assertEquals("test", objArray.get(1));
  135. // This goes through the set(int, double) code path rather than set(int, Object)
  136. objArray.set(2, 5);
  137. assertEquals(5.0, objArray.get(2));
  138. try {
  139. objArray.set(4, 2);
  140. fail("out of bounds");
  141. } catch (IndexOutOfBoundsException e) {
  142. // Pass
  143. }
  144. }
  145. /**
  146. * Test get() - gets the object stored at a given index.
  147. */
  148. @Test
  149. public void testGet() {
  150. // Test some arbitrary values
  151. for (int i = 1; i <= 5; i++) {
  152. assertEquals(i, intArray.get(i - 1));
  153. }
  154. assertEquals(1.1, doubleArray.get(0));
  155. assertEquals(2.2, doubleArray.get(1));
  156. assertEquals(3.3, doubleArray.get(2));
  157. assertEquals(4.4, doubleArray.get(3));
  158. assertEquals(5.5, doubleArray.get(4));
  159. assertEquals("one", collectionArray.get(0));
  160. assertEquals("two", collectionArray.get(1));
  161. assertEquals("three", collectionArray.get(2));
  162. assertEquals("one", objArray.get(0));
  163. assertEquals(2, objArray.get(1));
  164. assertEquals(0, Double.compare(3.0, (Float) objArray.get(2)));
  165. assertEquals(num, objArray.get(3));
  166. }
  167. /**
  168. * Tests add() - tests that objects are appended to the end of the array as expected.
  169. */
  170. @Test
  171. public void testAdd() {
  172. intArray.add(Integer.valueOf(6));
  173. doubleArray.add(6.6);
  174. // Test some arbitrary values
  175. for (int i = 1; i <= 6; i++) {
  176. assertEquals(i, intArray.get(i - 1));
  177. }
  178. assertEquals(1.1, doubleArray.get(0));
  179. assertEquals(2.2, doubleArray.get(1));
  180. assertEquals(3.3, doubleArray.get(2));
  181. assertEquals(4.4, doubleArray.get(3));
  182. assertEquals(5.5, doubleArray.get(4));
  183. assertEquals(6.6, doubleArray.get(5));
  184. collectionArray.add(1);
  185. assertEquals("one", collectionArray.get(0));
  186. assertEquals("two", collectionArray.get(1));
  187. assertEquals("three", collectionArray.get(2));
  188. assertEquals(1.0, collectionArray.get(3));
  189. objArray.add("four");
  190. assertEquals("one", objArray.get(0));
  191. assertEquals(2, objArray.get(1));
  192. assertEquals(0, Double.compare(3.0, (Float) objArray.get(2)));
  193. assertEquals("four", objArray.get(4));
  194. }
  195. /**
  196. * Tests output() - tests that this object is properly streamed to the PDF document.
  197. * @throws IOException error caused by I/O
  198. */
  199. @Test
  200. public void testOutput() throws IOException {
  201. testOutputStreams(intArrayOutput, intArray);
  202. testOutputStreams(doubleArrayOutput, doubleArray);
  203. testOutputStreams(collectionArrayOutput, collectionArray);
  204. testOutputStreams(objArrayOutput, objArray);
  205. }
  206. }