diff options
author | Rogan Creswick <creswick@gmail.com> | 2012-05-15 11:27:55 -0700 |
---|---|---|
committer | Rogan Creswick <creswick@gmail.com> | 2012-05-15 11:27:55 -0700 |
commit | 75c306329fae6c4c9ba39addc95b58671991bfc1 (patch) | |
tree | 1900b1525984b5e9e527a2493beb17e499bc1b6e | |
parent | 3626fd6c06c45a413ec1f0f9141734613eeab1ef (diff) |
updated batch runner to read a json-formatted rule test description, increasing robustness and expressiveness of the test system
13 files changed, 274 insertions, 204 deletions
diff --git a/contexts/batch/src/main/java/com/galois/fiveui/BatchExecutor.java b/contexts/batch/src/main/java/com/galois/fiveui/BatchExecutor.java index dcc006f..0050fd3 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/BatchExecutor.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/BatchExecutor.java @@ -17,12 +17,14 @@ */ package com.galois.fiveui; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; @@ -32,56 +34,62 @@ import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; import com.google.common.collect.Lists; +import com.google.gson.Gson; /** * @author creswick * */ public class BatchExecutor { - - private static final Predicate<String> isRuleSet = new Predicate<String>() { - public boolean apply(final String input) { - return input.endsWith(".json"); - } - }; - - private static final Predicate<String> isUrl = new Predicate<String>() { - public boolean apply(final String input) { - return input.startsWith("http"); - } - }; +// +// private static final Predicate<String> isRuleSet = new Predicate<String>() { +// public boolean apply(final String input) { +// return input.endsWith(".json"); +// } +// }; +// +// private static final Predicate<String> isUrl = new Predicate<String>() { +// public boolean apply(final String input) { +// return input.startsWith("http"); +// } +// }; /** * @param args * @throws IOException + * @throws URISyntaxException */ - public static void main(final String[] args) throws IOException { - Config conf = parseArgs(Lists.newArrayList(args)); - - System.out.println("Running tests with configuration:\n" + conf); - - Builder<URITest> testBuilder = ImmutableList.builder(); - for (URI uri : conf.getTestUrls()) { - List<RuleSet> ruleSets = - Lists.transform(readRuleSets(conf), RuleSet.PARSE); - - for (RuleSet ruleSet : ruleSets) { - for (Rule rule : ruleSet.getRules()) { - RuleSet newRS = new RuleSet(ruleSet.getName(), - ruleSet.getDescription(), ImmutableList.of(rule)); - URITest test = new URITest(uri, newRS); - testBuilder.add(test); - } - } - } - + public static void main(final String[] args) throws IOException, URISyntaxException { + //Config conf = parseArgs(Lists.newArrayList(args)); + + //System.out.println("Running tests with configuration:\n" + conf); + + Gson gson = new Gson(); + Reader in = new InputStreamReader(new FileInputStream(args[0])); + RunDescription descr = gson.fromJson(in, RunDescription.class); + +// Builder<URITest> testBuilder = ImmutableList.builder(); +// for (URI uri : conf.getTestUrls()) { +// List<RuleSet> ruleSets = +// Lists.transform(readRuleSets(conf), RuleSet.PARSE); +// +// for (RuleSet ruleSet : ruleSets) { +// for (Rule rule : ruleSet.getRules()) { +// RuleSet newRS = new RuleSet(ruleSet.getName(), +// ruleSet.getDescription(), ImmutableList.of(rule)); +// URITest test = new URITest(uri, newRS); +// testBuilder.add(test); +// } +// } +// } +// WebDriver driver = new FirefoxDriver(); // WebDriver driver = new HtmlUnitDriver(true); ImmutableList<Result> results; try { BatchRunner runner = new BatchRunner(driver); - results = runner.runTests(testBuilder.build()); + results = runner.runTests(descr.getTests()); for (Result result : results) { System.out.println(result); @@ -92,36 +100,36 @@ public class BatchExecutor { driver.close(); } } - - private static ImmutableList<String> readRuleSets(Config conf) { - Builder<String> contentBuilder = ImmutableList.builder(); - for (String fileName : conf.getRuleSets()) { - try { - contentBuilder.add(Utils.readFile(fileName)); - } catch (IOException e) { - System.err.println("could not read file: " + fileName); - e.printStackTrace(); - } - } - - return contentBuilder.build(); - } - - private static Config parseArgs(final ArrayList<String> args) { - - Collection<String> ruleSets = Collections2.filter(args, isRuleSet); - Collection<String> rawUrls = Collections2.filter(args, isUrl); - - Builder<URI> b = ImmutableList.builder(); - for (String str : rawUrls) { - try { - b.add(new URI(str)); - } catch (URISyntaxException e) { - System.err.println("Warning: Could not parse: " + str - + " into URI: \n" + e); - System.err.println(" Discarding " + str + " as uri."); - } - } - return new Config(ImmutableList.copyOf(ruleSets), b.build()); - } +// +// private static ImmutableList<String> readRuleSets(Config conf) { +// Builder<String> contentBuilder = ImmutableList.builder(); +// for (String fileName : conf.getRuleSets()) { +// try { +// contentBuilder.add(Utils.readFile(fileName)); +// } catch (IOException e) { +// System.err.println("could not read file: " + fileName); +// e.printStackTrace(); +// } +// } +// +// return contentBuilder.build(); +// } +// +// private static Config parseArgs(final ArrayList<String> args) { +// +// Collection<String> ruleSets = Collections2.filter(args, isRuleSet); +// Collection<String> rawUrls = Collections2.filter(args, isUrl); +// +// Builder<URI> b = ImmutableList.builder(); +// for (String str : rawUrls) { +// try { +// b.add(new URI(str)); +// } catch (URISyntaxException e) { +// System.err.println("Warning: Could not parse: " + str +// + " into URI: \n" + e); +// System.err.println(" Discarding " + str + " as uri."); +// } +// } +// return new Config(ImmutableList.copyOf(ruleSets), b.build()); +// } } diff --git a/contexts/batch/src/main/java/com/galois/fiveui/BatchRunner.java b/contexts/batch/src/main/java/com/galois/fiveui/BatchRunner.java index 6d03f26..3ab8a7c 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/BatchRunner.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/BatchRunner.java @@ -49,9 +49,9 @@ public class BatchRunner { _exe = (JavascriptExecutor) _driver; } - public ImmutableList<Result> runTests(ImmutableList<URITest> build) { + public ImmutableList<Result> runTests(ImmutableList<RuleTest> build) { Builder<Result> resBuilder = ImmutableList.builder(); - for (URITest uriTest : build) { + for (RuleTest uriTest : build) { resBuilder.addAll(runTest(uriTest)); } return resBuilder.build(); @@ -61,14 +61,14 @@ public class BatchRunner { * Run a URITest, returning the result (success, failure details, or * indicator of exceptional conditions.) * - * @param uriTest + * @param test */ - public ImmutableList<Result> runTest(final URITest uriTest) { - RuleSet rule = uriTest.getRuleSet(); + public ImmutableList<Result> runTest(final RuleTest test) { + RuleSet rule = test.getRule(); ImmutableList<Result> results; try { - _driver.get(uriTest.getUri().toString()); + _driver.get(test.getUri().toString()); results = runRule(rule); System.out.println(results); } catch (Exception e) { diff --git a/contexts/batch/src/main/java/com/galois/fiveui/Config.java b/contexts/batch/src/main/java/com/galois/fiveui/Config.java deleted file mode 100644 index 0cd4435..0000000 --- a/contexts/batch/src/main/java/com/galois/fiveui/Config.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Module : Config.java Copyright : (c) 2011-2012, Galois, Inc. - * - * Maintainer : Stability : Provisional Portability: Portable - * - * 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. - */ - -package com.galois.fiveui; - -import java.net.URI; - -import com.google.common.collect.ImmutableList; - -public class Config { - private final ImmutableList<String> _ruleSets; - private final ImmutableList<URI> _testUrls; - - public Config(ImmutableList<String> immutableList, - ImmutableList<URI> testUrls) { - - this._ruleSets = immutableList; - this._testUrls = testUrls; - } - - public ImmutableList<String> getRuleSets() { - return _ruleSets; - } - - public ImmutableList<URI> getTestUrls() { - return _testUrls; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder("Urls:\n"); - for (URI uri : _testUrls) { - builder.append(" " + uri.toString()); - } - builder.append("\n"); - builder.append("Rule Sets:\n"); - for (String rs : _ruleSets) { - builder.append("Rule Set:\n"); - builder.append(" " + rs); - } - return builder.toString(); - } -} diff --git a/contexts/batch/src/main/java/com/galois/fiveui/ResType.java b/contexts/batch/src/main/java/com/galois/fiveui/ResType.java index 54d5869..671d74c 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/ResType.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/ResType.java @@ -20,4 +20,4 @@ package com.galois.fiveui; public enum ResType { Pass, Error, Warning, Exception -} +}
\ No newline at end of file diff --git a/contexts/batch/src/main/java/com/galois/fiveui/Result.java b/contexts/batch/src/main/java/com/galois/fiveui/Result.java index 6158210..91f889b 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/Result.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/Result.java @@ -35,9 +35,9 @@ public class Result { return new Result(ResType.Warning, res); } - private final ResType _type; - private final String _desc; - + private ResType _type; + private String _desc; + private Result(ResType type, String desc) { super(); _type = type; diff --git a/contexts/batch/src/main/java/com/galois/fiveui/Rule.java b/contexts/batch/src/main/java/com/galois/fiveui/Rule.java index 40f6d60..fc3e4b2 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/Rule.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/Rule.java @@ -45,17 +45,20 @@ public class Rule { String name = (String) res.get("name"); String desc = (String) res.get("description"); String rule = res.get("rule").toString(); - return new Rule(name, desc, rule); + int id = ((Long)res.get("id")).intValue(); + return new Rule(name, desc, rule, id); } private final String _name; private final String _desc; private final String _rule; + private final int _id; - public Rule(final String name, final String desc, final String rule) { + public Rule(final String name, final String desc, final String rule, final int id) { this._name = name; this._desc = desc; this._rule = rule; + this._id = id; } public String getName() { @@ -70,11 +73,16 @@ public class Rule { return _rule; } + public int getId() { + return _id; + } + @Override public String toString() { Gson gson = new Gson(); - return "{ 'name': " + gson.toJson(getName()) + ",\n" + + return "{ 'id': " + gson.toJson(getId()) + ",\n " + + " 'name': " + gson.toJson(getName()) + ",\n" + " 'description': " + gson.toJson(getDescription()) + ",\n" + " 'ruleStr': " + gson.toJson(getRule()) + "\n" + "}"; diff --git a/contexts/batch/src/main/java/com/galois/fiveui/RuleSet.java b/contexts/batch/src/main/java/com/galois/fiveui/RuleSet.java index 28f780a..de53abb 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/RuleSet.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/RuleSet.java @@ -110,6 +110,15 @@ public class RuleSet { " 'rules': [" + rules.toString() + "]" + "}"; } + + public Rule getRule(int ruleId) { + for (Rule rule : getRules()) { + if ( ruleId == rule.getId()) { + return rule; + } + } + return null; + } } diff --git a/contexts/batch/src/main/java/com/galois/fiveui/RuleTest.java b/contexts/batch/src/main/java/com/galois/fiveui/RuleTest.java new file mode 100644 index 0000000..d22cda3 --- /dev/null +++ b/contexts/batch/src/main/java/com/galois/fiveui/RuleTest.java @@ -0,0 +1,68 @@ +/** + * Module : RuleTest.java + * Copyright : (c) 2011-2012, Galois, Inc. + * + * Maintainer : + * Stability : Provisional + * Portability: Portable + * + * 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. + */ +package com.galois.fiveui; + +import java.net.URI; + +import com.google.common.collect.ImmutableList; + +/** + * @author creswick + * + */ +public class RuleTest { + + private final URI _uri; + private final RuleSet _ruleSet; + private final int _ruleId; + private final ResType _oracle; + + public RuleTest(URI uri, RuleSet ruleSet, int ruleId, ResType oracle) { + _uri = uri; + _ruleSet = ruleSet; + _ruleId = ruleId; + _oracle = oracle; + } + + public RuleSet getRule() { + RuleSet newRS = new RuleSet(_ruleSet.getName(), + _ruleSet.getDescription(), ImmutableList.of(_ruleSet.getRule(_ruleId))); + return newRS; + } + + public URI getUri() { + return _uri; + } + + public RuleSet getRuelSet() { + return _ruleSet; + } + + public int getRuleId() { + return _ruleId; + } + + public ResType getOracle() { + return _oracle; + } + + +} diff --git a/contexts/batch/src/main/java/com/galois/fiveui/RunDescription.java b/contexts/batch/src/main/java/com/galois/fiveui/RunDescription.java new file mode 100644 index 0000000..5465ab5 --- /dev/null +++ b/contexts/batch/src/main/java/com/galois/fiveui/RunDescription.java @@ -0,0 +1,61 @@ +package com.galois.fiveui; + +import java.io.IOException; +import java.net.URI; +import java.util.List; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.gson.Gson; + +public class RunDescription { + + private String ruleSet; + private List<URIMap> tests; + + private transient RuleSet _parsedRuleSet; + + /** + * Empty constructor for Gson deserialization. + */ + RunDescription() {} + + public RuleSet getRuleSet() throws IOException { + String ruleSetStr = Utils.readFile(ruleSet); + if (null == _parsedRuleSet) { + _parsedRuleSet = RuleSet.parse(ruleSetStr); + } + + return _parsedRuleSet; + } + + public ImmutableList<RuleTest> getTests() throws IOException { + Builder<RuleTest> builder = ImmutableList.builder(); + for (URIMap uriMap : tests) { + for (RuleMap rMap : uriMap.oracle) { + for (ResType oracle : rMap.results) { + builder.add( + new RuleTest(uriMap.url, getRuleSet(), rMap.ruleId, oracle)); + } + } + } + return builder.build(); + } + + public String toString() { + Gson gson = new Gson(); + return gson.toJson(this); + } + + private static class URIMap { + public URI url; + public List<RuleMap> oracle; + + URIMap(){}; + } + + private static class RuleMap { + public int ruleId; + public List<ResType> results; + } +} diff --git a/contexts/batch/src/main/java/com/galois/fiveui/URITest.java b/contexts/batch/src/main/java/com/galois/fiveui/URITest.java index 36bd5a1..5a0f920 100644 --- a/contexts/batch/src/main/java/com/galois/fiveui/URITest.java +++ b/contexts/batch/src/main/java/com/galois/fiveui/URITest.java @@ -18,28 +18,38 @@ package com.galois.fiveui; import java.net.URI; +import java.util.List; + +import com.google.common.collect.ImmutableList; public class URITest { - private final URI _uri; - private final RuleSet _ruleSet; + private URI uri; + private int id; + private List<ResType> oracle; - public URITest(URI uri, RuleSet ruleSet) { - assert(1 == ruleSet.getRules().size()); - - this._uri = uri; - this._ruleSet = ruleSet; - } + /** + * Deserialization constructor. + */ + URITest() {} + +// public URITest(URI uri, RuleSet ruleSet, ResType oracle) { +// assert(1 == ruleSet.getRules().size()); +// +// this._uri = uri; +// this._ruleSet = ruleSet; +// this._oracle = oracle; +// } public URI getUri() { - return _uri; + return uri; } - - public Rule getRule() { - return _ruleSet.getRules().get(0); + + public int getId() { + return id; } - public RuleSet getRuleSet() { - return _ruleSet; + public ImmutableList<ResType> getOracle() { + return ImmutableList.copyOf(oracle); } } diff --git a/contexts/batch/src/resources/sample.json b/contexts/batch/src/resources/sample.json new file mode 100644 index 0000000..cd1961f --- /dev/null +++ b/contexts/batch/src/resources/sample.json @@ -0,0 +1,13 @@ +{ + 'ruleSet': '../../exampleData/ruleSets/headingGuidelines.json', + 'tests': [ { 'url': 'http://localhost:8000/exampleData/basic/headings.html', + 'oracle': [ { 'ruleId': 1 + , 'results': ['Error'] + }, + { 'ruleId': 2 + , 'results': ['Error'] + } + ] + } + ] +}
\ No newline at end of file diff --git a/contexts/batch/src/test/java/com/galois/fiveui/RuleTest.java b/contexts/batch/src/test/java/com/galois/fiveui/RuleTest.java deleted file mode 100644 index edef5da..0000000 --- a/contexts/batch/src/test/java/com/galois/fiveui/RuleTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Module : RuleTest.java - * Copyright : (c) 2011-2012, Galois, Inc. - * - * Maintainer : - * Stability : Provisional - * Portability: Portable - * - * 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. - */ -package com.galois.fiveui; - -import static org.junit.Assert.*; - -import org.junit.Test; - -/** - * @author creswick - * - */ -public class RuleTest { - - /** - * Test method for {@link com.galois.fiveui.Rule#parse(java.lang.String)}. - */ - @Test - public final void testParse() { -// String name = "a rule"; -// String desc = "a description"; -// String ruleStr = -// "{ 'name': '" + name + "'," -// + "'description': '" + desc + "'," -// + "'rule': function() { } " -// + "}"; -// -// Rule r = Rule.parse(ruleStr); -// assertEquals("Wrong name", name, r.getName()); -// assertEquals("Wrong description", desc, r.getDescription()); -// assertNotNull("Rule string should not be null", r.getRule()); - } -} diff --git a/exampleData/ruleSets/headingGuidelines.json b/exampleData/ruleSets/headingGuidelines.json index 43ee1a4..be69fac 100644 --- a/exampleData/ruleSets/headingGuidelines.json +++ b/exampleData/ruleSets/headingGuidelines.json @@ -1,6 +1,7 @@ { "name": "Heading Guidelines" , "description": "Guidelines pertaining to the formatting and content of headings." -, "rules": [ { "name": "Headings are capitalized" +, "rules": [ { "id": 1 + , "name": "Headings are capitalized" , "description": "Check to see if all headings use leading capital letters." , "rule": function() { @@ -20,7 +21,8 @@ } }, - { "name": "Disallow Empty Headers" + { "id": 2 + , "name": "Disallow Empty Headers" , "description": "Heading elements should contain text." , "rule": function() { fiveui.query(':header').each( |