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.

AbstractTripletStructuredObjectTestCase.java 5.2KB

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