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

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