You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by vh...@apache.org on 2013/03/19 17:21:16 UTC

svn commit: r1458382 - in /xmlgraphics/fop/trunk: lib/xmlgraphics-commons-svn-trunk.jar src/java/org/apache/fop/render/intermediate/IFSerializer.java test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java

Author: vhennebert
Date: Tue Mar 19 16:21:15 2013
New Revision: 1458382

URL: http://svn.apache.org/r1458382
Log:
FOP-2226: Image resources are not closed when rendering into the Intermediate Format

Added:
    xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java   (with props)
Modified:
    xmlgraphics/fop/trunk/lib/xmlgraphics-commons-svn-trunk.jar
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/intermediate/IFSerializer.java

Modified: xmlgraphics/fop/trunk/lib/xmlgraphics-commons-svn-trunk.jar
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/lib/xmlgraphics-commons-svn-trunk.jar?rev=1458382&r1=1458381&r2=1458382&view=diff
==============================================================================
Binary files - no diff available.

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/intermediate/IFSerializer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/intermediate/IFSerializer.java?rev=1458382&r1=1458381&r2=1458382&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/intermediate/IFSerializer.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/intermediate/IFSerializer.java Tue Mar 19 16:21:15 2013
@@ -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, IFDoc
             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);
         }
     }
 

Added: xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java?rev=1458382&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java (added)
+++ xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java Tue Mar 19 16:21:15 2013
@@ -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));
+    }
+
+}

Propchange: xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/trunk/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Revision Id



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org