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.

IFSerializerTestCase.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.render.intermediate;
  19. import java.awt.Rectangle;
  20. import java.io.File;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import javax.xml.transform.sax.SAXResult;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import static org.junit.Assert.assertTrue;
  28. import static org.mockito.ArgumentMatchers.nullable;
  29. import static org.mockito.Matchers.eq;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.verify;
  32. import static org.mockito.Mockito.when;
  33. import org.apache.xmlgraphics.image.loader.ImageManager;
  34. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.apps.FopFactory;
  37. import org.apache.fop.events.EventChecker;
  38. import org.apache.fop.render.afp.AFPDocumentHandler;
  39. public class IFSerializerTestCase {
  40. private static final String IMAGE = "image.png";
  41. private IFSerializer sut;
  42. private ImageManager imageManager;
  43. @Before
  44. public void setUp() throws IFException {
  45. imageManager = mock(ImageManager.class);
  46. IFContext context = mockContext();
  47. sut = new IFSerializer(context);
  48. }
  49. private IFContext mockContext() {
  50. FOUserAgent userAgent = mock(FOUserAgent.class);
  51. when(userAgent.getImageManager()).thenReturn(imageManager);
  52. return new IFContext(userAgent);
  53. }
  54. @Test
  55. public void drawImageShouldCloseResources() throws IFException {
  56. sut.setResult(new SAXResult(new DefaultHandler()));
  57. whenDrawImageIsCalled(true);
  58. thenImageResourcesMustBeClosed();
  59. }
  60. @Test
  61. public void failingDrawImageShouldCloseResources() throws IFException {
  62. // Make drawImage artificially fail by not calling setResult
  63. whenDrawImageIsCalled(false);
  64. thenImageResourcesMustBeClosed();
  65. }
  66. private void whenDrawImageIsCalled(boolean terminatesNormally) throws IFException {
  67. boolean exceptionThrown = false;
  68. try {
  69. sut.drawImage(IMAGE, new Rectangle(10, 10));
  70. } catch (Exception e) {
  71. exceptionThrown = true;
  72. }
  73. if (!terminatesNormally) {
  74. assertTrue(exceptionThrown);
  75. }
  76. }
  77. private void thenImageResourcesMustBeClosed() {
  78. verify(imageManager).closeImage(eq(IMAGE), nullable(ImageSessionContext.class));
  79. }
  80. @Test
  81. public void testPageEvent() throws IFException {
  82. FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  83. Map<String, Object> params = new HashMap<String, Object>();
  84. params.put("number", 1);
  85. EventChecker eventChecker = new EventChecker("org.apache.fop.render.RendererEventProducer.endPage", params);
  86. userAgent.getEventBroadcaster().addEventListener(eventChecker);
  87. sut.mimicDocumentHandler(new AFPDocumentHandler(new IFContext(userAgent)));
  88. sut.setResult(new SAXResult(new DefaultHandler()));
  89. sut.endPage();
  90. eventChecker.end();
  91. }
  92. }