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.

AbstractAFPObjectTestCase.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.afp.modca;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import junit.framework.TestCase;
  26. import org.apache.fop.afp.Streamable;
  27. /**
  28. * Tests the {@link AbstractAFPObject} class.
  29. */
  30. public abstract class AbstractAFPObjectTestCase<S extends AbstractAFPObject>
  31. extends TestCase {
  32. private S sut;
  33. protected final S getSut() {
  34. return sut;
  35. }
  36. protected final void setSut(S sut) {
  37. if ( this.sut == null) {
  38. this.sut = sut;
  39. }
  40. }
  41. private byte[] header = new byte[] {
  42. 0x5A, // Structured field identifier
  43. 0x00, // Length byte 1
  44. 0x10, // Length byte 2
  45. 0x00, // Structured field id byte 1
  46. 0x00, // Structured field id byte 2
  47. 0x00, // Structured field id byte 3
  48. 0x00, // Flags
  49. 0x00, // Reserved
  50. 0x00 // Reserved
  51. };
  52. public void testCopySFStatic() {
  53. byte[] actual = new byte[9];
  54. Arrays.fill(actual, (byte)-1);
  55. S.copySF(actual, (byte)0, (byte)0, (byte)0);
  56. assertTrue(Arrays.equals(actual, header));
  57. byte[] expected2 = new byte[9];
  58. System.arraycopy(header, 0, expected2, 0, header.length);
  59. final byte clazz = (byte) 0x01;
  60. final byte type = (byte) 0x02;
  61. final byte catagory = (byte) 0x03;
  62. expected2[3] = clazz;
  63. expected2[4] = type;
  64. expected2[5] = catagory;
  65. AbstractAFPObject.copySF(actual, clazz, type, catagory);
  66. assertTrue(Arrays.equals(actual, expected2));
  67. }
  68. public void testCopySF() {
  69. byte[] expected = new byte[9];
  70. S.copySF(expected, (byte) 0xD3, (byte)0, (byte)0);
  71. byte[] actual = new byte[9];
  72. Arrays.fill(actual, (byte)-1);
  73. getSut().copySF(actual, (byte)0, (byte)0);
  74. assertTrue(Arrays.equals(actual, expected));
  75. byte[] expected2 = new byte[9];
  76. System.arraycopy(expected, 0, expected2, 0, expected.length);
  77. final byte type = (byte)1;
  78. final byte catagory = (byte)2;
  79. expected2[4] = type;
  80. expected2[5] = catagory;
  81. getSut().copySF(actual, type, catagory);
  82. assertTrue(Arrays.equals(actual, expected2));
  83. }
  84. /**
  85. *
  86. */
  87. public void testwriteObjects() {
  88. final byte[][] expected = {{(byte)0, (byte)1}, {(byte)2, (byte)3}, {(byte)4, (byte)5}};
  89. List<Streamable> objects = new ArrayList<Streamable>() {
  90. {
  91. add(StreamableObject.instance(expected[0]));
  92. add(StreamableObject.instance(expected[1]));
  93. add(StreamableObject.instance(expected[2]));
  94. } };
  95. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  96. try {
  97. getSut().writeObjects(objects, baos);
  98. } catch (IOException e) {
  99. fail();
  100. }
  101. byte[] actual = baos.toByteArray();
  102. int index = 0;
  103. for (int i = 0; i < expected.length; i++) {
  104. for (int j = 0; j < expected[i].length; j++) {
  105. assertTrue("" + index, actual[index] == expected[i][j]);
  106. index++;
  107. }
  108. }
  109. }
  110. /**
  111. *
  112. */
  113. public void testTruncate() {
  114. String expected = "abc";
  115. assertTrue(AbstractAFPObject.truncate(expected, 4) == expected);
  116. assertTrue(AbstractAFPObject.truncate(expected, 3) == expected);
  117. assertEquals(AbstractAFPObject.truncate(expected + "d", 3), expected);
  118. assertEquals(AbstractAFPObject.truncate(expected, 0), "");
  119. try {
  120. assertTrue(AbstractAFPObject.truncate(null, 4) == null);
  121. fail();
  122. } catch (NullPointerException e) {
  123. // PASS
  124. }
  125. }
  126. /**
  127. *
  128. */
  129. public void testWriteChunksToStream() throws IOException {
  130. final byte[] data = new byte[256];
  131. int counter = 0;
  132. for (int i = 0; i < data.length; i++) {
  133. data[i] = (byte) counter++;
  134. }
  135. byte[] header = new byte[9];
  136. // Test when chunk size % data.length == 0
  137. testWithGivenChunkSize(data, header, 16);
  138. // test when chunk size % data.length != 0
  139. testWithGivenChunkSize(data, header, 10);
  140. // test with an odd number...
  141. testWithGivenChunkSize(data, header, 13);
  142. }
  143. private void testWithGivenChunkSize(byte[] data, byte[] header, int chunkSize)
  144. throws IOException {
  145. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  146. S.writeChunksToStream(data, header, 0, chunkSize, baos);
  147. byte[] testData = baos.toByteArray();
  148. int numberOfFullDataChunks = data.length / chunkSize;
  149. int lastChunkSize = data.length % chunkSize;
  150. int lengthOfTestData = numberOfFullDataChunks * (chunkSize + header.length);
  151. lengthOfTestData += lastChunkSize == 0 ? 0 : header.length + lastChunkSize;
  152. putLengthInHeader(header, chunkSize);
  153. assertEquals(lengthOfTestData, testData.length);
  154. int testIndex = 0;
  155. int expectedIndex = 0;
  156. for (int i = 0; i < numberOfFullDataChunks; i++) {
  157. checkHeaderAndData(header, data, testData, expectedIndex, testIndex, chunkSize);
  158. expectedIndex += chunkSize + header.length;
  159. testIndex += chunkSize;
  160. }
  161. putLengthInHeader(header, lastChunkSize);
  162. // check last chunk
  163. if (lastChunkSize != 0) {
  164. checkHeaderAndData(header, data, testData, expectedIndex, testIndex, lastChunkSize);
  165. }
  166. }
  167. private void putLengthInHeader(byte[] header, int chunkSize) {
  168. header[0] = 0;
  169. header[1] = (byte) (chunkSize + header.length);
  170. }
  171. private void checkHeaderAndData(byte[] header, byte[] data, byte[] testData, int expectedIndex,
  172. int testIndex, int chunkSize) {
  173. for (int i = 0; i < header.length; i++) {
  174. assertEquals(testData[expectedIndex++], header[i]);
  175. }
  176. for (int i = 0; i < chunkSize; i++) {
  177. assertEquals(testData[expectedIndex++], data[i + testIndex]);
  178. }
  179. }
  180. /**
  181. *
  182. */
  183. private static class StreamableObject implements Streamable {
  184. private byte[] bytes;
  185. StreamableObject(byte[] bytes) {
  186. this.bytes = new byte[bytes.length];
  187. System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
  188. }
  189. private static Streamable instance(byte[] bytes) {
  190. return new StreamableObject(bytes);
  191. }
  192. public void writeToStream(OutputStream os) throws IOException {
  193. os.write(bytes);
  194. }
  195. }
  196. }