diff options
Diffstat (limited to 'test/java/org/apache/fop/render')
9 files changed, 407 insertions, 52 deletions
diff --git a/test/java/org/apache/fop/render/AbstractRenderingTestCase.java b/test/java/org/apache/fop/render/AbstractRenderingTestCase.java new file mode 100644 index 000000000..daaa94e41 --- /dev/null +++ b/test/java/org/apache/fop/render/AbstractRenderingTestCase.java @@ -0,0 +1,106 @@ +/* + * 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; + +import java.io.File; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import java.util.MissingResourceException; + +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; + +/** + * Abstract base class for rendering (output) verification tests. + */ +public abstract class AbstractRenderingTestCase extends TestCase { + + private static final Map<String, String> MIME_MAP = new java.util.HashMap<String, String>(); + + static { + MIME_MAP.put(MimeConstants.MIME_PDF, ".pdf"); + MIME_MAP.put(MimeConstants.MIME_POSTSCRIPT, ".ps"); + MIME_MAP.put(MimeConstants.MIME_AFP, ".afp"); + } + + /** the JAXP TransformerFactory */ + protected TransformerFactory tFactory = TransformerFactory.newInstance(); + /** the FopFactory */ + protected FopFactory fopFactory = FopFactory.newInstance(); + + /** + * Renders a test file. + * @param ua the user agent (with override set!) + * @param resourceName the resource name for the FO file + * @param suffix a suffix for the output filename + * @param outputFormat MIME type of the requested output format + * @return the output file + * @throws Exception if an error occurs + */ + protected File renderFile(FOUserAgent ua, String resourceName, String suffix, + String outputFormat) throws Exception { + String extension = MIME_MAP.get(outputFormat); + assert extension != null; + File outputFile = new File("build/test-results/" + resourceName + suffix + extension); + File outputDir = outputFile.getParentFile(); + FileUtils.forceMkdir(outputDir); + + // Prepare input file + InputStream in = getClass().getResourceAsStream(resourceName); + if (in == null) { + throw new MissingResourceException(resourceName + " not found in resources", + getClass().getName(), null); + } + try { + Source src = new StreamSource(in); + + // Create output file + OutputStream out = new java.io.FileOutputStream(outputFile); + out = new java.io.BufferedOutputStream(out); + try { + Fop fop = fopFactory.newFop(outputFormat, ua, out); + SAXResult res = new SAXResult(fop.getDefaultHandler()); + + Transformer transformer = tFactory.newTransformer(); + transformer.transform(src, res); + } finally { + IOUtils.closeQuietly(out); + } + } finally { + IOUtils.closeQuietly(in); + } + return outputFile; + } + +} diff --git a/test/java/org/apache/fop/render/afp/AFPTestSuite.java b/test/java/org/apache/fop/render/afp/AFPTestSuite.java new file mode 100644 index 000000000..3f12746bf --- /dev/null +++ b/test/java/org/apache/fop/render/afp/AFPTestSuite.java @@ -0,0 +1,42 @@ +/* + * 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.afp; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite for FOP's AFP output. + */ +public class AFPTestSuite { + + /** + * Builds the test suite + * @return the test suite + */ + public static Test suite() { + TestSuite suite = new TestSuite( + "Test suite for AFP output"); + //$JUnit-BEGIN$ + suite.addTest(new TestSuite(NoOperationTestCase.class)); + //$JUnit-END$ + return suite; + } +} diff --git a/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java b/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java new file mode 100644 index 000000000..7f081a4d0 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/AbstractAFPTestCase.java @@ -0,0 +1,47 @@ +/* + * 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.afp; + +import java.io.File; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.MimeConstants; +import org.apache.fop.render.AbstractRenderingTestCase; + +/** + * Abstract base class for AFP verification tests. + */ +abstract class AbstractAFPTestCase extends AbstractRenderingTestCase { + + /** + * Renders a test file. + * @param ua the user agent (with override set!) + * @param resourceName the resource name for the FO file + * @param suffix a suffix for the output filename + * @return the output file + * @throws Exception if an error occurs + */ + protected File renderFile(FOUserAgent ua, String resourceName, String suffix) + throws Exception { + return renderFile(ua, resourceName, suffix, MimeConstants.MIME_AFP); + } + + +} diff --git a/test/java/org/apache/fop/render/afp/NoOperationTestCase.java b/test/java/org/apache/fop/render/afp/NoOperationTestCase.java new file mode 100644 index 000000000..56e4551d1 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/NoOperationTestCase.java @@ -0,0 +1,123 @@ +/* + * 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.afp; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import junit.framework.Assert; + +import org.apache.commons.io.IOUtils; + +import org.apache.fop.afp.AFPConstants; +import org.apache.fop.afp.parser.MODCAParser; +import org.apache.fop.afp.parser.UnparsedStructuredField; +import org.apache.fop.apps.FOUserAgent; + +/** + * Tests generation of afp:no-operation (NOPs). + */ +public class NoOperationTestCase extends AbstractAFPTestCase { + + /** + * Tests afp:no-operation. + * @throws Exception if an error occurs + */ + public void testNoOperation() throws Exception { + FOUserAgent ua = fopFactory.newFOUserAgent(); + File outputFile = renderFile(ua, "nops.fo", ""); + + InputStream in = new java.io.FileInputStream(outputFile); + try { + MODCAParser parser = new MODCAParser(in); + UnparsedStructuredField field = skipTo(parser, 0xD3A8A8); //Begin Document + + //NOP in fo:declarations + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:declarations", getNopText(field)); + + field = parser.readNextStructuredField(); + assertEquals(0xD3A8AD, field.getSfTypeID()); //Begin Named Page Group + + //NOPs in fo:page-sequence + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:page-sequence: start", getNopText(field)); + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:page-sequence: end", getNopText(field)); + + field = parser.readNextStructuredField(); + assertEquals(0xD3A8AF, field.getSfTypeID()); //Begin Page + + field = skipTo(parser, 0xD3A9C9); //End Active Environment Group + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:simple-page-master: first", getNopText(field)); + + field = skipTo(parser, 0xD3A9C9); //End Active Environment Group + field = parser.readNextStructuredField(); + assertEquals(0xD3EEEE, field.getSfTypeID()); + assertEquals("fo:simple-page-master: rest", getNopText(field)); + } finally { + IOUtils.closeQuietly(in); + } + + int counter = 0; + in = new java.io.FileInputStream(outputFile); + try { + MODCAParser parser = new MODCAParser(in); + while (true) { + UnparsedStructuredField field = parser.readNextStructuredField(); + if (field == null) { + break; + } + if (field.getSfTypeID() == 0xD3EEEE) { + counter++; + } + } + } finally { + IOUtils.closeQuietly(in); + } + assertEquals(6, counter); //decl, 2 * ps, 3 * page/spm + } + + private String getNopText(UnparsedStructuredField field) throws UnsupportedEncodingException { + byte[] data = field.getData(); + String text = new String(data, AFPConstants.EBCIDIC_ENCODING); + return text; + } + + private UnparsedStructuredField skipTo(MODCAParser parser, int typeID) throws IOException { + UnparsedStructuredField field = null; + do { + field = parser.readNextStructuredField(); + if (field.getSfTypeID() == typeID) { + return field; + } + } while (field != null); + Assert.fail("Structured field not found: " + Integer.toHexString(typeID)); + return null; + } + +} diff --git a/test/java/org/apache/fop/render/afp/nops.fo b/test/java/org/apache/fop/render/afp/nops.fo new file mode 100644 index 000000000..96c6e0d24 --- /dev/null +++ b/test/java/org/apache/fop/render/afp/nops.fo @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" + xmlns:afp="http://xmlgraphics.apache.org/fop/extensions/afp"> + <fo:layout-master-set> + <fo:simple-page-master master-name="first" page-height="10.5cm" page-width="14.85cm" margin="2cm"> + <afp:no-operation name="spm">fo:simple-page-master: first</afp:no-operation> + <fo:region-body margin-top="2em "/> + <fo:region-before extent="2em"/> + </fo:simple-page-master> + <fo:simple-page-master master-name="rest" page-height="10.5cm" page-width="14.85cm" margin="2cm"> + <afp:no-operation name="spm">fo:simple-page-master: rest</afp:no-operation> + <fo:region-body margin-top="2em "/> + <fo:region-before extent="2em"/> + </fo:simple-page-master> + <fo:page-sequence-master master-name="main"> + <fo:repeatable-page-master-alternatives> + <fo:conditional-page-master-reference master-reference="first" page-position="first"/> + <fo:conditional-page-master-reference master-reference="rest"/> + </fo:repeatable-page-master-alternatives> + </fo:page-sequence-master> + </fo:layout-master-set> + + <fo:declarations> + <afp:no-operation name="declarations">fo:declarations</afp:no-operation> + </fo:declarations> + + <fo:page-sequence master-reference="main" id="doc1"> + <afp:no-operation name="ps">fo:page-sequence: start</afp:no-operation> + <fo:flow flow-name="xsl-region-body"> + <fo:block>Page 1</fo:block> + <fo:block>Page 1</fo:block> + <fo:block break-after="page"></fo:block> + <fo:block>Page 2</fo:block> + <fo:block>Page 2</fo:block> + <fo:block break-after="page"></fo:block> + <fo:block>Page 3</fo:block> + <fo:block>Page 3</fo:block> + </fo:flow> + <afp:no-operation name="ps">fo:page-sequence: end</afp:no-operation> + </fo:page-sequence> +</fo:root> diff --git a/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java b/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java index 522d193ed..30e499846 100644 --- a/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java +++ b/test/java/org/apache/fop/render/ps/AbstractPostScriptTestCase.java @@ -21,20 +21,6 @@ package org.apache.fop.render.ps; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.MissingResourceException; - -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.stream.StreamSource; - -import junit.framework.TestCase; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.apache.xmlgraphics.ps.PSResource; import org.apache.xmlgraphics.ps.dsc.DSCException; @@ -44,19 +30,13 @@ import org.apache.xmlgraphics.ps.dsc.events.DSCComment; import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; import org.apache.fop.apps.FOUserAgent; -import org.apache.fop.apps.Fop; -import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; +import org.apache.fop.render.AbstractRenderingTestCase; /** * Abstract base class for PostScript verification tests. */ -public abstract class AbstractPostScriptTestCase extends TestCase { - - /** the JAXP TransformerFactory */ - protected TransformerFactory tFactory = TransformerFactory.newInstance(); - /** the FopFactory */ - protected FopFactory fopFactory = FopFactory.newInstance(); +public abstract class AbstractPostScriptTestCase extends AbstractRenderingTestCase { /** * Renders a test file. @@ -68,35 +48,7 @@ public abstract class AbstractPostScriptTestCase extends TestCase { */ protected File renderFile(FOUserAgent ua, String resourceName, String suffix) throws Exception { - File outputFile = new File("build/test-results/" + resourceName + suffix + ".ps"); - File outputDir = outputFile.getParentFile(); - FileUtils.forceMkdir(outputDir); - - // Prepare input file - InputStream in = getClass().getResourceAsStream(resourceName); - if (in == null) { - throw new MissingResourceException(resourceName + " not found in resources", - getClass().getName(), null); - } - try { - Source src = new StreamSource(in); - - // Create PostScript - OutputStream out = new java.io.FileOutputStream(outputFile); - out = new java.io.BufferedOutputStream(out); - try { - Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, ua, out); - SAXResult res = new SAXResult(fop.getDefaultHandler()); - - Transformer transformer = tFactory.newTransformer(); - transformer.transform(src, res); - } finally { - IOUtils.closeQuietly(out); - } - } finally { - IOUtils.closeQuietly(in); - } - return outputFile; + return renderFile(ua, resourceName, suffix, MimeConstants.MIME_POSTSCRIPT); } /** diff --git a/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java b/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java index 72c677a0c..c127d82a5 100644 --- a/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java +++ b/test/java/org/apache/fop/render/ps/ImageHandlingTestCase.java @@ -86,6 +86,7 @@ public class ImageHandlingTestCase extends AbstractPostScriptTestCase { gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); + gotoDSCComment(parser, DSCConstants.BEGIN_RESOURCE); PSResource form2 = new PSResource(PSResource.TYPE_FORM, "FOPForm:2"); checkResourceComment(parser, DSCConstants.BEGIN_RESOURCE, form2); diff --git a/test/java/org/apache/fop/render/ps/PSTestSuite.java b/test/java/org/apache/fop/render/ps/PSTestSuite.java new file mode 100644 index 000000000..05ba2dac7 --- /dev/null +++ b/test/java/org/apache/fop/render/ps/PSTestSuite.java @@ -0,0 +1,43 @@ +/* + * 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.ps; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test suite for FOP's PostScript output. + */ +public class PSTestSuite { + + /** + * Builds the test suite + * @return the test suite + */ + public static Test suite() { + TestSuite suite = new TestSuite( + "Test suite for PostScript output"); + //$JUnit-BEGIN$ + suite.addTest(new TestSuite(ImageHandlingTestCase.class)); + suite.addTest(new TestSuite(ResourceOptimizationTestCase.class)); + //$JUnit-END$ + return suite; + } +} diff --git a/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java b/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java index 862ad5205..327c86548 100644 --- a/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java +++ b/test/java/org/apache/fop/render/ps/ResourceOptimizationTestCase.java @@ -90,7 +90,7 @@ public class ResourceOptimizationTestCase extends AbstractPostScriptTestCase { = (DSCCommentDocumentSuppliedResources)gotoDSCComment(parser, DSCConstants.DOCUMENT_SUPPLIED_RESOURCES); Set resources = supplied.getResources(); - assertEquals(4, resources.size()); + assertEquals(5, resources.size()); assertTrue(resources.contains(form1)); assertTrue("Expected barcode.eps as supplied resource", resources.contains(new PSResource(PSResource.TYPE_FILE, |