From 4959d62981b03bb84c3b0bb09d79272ba6d459ae Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Thu, 8 Jun 2006 12:36:23 +0000 Subject: [PATCH] Set a PrefixResolver for the "true" check, too, not only for "eval". Added optional "tolerance" value for number comparisons using the "eval" check. Simplifies certain checks. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@412732 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/layoutengine/EvalCheck.java | 32 +++++++++++-------- .../apache/fop/layoutengine/TrueCheck.java | 6 +++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/test/java/org/apache/fop/layoutengine/EvalCheck.java b/test/java/org/apache/fop/layoutengine/EvalCheck.java index 0b3d98afb..497b324a9 100644 --- a/test/java/org/apache/fop/layoutengine/EvalCheck.java +++ b/test/java/org/apache/fop/layoutengine/EvalCheck.java @@ -33,6 +33,7 @@ public class EvalCheck implements LayoutEngineCheck { private String expected; private String xpath; + private double tolerance; private PrefixResolver prefixResolver; /** @@ -52,6 +53,10 @@ public class EvalCheck implements LayoutEngineCheck { public EvalCheck(Node node) { this.expected = node.getAttributes().getNamedItem("expected").getNodeValue(); this.xpath = node.getAttributes().getNamedItem("xpath").getNodeValue(); + Node nd = node.getAttributes().getNamedItem("tolerance"); + if (nd != null) { + this.tolerance = Double.parseDouble(nd.getNodeValue()); + } this.prefixResolver = new PrefixResolverDefault(node); } @@ -64,12 +69,21 @@ public class EvalCheck implements LayoutEngineCheck { throw new RuntimeException("XPath evaluation failed: " + e.getMessage()); } String actual = res.str(); //Second str() seems to fail. D'oh! - if (!expected.equals(actual)) { - throw new RuntimeException( - "Expected XPath expression to evaluate to '" + expected + "', but got '" - + actual + "' (" + this + ")"); + if (tolerance != 0) { + double v1 = Double.parseDouble(expected); + double v2 = Double.parseDouble(actual); + if (Math.abs(v1 - v2) > tolerance) { + throw new RuntimeException( + "Expected XPath expression to evaluate to '" + expected + "', but got '" + + actual + "' (" + this + ", outside tolerance)"); + } + } else { + if (!expected.equals(actual)) { + throw new RuntimeException( + "Expected XPath expression to evaluate to '" + expected + "', but got '" + + actual + "' (" + this + ")"); + } } - } /** @see java.lang.Object#toString() */ @@ -77,12 +91,4 @@ public class EvalCheck implements LayoutEngineCheck { return "XPath: " + xpath; } - private class MyPrefixResolver extends PrefixResolverDefault { - - public MyPrefixResolver(Node xpathExpressionContext) { - super(xpathExpressionContext); - } - - } - } diff --git a/test/java/org/apache/fop/layoutengine/TrueCheck.java b/test/java/org/apache/fop/layoutengine/TrueCheck.java index eb2c3a40b..afb712c08 100644 --- a/test/java/org/apache/fop/layoutengine/TrueCheck.java +++ b/test/java/org/apache/fop/layoutengine/TrueCheck.java @@ -20,6 +20,8 @@ package org.apache.fop.layoutengine; import javax.xml.transform.TransformerException; +import org.apache.xml.utils.PrefixResolver; +import org.apache.xml.utils.PrefixResolverDefault; import org.apache.xpath.XPathAPI; import org.apache.xpath.objects.XBoolean; import org.apache.xpath.objects.XObject; @@ -32,6 +34,7 @@ public class TrueCheck implements LayoutEngineCheck { private String xpath; private String failureMessage; + private PrefixResolver prefixResolver; /** * Creates a new instance @@ -51,13 +54,14 @@ public class TrueCheck implements LayoutEngineCheck { if (nd != null) { this.failureMessage = nd.getNodeValue(); } + this.prefixResolver = new PrefixResolverDefault(node); } /** @see org.apache.fop.layoutengine.LayoutEngineCheck */ public void check(LayoutResult result) { XObject res; try { - res = XPathAPI.eval(result.getAreaTree(), xpath); + res = XPathAPI.eval(result.getAreaTree(), xpath, prefixResolver); } catch (TransformerException e) { throw new RuntimeException("XPath evaluation failed: " + e.getMessage()); } -- 2.39.5