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.

AbstractTripletStructuredObjectTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertFalse;
  29. import static org.junit.Assert.assertTrue;
  30. import org.apache.fop.afp.modca.triplets.AbstractTriplet;
  31. import org.apache.fop.afp.modca.triplets.AttributeQualifierTriplet;
  32. import org.apache.fop.afp.modca.triplets.CommentTriplet;
  33. import org.apache.fop.afp.modca.triplets.ObjectAreaSizeTriplet;
  34. import org.apache.fop.afp.modca.triplets.Triplet;
  35. /**
  36. * Test {@link AbstractTripletStructuredObject}
  37. */
  38. public class AbstractTripletStructuredObjectTest<S extends AbstractTripletStructuredObject>
  39. extends AbstractStructuredObjectTest<AbstractTripletStructuredObject> {
  40. private static final List<AbstractTriplet> TRIPLETS;
  41. static {
  42. List<AbstractTriplet> triplets = new ArrayList<AbstractTriplet>();
  43. triplets.add(new CommentTriplet((byte) 0x01, "test comment"));
  44. triplets.add(new AttributeQualifierTriplet(1, 1));
  45. triplets.add(new ObjectAreaSizeTriplet(10, 20));
  46. TRIPLETS = Collections.unmodifiableList(triplets);
  47. }
  48. private AbstractTripletStructuredObject emptyStructuredObject
  49. = new AbstractTripletStructuredObject() { };
  50. @Before
  51. public void setUp() throws Exception {
  52. AbstractTripletStructuredObject sut = getSut();
  53. for (AbstractTriplet triplet : TRIPLETS) {
  54. sut.addTriplet(triplet);
  55. }
  56. }
  57. /**
  58. * Test getTripletLength() - ensure a sum of all enclosing object lengths is returned.
  59. */
  60. public void testGetTripletLength() {
  61. int dataLength = 0;
  62. for (Triplet t : TRIPLETS) {
  63. dataLength += t.getDataLength();
  64. }
  65. assertEquals(dataLength, getSut().getTripletDataLength());
  66. assertEquals(0, emptyStructuredObject.getTripletDataLength());
  67. }
  68. /**
  69. * Test hasTriplets()
  70. */
  71. public void testHasTriplets() {
  72. assertTrue(getSut().hasTriplets());
  73. assertFalse(emptyStructuredObject.hasTriplets());
  74. }
  75. /**
  76. * Test writeTriplets() - Ensure the triplets are written properly.
  77. *
  78. * @throws IOException -
  79. */
  80. public void testWriteObjects() throws IOException {
  81. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  82. for (AbstractTriplet triplet : TRIPLETS) {
  83. triplet.writeToStream(baos);
  84. }
  85. byte[] expected = baos.toByteArray();
  86. baos.reset();
  87. getSut().writeTriplets(baos);
  88. assertTrue(Arrays.equals(expected, baos.toByteArray()));
  89. baos.reset();
  90. // Ensure it doesn't die if no data has been added
  91. emptyStructuredObject.writeTriplets(baos);
  92. byte[] emptyArray = baos.toByteArray();
  93. assertTrue(Arrays.equals(emptyArray, new byte[0]));
  94. }
  95. /**
  96. * Test hasTriplet() - ensure both positive and negative values are returned.
  97. */
  98. public void testHasTriplet() {
  99. for (AbstractTriplet triplet : TRIPLETS) {
  100. assertTrue(getSut().hasTriplet(triplet.getId()));
  101. assertFalse(emptyStructuredObject.hasTriplet(triplet.getId()));
  102. }
  103. CommentTriplet notInSystem = new CommentTriplet((byte) 0x30, "This should return false");
  104. assertFalse(getSut().hasTriplet(notInSystem.getId()));
  105. }
  106. /**
  107. * Test addTriplet() - mostly tested above, but check boundary cases
  108. */
  109. public void testAddTriplet() {
  110. // ensure null doesn't kill it... not sure what else to test
  111. getSut().addTriplet(null);
  112. }
  113. /**
  114. * Test addTriplets() - ensure all triplets are added.
  115. */
  116. @Test
  117. public void testAddTriplets() {
  118. // Tested on empty object
  119. List<AbstractTriplet> expectedList = TRIPLETS;
  120. emptyStructuredObject.addTriplets(expectedList);
  121. // checks equals() on each member of both lists
  122. assertEquals(expectedList, emptyStructuredObject.getTriplets());
  123. // Add a list to an already populated list
  124. getSut().addTriplets(expectedList);
  125. List<AbstractTriplet> newExpected = new ArrayList<AbstractTriplet>(expectedList);
  126. newExpected.addAll(expectedList);
  127. assertEquals(newExpected, getSut().getTriplets());
  128. // Ensure null doesn't throw exception
  129. emptyStructuredObject.addTriplets(null);
  130. }
  131. }