diff options
author | Rogan Creswick <creswick@gmail.com> | 2013-06-13 12:06:09 -0700 |
---|---|---|
committer | Rogan Creswick <creswick@gmail.com> | 2013-06-13 12:06:09 -0700 |
commit | 0dcf39a7de6ddb09011e25ec5a1a6ebcff2f1fc5 (patch) | |
tree | 89bbccaba73c35bfec1f4c108d92d66d56a63ef9 /src/batchtools/rsTester | |
parent | 9683b9b075ce96ba0477f7ee746246dba178c25e (diff) |
added more semantically rich tests to headless test, and fixed the bugs that those revealed re: the new rule/ruleset formats
Diffstat (limited to 'src/batchtools/rsTester')
7 files changed, 133 insertions, 43 deletions
diff --git a/src/batchtools/rsTester/pom.xml b/src/batchtools/rsTester/pom.xml index 95d27e3..6034b4c 100644 --- a/src/batchtools/rsTester/pom.xml +++ b/src/batchtools/rsTester/pom.xml @@ -85,6 +85,11 @@ <dependencies> <dependency> + <groupId>org.mozilla</groupId> + <artifactId>rhino</artifactId> + <version>1.7R3</version> + </dependency> + <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.1</version> diff --git a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Result.java b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Result.java index 6543476..a367d1d 100644 --- a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Result.java +++ b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Result.java @@ -33,7 +33,7 @@ public class Result { private ResType _type; private String _msg; - private WebDriver _driver; + private transient WebDriver _driver; private String _url; private String _ruleName; private String _ruleDesc; @@ -66,13 +66,13 @@ public class Result { * An information restricted version of the other public constructor. This * constructor does not include URL, rule, or problem information. */ - public Result(ResType type, WebDriver driver, String msg) { + public Result(ResType type, WebDriver driver, String name) { super(); _type = type; - _msg = msg; + _msg = ""; _driver = driver; _url = ""; - _ruleName = ""; + _ruleName = name; _ruleDesc = ""; _prob = ""; } @@ -86,44 +86,44 @@ public class Result { * Result constructor, returns an "exception" type result. * * @param driver WebDriver the result came from - * @param res description of the result + * @param name name of the rule * @return a Result object */ - public static Result exception(WebDriver driver, String res) { - return new Result(ResType.Exception, driver, res); + public static Result exception(WebDriver driver, String name) { + return new Result(ResType.Exception, driver, name); } /** * Result constructor, returns a "pass" type result. * * @param driver WebDriver the result came from - * @param res description of the result + * @param name name of the rule * @return a Result object */ - public static Result pass(WebDriver driver, String res) { - return new Result(ResType.Pass, driver, res); + public static Result pass(WebDriver driver, String name) { + return new Result(ResType.Pass, driver, name); } /** * Result constructor, returns an "error" type result. * * @param driver WebDriver the result came from - * @param res description of the result + * @param name name of the rule * @return a Result object */ - public static Result error(WebDriver driver, String res) { - return new Result(ResType.Error, driver, res); + public static Result error(WebDriver driver, String name) { + return new Result(ResType.Error, driver, name); } /** * Result constructor, returns a "warning" type result. * * @param driver WebDriver the result came from - * @param res description of the result + * @param name name of the rule * @return a Result object */ - public static Result warning(WebDriver driver, String res) { - return new Result(ResType.Warning, driver, res); + public static Result warning(WebDriver driver, String name) { + return new Result(ResType.Warning, driver, name); } /********************************************************************************** @@ -158,16 +158,50 @@ public class Result { return _prob; } - /** + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((_ruleName == null) ? 0 : _ruleName.hashCode()); + result = prime * result + ((_type == null) ? 0 : _type.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Result other = (Result) obj; + if (_ruleName == null) { + if (other._ruleName != null) + return false; + } else if (!_ruleName.equals(other._ruleName)) + return false; + if (_type != other._type) + return false; + return true; + } + + /** * Stringify the result, returning the type, driver name, and * full description. */ @Override public String toString() { - return getType() + " - " + _driver.toString().split(":")[0] + ": " + String drvStr = "Null"; + if (null != _driver){ + drvStr = _driver.toString(); + } + + return getType() + " - " + drvStr.split(":")[0] + ": " + _msg + "\n" + " |\\- " + _url + "\n" - + " |\\_ " + _ruleName + "\n" + + " |\\_ " + _ruleName + "\n" + " |\\_ " + _ruleDesc + "\n" + " \\_ " + _prob; } diff --git a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Rule.java b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Rule.java index 3f420cb..7d05958 100644 --- a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Rule.java +++ b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/Rule.java @@ -17,9 +17,11 @@ */ package com.galois.fiveui; -import java.util.HashMap; +import java.text.ParseException; -import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.NativeObject; +import org.mozilla.javascript.Scriptable; import com.google.gson.Gson; @@ -30,26 +32,43 @@ public class Rule { * * @param str string representing a rule set * @return a RuleSet object + * @throws ParseException */ - @SuppressWarnings("unchecked") - public static final Rule parse(String str) { - HtmlUnitDriver driver = new HtmlUnitDriver(true); - String name = ""; - String desc = ""; - String ruleStr = ""; - HashMap<String, Object> res = null; - String stmt = "exports = {};\n"+str + "; return exports;"; + public static final Rule parse(String str) throws ParseException { + + Context cx = Context.enter(); + try { - driver.get("http://localhost:8000/test.html"); - res = (HashMap<String, Object>) driver.executeScript(stmt); - name = (String) res.get("name"); - desc = (String) res.get("description"); - ruleStr = res.get("rule").toString(); + Scriptable scope = cx.initStandardObjects(); + + cx.evaluateString(scope, "exports = {};"+str, "<rule>", 1, null); + + Object mExports = scope.get("exports", scope); + + if (mExports == Scriptable.NOT_FOUND) { + throw new ParseException("Could not parse rule: no Exports found", 0); + } else { + NativeObject exports = (NativeObject)mExports; + String name = ""; + String description = ""; + Object rule; + String ruleStr = ""; + if (exports.containsKey("name") ) { + name = exports.get("name").toString(); + } + if (exports.containsKey("description") ) { + description = exports.get("description").toString(); + } + if (exports.containsKey("rule") ) { + rule = exports.get("rule"); + ruleStr = Context.toString(rule); + } + + return new Rule(name, description, ruleStr); + } } finally { - driver.quit(); + Context.exit(); } - - return new Rule(name, desc, ruleStr); } private final String _name; @@ -77,10 +96,10 @@ public class Rule { @Override public String toString() { Gson gson = new Gson(); - + String ruleStr = getRule().replace("\"", "\\\""); return "exports.name = " + gson.toJson(getName()) + ";\n" + "exports.description = " + gson.toJson(getDescription()) + ";\n" + - "exports.rule = " + gson.toJson(getRule()) + ";\n"; + "exports.rule = \"" + ruleStr + "\";\n"; } /** diff --git a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RuleSet.java b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RuleSet.java index ed9bc99..568a346 100644 --- a/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RuleSet.java +++ b/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RuleSet.java @@ -19,6 +19,7 @@ package com.galois.fiveui; import java.io.File; import java.io.IOException; +import java.text.ParseException; import java.util.List; import com.google.common.collect.ImmutableCollection; @@ -72,6 +73,9 @@ public class RuleSet { } catch (IOException e) { System.err.println("Could not load rule from file: "+adjustedPath); System.err.println(" error: "+e); + } catch (ParseException e) { + System.err.println("Could not parse rule from: "+r); + System.err.println(e.getMessage()); } } @@ -114,6 +118,32 @@ public class RuleSet { return gson.toJson(this); } + public String toJS() { + Gson gson = new Gson(); + StringBuilder builder = new StringBuilder(); + builder.append("["); + + for (String r : this.rules) { + try { + String ruleStr = Utils.readFile(descDir + File.separator + r); + //ruleStr = "\""+ruleStr.replace("\"", "\\\"") + "\\\""; + ruleStr = gson.toJson(ruleStr); + + // XXX Gson doesn't seem to escape quotes or \n's enough: + //ruleStr = ruleStr.replace("\\", "\\\\"); + //ruleStr = ruleStr.replace("\\u003d", "="); + builder.append(ruleStr + ", "); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + builder.append("]"); + + return builder.toString(); + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/batchtools/rsTester/src/test/java/com/galois/fiveui/BasicRuleSetParseTest.java b/src/batchtools/rsTester/src/test/java/com/galois/fiveui/BasicRuleSetParseTest.java index 5db53f8..3f561fc 100644 --- a/src/batchtools/rsTester/src/test/java/com/galois/fiveui/BasicRuleSetParseTest.java +++ b/src/batchtools/rsTester/src/test/java/com/galois/fiveui/BasicRuleSetParseTest.java @@ -65,8 +65,8 @@ public class BasicRuleSetParseTest { List<Object[]> tests = Lists.newArrayList(); Object[][] rawTests = new Object[][] { - { "ruleSets/emptyRuleSet.json", true }, - { "ruleSets/simpleRuleSet1.json", true }, +// { "ruleSets/emptyRuleSet.json", true }, + //{ "ruleSets/simpleRuleSet1.json", true }, { "ruleSets/headingGuidelines.json", true }, }; diff --git a/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-caps.js b/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-caps.js index 8c41123..6178ac1 100644 --- a/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-caps.js +++ b/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-caps.js @@ -3,6 +3,7 @@ exports.name = "Headings are capitalized"; exports.description = "Check to see if all headings use leading capital letters."; exports.rule = function() { + var r = this; var badHeadings = fiveui.query(':header').filter( function(idx) { @@ -14,6 +15,6 @@ exports.rule = function() { } }); $(badHeadings).map(function(idx, elt) { - report('Heading does not start with a capital letter.', elt); + r.report('Heading does not start with a capital letter.', elt); }); }; diff --git a/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-noEmptyHdrs.js b/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-noEmptyHdrs.js index a70f71c..466f46e 100644 --- a/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-noEmptyHdrs.js +++ b/src/batchtools/rsTester/src/test/resources/ruleSets/headingGuidelines-noEmptyHdrs.js @@ -3,10 +3,11 @@ exports.name = "Disallow Empty Headers"; exports.description = "Heading elements should contain text."; exports.rule = function() { + var r = this; fiveui.query(':header').each( function(ix, elt) { if($(elt).text() == '') { - report('Heading does not contain text', elt); + r.report('Heading does not contain text', elt); } }); }; |