Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractTripletStructuredObjectTestCase.java 5.1KB

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