diff options
author | Vincent Hennebert <vhennebert@apache.org> | 2011-04-18 18:36:05 +0000 |
---|---|---|
committer | Vincent Hennebert <vhennebert@apache.org> | 2011-04-18 18:36:05 +0000 |
commit | 427df0fa7d2f67a4d21740cbf7264d3b11739a41 (patch) | |
tree | 42dba5cd4eaf64a6d2a361415b313a32b9cfbe0d /test/java/org/apache/fop/layoutengine | |
parent | 06a7b3804cdff092d2f3d4265e463b1c20454842 (diff) | |
download | xmlgraphics-fop-427df0fa7d2f67a4d21740cbf7264d3b11739a41.tar.gz xmlgraphics-fop-427df0fa7d2f67a4d21740cbf7264d3b11739a41.zip |
Added id element to intermediate format to track the origin of content.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1094690 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/java/org/apache/fop/layoutengine')
8 files changed, 167 insertions, 306 deletions
diff --git a/test/java/org/apache/fop/layoutengine/EvalCheck.java b/test/java/org/apache/fop/layoutengine/EvalCheck.java index 32d9e689b..8065512a7 100644 --- a/test/java/org/apache/fop/layoutengine/EvalCheck.java +++ b/test/java/org/apache/fop/layoutengine/EvalCheck.java @@ -42,16 +42,6 @@ public class EvalCheck implements LayoutEngineCheck, IFCheck { private PrefixResolver prefixResolver; /** - * Creates a new instance - * @param expected expected value - * @param xpath XPath statement that needs to be evaluated - */ - public EvalCheck(String expected, String xpath) { - this.expected = expected; - this.xpath = xpath; - } - - /** * Creates a new instance from a DOM node. * @param node DOM node that defines this check */ diff --git a/test/java/org/apache/fop/layoutengine/LayoutEngineCheck.java b/test/java/org/apache/fop/layoutengine/LayoutEngineCheck.java index 155db2263..ca979efe5 100644 --- a/test/java/org/apache/fop/layoutengine/LayoutEngineCheck.java +++ b/test/java/org/apache/fop/layoutengine/LayoutEngineCheck.java @@ -19,10 +19,12 @@ package org.apache.fop.layoutengine; +import org.apache.fop.check.Check; + /** * Defines the interface for check operations. */ -public interface LayoutEngineCheck { +public interface LayoutEngineCheck extends Check { /** * Called to perform the check. diff --git a/test/java/org/apache/fop/layoutengine/LayoutEngineChecksFactory.java b/test/java/org/apache/fop/layoutengine/LayoutEngineChecksFactory.java new file mode 100644 index 000000000..bea54c5f8 --- /dev/null +++ b/test/java/org/apache/fop/layoutengine/LayoutEngineChecksFactory.java @@ -0,0 +1,62 @@ +/* + * 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.layoutengine; + +import org.w3c.dom.Element; + +import org.apache.fop.check.ChecksFactory; + +/** + * A factory class for creating {@link LayoutEngineCheck} instances. + */ +final class LayoutEngineChecksFactory extends ChecksFactory<LayoutEngineCheck> { + + LayoutEngineChecksFactory() { + registerCheckFactory("true", new CheckFactory<LayoutEngineCheck>() { + + public LayoutEngineCheck createCheck(Element element) { + return new TrueCheck(element); + } + + }); + registerCheckFactory("eval", new CheckFactory<LayoutEngineCheck>() { + + public LayoutEngineCheck createCheck(Element element) { + return new EvalCheck(element); + } + + }); + registerCheckFactory("element-list", new CheckFactory<LayoutEngineCheck>() { + + public LayoutEngineCheck createCheck(Element element) { + return new ElementListCheck(element); + } + + }); + registerCheckFactory("result", new CheckFactory<LayoutEngineCheck>() { + + public LayoutEngineCheck createCheck(Element element) { + return new ResultCheck(element); + } + + }); + } + +} diff --git a/test/java/org/apache/fop/layoutengine/LayoutEngineTestSuite.java b/test/java/org/apache/fop/layoutengine/LayoutEngineTestSuite.java index 32a48fb0b..397374657 100644 --- a/test/java/org/apache/fop/layoutengine/LayoutEngineTestSuite.java +++ b/test/java/org/apache/fop/layoutengine/LayoutEngineTestSuite.java @@ -56,12 +56,16 @@ import org.apache.fop.DebugHelper; /** * JUnit test suit for running layout engine test under JUnit control. */ -public class LayoutEngineTestSuite { +public final class LayoutEngineTestSuite { static { DebugHelper.registerStandardElementListObservers(); } + private LayoutEngineTestSuite() { + // This is a utility class + } + public static String[] readDisabledTestcases(File f) throws IOException { List lines = new java.util.ArrayList(); Source stylesheet = new StreamSource( @@ -178,45 +182,33 @@ public class LayoutEngineTestSuite { Iterator i = files.iterator(); while (i.hasNext()) { File f = (File)i.next(); - addTestCase(suite, tester, f); + suite.addTest(new LayoutEngineTestCase(f, tester)); } return suite; } - private static void addTestCase(TestSuite suite, - final LayoutEngineTester tester, final File f) { - suite.addTest(new LayoutEngineTestCase(f.getName()) { - public void runTest() throws Exception { - try { - prepare(tester, f); - testMain(); - } catch (Exception e) { - org.apache.commons.logging.LogFactory.getLog( - this.getClass()).error("Error on " + f.getName()); - throw e; - } - } - }); - } - private static class LayoutEngineTestCase extends TestCase { - private LayoutEngineTester tester; - private File testFile; + private final File testFile; - public LayoutEngineTestCase(String name) { - super(name); - } + private final LayoutEngineTester tester; - public void prepare(LayoutEngineTester tester, File testFile) { - //super(testFile.getName()); - this.tester = tester; + LayoutEngineTestCase(File testFile, LayoutEngineTester tester) { + super(testFile.getName()); this.testFile = testFile; + this.tester = tester; } - public void testMain() throws Exception { - tester.runTest(testFile); + @Override + protected void runTest() throws Throwable { + try { + tester.runTest(testFile); + } catch (Exception e) { + org.apache.commons.logging.LogFactory.getLog( + this.getClass()).error("Error on " + getName()); + throw e; + } } } } diff --git a/test/java/org/apache/fop/layoutengine/LayoutEngineTester.java b/test/java/org/apache/fop/layoutengine/LayoutEngineTester.java index 2c0cf8504..51c05a95f 100644 --- a/test/java/org/apache/fop/layoutengine/LayoutEngineTester.java +++ b/test/java/org/apache/fop/layoutengine/LayoutEngineTester.java @@ -21,37 +21,42 @@ package org.apache.fop.layoutengine; import java.io.File; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.util.Iterator; import java.util.List; -import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.TransformerHandler; -import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.Node; import org.w3c.dom.NodeList; - +import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.FormattingResults; +import org.apache.fop.area.AreaTreeModel; +import org.apache.fop.area.AreaTreeParser; +import org.apache.fop.area.RenderPagesModel; import org.apache.fop.events.model.EventSeverity; +import org.apache.fop.fonts.FontInfo; import org.apache.fop.intermediate.IFTester; +import org.apache.fop.intermediate.TestAssistant; import org.apache.fop.layoutmgr.ElementListObserver; +import org.apache.fop.render.intermediate.IFContext; +import org.apache.fop.render.intermediate.IFRenderer; +import org.apache.fop.render.intermediate.IFSerializer; import org.apache.fop.render.xml.XMLRenderer; import org.apache.fop.util.ConsoleEventListenerForTests; +import org.apache.fop.util.DelegatingContentHandler; /** * Class for testing the FOP's layout engine using testcases specified in XML @@ -59,19 +64,13 @@ import org.apache.fop.util.ConsoleEventListenerForTests; */ public class LayoutEngineTester { - private static final Map AT_CHECK_CLASSES = new java.util.HashMap(); - - private TestEnvironment env = new TestEnvironment(); + private TestAssistant testAssistant = new TestAssistant(); + private LayoutEngineChecksFactory layoutEngineChecksFactory = new LayoutEngineChecksFactory(); private File areaTreeBackupDir; private IFTester ifTester; - static { - AT_CHECK_CLASSES.put("true", TrueCheck.class); - AT_CHECK_CLASSES.put("eval", EvalCheck.class); - AT_CHECK_CLASSES.put("element-list", ElementListCheck.class); - AT_CHECK_CLASSES.put("result", ResultCheck.class); - } + private TransformerFactory tfactory = TransformerFactory.newInstance(); /** * Constructs a new instance. @@ -80,7 +79,7 @@ public class LayoutEngineTester { */ public LayoutEngineTester(File areaTreeBackupDir) { this.areaTreeBackupDir = areaTreeBackupDir; - this.ifTester = new IFTester(areaTreeBackupDir); + this.ifTester = new IFTester(tfactory, areaTreeBackupDir); } /** @@ -100,18 +99,18 @@ public class LayoutEngineTester { ElementListObserver.addObserver(elCollector); Fop fop; - + FopFactory effFactory; try { - Document testDoc = env.loadTestCase(testFile); - FopFactory effFactory = env.getFopFactory(testDoc); + Document testDoc = testAssistant.loadTestCase(testFile); + effFactory = testAssistant.getFopFactory(testDoc); //Setup Transformer to convert the testcase XML to XSL-FO - Transformer transformer = env.getTestcase2FOStylesheet().newTransformer(); + Transformer transformer = testAssistant.getTestcase2FOStylesheet().newTransformer(); Source src = new DOMSource(testDoc); //Setup Transformer to convert the area tree to a DOM TransformerHandler athandler; - athandler = env.getTransformerFactory().newTransformerHandler(); + athandler = testAssistant.getTransformerFactory().newTransformerHandler(); athandler.setResult(domres); //Setup FOP for area tree rendering @@ -134,91 +133,96 @@ public class LayoutEngineTester { Document doc = (Document)domres.getNode(); if (this.areaTreeBackupDir != null) { - env.saveDOM(doc, + testAssistant.saveDOM(doc, new File(this.areaTreeBackupDir, testFile.getName() + ".at.xml")); } FormattingResults results = fop.getResults(); LayoutResult result = new LayoutResult(doc, elCollector, results); - checkAll(testFile, result); + checkAll(effFactory, testFile, result); } /** - * Factory method to create AT checks from DOM elements. - * @param el DOM element to create the check from - * @return The newly create check - */ - protected LayoutEngineCheck createATCheck(Element el) { - String name = el.getTagName(); - Class clazz = (Class)AT_CHECK_CLASSES.get(name); - if (clazz != null) { - try { - Constructor c = clazz.getDeclaredConstructor(new Class[] {Node.class}); - LayoutEngineCheck instance = (LayoutEngineCheck)c.newInstance(new Object[] {el}); - return instance; - } catch (Exception e) { - throw new RuntimeException("Error while instantiating check '" - + name + "': " + e.getMessage()); - } - } else { - throw new IllegalArgumentException("No check class found: " + name); - } - } - - - /** * Perform all checks on the area tree and, optionally, on the intermediate format. * @param testFile Test case XML file * @param result The layout results * @throws TransformerException if a problem occurs in XSLT/JAXP */ - protected void checkAll(File testFile, LayoutResult result) throws TransformerException { - Transformer transformer = env.getTestcase2ChecksStylesheet().newTransformer(); - Source src = new StreamSource(testFile); - DOMResult res = new DOMResult(); - transformer.transform(src, res); - - Document doc = (Document)res.getNode(); - Element root = doc.getDocumentElement(); + protected void checkAll(FopFactory fopFactory, File testFile, LayoutResult result) + throws TransformerException { + Element testRoot = testAssistant.getTestRoot(testFile); NodeList nodes; //AT tests only when checks are available - nodes = root.getElementsByTagName("at-checks"); + nodes = testRoot.getElementsByTagName("at-checks"); if (nodes.getLength() > 0) { Element atChecks = (Element)nodes.item(0); doATChecks(atChecks, result); } //IF tests only when checks are available - nodes = root.getElementsByTagName("if-checks"); + nodes = testRoot.getElementsByTagName("if-checks"); if (nodes.getLength() > 0) { Element ifChecks = (Element)nodes.item(0); - ifTester.doIFChecks(testFile, ifChecks, result.getAreaTree()); + Document ifDocument = createIF(fopFactory, testFile, result.getAreaTree()); + ifTester.doIFChecks(testFile.getName(), ifChecks, ifDocument); } } - private void doATChecks(Element checksRoot, LayoutResult result) { - //First create check before actually running them - List checks = new java.util.ArrayList(); - NodeList nodes = checksRoot.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (node instanceof Element) { - checks.add(createATCheck((Element)node)); - } - } + private Document createIF(FopFactory fopFactory, File testFile, Document areaTreeXML) + throws TransformerException { + try { + FOUserAgent ua = fopFactory.newFOUserAgent(); + ua.setBaseURL(testFile.getParentFile().toURI().toURL().toExternalForm()); + ua.getEventBroadcaster().addEventListener( + new ConsoleEventListenerForTests(testFile.getName(), EventSeverity.WARN)); - if (checks.size() == 0) { - throw new RuntimeException("No checks are available!"); - } + IFRenderer ifRenderer = new IFRenderer(); + ifRenderer.setUserAgent(ua); + + IFSerializer serializer = new IFSerializer(); + serializer.setContext(new IFContext(ua)); + DOMResult result = new DOMResult(); + serializer.setResult(result); + ifRenderer.setDocumentHandler(serializer); + + ua.setRendererOverride(ifRenderer); + FontInfo fontInfo = new FontInfo(); + //Construct the AreaTreeModel that will received the individual pages + final AreaTreeModel treeModel = new RenderPagesModel(ua, + null, fontInfo, null); + + //Iterate over all intermediate files + AreaTreeParser parser = new AreaTreeParser(); + ContentHandler handler = parser.getContentHandler(treeModel, ua); + + DelegatingContentHandler proxy = new DelegatingContentHandler() { - //Run the actual tests now that we know that the checks themselves are ok - doATChecks(checks, result); + public void endDocument() throws SAXException { + super.endDocument(); + //Signal the end of the processing. + //The renderer can finalize the target document. + treeModel.endDocument(); + } + + }; + proxy.setDelegateContentHandler(handler); + + Transformer transformer = tfactory.newTransformer(); + transformer.transform(new DOMSource(areaTreeXML), new SAXResult(proxy)); + + return (Document)result.getNode(); + } catch (Exception e) { + throw new TransformerException( + "Error while generating intermediate format file: " + e.getMessage(), e); + } } - private void doATChecks(List checks, LayoutResult result) { - Iterator i = checks.iterator(); - while (i.hasNext()) { - LayoutEngineCheck check = (LayoutEngineCheck)i.next(); + private void doATChecks(Element checksRoot, LayoutResult result) { + List<LayoutEngineCheck> checks = layoutEngineChecksFactory.createCheckList(checksRoot); + if (checks.size() == 0) { + throw new RuntimeException("No available area tree check"); + } + for (LayoutEngineCheck check : checks) { check.check(result); } } diff --git a/test/java/org/apache/fop/layoutengine/ResultCheck.java b/test/java/org/apache/fop/layoutengine/ResultCheck.java index 54af77a43..ce95c5024 100644 --- a/test/java/org/apache/fop/layoutengine/ResultCheck.java +++ b/test/java/org/apache/fop/layoutengine/ResultCheck.java @@ -19,9 +19,10 @@ package org.apache.fop.layoutengine; -import org.apache.fop.apps.FormattingResults; import org.w3c.dom.Node; +import org.apache.fop.apps.FormattingResults; + /** * Simple check that requires a result property to evaluate to the expected value */ @@ -31,16 +32,6 @@ public class ResultCheck implements LayoutEngineCheck { private String property; /** - * Creates a new instance - * @param expected expected value - * @param property property of which the value needs to be evaluated - */ - public ResultCheck(String expected, String property) { - this.expected = expected; - this.property = property; - } - - /** * Creates a new instance from a DOM node. * @param node DOM node that defines this check */ diff --git a/test/java/org/apache/fop/layoutengine/TestEnvironment.java b/test/java/org/apache/fop/layoutengine/TestEnvironment.java deleted file mode 100644 index ad976ff8c..000000000 --- a/test/java/org/apache/fop/layoutengine/TestEnvironment.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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.layoutengine; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Templates; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.sax.SAXTransformerFactory; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; - -import org.w3c.dom.Document; - -import org.apache.xpath.XPathAPI; -import org.apache.xpath.objects.XObject; - -import org.apache.fop.apps.FopFactory; - -/** - * Test environment and helper code for running FOP tests. - */ -public class TestEnvironment { - - // configure fopFactory as desired - private FopFactory fopFactory = FopFactory.newInstance(); - private FopFactory fopFactoryWithBase14Kerning = FopFactory.newInstance(); - - private SAXTransformerFactory tfactory - = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); - - private DocumentBuilderFactory domBuilderFactory; - - private Templates testcase2fo; - private Templates testcase2checks; - - /** - * Main constructor. - */ - public TestEnvironment() { - fopFactory.getFontManager().setBase14KerningEnabled(false); - fopFactoryWithBase14Kerning.getFontManager().setBase14KerningEnabled(true); - domBuilderFactory = DocumentBuilderFactory.newInstance(); - domBuilderFactory.setNamespaceAware(true); - domBuilderFactory.setValidating(false); - } - - /** - * Returns the stylesheet for convert extracting the XSL-FO part from the test case. - * @return the stylesheet - * @throws TransformerConfigurationException if an error occurs loading the stylesheet - */ - public Templates getTestcase2FOStylesheet() throws TransformerConfigurationException { - if (testcase2fo == null) { - //Load and cache stylesheet - Source src = new StreamSource(new File("test/layoutengine/testcase2fo.xsl")); - testcase2fo = tfactory.newTemplates(src); - } - return testcase2fo; - } - - /** - * Returns the stylesheet for convert extracting the checks from the test case. - * @return the stylesheet - * @throws TransformerConfigurationException if an error occurs loading the stylesheet - */ - public Templates getTestcase2ChecksStylesheet() throws TransformerConfigurationException { - if (testcase2checks == null) { - //Load and cache stylesheet - Source src = new StreamSource(new File("test/layoutengine/testcase2checks.xsl")); - testcase2checks = tfactory.newTemplates(src); - } - return testcase2checks; - } - - public FopFactory getFopFactory(boolean base14KerningEnabled) { - FopFactory effFactory = (base14KerningEnabled ? fopFactoryWithBase14Kerning : fopFactory); - return effFactory; - } - - public FopFactory getFopFactory(Document testDoc) { - boolean base14KerningEnabled = isBase14KerningEnabled(testDoc); - FopFactory effFactory = getFopFactory(base14KerningEnabled); - - boolean strictValidation = isStrictValidation(testDoc); - effFactory.setStrictValidation(strictValidation); - - return effFactory; - } - - private boolean isBase14KerningEnabled(Document testDoc) { - try { - XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/base14kerning"); - String s = xo.str(); - return ("true".equalsIgnoreCase(s)); - } catch (TransformerException e) { - throw new RuntimeException("Error while evaluating XPath expression", e); - } - } - - private boolean isStrictValidation(Document testDoc) { - try { - XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/strict-validation"); - return !("false".equalsIgnoreCase(xo.str())); - } catch (TransformerException e) { - throw new RuntimeException("Error while evaluating XPath expression", e); - } - } - - /** - * Loads a test case into a DOM document. - * @param testFile the test file - * @return the loaded test case - * @throws IOException if an I/O error occurs loading the test case - */ - public Document loadTestCase(File testFile) - throws IOException { - try { - DocumentBuilder builder = domBuilderFactory.newDocumentBuilder(); - Document testDoc = builder.parse(testFile); - return testDoc; - } catch (Exception e) { - throw new IOException("Error while loading test case: " + e.getMessage()); - } - } - - /** - * Serialize the DOM for later inspection. - * @param doc the DOM document - * @param target target file - * @throws TransformerException if a problem occurs during serialization - */ - public void saveDOM(Document doc, File target) throws TransformerException { - Transformer transformer = getTransformerFactory().newTransformer(); - Source src = new DOMSource(doc); - Result res = new StreamResult(target); - transformer.transform(src, res); - } - - /** - * Returns the SAXTransformerFactory. - * @return the SAXTransformerFactory - */ - public SAXTransformerFactory getTransformerFactory() { - return tfactory; - } -} diff --git a/test/java/org/apache/fop/layoutengine/TrueCheck.java b/test/java/org/apache/fop/layoutengine/TrueCheck.java index 94ae942de..77d76b91d 100644 --- a/test/java/org/apache/fop/layoutengine/TrueCheck.java +++ b/test/java/org/apache/fop/layoutengine/TrueCheck.java @@ -42,14 +42,6 @@ public class TrueCheck implements LayoutEngineCheck, IFCheck { private PrefixResolver prefixResolver; /** - * Creates a new instance - * @param xpath XPath statement that needs to be evaluated - */ - public TrueCheck(String xpath) { - this.xpath = xpath; - } - - /** * Creates a new instance from a DOM node. * @param node DOM node that defines this check */ |