]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Initial commit for a FO tree testing facility.
authorJeremias Maerki <jeremias@apache.org>
Tue, 30 Aug 2005 06:09:14 +0000 (06:09 +0000)
committerJeremias Maerki <jeremias@apache.org>
Tue, 30 Aug 2005 06:09:14 +0000 (06:09 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@264709 13f79535-47bb-0310-9956-ffa450edef68

test/fotree/testcases/demo-test-failure.fo [new file with mode: 0644]
test/fotree/testcases/demo-test-success.fo [new file with mode: 0644]
test/java/META-INF/services/org.apache.fop.fo.ElementMapping [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/DummyFOEventHandler.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/FOTreeTestSuite.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/FOTreeTester.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/ResultCollector.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/ext/AssertElement.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/ext/TestElementMapping.java [new file with mode: 0644]
test/java/org/apache/fop/fotreetest/ext/TestObj.java [new file with mode: 0644]

diff --git a/test/fotree/testcases/demo-test-failure.fo b/test/fotree/testcases/demo-test-failure.fo
new file mode 100644 (file)
index 0000000..b3784b4
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:test="http://xmlgraphics.apache.org/fop/test">\r
+  <fo:layout-master-set>\r
+    <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">\r
+      <fo:region-body/>\r
+    </fo:simple-page-master>\r
+  </fo:layout-master-set>\r
+  <fo:page-sequence master-reference="simpleA4">\r
+    <fo:flow flow-name="xsl-region-body">\r
+      <fo:block font-size="14pt">Hello World!\r
+        <test:assert property="font-family" expected="sans-serif"/>\r
+        <test:assert property="font-size" expected="12000mpt"/>\r
+      </fo:block>\r
+      <fo:block font-family="ZapfDingbats">&#2702;</fo:block>\r
+    </fo:flow>\r
+  </fo:page-sequence>\r
+</fo:root>\r
diff --git a/test/fotree/testcases/demo-test-success.fo b/test/fotree/testcases/demo-test-success.fo
new file mode 100644 (file)
index 0000000..7912c91
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:test="http://xmlgraphics.apache.org/fop/test">\r
+  <fo:layout-master-set>\r
+    <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">\r
+      <fo:region-body/>\r
+    </fo:simple-page-master>\r
+  </fo:layout-master-set>\r
+  <fo:page-sequence master-reference="simpleA4">\r
+    <fo:flow flow-name="xsl-region-body">\r
+      <fo:block>Hello World!\r
+        <test:assert property="font-family" expected="sans-serif"/>\r
+        <test:assert property="font-size" expected="12000mpt"/>\r
+      </fo:block>\r
+      <fo:block font-family="ZapfDingbats">&#2702;</fo:block>\r
+    </fo:flow>\r
+  </fo:page-sequence>\r
+</fo:root>\r
diff --git a/test/java/META-INF/services/org.apache.fop.fo.ElementMapping b/test/java/META-INF/services/org.apache.fop.fo.ElementMapping
new file mode 100644 (file)
index 0000000..f270d50
--- /dev/null
@@ -0,0 +1,2 @@
+org.apache.fop.fotreetest.ext.TestElementMapping\r
+\r
diff --git a/test/java/org/apache/fop/fotreetest/DummyFOEventHandler.java b/test/java/org/apache/fop/fotreetest/DummyFOEventHandler.java
new file mode 100644 (file)
index 0000000..d6e97db
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.fotreetest;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.fo.FOEventHandler;
+
+/**
+ * Dummy FOEventHandler implementation that does nothing.
+ */
+public class DummyFOEventHandler extends FOEventHandler {
+
+    /**
+     * Main constructor.
+     * @param foUserAgent the user agent
+     */
+    public DummyFOEventHandler(FOUserAgent foUserAgent) {
+        super(foUserAgent);
+    }
+
+}
diff --git a/test/java/org/apache/fop/fotreetest/FOTreeTestSuite.java b/test/java/org/apache/fop/fotreetest/FOTreeTestSuite.java
new file mode 100644 (file)
index 0000000..26f8994
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.fotreetest;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.AndFileFilter;
+import org.apache.commons.io.filefilter.IOFileFilter;
+import org.apache.commons.io.filefilter.NameFileFilter;
+import org.apache.commons.io.filefilter.PrefixFileFilter;
+import org.apache.commons.io.filefilter.SuffixFileFilter;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.fop.DebugHelper;
+import org.apache.fop.layoutengine.LayoutEngineTestSuite;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * JUnit test suit for running layout engine test under JUnit control.
+ */
+public final class FOTreeTestSuite {
+
+    static {
+        DebugHelper.registerStandardElementListObservers();
+    }
+    
+    private FOTreeTestSuite() {
+        //don't instantiate!
+    }
+    
+    /**
+     * @return the test suite with all the tests (one for each XML file)
+     * @throws IOException in case of an I/O problem
+     */
+    public static Test suite() throws IOException {
+        TestSuite suite = new TestSuite();
+
+        File mainDir = new File("test/fotree");
+
+        final FOTreeTester tester = new FOTreeTester();
+        
+        IOFileFilter filter;
+        String single = System.getProperty("fop.fotree.single");
+        String startsWith = System.getProperty("fop.fotree.starts-with");
+        if (single != null) {
+            filter = new NameFileFilter(single);
+        } else if (startsWith != null) {
+            filter = new PrefixFileFilter(startsWith);
+            filter = new AndFileFilter(filter, new SuffixFileFilter(".fo"));
+        } else {
+            filter = new SuffixFileFilter(".fo");
+            filter = LayoutEngineTestSuite.decorateWithDisabledList(filter);
+        }
+        Collection files = FileUtils.listFiles(new File(mainDir, "testcases"), 
+                filter, TrueFileFilter.INSTANCE);
+        String privateTests = System.getProperty("fop.fotree.private");
+        if ("true".equalsIgnoreCase(privateTests)) {
+            Collection privateFiles = FileUtils.listFiles(
+                    new File(mainDir, "private-testcases"), 
+                    filter, TrueFileFilter.INSTANCE);
+            files.addAll(privateFiles);
+        }
+        Iterator i = files.iterator();
+        while (i.hasNext()) {
+            File f = (File)i.next();
+            addTestCase(suite, tester, f);
+        }
+        
+        return suite;
+    }
+    
+    private static void addTestCase(TestSuite suite, 
+                final FOTreeTester tester, final File f) {
+        suite.addTest(new FOTreeTestCase(f.getName()) {
+            public void runTest() throws Exception {
+                prepare(tester, f);
+                testMain();
+            }
+        });
+    }
+    
+    private static class FOTreeTestCase extends TestCase {
+        
+        private FOTreeTester tester;
+        private File testFile;
+        
+        public FOTreeTestCase(String name) {
+            super(name);
+        }
+        
+        public void prepare(FOTreeTester tester, File testFile) {
+            //super(testFile.getName());
+            this.tester = tester;
+            this.testFile = testFile;
+        }
+        
+        public void testMain() throws Exception {
+            tester.runTest(testFile);
+        }
+    }    
+}
diff --git a/test/java/org/apache/fop/fotreetest/FOTreeTester.java b/test/java/org/apache/fop/fotreetest/FOTreeTester.java
new file mode 100644 (file)
index 0000000..4193110
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.fotreetest;
+
+import java.io.File;
+import java.util.List;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.Fop;
+import org.apache.fop.fo.Constants;
+
+/**
+ * Test driver class for FO tree tests.
+ */
+public class FOTreeTester {
+
+    private SAXTransformerFactory tfactory 
+            = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
+
+    /**
+     * Runs a test.
+     * @param testFile the test file.
+     * @throws Exception if a test or FOP itself fails
+     */
+    public void runTest(File testFile) throws Exception {
+        ResultCollector collector = ResultCollector.getInstance();
+        collector.reset();
+        
+        //Setup identity Transformer 
+        Transformer transformer = tfactory.newTransformer();
+        Source src = new StreamSource(testFile);
+        
+        //Setup FOP for area tree rendering
+        FOUserAgent ua = new FOUserAgent();
+        ua.setBaseURL(testFile.getParentFile().toURL().toString());
+        ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
+        Fop fop = new Fop(Constants.RENDER_XML, ua);
+        
+        SAXResult fores = new SAXResult(fop.getDefaultHandler());
+        transformer.transform(src, fores);
+        
+        List results = collector.getResults();
+        if (results.size() > 0) {
+            for (int i = 0; i < results.size(); i++) {
+                System.out.println(((Exception)results.get(i)).getMessage());
+            }
+            throw (Exception)results.get(0);
+        }
+    }
+
+}
diff --git a/test/java/org/apache/fop/fotreetest/ResultCollector.java b/test/java/org/apache/fop/fotreetest/ResultCollector.java
new file mode 100644 (file)
index 0000000..9c85ec9
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.fotreetest;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * This class collects the results from assertions injected into the FO stream. 
+ */
+public class ResultCollector {
+
+    private static ResultCollector instance = null;
+    
+    private List results = new java.util.ArrayList();
+    
+    /** @return the ResultColletor singleton */
+    public static ResultCollector getInstance() {
+        if (instance == null) {
+            instance = new ResultCollector();
+        }
+        return instance;
+    }
+    
+    /** Main constructor. */
+    public ResultCollector() {
+        //nop
+    }
+    
+    /**
+     * This notifies the ResultCollector about an Exception.
+     * @param e the exception
+     */
+    public void notifyException(Exception e) {
+        System.out.println(e.getMessage());
+        results.add(e);
+    }
+    
+    /** Resets the result list. */
+    public void reset() {
+        results.clear();
+    }
+    
+    /** @return the list of results */
+    public List getResults() {
+        return Collections.unmodifiableList(results);
+    }
+}
diff --git a/test/java/org/apache/fop/fotreetest/ext/AssertElement.java b/test/java/org/apache/fop/fotreetest/ext/AssertElement.java
new file mode 100644 (file)
index 0000000..1697b89
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed 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.fotreetest.ext;
+
+
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.FOPropertyMapping;
+import org.apache.fop.fo.PropertyList;
+import org.apache.fop.fo.properties.Property;
+import org.apache.fop.fotreetest.ResultCollector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+
+/**
+ * Defines the assert element for the FOP Test extension.
+ */
+public class AssertElement extends TestObj {
+
+    /**
+     * @see org.apache.fop.fo.FONode#FONode(FONode)
+     */
+    public AssertElement(FONode parent) {
+        super(parent);
+    }
+
+    /**
+     * @see org.apache.fop.fo.FONode#processNode
+     */
+    public void processNode(String elementName, 
+                            Locator locator, 
+                            Attributes attlist, 
+                            PropertyList propertyList) throws FOPException {
+        super.processNode(elementName, locator, attlist, propertyList);
+
+        ResultCollector collector = ResultCollector.getInstance();
+        String propName = attlist.getValue("property"); 
+        int propID = FOPropertyMapping.getPropertyId(propName);
+        if (propID < 0) {
+            collector.notifyException(new IllegalArgumentException(
+                    "Property not found: " + propName));
+        } else {
+            Property prop = propertyList.get(propID);
+            String s = String.valueOf(prop);
+            String expected = attlist.getValue("expected");
+            if (!expected.equals(s)) {
+                collector.notifyException(new IllegalStateException("Property '" + propName 
+                        + "' expected to evaluate to '" + expected + "' but got: " + s));
+            }
+        }
+        
+    }
+
+}
+
diff --git a/test/java/org/apache/fop/fotreetest/ext/TestElementMapping.java b/test/java/org/apache/fop/fotreetest/ext/TestElementMapping.java
new file mode 100644 (file)
index 0000000..9032501
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed 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.fotreetest.ext;
+
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.ElementMapping;
+
+/**
+ * This class provides the element mapping for FOP.
+ */
+public class TestElementMapping extends ElementMapping {
+
+    /** MathML Namespace */
+    public static final String NAMESPACE = "http://xmlgraphics.apache.org/fop/test"; 
+
+    /**
+     * Main constructor
+     */
+    public TestElementMapping() {
+        this.namespaceURI = NAMESPACE;
+    }
+
+    /** @see org.apache.fop.fo.ElementMapping#initialize() */
+    protected void initialize() {
+        if (foObjs == null) {
+            foObjs = new java.util.HashMap();
+            foObjs.put("assert", new AssertMaker());
+            foObjs.put(DEFAULT, new TestMaker());
+        }
+    }
+
+    static class TestMaker extends ElementMapping.Maker {
+        public FONode make(FONode parent) {
+            return new TestObj(parent);
+        }
+    }
+
+    static class AssertMaker extends ElementMapping.Maker {
+        public FONode make(FONode parent) {
+            return new AssertElement(parent);
+        }
+    }
+
+}
diff --git a/test/java/org/apache/fop/fotreetest/ext/TestObj.java b/test/java/org/apache/fop/fotreetest/ext/TestObj.java
new file mode 100644 (file)
index 0000000..245f81b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed 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.fotreetest.ext;
+
+// FOP
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.FObj;
+
+/**
+ * Catch all FOP Test objects as default element.
+ */
+public class TestObj extends FObj {
+
+    /**
+     * @see org.apache.fop.fo.FONode#FONode(FONode)
+     */
+    public TestObj(FONode parent) {
+        super(parent);
+    }
+
+    /**
+     * @see org.apache.fop.fo.XMLObj#getNameSpace()
+     */
+    public String getNameSpace() {
+        return TestElementMapping.NAMESPACE;
+    }
+}
+