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.

AbstractAFPObjectTest.java 7.5KB

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