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.

IncludeObjectTestCase.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.util.Arrays;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertTrue;
  25. import org.apache.fop.afp.util.BinaryUtils;
  26. /**
  27. * Test {@link IncludeObject}
  28. */
  29. public class IncludeObjectTestCase extends AbstractNamedAFPObjectTest<IncludeObject> {
  30. @Before
  31. public void setUp() throws Exception {
  32. setSut(new IncludeObject("8__chars"));
  33. }
  34. /**
  35. * Test writeToStream()
  36. * @throws IOException -
  37. */
  38. @Test
  39. public void testWriteToStream() throws IOException {
  40. final IncludeObject sut = getSut();
  41. byte[] expected = defaultIncludeObjectBytes(sut.getTripletDataLength(), sut.getNameBytes());
  42. testWriteToStreamHelper(sut, expected);
  43. }
  44. /**
  45. * Test writeToStream() - the orientation of the referenced object is a right-
  46. * handed with a 180 x-axis
  47. * @throws IOException -
  48. */
  49. @Test
  50. public void testWriteToStreamForOrientation() throws IOException {
  51. final IncludeObject sut = getSut();
  52. byte[] expected = defaultIncludeObjectBytes(sut.getTripletDataLength(), sut.getNameBytes());
  53. expected[25] = (byte)0x5A;
  54. expected[26] = (byte)0x00;
  55. expected[27] = (byte)0x87;
  56. expected[28] = (byte)0x00;
  57. sut.setObjectAreaOrientation(180);
  58. testWriteToStreamHelper(sut, expected);
  59. }
  60. private void testWriteToStreamHelper(IncludeObject sut, byte[] expected) throws IOException {
  61. final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  62. sut.writeToStream(baos);
  63. byte[] actual = baos.toByteArray();
  64. assertTrue(Arrays.equals(actual, expected));
  65. }
  66. private byte[] defaultIncludeObjectBytes(int tripletDataLength, byte[] nameData) {
  67. byte[] expected = new byte[36];
  68. byte[] header = new byte[] {
  69. 0x5A, // Structured field identifier
  70. 0x00, // Length byte 1
  71. 0x10, // Length byte 2
  72. (byte)0xD3, // Structured field id byte 1
  73. (byte)0xAF, // Structured field id byte 2 - type 'input'
  74. (byte)0xC3, // Structured field id byte 3 - category 'data resource'
  75. 0x00, // Flags
  76. 0x00, // Reserved
  77. 0x00, // Reserved
  78. };
  79. System.arraycopy(header, 0, expected, 0, header.length);
  80. byte[] lengthBytes = BinaryUtils.convert(35 + tripletDataLength, 2); //Ignore first byte
  81. expected[1] = lengthBytes[0];
  82. expected[2] = lengthBytes[1];
  83. System.arraycopy(nameData, 0, expected, 9, nameData.length);
  84. expected[18] = (byte)0x92; // object type 'other'
  85. expected[27] = (byte)0x2D; // orientation of the reference object
  86. writeOsetTo(expected, 29, -1); // the X-axis origin defined in the object
  87. writeOsetTo(expected, 32, -1); // the Y-axis origin defined in the object
  88. expected[35] = 0x01; // Page or overlay coordinate system
  89. return expected;
  90. }
  91. private static void writeOsetTo(byte[] out, int offset, int oset) {
  92. if (oset > -1) {
  93. byte[] y = BinaryUtils.convert(oset, 3);
  94. out[offset] = y[0];
  95. out[offset + 1] = y[1];
  96. out[offset + 2] = y[2];
  97. } else {
  98. out[offset] = (byte)0xFF;
  99. out[offset + 1] = (byte)0xFF;
  100. out[offset + 2] = (byte)0xFF;
  101. }
  102. }
  103. }