diff options
author | Vincent Hennebert <vhennebert@apache.org> | 2013-03-19 16:21:15 +0000 |
---|---|---|
committer | Vincent Hennebert <vhennebert@apache.org> | 2013-03-19 16:21:15 +0000 |
commit | e3c9f13598dc3bbcbe1ea34920972910625567c0 (patch) | |
tree | 0c18e0a4be3dd7fe0b10c2de71d6060fcd400949 /test/java | |
parent | 92a5ef654877571635164b70e5c211e9dc0cc740 (diff) | |
download | xmlgraphics-fop-e3c9f13598dc3bbcbe1ea34920972910625567c0.tar.gz xmlgraphics-fop-e3c9f13598dc3bbcbe1ea34920972910625567c0.zip |
FOP-2226: Image resources are not closed when rendering into the Intermediate Format
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1458382 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/java')
-rw-r--r-- | test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java | 93 |
1 files changed, 93 insertions, 0 deletions
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 index 000000000..0340c190e --- /dev/null +++ b/test/java/org/apache/fop/render/intermediate/IFSerializerTestCase.java @@ -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)); + } + +} |