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.

AFPResourceManagerTestCase.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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;
  19. import java.awt.Dimension;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertFalse;
  30. import static org.junit.Assert.assertTrue;
  31. import org.apache.xmlgraphics.java2d.GraphicContext;
  32. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  33. import org.apache.xmlgraphics.util.MimeConstants;
  34. import org.apache.fop.apps.io.ResourceResolverFactory;
  35. import org.apache.fop.render.afp.AFPParser;
  36. /**
  37. * Test case for {@link AFPResourceManager}.
  38. */
  39. public class AFPResourceManagerTestCase {
  40. private AFPResourceManager sut;
  41. @Before
  42. public void setUp() throws IOException {
  43. sut = new AFPResourceManager(ResourceResolverFactory.createDefaultInternalResourceResolver(
  44. new File(".").toURI()));
  45. AFPPaintingState paintingState = new AFPPaintingState();
  46. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  47. DataStream stream = sut.createDataStream(paintingState, outStream);
  48. stream.startPage(0, 0, 0, 10, 10);
  49. }
  50. /**
  51. * Ensures that if tryIncludeObject() is called with a new object, it returns false suggesting
  52. * that we have to create said object. However, if it is called with an object that has already
  53. * been created, it returns true suggesting that we don't have to create that object again.
  54. * Page-segment is false.
  55. *
  56. * @throws IOException if an I/O error occurs
  57. */
  58. @Test
  59. public void testTryIncludeObjectWithPageSegFalse() throws IOException {
  60. AFPDataObjectInfo dataInfo = createAFPDataObjectInfo();
  61. // An empty object needs to be created every time!
  62. assertFalse(sut.tryIncludeObject(dataInfo));
  63. sut.createObject(dataInfo);
  64. assertTrue(sut.tryIncludeObject(dataInfo));
  65. }
  66. /**
  67. * {@code testTryIncludeObjectWithPageSegFalse()} but with page-segment true.
  68. *
  69. * @throws IOException if an I/O error occurs
  70. */
  71. @Test
  72. public void testTryIncludeObjectWithPageSegTrue() throws IOException {
  73. AFPDataObjectInfo dataInfo = createAFPDataObjectInfo();
  74. dataInfo.setCreatePageSegment(true);
  75. // An empty object needs to be created every time!
  76. assertFalse(sut.tryIncludeObject(dataInfo));
  77. sut.createObject(dataInfo);
  78. assertTrue(sut.tryIncludeObject(dataInfo));
  79. }
  80. private AFPDataObjectInfo createAFPDataObjectInfo() {
  81. AFPDataObjectInfo dataInfo = new AFPDataObjectInfo();
  82. dataInfo.setMimeType(MimeConstants.MIME_TIFF);
  83. dataInfo.setData(new byte[1]);
  84. AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo(0, 0, 10, 10, 1, 0);
  85. dataInfo.setObjectAreaInfo(objectAreaInfo);
  86. return dataInfo;
  87. }
  88. @Test
  89. public void testIncludeObject() throws IOException {
  90. sut.createObject(createAFPGraphicsObjectInfo());
  91. sut.createObject(createAFPGraphicsObjectInfo());
  92. StringBuilder sb = new StringBuilder();
  93. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  94. sut.getDataStream().getCurrentPage().writeToStream(bos);
  95. new AFPParser(true).read(new ByteArrayInputStream(bos.toByteArray()), sb);
  96. assertEquals(sb.toString(), "BEGIN PAGE PGN00001\n"
  97. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  98. + "DESCRIPTOR PAGE\n"
  99. + "MIGRATION PRESENTATION_TEXT\n"
  100. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  101. + "INCLUDE DATA_RESOURCE\n"
  102. + "INCLUDE DATA_RESOURCE\n"
  103. + "INCLUDE DATA_RESOURCE\n"
  104. + "INCLUDE DATA_RESOURCE\n");
  105. }
  106. private AFPGraphicsObjectInfo createAFPGraphicsObjectInfo() {
  107. final AFPGraphicsObjectInfo dataInfo = new AFPGraphicsObjectInfo();
  108. final String uri = "test";
  109. dataInfo.setUri(uri);
  110. AFPGraphics2D graphics2D = new AFPGraphics2D(false, new AFPPaintingState(), null, null, null);
  111. graphics2D.setGraphicContext(new GraphicContext());
  112. dataInfo.setGraphics2D(graphics2D);
  113. dataInfo.setPainter(new Graphics2DImagePainter() {
  114. public void paint(Graphics2D g2d, Rectangle2D area) {
  115. try {
  116. AFPDataObjectInfo dataObjectInfo = createAFPDataObjectInfo();
  117. dataObjectInfo.setUri(uri);
  118. sut.createObject(dataObjectInfo);
  119. } catch (IOException e) {
  120. throw new RuntimeException(e);
  121. }
  122. }
  123. public Dimension getImageSize() {
  124. return null;
  125. }
  126. });
  127. dataInfo.setObjectAreaInfo(new AFPObjectAreaInfo(0, 0, 0, 0, 0, 0));
  128. return dataInfo;
  129. }
  130. }