]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-2226: Image resources are not closed when rendering into the Intermediate Format
authorVincent Hennebert <vhennebert@apache.org>
Tue, 19 Mar 2013 16:21:15 +0000 (16:21 +0000)
committerVincent Hennebert <vhennebert@apache.org>
Tue, 19 Mar 2013 16:21:15 +0000 (16:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1458382 13f79535-47bb-0310-9956-ffa450edef68

lib/xmlgraphics-commons-svn-trunk.jar
src/java/org/apache/fop/render/intermediate/IFSerializer.java
test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java [new file with mode: 0644]

index 9b0b4b61b95a12608de43171d785b120e57df24e..71b32fd5b9f49f12e6bb52c34edd070d5b35ac60 100644 (file)
Binary files a/lib/xmlgraphics-commons-svn-trunk.jar and b/lib/xmlgraphics-commons-svn-trunk.jar differ
index 0a20437c007095accb1c3c329dce033d890d2bde..ff3f761bf373b1f9e84181cdd149eddd66729b37 100644 (file)
@@ -34,6 +34,8 @@ import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
 
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
 import org.apache.xmlgraphics.util.QName;
 import org.apache.xmlgraphics.util.XMLizable;
 
@@ -466,6 +468,10 @@ implements IFConstants, IFPainter, IFDocumentNavigationHandler {
             handler.element(EL_IMAGE, atts);
         } catch (SAXException e) {
             throw new IFException("SAX error in startGroup()", e);
+        } finally {
+            ImageSessionContext session = getUserAgent().getImageSessionContext();
+            ImageManager imageManager = getUserAgent().getImageManager();
+            imageManager.closeImage(uri, session);
         }
     }
 
diff --git a/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java b/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java
new file mode 100644 (file)
index 0000000..0340c19
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.render.intermediate;
+
+import java.awt.Rectangle;
+
+import javax.xml.transform.sax.SAXResult;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.xml.sax.helpers.DefaultHandler;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+
+import org.apache.fop.apps.FOUserAgent;
+
+public class IFSerializerTestCase {
+
+    private static final String IMAGE = "image.png";
+
+    private IFSerializer sut;
+
+    private ImageManager imageManager;
+
+    @Before
+    public void setUp() throws IFException {
+        imageManager = mock(ImageManager.class);
+        IFContext context = mockContext();
+        sut = new IFSerializer(context);
+    }
+
+    private IFContext mockContext() {
+        FOUserAgent userAgent = mock(FOUserAgent.class);
+        when(userAgent.getImageManager()).thenReturn(imageManager);
+        return new IFContext(userAgent);
+    }
+
+    @Test
+    public void drawImageShouldCloseResources() throws IFException {
+        sut.setResult(new SAXResult(new DefaultHandler()));
+        whenDrawImageIsCalled(true);
+        thenImageResourcesMustBeClosed();
+    }
+
+    @Test
+    public void failingDrawImageShouldCloseResources() throws IFException {
+        // Make drawImage artificially fail by not calling setResult
+        whenDrawImageIsCalled(false);
+        thenImageResourcesMustBeClosed();
+    }
+
+    private void whenDrawImageIsCalled(boolean terminatesNormally) throws IFException {
+        boolean exceptionThrown = false;
+        try {
+            sut.drawImage(IMAGE, new Rectangle(10, 10));
+        } catch (Exception e) {
+            exceptionThrown = true;
+        }
+        if (!terminatesNormally) {
+            assertTrue(exceptionThrown);
+        }
+    }
+
+    private void thenImageResourcesMustBeClosed() {
+        verify(imageManager).closeImage(eq(IMAGE), any(ImageSessionContext.class));
+    }
+
+}