diff options
| author | Keith Wall <kwall@apache.org> | 2012-07-18 11:45:01 +0000 |
|---|---|---|
| committer | Keith Wall <kwall@apache.org> | 2012-07-18 11:45:01 +0000 |
| commit | 8b6e924b6d11be40f693d59f306eba495de7b959 (patch) | |
| tree | 1fa2c0aecb613130529a7657bf4dd5ad8bfd9701 /qpid/java/perftests | |
| parent | 67ecb7f0c6bc8a12373a01f80ade9db8adf7cfb2 (diff) | |
| download | qpid-python-8b6e924b6d11be40f693d59f306eba495de7b959.tar.gz | |
QPID-4103: Refactor test approach to avoid comparing JSON strings.
Applied patch from Philip Harvey <phil@philharveyonline.com>.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1362891 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/perftests')
3 files changed, 45 insertions, 135 deletions
diff --git a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-expected-json.json b/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-expected-json.json deleted file mode 100644 index d56def5699..0000000000 --- a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-expected-json.json +++ /dev/null @@ -1,94 +0,0 @@ -{ - "_tests":[ - { - "_queues":[ - { - "_name":"Json-Queue-Name" - } - ], - "_clients":[ - { - "_connections":[ - { - "_name":"connection1", - "_sessions":[ - { - "_acknowledgeMode":"0", - "_sessionName":"session1", - "_consumers":[ - - ] - } - ] - } - ], - "_name":"repeatingClient0" - }, - { - "_connections":[ - { - "_name":"connection1", - "_sessions":[ - { - "_acknowledgeMode":"0", - "_sessionName":"session1", - "_consumers":[ - - ] - } - ] - } - ], - "_name":"repeatingClient1" - } - ], - "_iterationNumber":0, - "_name":"Test 1" - }, - { - "_queues":[ - { - "_name":"Json-Queue-Name" - } - ], - "_clients":[ - { - "_connections":[ - { - "_name":"connection1", - "_sessions":[ - { - "_acknowledgeMode":"1", - "_sessionName":"session1", - "_consumers":[ - - ] - } - ] - } - ], - "_name":"repeatingClient0" - }, - { - "_connections":[ - { - "_name":"connection1", - "_sessions":[ - { - "_acknowledgeMode":"1", - "_sessionName":"session1", - "_consumers":[ - - ] - } - ] - } - ], - "_name":"repeatingClient1" - } - ], - "_iterationNumber":1, - "_name":"Test 1" - } - ] -}
\ No newline at end of file diff --git a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-test-config.js b/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-test-config.js index 07f8bf9d92..f64af82feb 100644 --- a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-test-config.js +++ b/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest-test-config.js @@ -1,33 +1,22 @@ jsonObject = { - "_tests": - QPID.iterations( { "__ACK_MODE": [ 0, 1 ] }, + "_countries": + QPID.iterations( { "__ITERATING_VALUE": [ 0, 1 ] }, { // this is a comment - it wouldn't be allowed if this were pure JSON - "_name": "Test 1", - "_queues": [ + "_name": "Country", + "_regions": QPID.times(2, { - "_name": "Json-Queue-Name" - } - ], - - "_clients": QPID.times(2, - { - "_name": "repeatingClient__CLIENT_INDEX", - "_connections": [ + "_name": "repeatingRegion__REGION_INDEX", + "_towns": [ { - "_name": "connection1", - "_sessions": [ - { - "_sessionName": "session1", - "_acknowledgeMode": "__ACK_MODE", - "_consumers": [] - } - ] + "_name": "town1", + "_iteratingAttribute": "__ITERATING_VALUE", + "_consumers": [] } ] }, - "__CLIENT_INDEX" + "__REGION_INDEX" ) }) diff --git a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest.java b/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest.java index 7edb62f060..eb4063888b 100644 --- a/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest.java +++ b/qpid/java/perftests/src/test/java/org/apache/qpid/disttest/controller/config/JavaScriptConfigEvaluatorTest.java @@ -20,10 +20,10 @@ */ package org.apache.qpid.disttest.controller.config; -import java.io.File; -import java.util.TreeMap; +import static org.apache.commons.beanutils.PropertyUtils.getProperty; -import org.apache.qpid.util.FileUtils; +import java.util.List; +import java.util.TreeMap; import junit.framework.TestCase; @@ -37,30 +37,45 @@ public class JavaScriptConfigEvaluatorTest extends TestCase String rawConfig = new JavaScriptConfigEvaluator().evaluateJavaScript(jsFilePath); - String config = formatForComparison(rawConfig); - assertTrue(config.contains("\"_iterationNumber\":1")); + Object configAsObject = getObject(rawConfig); + + // Tests are produced by the QPID.iterations js function + assertEquals("Unexpected number of countries", 2, getPropertyAsList(configAsObject, "_countries").size()); - File expectedJsonFile = new File(getClass().getResource("JavaScriptConfigEvaluatorTest-expected-json.json").toURI().getPath()); - String rawExpected = FileUtils.readFileAsString(expectedJsonFile); + Object country0 = getProperty(configAsObject, "_countries.[0]"); + assertEquals("Unexpected country name", "Country", getProperty(country0, "_name")); + assertEquals("Unexpected country iteration number", 0, getPropertyAsInt(country0, "_iterationNumber")); - String expected = formatForComparison(rawExpected); + assertEquals("Unexpected number of regions", 2, getPropertyAsList(country0, "_regions").size()); + // Region names are produced by the QPID.times js function + assertEquals("Unexpected region name", "repeatingRegion0", getProperty(country0, "_regions.[0]._name")); + assertEquals("Unexpected region name", "repeatingRegion1", getProperty(country0, "_regions.[1]._name")); + // Iterating attribute are produced by the QPID.iterations js function + assertEquals("Unexpected iterating attribute", "0", getProperty(country0, "_regions.[0]._towns.[0]._iteratingAttribute")); - assertEquals("Unexpected configuration", expected, config); + Object country1 = getProperty(configAsObject, "_countries.[1]"); + assertEquals("Unexpected country iteration number", 1, getPropertyAsInt(country1, "_iterationNumber")); + assertEquals("Unexpected iterating attribute", "1", getProperty(country1, "_regions.[0]._towns.[0]._iteratingAttribute")); } - /** - * Does an unmarshall-then-marshall on the supplied JSON string so that - * we can compare the output when testing for equivalent JSON strings, - * ignoring ordering of attributes. - */ - private String formatForComparison(String jsonStringIn) + private int getPropertyAsInt(Object configAsObject, String property) throws Exception { - Gson gson = new Gson(); + Number propertyValue = (Number) getProperty(configAsObject, property); - @SuppressWarnings("rawtypes") - TreeMap configObj = gson.fromJson(jsonStringIn, TreeMap.class); + return propertyValue.intValue(); + } - String jsonStringOut = gson.toJson(configObj); - return jsonStringOut; + private List<?> getPropertyAsList(Object configAsObject, String property) + throws Exception + { + return (List<?>)getProperty(configAsObject, property); + } + + private Object getObject(String jsonStringIn) + { + Gson gson = new Gson(); + @SuppressWarnings("rawtypes") + TreeMap object = gson.fromJson(jsonStringIn, TreeMap.class); + return object; } } |
