diff --git a/.github/workflows/deploy-website.yml b/.github/workflows/deploy-website.yml new file mode 100644 index 00000000..fb527d76 --- /dev/null +++ b/.github/workflows/deploy-website.yml @@ -0,0 +1,50 @@ +# Simple workflow for deploying static content to GitHub Pages +name: Deploy static content to Pages + +on: + # Runs on pushes targeting the default branch - disabled, manual only + #push: + # branches: ["master"] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages +permissions: + contents: read + pages: write + id-token: write + +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. +concurrency: + group: "pages" + cancel-in-progress: false + +jobs: + # Single deploy job since we're just deploying + deploy: + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Pages + uses: actions/configure-pages@v5 + - uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + - name: Run the Maven javadoc command + run: mvn javadoc:aggregate -DreportOutputDirectory=src/site/resources/apidocs + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + with: + # Upload entire repository + path: './src/site/resources' + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..ec376bb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +target \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c9fef76..1b33ee64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,34 @@ Changelog ========= +Version 2.0-rc1 - 7/28/2024 +------------------- + - Release candidate + - ** Switches JSON implementation to use org.json:json:20240303 ** + - Deployment still built with Java version 8 to maximize compatibility + - Cannot insert null directly into JSONArray without casting. Recommend to use JSONObject.Null + - JSONException is now a RuntimeException. Is not defined as thrown in method signatures anynmore. + +Version 1.5.3 - 6/28/2024 +------------------------- + - Revert Java release version from 21 to 8 due to breaking older compilers. + +Version 1.5.2 - 6/14/2024 +------------------------- + - Fix CVE-2020-15250 JUnit vulnerability (https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-15250). Bump + dependencies. + - Add gitIgnore file + - README syntax error fix + - Accidentally upgraded release to Java version 21 + +Version 1.5.1 - 7/4/2022 +------------------------ +Going to try to catch up on some ancient PRs, mainly around security and cleanup. Starting with accepted PRs that +didn't get released yet. To be followed hopefully shortly with another release. + - Added convenience methods for JSONObject comparison using a custom JSONComparator (thanks jakob-o@!) + - Fix issue #105: Issue when comparing JSONArray if any value is null (thanks suraj1291993@!) + - Fixes security vulnerability associated with older version of junit + Version 1.5.0 - 3/19/2017 ------------------------- - JSONassert now supports user-supplied error messages (thanks yasin3061@!) @@ -16,12 +44,12 @@ Version 1.4.0 - 10/30/2016 Version 1.3.0 - 12/16/2015 -------------------------- - - Fix & improve ArrayValueMatcher JavaDoc (thanks dmackinder!) + - Fix & improve ArrayValueMatcher JavaDoc (thanks dmackinder@!) Fix final JavaDoc example and add new example showing how to verify every array element using a custom comparator - - Fix URL in pom.xml (aukevanleeuwen) - - Update JSONCompareResult.java adding 2 new lists for missing and unexpected fileds (thanks riccorazza!) - - Includes missing imports in test class (thanks javierseixas!) + - Fix URL in pom.xml (aukevanleeuwen@) + - Update JSONCompareResult.java adding 2 new lists for missing and unexpected fileds (thanks riccorazza@!) + - Includes missing imports in test class (thanks javierseixas@!) Version 1.2.3 - 2/5/2014 ------------------------ @@ -38,7 +66,8 @@ Version 1.2.2 - 12/31/2013 Version 1.2.1 - 10/24/2013 -------------------------- - Remove commons-collection dependency - - Updated Customization class to allow path-matching, and matching of expected and actual values with user-provided EqualityComparator. + - Updated Customization class to allow path-matching, and matching of expected and actual values with user-provided + EqualityComparator. - Added AssertNotEquals Version 1.2.0 - 3/17/2013 diff --git a/README.md b/README.md index c28ba6de..871b1e19 100644 --- a/README.md +++ b/README.md @@ -29,22 +29,23 @@ Assert.assertTrue(data.has("friends")); Object friendsObject = data.get("friends"); Assert.assertTrue(friendsObject instanceof JSONArray); JSONArray friends = (JSONArray) friendsObject; -Assert.assertEquals(2, data.length()); -JSONObject friend1Obj = friends.getJSONObject(data.get(0)); -Assert.true(friend1Obj.has("id")); -Assert.true(friend1Obj.has("name")); -JSONObject friend2Obj = friends.getJSONObject(data.get(1)); -Assert.true(friend2Obj.has("id")); -Assert.true(friend2Obj.has("name")); +Assert.assertEquals(2, friends.length()); +JSONObject friend1Obj = friends.getJSONObject(0); +Assert.assertTrue(friend1Obj.has("id")); +Assert.assertTrue(friend1Obj.has("name")); +JSONObject friend2Obj = friends.getJSONObject(1); +Assert.assertTrue(friend2Obj.has("id")); +Assert.assertTrue(friend2Obj.has("name")); + if ("Carter Page".equals(friend1Obj.getString("name"))) { - Assert.assertEquals(123, friend1Obj.getInt("id")); + Assert.assertEquals(456, friend1Obj.getInt("id")); Assert.assertEquals("Corby Page", friend2Obj.getString("name")); - Assert.assertEquals(456, friend2Obj.getInt("id")); + Assert.assertEquals(123, friend2Obj.getInt("id")); } else if ("Corby Page".equals(friend1Obj.getString("name"))) { - Assert.assertEquals(456, friend1Obj.getInt("id")); + Assert.assertEquals(123, friend1Obj.getInt("id")); Assert.assertEquals("Carter Page", friend2Obj.getString("name")); - Assert.assertEquals(123, friend2Obj.getInt("id")); + Assert.assertEquals(456, friend2Obj.getInt("id")); } else { Assert.fail("Expected either Carter or Corby, Got: " + friend1Obj.getString("name")); @@ -76,8 +77,8 @@ To use, [download the JAR](https://github.com/skyscreamer/JSONassert/releases) o org.skyscreamer jsonassert - 1.5.0 - test + 2.0-rc1 + test Write tests like this: @@ -96,16 +97,15 @@ Who uses JSONassert? + [GroupDocs](http://groupdocs.com/) + [Shazam](http://www.shazam.com/) + [Thucydides](http://thucydides.net/) + + [and over a thousand more](https://mvnrepository.com/artifact/org.skyscreamer/jsonassert)... * * * org.json -------- -This implementation uses a clean-room implementation of the org.json -library implemented for the Android system, released under the Apache 2.0 license. See -[com.vaadin.external.google:android-json](http://search.maven.org/#artifactdetails%7Ccom.vaadin.external.google%7Candroid-json%7C0.0.20131108.vaadin1%7Cjar) -That jar does **not** include the org.json.JSONString interface, so a new implementation of that interface is added to this source. +As of v2, JSONAssert uses @stleary's [JSON-java](https://github.com/stleary/JSON-java) implementation of org.json, the +most commonly used reference implementation for JSON in Java. Resources --------- diff --git a/pom.xml b/pom.xml index 70a0456a..86738534 100644 --- a/pom.xml +++ b/pom.xml @@ -2,21 +2,19 @@ 4.0.0 - - org.sonatype.oss - oss-parent - 7 - - org.skyscreamer jsonassert - 1.5.1-SNAPSHOT + 2.0-rc1 jar JSONassert - A library to develop RESTful but flexible APIs + Write JSON unit tests in less code. Great for testing REST interfaces. https://github.com/skyscreamer/JSONassert + + 8 + + The Apache Software License, Version 2.0 @@ -36,28 +34,28 @@ carter@skyscreamer.org - cepage - Corby Page - corby@skyscreamer.org - - - sduskis - Solomon Duskis - solomon@skyscreamer.org + hertzsprung + James Shaw - com.vaadin.external.google - android-json - 0.0.20131108.vaadin1 + org.json + json + 20240303 junit junit - 4.10 + 4.13.2 + test + + + org.hamcrest + hamcrest + 2.2 test @@ -67,44 +65,27 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.1 - - 1.6 - 1.6 - + 3.11.0 - org.apache.maven.plugins - maven-site-plugin - 3.3 + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true - - - org.codehaus.mojo - cobertura-maven-plugin - 2.5.1 - - + ossrh + https://oss.sonatype.org/ + false + + + com.thoughtworks.xstream + xstream + 1.4.15 + + - - - org.apache.maven.scm - maven-scm-provider-gitexe - 1.3 - - - org.apache.maven.scm - maven-scm-manager-plexus - 1.3 - - - org.kathrynhuxtable.maven.wagon - wagon-gitsite - 0.3.1 - - @@ -112,23 +93,51 @@ github-project-site gitsite:git@github.com/skyscreamer/JSONassert.git + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + - release-sign-artifacts - - - performRelease - true - - + deploy + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + ${java.home}/bin/javadoc + ${maven.compiler.release} + + org.apache.maven.plugins maven-gpg-plugin - 1.1 + 1.5 sign-artifacts diff --git a/src/main/java/org/json/JSONString.java b/src/main/java/org/json/JSONString.java deleted file mode 100644 index 7360ddd3..00000000 --- a/src/main/java/org/json/JSONString.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 org.json; - -/** - * The JSONString interface allows a toJSONString() method so that a class can change - * the behavior of JSONObject.toString(), JSONArray.toString(), and JSONWriter.value(Object). - * The toJSONString method will be used instead of the default behavior of using the - * Object's toString() method and quoting the result. - * - * @author sasdjb - * - */ -public interface JSONString { - - /** - * The toJSONString method allows a class to produce its own JSON - * serialization. - * - * @return String representation of JSON object - * */ - String toJSONString(); - -} diff --git a/src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java b/src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java index ea3ebaf8..2d4dd8ec 100644 --- a/src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java +++ b/src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java @@ -133,7 +133,7 @@ *

To simplify complexity of expected JSON string, the value "{a:[[9]]}" may be replaced by "{a:[9]}" or "{a:9}"

* * @author Duncan Mackinder - * + * @param Array Type */ public class ArrayValueMatcher implements LocationAwareValueMatcher { private final JSONComparator comparator; diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java index d2a31cd1..5a2eec18 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java @@ -15,7 +15,6 @@ package org.skyscreamer.jsonassert; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.comparator.JSONComparator; @@ -59,10 +58,8 @@ private JSONAssert() {} * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, JSONObject actual, boolean strict) - throws JSONException { + public static void assertEquals(String expectedStr, JSONObject actual, boolean strict) { assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -74,10 +71,8 @@ public static void assertEquals(String expectedStr, JSONObject actual, boolean s * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, JSONObject actual, boolean strict) - throws JSONException { + public static void assertEquals(String message, String expectedStr, JSONObject actual, boolean strict) { assertEquals(message, expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -90,10 +85,8 @@ public static void assertEquals(String message, String expectedStr, JSONObject a * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, JSONObject actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String expectedStr, JSONObject actual, boolean strict) { assertNotEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -107,10 +100,8 @@ public static void assertNotEquals(String expectedStr, JSONObject actual, boolea * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, JSONObject actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, JSONObject actual, boolean strict) { assertNotEquals(message, expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -121,10 +112,8 @@ public static void assertNotEquals(String message, String expectedStr, JSONObjec * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) { assertEquals("", expectedStr, actual, compareMode); } @@ -136,10 +125,9 @@ public static void assertEquals(String expectedStr, JSONObject actual, JSONCompa * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertEquals(message, (JSONObject)expected, actual, compareMode); @@ -158,10 +146,8 @@ public static void assertEquals(String message, String expectedStr, JSONObject a * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode) { assertNotEquals("", expectedStr, actual, compareMode); } @@ -175,10 +161,9 @@ public static void assertNotEquals(String expectedStr, JSONObject actual, JSONCo * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, JSONObject actual, + JSONCompareMode compareMode) { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONObject) { assertNotEquals(message, (JSONObject) expected, actual, compareMode); @@ -195,10 +180,8 @@ public static void assertNotEquals(String message, String expectedStr, JSONObjec * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, JSONArray actual, boolean strict) - throws JSONException { + public static void assertEquals(String expectedStr, JSONArray actual, boolean strict) { assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -210,10 +193,8 @@ public static void assertEquals(String expectedStr, JSONArray actual, boolean st * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, JSONArray actual, boolean strict) - throws JSONException { + public static void assertEquals(String message, String expectedStr, JSONArray actual, boolean strict) { assertEquals(message, expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -224,10 +205,8 @@ public static void assertEquals(String message, String expectedStr, JSONArray ac * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, JSONArray actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String expectedStr, JSONArray actual, boolean strict) { assertNotEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -239,10 +218,8 @@ public static void assertNotEquals(String expectedStr, JSONArray actual, boolean * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, JSONArray actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, JSONArray actual, boolean strict) { assertNotEquals(message, expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -253,10 +230,8 @@ public static void assertNotEquals(String message, String expectedStr, JSONArray * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) { assertEquals("", expectedStr, actual, compareMode); } @@ -268,10 +243,8 @@ public static void assertEquals(String expectedStr, JSONArray actual, JSONCompar * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertEquals(message, (JSONArray) expected, actual, compareMode); @@ -288,10 +261,8 @@ public static void assertEquals(String message, String expectedStr, JSONArray ac * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode) { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals((JSONArray) expected, actual, compareMode); @@ -309,10 +280,9 @@ public static void assertNotEquals(String expectedStr, JSONArray actual, JSONCom * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, JSONArray actual, + JSONCompareMode compareMode) { Object expected = JSONParser.parseJSON(expectedStr); if (expected instanceof JSONArray) { assertNotEquals(message, (JSONArray) expected, actual, compareMode); @@ -329,10 +299,8 @@ public static void assertNotEquals(String message, String expectedStr, JSONArray * @param expectedStr Expected JSON string * @param actualStr String to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, String actualStr, boolean strict) - throws JSONException { + public static void assertEquals(String expectedStr, String actualStr, boolean strict) { assertEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -344,10 +312,8 @@ public static void assertEquals(String expectedStr, String actualStr, boolean st * @param expectedStr Expected JSON string * @param actualStr String to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, String actualStr, boolean strict) - throws JSONException { + public static void assertEquals(String message, String expectedStr, String actualStr, boolean strict) { assertEquals(message, expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -358,10 +324,8 @@ public static void assertEquals(String message, String expectedStr, String actua * @param expectedStr Expected JSON string * @param actualStr String to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, String actualStr, boolean strict) - throws JSONException { + public static void assertNotEquals(String expectedStr, String actualStr, boolean strict) { assertNotEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -373,10 +337,8 @@ public static void assertNotEquals(String expectedStr, String actualStr, boolean * @param expectedStr Expected JSON string * @param actualStr String to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, String actualStr, boolean strict) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, String actualStr, boolean strict) { assertNotEquals(message, expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -387,10 +349,8 @@ public static void assertNotEquals(String message, String expectedStr, String ac * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) { assertEquals("", expectedStr, actualStr, compareMode); } @@ -402,10 +362,8 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) { if (expectedStr==actualStr) return; if (expectedStr==null){ throw new AssertionError("Expected string is null."); @@ -425,10 +383,8 @@ public static void assertEquals(String message, String expectedStr, String actua * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String expectedStr, String actualStr, JSONCompareMode compareMode) { assertNotEquals("", expectedStr, actualStr, compareMode); } @@ -440,10 +396,9 @@ public static void assertNotEquals(String expectedStr, String actualStr, JSONCom * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, String actualStr, + JSONCompareMode compareMode) { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); @@ -457,10 +412,8 @@ public static void assertNotEquals(String message, String expectedStr, String ac * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator - * @throws JSONException JSON parsing error */ - public static void assertEquals(String expectedStr, String actualStr, JSONComparator comparator) - throws JSONException { + public static void assertEquals(String expectedStr, String actualStr, JSONComparator comparator) { assertEquals("", expectedStr, actualStr, comparator); } @@ -473,10 +426,8 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) - throws JSONException { + public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); @@ -490,10 +441,8 @@ public static void assertEquals(String message, String expectedStr, String actua * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String expectedStr, String actualStr, JSONComparator comparator) - throws JSONException { + public static void assertNotEquals(String expectedStr, String actualStr, JSONComparator comparator) { assertNotEquals("", expectedStr, actualStr, comparator); } @@ -505,16 +454,72 @@ public static void assertNotEquals(String expectedStr, String actualStr, JSONCom * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) - throws JSONException { + public static void assertNotEquals(String message, String expectedStr, String actualStr, + JSONComparator comparator) { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } } + /** + * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an + * {@link AssertionError}. + * + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + */ + public static void assertEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) { + assertEquals("", expected, actual, comparator); + } + + /** + * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an + * {@link AssertionError}. + * + * @param message Error message to be displayed in case of assertion failure + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + */ + public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONComparator comparator) { + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, comparator); + if (result.failed()) { + throw new AssertionError(getCombinedMessage(message, result.getMessage())); + } + } + + /** + * Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an + * {@link AssertionError}. + * + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + */ + public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONComparator comparator) { + assertNotEquals("", expected, actual, comparator); + } + + /** + * Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an + * {@link AssertionError}. + * + * @param message Error message to be displayed in case of assertion failure + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param comparator Comparator + */ + public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, + JSONComparator comparator) { + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, comparator); + if (result.passed()) { + throw new AssertionError(getCombinedMessage(message, result.getMessage())); + } + } + /** * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an * {@link AssertionError}. @@ -522,10 +527,8 @@ public static void assertNotEquals(String message, String expectedStr, String ac * @param expected Expected JSONObject * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(JSONObject expected, JSONObject actual, boolean strict) - throws JSONException { + public static void assertEquals(JSONObject expected, JSONObject actual, boolean strict) { assertEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -537,10 +540,8 @@ public static void assertEquals(JSONObject expected, JSONObject actual, boolean * @param expected Expected JSONObject * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, JSONObject expected, JSONObject actual, boolean strict) - throws JSONException { + public static void assertEquals(String message, JSONObject expected, JSONObject actual, boolean strict) { assertEquals(message, expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -551,10 +552,8 @@ public static void assertEquals(String message, JSONObject expected, JSONObject * @param expected Expected JSONObject * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(JSONObject expected, JSONObject actual, boolean strict) - throws JSONException { + public static void assertNotEquals(JSONObject expected, JSONObject actual, boolean strict) { assertNotEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -566,10 +565,8 @@ public static void assertNotEquals(JSONObject expected, JSONObject actual, boole * @param expected Expected JSONObject * @param actual JSONObject to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, boolean strict) { assertNotEquals(message, expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -580,10 +577,8 @@ public static void assertNotEquals(String message, JSONObject expected, JSONObje * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) { assertEquals("", expected, actual, compareMode); } @@ -595,10 +590,9 @@ public static void assertEquals(JSONObject expected, JSONObject actual, JSONComp * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String message, JSONObject expected, JSONObject actual, + JSONCompareMode compareMode) { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); @@ -612,10 +606,8 @@ public static void assertEquals(String message, JSONObject expected, JSONObject * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode) { assertNotEquals("", expected, actual, compareMode); } @@ -627,10 +619,9 @@ public static void assertNotEquals(JSONObject expected, JSONObject actual, JSONC * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, + JSONCompareMode compareMode) { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); @@ -644,10 +635,8 @@ public static void assertNotEquals(String message, JSONObject expected, JSONObje * @param expected Expected JSONArray * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict) - throws JSONException { + public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict) { assertEquals("", expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -659,10 +648,8 @@ public static void assertEquals(JSONArray expected, JSONArray actual, boolean st * @param expected Expected JSONArray * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, JSONArray expected, JSONArray actual, boolean strict) - throws JSONException { + public static void assertEquals(String message, JSONArray expected, JSONArray actual, boolean strict) { assertEquals(message, expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -673,10 +660,8 @@ public static void assertEquals(String message, JSONArray expected, JSONArray ac * @param expected Expected JSONArray * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(JSONArray expected, JSONArray actual, boolean strict) - throws JSONException { + public static void assertNotEquals(JSONArray expected, JSONArray actual, boolean strict) { assertNotEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -688,10 +673,8 @@ public static void assertNotEquals(JSONArray expected, JSONArray actual, boolean * @param expected Expected JSONArray * @param actual JSONArray to compare * @param strict Enables strict checking - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, boolean strict) - throws JSONException { + public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, boolean strict) { assertNotEquals(message, expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); } @@ -702,10 +685,8 @@ public static void assertNotEquals(String message, JSONArray expected, JSONArray * @param expected Expected JSONArray * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) { assertEquals("", expected, actual, compareMode); } @@ -717,10 +698,8 @@ public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompar * @param expected Expected JSONArray * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); @@ -734,10 +713,8 @@ public static void assertEquals(String message, JSONArray expected, JSONArray ac * @param expected Expected JSONArray * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) { assertNotEquals("", expected, actual, compareMode); } @@ -749,10 +726,9 @@ public static void assertNotEquals(JSONArray expected, JSONArray actual, JSONCom * @param expected Expected JSONArray * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use - * @throws JSONException JSON parsing error */ - public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) - throws JSONException { + public static void assertNotEquals(String message, JSONArray expected, JSONArray actual, + JSONCompareMode compareMode) { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..c17115df 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -15,7 +15,6 @@ package org.skyscreamer.jsonassert; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.json.JSONString; import org.skyscreamer.jsonassert.comparator.DefaultComparator; @@ -41,11 +40,9 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) { * @param actualStr JSON string to compare * @param comparator Comparator to use * @return result of the comparison - * @throws JSONException JSON parsing error * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr */ - public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) - throws JSONException { + public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) { Object expected = JSONParser.parseJSON(expectedStr); Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { @@ -72,10 +69,8 @@ else if (expected instanceof JSONObject) { * @param actual actual json object * @param comparator comparator to use * @return result of the comparison - * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator) - throws JSONException { + public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator) { return comparator.compareJSON(expected, actual); } @@ -86,10 +81,8 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu * @param actual actual json array * @param comparator comparator to use * @return result of the comparison - * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONComparator comparator) - throws JSONException { + public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONComparator comparator) { return comparator.compareJSON(expected, actual); } @@ -118,10 +111,8 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO * @param actualStr JSON string to compare * @param mode Defines comparison behavior * @return result of the comparison - * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) - throws JSONException { + public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) { return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); } @@ -132,10 +123,8 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr * @param actual JSONObject to compare * @param mode Defines comparison behavior * @return result of the comparison - * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode) - throws JSONException { + public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode) { return compareJSON(expected, actual, getComparatorForMode(mode)); } @@ -147,10 +136,8 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu * @param actual JSONArray to compare * @param mode Defines comparison behavior * @return result of the comparison - * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode) - throws JSONException { + public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode) { return compareJSON(expected, actual, getComparatorForMode(mode)); } diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java index d676be45..8185b065 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java @@ -18,7 +18,10 @@ *

These different modes define different behavior for the comparison of JSON for testing. * Each mode encapsulates two underlying behaviors: extensibility and strict ordering.

* - * + *
+ * * * * diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java index 28406fbe..55672099 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java @@ -245,7 +245,7 @@ private static String describe(Object value) { } else if (value instanceof JSONObject) { return "a JSON object"; } else { - return value.toString(); + return String.valueOf(value); } } diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONParser.java b/src/main/java/org/skyscreamer/jsonassert/JSONParser.java index 882d25da..a70af96c 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONParser.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONParser.java @@ -36,9 +36,8 @@ private JSONParser() {} * * @param s Raw JSON string to be parsed * @return JSONObject or JSONArray - * @throws JSONException JSON parsing error */ - public static Object parseJSON(final String s) throws JSONException { + public static Object parseJSON(final String s) { if (s.trim().startsWith("{")) { return new JSONObject(s); } diff --git a/src/main/java/org/skyscreamer/jsonassert/LocationAwareValueMatcher.java b/src/main/java/org/skyscreamer/jsonassert/LocationAwareValueMatcher.java index 32891b62..cc2b7449 100644 --- a/src/main/java/org/skyscreamer/jsonassert/LocationAwareValueMatcher.java +++ b/src/main/java/org/skyscreamer/jsonassert/LocationAwareValueMatcher.java @@ -21,7 +21,7 @@ * A ValueMatcher extension that provides location in form of prefix to the equals method. * * @author Duncan Mackinder - * + * @param Generic Type */ public interface LocationAwareValueMatcher extends ValueMatcher { diff --git a/src/main/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcher.java b/src/main/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcher.java index 2a9efa54..19a4ef1d 100644 --- a/src/main/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcher.java +++ b/src/main/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcher.java @@ -28,7 +28,7 @@ * specify regular expression pattern that actual value must match. * * @author Duncan Mackinder - * + * @param Generic Type */ public class RegularExpressionValueMatcher implements ValueMatcher { diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java index 41b0ea0f..190e47ea 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java @@ -15,7 +15,6 @@ package org.skyscreamer.jsonassert.comparator; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -26,18 +25,25 @@ /** * This class provides a skeletal implementation of the {@link JSONComparator} * interface, to minimize the effort required to implement this interface. + * + * */ public abstract class AbstractComparator implements JSONComparator { + /** + * Default constructor + */ + public AbstractComparator() { + } + /** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. * * @param expected Expected JSONObject * @param actual JSONObject to compare - * @throws JSONException JSON parsing error */ @Override - public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException { + public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) { JSONCompareResult result = new JSONCompareResult(); compareJSON("", expected, actual, result); return result; @@ -48,15 +54,20 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua * * @param expected Expected JSONArray * @param actual JSONArray to compare - * @throws JSONException JSON parsing error */ @Override - public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) throws JSONException { + public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) { JSONCompareResult result = new JSONCompareResult(); compareJSONArray("", expected, actual, result); return result; } + /** + * @param prefix + * @param expected + * @param actual + * @param result + */ protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) { Set actualKeys = getKeys(actual); for (String key : actualKeys) { @@ -66,7 +77,14 @@ protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject exp } } - protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException { + /** + * + * @param prefix + * @param expected + * @param actual + * @param result + */ + protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) { Set expectedKeys = getKeys(expected); for (String key : expectedKeys) { Object expectedValue = expected.get(key); @@ -79,7 +97,7 @@ protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject exp } } - protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { + protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) { String uniqueKey = findUniqueKey(expected); if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) { // An expensive last resort @@ -104,7 +122,7 @@ protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSO } } - protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { + protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) { Map expectedCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(expected)); Map actualCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(actual)); for (Object o : expectedCount.keySet()) { @@ -122,10 +140,10 @@ protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JS } } - protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { + protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) { for (int i = 0; i < expected.length(); ++i) { - Object expectedValue = expected.get(i); - Object actualValue = actual.get(i); + Object expectedValue = JSONCompareUtil.getObjectOrNull(expected, i); + Object actualValue = JSONCompareUtil.getObjectOrNull(actual, i); compareValues(key + "[" + i + "]", expectedValue, actualValue, result); } } @@ -135,13 +153,20 @@ protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, J // This is expensive (O(n^2) -- yuck), but may be the only resort for some cases with loose array ordering, and no // easy way to uniquely identify each element. protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONArray actual, - JSONCompareResult result) throws JSONException { + JSONCompareResult result) { Set matched = new HashSet(); for (int i = 0; i < expected.length(); ++i) { - Object expectedElement = expected.get(i); + Object expectedElement = JSONCompareUtil.getObjectOrNull(expected, i); boolean matchFound = false; for (int j = 0; j < actual.length(); ++j) { - Object actualElement = actual.get(j); + Object actualElement = JSONCompareUtil.getObjectOrNull(actual, j); + if (expectedElement == actualElement) { + matchFound = true; + break; + } + if ((expectedElement == null && actualElement != null) || (expectedElement != null && actualElement == null)) { + continue; + } if (matched.contains(j) || !actualElement.getClass().equals(expectedElement.getClass())) { continue; } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java index 35a62981..7e348b82 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java @@ -17,7 +17,6 @@ import java.text.MessageFormat; import org.json.JSONArray; -import org.json.JSONException; import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -69,7 +68,7 @@ public ArraySizeComparator(JSONCompareMode mode) { */ @Override public void compareJSONArray(String prefix, JSONArray expected, - JSONArray actual, JSONCompareResult result) throws JSONException { + JSONArray actual, JSONCompareResult result) { String arrayPrefix = prefix + "[]"; if (expected.length() < 1 || expected.length() > 2) { result.fail(MessageFormat diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/CustomComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/CustomComparator.java index ca326844..73337bdf 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/CustomComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/CustomComparator.java @@ -14,7 +14,6 @@ package org.skyscreamer.jsonassert.comparator; -import org.json.JSONException; import org.skyscreamer.jsonassert.Customization; import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -33,7 +32,7 @@ public CustomComparator(JSONCompareMode mode, Customization... customizations) } @Override - public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException { + public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) { Customization customization = getCustomization(prefix); if (customization != null) { try { diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java index d7a210dc..1e8efc01 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java @@ -15,7 +15,6 @@ package org.skyscreamer.jsonassert.comparator; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -36,8 +35,7 @@ public DefaultComparator(JSONCompareMode mode) { } @Override - public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) - throws JSONException { + public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) { // Check that actual contains all the expected values checkJsonObjectKeysExpectedInActual(prefix, expected, actual, result); @@ -48,9 +46,13 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J } @Override - public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) - throws JSONException { - if (areNumbers(expectedValue, actualValue)) { + public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) { + if (expectedValue == actualValue) { + return; + } + if (expectedValue == null || actualValue == null) { + result.fail(prefix, expectedValue, actualValue); + } else if (areNumbers(expectedValue, actualValue)) { if (areNotSameDoubles(expectedValue, actualValue)) { result.fail(prefix, expectedValue, actualValue); } @@ -68,8 +70,7 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu } @Override - public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) - throws JSONException { + public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) { if (expected.length() != actual.length()) { result.fail(prefix + "[]: Expected " + expected.length() + " values but got " + actual.length()); return; diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java index 65ee88e5..9a78d493 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java @@ -15,7 +15,6 @@ package org.skyscreamer.jsonassert.comparator; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONCompareResult; @@ -33,9 +32,8 @@ public interface JSONComparator { * @param expected the expected JSON object * @param actual the actual JSON object * @return the result of the comparison - * @throws JSONException JSON parsing error */ - JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException; + JSONCompareResult compareJSON(JSONObject expected, JSONObject actual); /** * Compares two {@link JSONArray}s and returns the result of the comparison in a {@link JSONCompareResult} object. @@ -43,9 +41,8 @@ public interface JSONComparator { * @param expected the expected JSON array * @param actual the actual JSON array * @return the result of the comparison - * @throws JSONException JSON parsing error */ - JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) throws JSONException; + JSONCompareResult compareJSON(JSONArray expected, JSONArray actual); /** * Compares two {@link JSONObject}s on the provided path represented by {@code prefix} and @@ -55,9 +52,8 @@ public interface JSONComparator { * @param expected the expected JSON object * @param actual the actual JSON object * @param result stores the actual state of the comparison result - * @throws JSONException JSON parsing error */ - void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException; + void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result); /** * Compares two {@link Object}s on the provided path represented by {@code prefix} and @@ -67,9 +63,8 @@ public interface JSONComparator { * @param expectedValue the expected value * @param actualValue the actual value * @param result stores the actual state of the comparison result - * @throws JSONException JSON parsing error */ - void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException; + void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result); /** * Compares two {@link JSONArray}s on the provided path represented by {@code prefix} and @@ -79,7 +74,6 @@ public interface JSONComparator { * @param expected the expected JSON array * @param actual the actual JSON array * @param result stores the actual state of the comparison result - * @throws JSONException JSON parsing error */ - void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException; + void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result); } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java index 617df012..8a7fe18d 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java @@ -25,7 +25,6 @@ import java.util.TreeSet; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; /** @@ -44,9 +43,8 @@ private JSONCompareUtil() { * @param array the JSON array to convert * @param uniqueKey the key to map the JSON objects to * @return the map of {@link JSONObject}s from {@code array} - * @throws JSONException JSON parsing error */ - public static Map arrayOfJsonObjectToMap(JSONArray array, String uniqueKey) throws JSONException { + public static Map arrayOfJsonObjectToMap(JSONArray array, String uniqueKey) { Map valueMap = new HashMap(); for (int i = 0; i < array.length(); ++i) { JSONObject jsonObject = (JSONObject) array.get(i); @@ -61,9 +59,8 @@ public static Map arrayOfJsonObjectToMap(JSONArray array, St * * @param expected the array to find the unique key of * @return the unique key if there's any, otherwise null - * @throws JSONException JSON parsing error */ - public static String findUniqueKey(JSONArray expected) throws JSONException { + public static String findUniqueKey(JSONArray expected) { // Find a unique key for the object (id, name, whatever) JSONObject o = (JSONObject) expected.get(0); // There's at least one at this point for (String candidate : getKeys(o)) { @@ -86,9 +83,8 @@ public static String findUniqueKey(JSONArray expected) throws JSONException { * @param candidate is usable as a unique key if every element in the * @param array is a JSONObject having that key, and no two values are the same. * @return true if the candidate can work as a unique id across array - * @throws JSONException JSON parsing error */ - public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) throws JSONException { + public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) { Set seenValues = new HashSet(); for (int i = 0; i < array.length(); i++) { Object item = array.get(i); @@ -116,27 +112,36 @@ public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) thr * * @param expected the JSON array to convert * @return the list of objects from the {@code expected} array - * @throws JSONException JSON parsing error */ - public static List jsonArrayToList(JSONArray expected) throws JSONException { + public static List jsonArrayToList(JSONArray expected) { List jsonObjects = new ArrayList(expected.length()); for (int i = 0; i < expected.length(); ++i) { - jsonObjects.add(expected.get(i)); + jsonObjects.add(getObjectOrNull(expected, i)); } return jsonObjects; } + /** + * Returns the value present in the given index position. If null value is present, it will return null + * + * @param jsonArray the JSON array to get value from + * @param index index of object to retrieve + * @return value at the given index position + */ + public static Object getObjectOrNull(JSONArray jsonArray, int index) { + return jsonArray.isNull(index) ? null : jsonArray.get(index); + } + /** * Returns whether all of the elements in the given array are simple values. * * @param array the JSON array to iterate through on * @return true if all the elements in {@code array} are simple values - * @throws JSONException JSON parsing error * @see #isSimpleValue(Object) */ - public static boolean allSimpleValues(JSONArray array) throws JSONException { + public static boolean allSimpleValues(JSONArray array) { for (int i = 0; i < array.length(); ++i) { - if (!isSimpleValue(array.get(i))) { + if (!array.isNull(i) && !isSimpleValue(array.get(i))) { return false; } } @@ -158,9 +163,8 @@ public static boolean isSimpleValue(Object o) { * * @param array the array to inspect * @return true if all the elements in the given array are JSONObjects - * @throws JSONException JSON parsing error */ - public static boolean allJSONObjects(JSONArray array) throws JSONException { + public static boolean allJSONObjects(JSONArray array) { for (int i = 0; i < array.length(); ++i) { if (!(array.get(i) instanceof JSONObject)) { return false; @@ -174,9 +178,8 @@ public static boolean allJSONObjects(JSONArray array) throws JSONException { * * @param array the array to inspect * @return true if all the elements in the given array are JSONArrays - * @throws JSONException JSON parsing error */ - public static boolean allJSONArrays(JSONArray array) throws JSONException { + public static boolean allJSONArrays(JSONArray array) { for (int i = 0; i < array.length(); ++i) { if (!(array.get(i) instanceof JSONArray)) { return false; diff --git a/src/site/resources/index.html b/src/site/resources/index.html index 98d14384..41f2c5ae 100644 --- a/src/site/resources/index.html +++ b/src/site/resources/index.html @@ -51,7 +51,7 @@

Introduction

  • JUnit
  • -


    The current version of JSONassert is 1.5.0

    +


    The current version of JSONassert is 2.0-rc1

    Examples

    diff --git a/src/site/resources/quickstart.html b/src/site/resources/quickstart.html index 67f7e637..371e3575 100644 --- a/src/site/resources/quickstart.html +++ b/src/site/resources/quickstart.html @@ -49,7 +49,7 @@

    Quick Start

    <dependency>
      <groupId>org.skyscreamer</groupId>
      <artifactId>jsonassert</artifactId>
    -   <version>1.5.0</version>
    +   <version>2.0-rc1</version>
    </dependency>
    diff --git a/src/test/java/org/skyscreamer/jsonassert/ArrayValueMatcherTest.java b/src/test/java/org/skyscreamer/jsonassert/ArrayValueMatcherTest.java index b7f29e1c..fa815d3c 100644 --- a/src/test/java/org/skyscreamer/jsonassert/ArrayValueMatcherTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/ArrayValueMatcherTest.java @@ -21,7 +21,6 @@ import java.text.MessageFormat; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; import org.skyscreamer.jsonassert.comparator.ArraySizeComparator; @@ -43,12 +42,12 @@ public class ArrayValueMatcherTest { private static final JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); private void doTest(String jsonPath, ArrayValueMatcher arrayValueMatcher, String expectedJSON, - String actualJSON) throws JSONException { + String actualJSON) { Customization customization = new Customization(jsonPath, arrayValueMatcher); JSONAssert.assertEquals(expectedJSON, actualJSON, new CustomComparator(JSONCompareMode.LENIENT, customization)); } - private void doFailingMatchTest(String jsonPath, ArrayValueMatcher arrayValueMatcher, String expectedJSON, String actualJSON, String expectedMessagePattern) throws JSONException { + private void doFailingMatchTest(String jsonPath, ArrayValueMatcher arrayValueMatcher, String expectedJSON, String actualJSON, String expectedMessagePattern) { try { doTest(jsonPath, arrayValueMatcher, expectedJSON, actualJSON); } @@ -61,12 +60,12 @@ private void doFailingMatchTest(String jsonPath, ArrayValueMatcher array } @Test - public void matchesSecondElementOfJSONObjectArray() throws JSONException { + public void matchesSecondElementOfJSONObjectArray() { doTest("a", new ArrayValueMatcher(comparator, 1), "{a:[{background:grey,id:2,type:row}]}", ARRAY_OF_JSONOBJECTS); } @Test - public void failsWhenSecondElementOfJSONObjectArrayDoesNotMatch() throws JSONException { + public void failsWhenSecondElementOfJSONObjectArrayDoesNotMatch() { doFailingMatchTest("a", new ArrayValueMatcher(comparator, 1), "{a:[{background:DOES_NOT_MATCH,id:2,type:row}]}", @@ -75,7 +74,7 @@ public void failsWhenSecondElementOfJSONObjectArrayDoesNotMatch() throws JSONExc } @Test - public void failsWhenThirdElementOfJSONObjectArrayDoesNotMatchInMultiplePlaces() throws JSONException { + public void failsWhenThirdElementOfJSONObjectArrayDoesNotMatchInMultiplePlaces() { doFailingMatchTest("a", new ArrayValueMatcher(comparator, 2), "{a:[{background:DOES_NOT_MATCH,id:3,type:WRONG_TYPE}]}", @@ -84,7 +83,7 @@ public void failsWhenThirdElementOfJSONObjectArrayDoesNotMatchInMultiplePlaces() } @Test - public void failsWhenTwoElementsOfJSONObjectArrayDoNotMatch() throws JSONException { + public void failsWhenTwoElementsOfJSONObjectArrayDoNotMatch() { doFailingMatchTest("a", new ArrayValueMatcher(comparator, 1, 2), "{a:[{background:DOES_NOT_MATCH,id:2,type:row},{background:white,id:3,type:WRONG_TYPE}]}", @@ -93,29 +92,29 @@ public void failsWhenTwoElementsOfJSONObjectArrayDoNotMatch() throws JSONExcepti } @Test - public void matchesThirdElementOfSimpleValueArray() throws JSONException { + public void matchesThirdElementOfSimpleValueArray() { doTest("a", new ArrayValueMatcher(comparator, 2), "{a:[3]}", ARRAY_OF_INTEGERS); } @Test - public void failsWhenTwoElementOfSimpleValueArrayDoNotMatch() throws JSONException { + public void failsWhenTwoElementOfSimpleValueArrayDoNotMatch() { doFailingMatchTest("a", new ArrayValueMatcher(comparator, 3, 4), "{a:[3,4]}", ARRAY_OF_INTEGERS, "a\\[3\\]\\s*Expected:\\s3\\s*got:\\s*4\\s*;\\s*a\\[4\\]\\s*Expected:\\s*4\\s*got:\\s*5\\s*"); } @Test - public void matchesFirstElementOfArrayOfJSONArrays() throws JSONException { + public void matchesFirstElementOfArrayOfJSONArrays() { doTest("a", new ArrayValueMatcher(comparator, 0), "{a:[[6,7,8]]}", ARRAY_OF_JSONARRAYS); } @Test - public void matchesSizeOfFirstThreeInnerArrays() throws JSONException { + public void matchesSizeOfFirstThreeInnerArrays() { JSONComparator innerArraySizeComparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER); doTest("a", new ArrayValueMatcher(innerArraySizeComparator, 0, 2), "{a:[[3]]}", ARRAY_OF_JSONARRAYS); } @Test - public void failsWhenInnerArraySizeDoesNotMatch() throws JSONException { + public void failsWhenInnerArraySizeDoesNotMatch() { JSONComparator innerArraySizeComparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER); doFailingMatchTest("a", new ArrayValueMatcher(innerArraySizeComparator), @@ -125,7 +124,7 @@ public void failsWhenInnerArraySizeDoesNotMatch() throws JSONException { } @Test - public void failsWhenInnerJSONObjectArrayElementDoesNotMatch() throws JSONException { + public void failsWhenInnerJSONObjectArrayElementDoesNotMatch() { ArrayValueMatcher innerArrayValueMatcher = new ArrayValueMatcher(comparator, 1); JSONComparator innerArrayComparator = new CustomComparator( JSONCompareMode.LENIENT, new Customization("a[2]", innerArrayValueMatcher)); @@ -137,12 +136,12 @@ public void failsWhenInnerJSONObjectArrayElementDoesNotMatch() throws JSONExcept } @Test - public void matchesEveryElementOfJSONObjectArray() throws JSONException { + public void matchesEveryElementOfJSONObjectArray() { doTest("a", new ArrayValueMatcher(comparator), "{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS); } @Test - public void failsWhenNotEveryElementOfJSONObjectArrayMatches() throws JSONException { + public void failsWhenNotEveryElementOfJSONObjectArrayMatches() { doFailingMatchTest("a", new ArrayValueMatcher(comparator), "{a:[{background:white}]}", @@ -151,22 +150,22 @@ public void failsWhenNotEveryElementOfJSONObjectArrayMatches() throws JSONExcept } @Test - public void matchesEveryElementOfJSONObjectArrayWhenRangeTooLarge() throws JSONException { + public void matchesEveryElementOfJSONObjectArrayWhenRangeTooLarge() { doTest("a", new ArrayValueMatcher(comparator, 0, 500), "{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS); } @Test - public void matchesElementPairsStartingFromElement1OfJSONObjectArrayWhenRangeTooLarge() throws JSONException { + public void matchesElementPairsStartingFromElement1OfJSONObjectArrayWhenRangeTooLarge() { doTest("a", new ArrayValueMatcher(comparator, 1, 500), "{a:[{background:grey},{background:white}]}", ARRAY_OF_JSONOBJECTS); } @Test - public void matchesElementPairsStartingFromElement0OfJSONObjectArrayWhenRangeTooLarge() throws JSONException { + public void matchesElementPairsStartingFromElement0OfJSONObjectArrayWhenRangeTooLarge() { doTest("a", new ArrayValueMatcher(comparator), "{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS); } @Test - public void failsWhenAppliedToNonArray() throws JSONException { + public void failsWhenAppliedToNonArray() { try { doTest("a", new ArrayValueMatcher(comparator), "{a:[{background:white}]}", "{a:{attr1:value1,attr2:value2}}"); } @@ -185,12 +184,12 @@ public void failsWhenAppliedToNonArray() throws JSONException { */ @Test - public void simpleValueMatchesSecondElementOfJSONObjectArray() throws JSONException { + public void simpleValueMatchesSecondElementOfJSONObjectArray() { doTest("a", new ArrayValueMatcher(comparator, 3), "{a:4}", ARRAY_OF_INTEGERS); } @Test - public void jsonObjectMatchesSecondElementOfJSONObjectArray() throws JSONException { + public void jsonObjectMatchesSecondElementOfJSONObjectArray() { doTest("a", new ArrayValueMatcher(comparator, 1), "{a:{background:grey,id:2,type:row}}", ARRAY_OF_JSONOBJECTS); } @@ -198,35 +197,35 @@ public void jsonObjectMatchesSecondElementOfJSONObjectArray() throws JSONExcepti * Following tests contain copies of code quoted in ArrayValueMatcher JavaDoc and are included to verify that the exact code documented works as expected. */ @Test - public void verifyIdAttributeOfFirstArrayElementMatches() throws JSONException { + public void verifyIdAttributeOfFirstArrayElementMatches() { JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 0)); JSONAssert.assertEquals("{a:[{id:1}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifyIdAttributeOfFirstArrayElementMatchesSimplifiedExpectedSyntax() throws JSONException { + public void verifyIdAttributeOfFirstArrayElementMatchesSimplifiedExpectedSyntax() { JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 0)); JSONAssert.assertEquals("{a:{id:1}}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifyTypeAttributeOfSecondAndThirdElementMatchesRow() throws JSONException { + public void verifyTypeAttributeOfSecondAndThirdElementMatchesRow() { JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 1, 2)); JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifyTypeAttributeOfEveryArrayElementMatchesRow() throws JSONException { + public void verifyTypeAttributeOfEveryArrayElementMatchesRow() { JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); Customization customization = new Customization("a", new ArrayValueMatcher(comparator)); JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifyEveryArrayElementWithCustomComparator() throws JSONException { + public void verifyEveryArrayElementWithCustomComparator() { // get length of array we will verify int aLength = ((JSONArray)((JSONObject)JSONParser.parseJSON(ARRAY_OF_JSONOBJECTS)).get("a")).length(); // create array of customizations one for each array element @@ -245,21 +244,21 @@ public void verifyEveryArrayElementWithCustomComparator() throws JSONException { } @Test - public void verifyBackgroundAttributesOfEveryArrayElementAlternateBetweenWhiteAndGrey() throws JSONException { + public void verifyBackgroundAttributesOfEveryArrayElementAlternateBetweenWhiteAndGrey() { JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT); Customization customization = new Customization("a", new ArrayValueMatcher(comparator)); JSONAssert.assertEquals("{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifyEveryElementOfArrayIsJSONArrayOfLength3() throws JSONException { + public void verifyEveryElementOfArrayIsJSONArrayOfLength3() { JSONComparator comparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 0, 2)); JSONAssert.assertEquals("{a:[[3]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization)); } @Test - public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9() throws JSONException { + public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9() { Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher(comparator, 0)); JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 1)); @@ -267,7 +266,7 @@ public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9() throws J } @Test - public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithSimpliedExpectedString() throws JSONException { + public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithSimpliedExpectedString() { Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher(comparator, 0)); JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 1)); @@ -275,7 +274,7 @@ public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithSimplie } @Test - public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithEvenMoreSimpliedExpectedString() throws JSONException { + public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithEvenMoreSimpliedExpectedString() { Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher(comparator, 0)); JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization); Customization customization = new Customization("a", new ArrayValueMatcher(comparator, 1)); diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java new file mode 100644 index 00000000..0bf11531 --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java @@ -0,0 +1,48 @@ +package org.skyscreamer.jsonassert; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; + +public class JSONArrayWithNullTest { + @Test + public void testJSONArrayWithNullValue() { + JSONArray jsonArray1 = getJSONArray1(); + JSONArray jsonArray2 = getJSONArray2(); + + JSONAssert.assertEquals(jsonArray1, jsonArray2, true); + JSONAssert.assertEquals(jsonArray1, jsonArray2, false); + } + + @Test + public void testJSONArrayWithNullValueAndJsonObject() { + JSONArray jsonArray1 = getJSONArray1(); + JSONObject jsonObject1 = new JSONObject(); + jsonObject1.put("hey", "value"); + + JSONArray jsonArray2 = getJSONArray2(); + JSONObject jsonObject2 = new JSONObject(); + jsonObject2.put("hey", "value"); + + JSONAssert.assertEquals(jsonArray1, jsonArray2, true); + JSONAssert.assertEquals(jsonArray1, jsonArray2, false); + } + + private JSONArray getJSONArray1() { + JSONArray jsonArray1 = new JSONArray(); + jsonArray1.put(1); + jsonArray1.put(JSONObject.NULL); + jsonArray1.put(3); + jsonArray1.put(2); + return jsonArray1; + } + + private JSONArray getJSONArray2() { + JSONArray jsonArray1 = new JSONArray(); + jsonArray1.put(1); + jsonArray1.put(JSONObject.NULL); + jsonArray1.put(3); + jsonArray1.put(2); + return jsonArray1; + } +} diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java index e39c80ba..e17f684b 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java @@ -24,7 +24,6 @@ import java.util.Arrays; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; @@ -36,7 +35,7 @@ */ public class JSONAssertTest { @Test - public void testString() throws JSONException { + public void testString() { testPass("\"Joe\"", "\"Joe\"", STRICT); testPass("\"Joe\"", "\"Joe\"", LENIENT); testPass("\"Joe\"", "\"Joe\"", NON_EXTENSIBLE); @@ -48,7 +47,7 @@ public void testString() throws JSONException { } @Test - public void testNumber() throws JSONException { + public void testNumber() { testPass("123", "123", STRICT); testPass("123", "123", LENIENT); testPass("123", "123", NON_EXTENSIBLE); @@ -66,7 +65,7 @@ public void testNumber() throws JSONException { } @Test - public void testSimple() throws JSONException { + public void testSimple() { testPass("{id:1}", "{id:1}", STRICT); testFail("{id:1}", "{id:2}", STRICT); testPass("{id:1}", "{id:1}", LENIENT); @@ -78,7 +77,7 @@ public void testSimple() throws JSONException { } @Test - public void testSimpleStrict() throws JSONException { + public void testSimpleStrict() { testPass("{id:1}", "{id:1,name:\"Joe\"}", LENIENT); testFail("{id:1}", "{id:1,name:\"Joe\"}", STRICT); testPass("{id:1}", "{id:1,name:\"Joe\"}", STRICT_ORDER); @@ -86,7 +85,7 @@ public void testSimpleStrict() throws JSONException { } @Test - public void testReversed() throws JSONException { + public void testReversed() { testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", LENIENT); testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", STRICT); testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", NON_EXTENSIBLE); @@ -94,7 +93,7 @@ public void testReversed() throws JSONException { } @Test // Currently JSONAssert assumes JSONObject. - public void testArray() throws JSONException { + public void testArray() { testPass("[1,2,3]","[1,2,3]", STRICT); testPass("[1,2,3]","[1,3,2]", LENIENT); testFail("[1,2,3]","[1,3,2]", STRICT); @@ -106,7 +105,7 @@ public void testArray() throws JSONException { } @Test - public void testNested() throws JSONException { + public void testNested() { testPass("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}", "{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}", STRICT); testFail("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}", @@ -114,7 +113,7 @@ public void testNested() throws JSONException { } @Test - public void testVeryNested() throws JSONException { + public void testVeryNested() { testPass("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}", "{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}", STRICT); testFail("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}", @@ -122,7 +121,7 @@ public void testVeryNested() throws JSONException { } @Test - public void testSimpleArray() throws JSONException { + public void testSimpleArray() { testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Exact to exact (strict) "{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", STRICT); @@ -153,40 +152,40 @@ public void testSimpleArray() throws JSONException { } @Test - public void testSimpleMixedArray() throws JSONException { + public void testSimpleMixedArray() { testPass("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 321]}", LENIENT); testFail("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 789]}", LENIENT); } @Test - public void testComplexMixedStrictArray() throws JSONException { + public void testComplexMixedStrictArray() { testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", STRICT); } @Test - public void testComplexMixedArray() throws JSONException { + public void testComplexMixedArray() { testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", LENIENT); } @Test - public void testComplexArrayNoUniqueID() throws JSONException { + public void testComplexArrayNoUniqueID() { testPass("{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}", "{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}", LENIENT); } @Test - public void testSimpleAndComplexStrictArray() throws JSONException { + public void testSimpleAndComplexStrictArray() { testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", STRICT); } @Test - public void testSimpleAndComplexArray() throws JSONException { + public void testSimpleAndComplexArray() { testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", LENIENT); } @Test - public void testComplexArray() throws JSONException { + public void testComplexArray() { testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", STRICT); // Exact to exact (strict) @@ -217,23 +216,23 @@ public void testComplexArray() throws JSONException { } @Test - public void testArrayOfArraysStrict() throws JSONException { + public void testArrayOfArraysStrict() { testPass("{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT); testFail("{id:1,stuff:[[1,2],[2,3],[3,4],[]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT); } @Test - public void testArrayOfArrays() throws JSONException { + public void testArrayOfArrays() { testPass("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", LENIENT); } @Test - public void testLenientArrayRecursion() throws JSONException { + public void testLenientArrayRecursion() { testPass("[{\"arr\":[5, 2, 1]}]", "[{\"b\":3, \"arr\":[1, 5, 2]}]", LENIENT); } @Test - public void testFieldMismatch() throws JSONException { + public void testFieldMismatch() { JSONCompareResult result = JSONCompare.compareJSON("{name:\"Pat\"}", "{name:\"Sue\"}", STRICT); FieldComparisonFailure comparisonFailure = result.getFieldFailures().iterator().next(); Assert.assertEquals("Pat", comparisonFailure.getExpected()); @@ -242,7 +241,7 @@ public void testFieldMismatch() throws JSONException { } @Test - public void testBooleanArray() throws JSONException { + public void testBooleanArray() { testPass("[true, false, true, true, false]", "[true, false, true, true, false]", STRICT); testPass("[false, true, true, false, true]", "[true, false, true, true, false]", LENIENT); testFail("[false, true, true, false, true]", "[true, false, true, true, false]", STRICT); @@ -251,34 +250,34 @@ public void testBooleanArray() throws JSONException { } @Test - public void testNullProperty() throws JSONException { + public void testNullProperty() { testFail("{id:1,name:\"Joe\"}", "{id:1,name:null}", STRICT); testFail("{id:1,name:null}", "{id:1,name:\"Joe\"}", STRICT); } @Test - public void testIncorrectTypes() throws JSONException { + public void testIncorrectTypes() { testFail("{id:1,name:\"Joe\"}", "{id:1,name:[]}", STRICT); testFail("{id:1,name:[]}", "{id:1,name:\"Joe\"}", STRICT); } @Test - public void testNullEquality() throws JSONException { + public void testNullEquality() { testPass("{id:1,name:null}", "{id:1,name:null}", STRICT); } @Test - public void testExpectedArrayButActualObject() throws JSONException { + public void testExpectedArrayButActualObject() { testFail("[1]", "{id:1}", LENIENT); } @Test - public void testExpectedObjectButActualArray() throws JSONException { + public void testExpectedObjectButActualArray() { testFail("{id:1}", "[1]", LENIENT); } @Test - public void testEquivalentIntAndLong() throws JSONException { + public void testEquivalentIntAndLong() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -288,7 +287,7 @@ public void testEquivalentIntAndLong() throws JSONException { } @Test - public void testEquivalentIntAndDouble() throws JSONException { + public void testEquivalentIntAndDouble() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -298,7 +297,7 @@ public void testEquivalentIntAndDouble() throws JSONException { } @Test(expected = AssertionError.class) - public void testAssertNotEqualsWhenEqualStrict() throws JSONException { + public void testAssertNotEqualsWhenEqualStrict() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -307,7 +306,7 @@ public void testAssertNotEqualsWhenEqualStrict() throws JSONException { } @Test(expected = AssertionError.class) - public void testAssertNotEqualsWhenEqualLenient() throws JSONException { + public void testAssertNotEqualsWhenEqualLenient() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -316,7 +315,7 @@ public void testAssertNotEqualsWhenEqualLenient() throws JSONException { } @Test() - public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException { + public void testAssertNotEqualsWhenEqualDiffObjectsStrict() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -326,7 +325,7 @@ public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException } @Test(expected = AssertionError.class) - public void testAssertNotEqualsWhenEqualDiffObjectsLenient() throws JSONException { + public void testAssertNotEqualsWhenEqualDiffObjectsLenient() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -337,7 +336,7 @@ public void testAssertNotEqualsWhenEqualDiffObjectsLenient() throws JSONExceptio } @Test() - public void testAssertNotEqualsWhenDifferentStrict() throws JSONException { + public void testAssertNotEqualsWhenDifferentStrict() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -346,7 +345,7 @@ public void testAssertNotEqualsWhenDifferentStrict() throws JSONException { } @Test() - public void testAssertNotEqualsWhenDifferentLenient() throws JSONException { + public void testAssertNotEqualsWhenDifferentLenient() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -355,7 +354,7 @@ public void testAssertNotEqualsWhenDifferentLenient() throws JSONException { } @Test() - public void testAssertNotEqualsString() throws JSONException { + public void testAssertNotEqualsString() { JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", STRICT); JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", LENIENT); JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", true); @@ -363,7 +362,7 @@ public void testAssertNotEqualsString() throws JSONException { } @Test() - public void testAssertEqualsString() throws JSONException { + public void testAssertEqualsString() { JSONAssert.assertEquals("[1,2,3]", "[1,2,3]", true); JSONAssert.assertEquals("{id:12345}", "{id:12345}", false); JSONAssert.assertEquals("{id:12345}", "{id:12345, name:\"john\"}", LENIENT); @@ -372,7 +371,7 @@ public void testAssertEqualsString() throws JSONException { } @Test() - public void testAssertNotEqualsStringAndJSONObject() throws JSONException { + public void testAssertNotEqualsStringAndJSONObject() { JSONObject actual = new JSONObject(); actual.put("id", Double.valueOf(12345)); JSONAssert.assertEquals("{id:12345}", actual, false); @@ -380,7 +379,7 @@ public void testAssertNotEqualsStringAndJSONObject() throws JSONException { } @Test() - public void testAssertNotEqualsJSONArray() throws JSONException { + public void testAssertNotEqualsJSONArray() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertEquals("[1,2,3]", actual, false); JSONAssert.assertNotEquals("[1,2,4]", actual, false); @@ -390,7 +389,7 @@ public void testAssertNotEqualsJSONArray() throws JSONException { } @Test - public void testAssertEqualsStringJSONArrayBooleanWithMessage() throws JSONException { + public void testAssertEqualsStringJSONArrayBooleanWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertEquals("Message", "[1,2,3]", actual, false); performAssertEqualsTestForMessageVerification("[1,2,4]", actual, false); @@ -398,7 +397,7 @@ public void testAssertEqualsStringJSONArrayBooleanWithMessage() throws JSONExcep } @Test - public void testAssertEqualsStringJSONArrayCompareModeWithMessage() throws JSONException { + public void testAssertEqualsStringJSONArrayCompareModeWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertEquals("Message", "[1,2,3]", actual, LENIENT); performAssertEqualsTestForMessageVerification("[1,2,4]", actual, LENIENT); @@ -406,7 +405,7 @@ public void testAssertEqualsStringJSONArrayCompareModeWithMessage() throws JSONE } @Test - public void testAssertEqualsJSONArray2BooleanWithMessage() throws JSONException { + public void testAssertEqualsJSONArray2BooleanWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, false); performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, false); @@ -414,7 +413,7 @@ public void testAssertEqualsJSONArray2BooleanWithMessage() throws JSONException } @Test - public void testAssertEqualsJSONArray2JSONCompareWithMessage() throws JSONException { + public void testAssertEqualsJSONArray2JSONCompareWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, LENIENT); @@ -423,7 +422,7 @@ public void testAssertEqualsJSONArray2JSONCompareWithMessage() throws JSONExcept } @Test - public void testAssertEqualsString2Boolean() throws JSONException { + public void testAssertEqualsString2Boolean() { JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", false); JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", false); @@ -432,7 +431,7 @@ public void testAssertEqualsString2Boolean() throws JSONException { } @Test - public void testAssertEqualsString2JSONCompare() throws JSONException { + public void testAssertEqualsString2JSONCompare() { JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", LENIENT); JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", LENIENT); @@ -441,7 +440,7 @@ public void testAssertEqualsString2JSONCompare() throws JSONException { } @Test - public void testAssertEqualsStringJSONObjectBoolean() throws JSONException { + public void testAssertEqualsStringJSONObjectBoolean() { JSONObject actual = new JSONObject(); actual.put("id", Double.valueOf(12345)); JSONAssert.assertEquals("Message", "{id:12345}", actual, false); @@ -450,7 +449,7 @@ public void testAssertEqualsStringJSONObjectBoolean() throws JSONException { } @Test - public void testAssertEqualsStringJSONObjectJSONCompare() throws JSONException { + public void testAssertEqualsStringJSONObjectJSONCompare() { JSONObject actual = new JSONObject(); actual.put("id", Double.valueOf(12345)); JSONAssert.assertEquals("Message", "{id:12345}", actual, LENIENT); @@ -459,7 +458,7 @@ public void testAssertEqualsStringJSONObjectJSONCompare() throws JSONException { } @Test - public void testAssertEqualsJSONObject2JSONCompare() throws JSONException { + public void testAssertEqualsJSONObject2JSONCompare() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -478,7 +477,7 @@ public void testAssertEqualsJSONObject2JSONCompare() throws JSONException { } @Test - public void testAssertEqualsJSONObject2Boolean() throws JSONException { + public void testAssertEqualsJSONObject2Boolean() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -497,15 +496,15 @@ public void testAssertEqualsJSONObject2Boolean() throws JSONException { } @Test - public void testAssertEqualsString2JsonComparator() throws IllegalArgumentException, JSONException { - JSONAssert.assertEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}", + public void testAssertEqualsString2JsonComparator() throws IllegalArgumentException { + JSONAssert.assertEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1}}", new CustomComparator( JSONCompareMode.STRICT, new Customization("entry.id", new RegularExpressionValueMatcher("\\d")) )); - performAssertEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":as}}", + performAssertEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":NOT_A_NUMBER}}", new CustomComparator( JSONCompareMode.STRICT, new Customization("entry.id", @@ -514,7 +513,7 @@ public void testAssertEqualsString2JsonComparator() throws IllegalArgumentExcept } @Test - public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() throws JSONException { + public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, false); JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, true); @@ -523,7 +522,7 @@ public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() throws JSONEx } @Test - public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() throws JSONException { + public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, LENIENT); JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, STRICT); @@ -532,7 +531,7 @@ public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() throws JS } @Test - public void testAssertNotEqualsJSONArray2BooleanWithMessage() throws JSONException { + public void testAssertNotEqualsJSONArray2BooleanWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, false); performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, false); @@ -540,7 +539,7 @@ public void testAssertNotEqualsJSONArray2BooleanWithMessage() throws JSONExcepti } @Test - public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() throws JSONException { + public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() { JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3)); JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, LENIENT); @@ -549,7 +548,7 @@ public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() throws JSONExc } @Test - public void testAssertNotEqualsString2Boolean() throws JSONException { + public void testAssertNotEqualsString2Boolean() { JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:45}", false); JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:345, name:\"john\"}", false); @@ -558,7 +557,7 @@ public void testAssertNotEqualsString2Boolean() throws JSONException { } @Test - public void testAssertNotEqualsString2JSONCompare() throws JSONException { + public void testAssertNotEqualsString2JSONCompare() { JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:123}", LENIENT); JSONAssert.assertNotEquals("Message", "{id:12345, name:\"John\"}", "{id:12345}", LENIENT); @@ -567,7 +566,7 @@ public void testAssertNotEqualsString2JSONCompare() throws JSONException { } @Test - public void testAssertNotEqualsStringJSONObjectBoolean() throws JSONException { + public void testAssertNotEqualsStringJSONObjectBoolean() { JSONObject actual = new JSONObject(); actual.put("id", Double.valueOf(12345)); JSONAssert.assertNotEquals("Message", "{id:1234}", actual, false); @@ -576,7 +575,7 @@ public void testAssertNotEqualsStringJSONObjectBoolean() throws JSONException { } @Test - public void testAssertNotEqualsStringJSONObjectJSONCompare() throws JSONException { + public void testAssertNotEqualsStringJSONObjectJSONCompare() { JSONObject actual = new JSONObject(); actual.put("id", Double.valueOf(12345)); JSONAssert.assertNotEquals("Message", "{id:1234}", actual, LENIENT); @@ -585,7 +584,7 @@ public void testAssertNotEqualsStringJSONObjectJSONCompare() throws JSONExceptio } @Test - public void testAssertNtEqualsJSONObject2JSONCompare() throws JSONException { + public void testAssertNtEqualsJSONObject2JSONCompare() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -605,7 +604,7 @@ public void testAssertNtEqualsJSONObject2JSONCompare() throws JSONException { } @Test - public void testAssertNotEqualsJSONObject2Boolean() throws JSONException { + public void testAssertNotEqualsJSONObject2Boolean() { JSONObject expected = new JSONObject(); JSONObject actual = new JSONObject(); expected.put("id", Integer.valueOf(12345)); @@ -625,15 +624,15 @@ public void testAssertNotEqualsJSONObject2Boolean() throws JSONException { } @Test - public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentException, JSONException { - JSONAssert.assertNotEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":hh}}", + public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentException { + JSONAssert.assertNotEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":NOT_A_NUMBER}}", new CustomComparator( JSONCompareMode.STRICT, new Customization("entry.id", new RegularExpressionValueMatcher("\\d")) )); - performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}", + performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1}}", new CustomComparator( JSONCompareMode.STRICT, new Customization("entry.id", @@ -642,7 +641,6 @@ public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentExc } private void testPass(String expected, String actual, JSONCompareMode compareMode) - throws JSONException { String message = expected + " == " + actual + " (" + compareMode + ")"; JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); @@ -650,7 +648,6 @@ private void testPass(String expected, String actual, JSONCompareMode compareMod } private void testFail(String expected, String actual, JSONCompareMode compareMode) - throws JSONException { String message = expected + " != " + actual + " (" + compareMode + ")"; JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); @@ -660,7 +657,7 @@ private void testFail(String expected, String actual, JSONCompareMode compareMod private void performAssertEqualsTestForMessageVerification( Object expected, Object actual, - Object strictMode) throws JSONException { + Object strictMode) { String message = "Message"; String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified."; @@ -754,7 +751,7 @@ private void performAssertNotEqualsTestForMessageVerification( Object expected, Object actual, Object strictMode) - throws JSONException { + { String message = "Message"; String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified."; diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java index 450adfde..3bcb7377 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java @@ -21,10 +21,10 @@ import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; +import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT; import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.json.JSONException; import org.junit.Test; import org.junit.internal.matchers.TypeSafeMatcher; @@ -33,18 +33,18 @@ */ public class JSONCompareTest { @Test - public void succeedsWithEmptyArrays() throws JSONException { + public void succeedsWithEmptyArrays() { assertTrue(compareJSON("[]", "[]", LENIENT).passed()); } @Test - public void reportsArraysOfUnequalLength() throws JSONException { + public void reportsArraysOfUnequalLength() { JSONCompareResult result = compareJSON("[4]", "[]", LENIENT); assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values but got 0"))); } @Test - public void reportsArrayMissingExpectedElement() throws JSONException { + public void reportsArrayMissingExpectedElement() { JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT); assertThat(result, failsWithMessage(equalTo("[]\nExpected: 4\n but none found\n ; []\nUnexpected: 7\n"))); assertEquals(result.getFieldMissing().size(), 1); @@ -52,65 +52,65 @@ public void reportsArrayMissingExpectedElement() throws JSONException { } @Test - public void reportsMismatchedFieldValues() throws JSONException { + public void reportsMismatchedFieldValues() { JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": 5}", LENIENT); assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n"))); assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n"))); } @Test - public void reportsMissingField() throws JSONException { + public void reportsMissingField() { JSONCompareResult result = compareJSON("{\"obj\": {\"id\": 3}}", "{\"obj\": {}}", LENIENT); assertThat(result, failsWithMessage(equalTo("obj\nExpected: id\n but none found\n"))); assertEquals(result.getFieldMissing().size(), 1); } @Test - public void reportsUnexpectedArrayWhenExpectingObject() throws JSONException { + public void reportsUnexpectedArrayWhenExpectingObject() { JSONCompareResult result = compareJSON("{}", "[]", LENIENT); assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON object\n got: a JSON array\n"))); } @Test - public void reportsUnexpectedObjectWhenExpectingArray() throws JSONException { + public void reportsUnexpectedObjectWhenExpectingArray() { JSONCompareResult result = compareJSON("[]", "{}", LENIENT); assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON array\n got: a JSON object\n"))); } @Test - public void reportsUnexpectedNull() throws JSONException { + public void reportsUnexpectedNull() { JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": null}", LENIENT); assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: null\n"))); } @Test - public void reportsUnexpectedNonNull() throws JSONException { + public void reportsUnexpectedNonNull() { JSONCompareResult result = compareJSON("{\"id\": null}", "{\"id\": \"abc\"}", LENIENT); assertThat(result, failsWithMessage(equalTo("id\nExpected: null\n got: abc\n"))); } @Test - public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException { + public void reportsUnexpectedFieldInNonExtensibleMode() { JSONCompareResult result = compareJSON("{\"obj\": {}}", "{\"obj\": {\"id\": 3}}", NON_EXTENSIBLE); assertThat(result, failsWithMessage(equalTo("obj\nUnexpected: id\n"))); assertEquals(result.getFieldUnexpected().size(), 1); } @Test - public void reportsMismatchedTypes() throws JSONException { + public void reportsMismatchedTypes() { JSONCompareResult result = compareJSON("{\"arr\":[]}", "{\"arr\":{}}", LENIENT); assertThat(result, failsWithMessage(equalTo("arr\nExpected: a JSON array\n got: a JSON object\n"))); } @Test - public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException { + public void reportsWrongSimpleValueCountInUnorderedArray() { JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT); assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []\nUnexpected: 7\n"))); assertEquals(result.getFieldUnexpected().size(), 1); } @Test - public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException { + public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() { JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT); assertThat(result, failsWithMessage(equalTo("[id=3]\nExpected: a JSON object\n but none found\n ; " + "[id=5]\nUnexpected: a JSON object\n"))); @@ -119,46 +119,72 @@ public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONE } @Test - public void reportsUnmatchedJSONObjectInUnorderedArray() throws JSONException { + public void reportsUnmatchedJSONObjectInUnorderedArray() { JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}]", "[{\"age\" : 23}]", LENIENT); assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"address\":{\"street\":\"Acacia Avenue\"}}"))); } @Test - public void succeedsWithNestedJSONObjectsInUnorderedArray() throws JSONException { + public void succeedsWithNestedJSONObjectsInUnorderedArray() { assertTrue(compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[5, {\"address\" : {\"street\" : \"Acacia Avenue\"}}]", LENIENT).passed()); } @Test - public void succeedsWithJSONObjectsWithNonUniqueKeyInUnorderedArray() throws JSONException { + public void succeedsWithJSONObjectsWithNonUniqueKeyInUnorderedArray() { String jsonDocument = "[{\"age\" : 43}, {\"age\" : 43}]"; assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed()); } @Test - public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONException { + public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() { String jsonDocument = "[{\"age\" : 43}, {\"age\" : {\"years\" : 43}}]"; assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed()); } @Test - public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() throws JSONException { + public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() { JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 2]", LENIENT); assertThat(result, failsWithMessage(equalTo("[1] Could not find match for element 5"))); } @Test - public void reportsUnmatchedJSONArrayWhereOnlyExpectedContainsJSONObjectWithUniqueKey() throws JSONException { + public void reportsUnmatchedJSONArrayWhereOnlyExpectedContainsJSONObjectWithUniqueKey() { JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[{}]", LENIENT); assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}"))); } @Test - public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKeyButActualContainsElementOfOtherType() throws JSONException { + public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKeyButActualContainsElementOfOtherType() { JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[5]", LENIENT); assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}"))); } + @Test + public void reportsUnmatchedJSONArrayWhereExpectedContainsNonnullIntegerButActualContainsNullElement() { + JSONCompareResult result = compareJSON("[ 3 ]", "[ null ]", LENIENT); + assertThat(result, failsWithMessage(equalTo("[]\nExpected: 3\n but none found\n ; " + + "[]\nUnexpected: null\n"))); + } + + @Test + public void reportsUnmatchedJSONArrayWhereExpectedContainsNullElementButActualContainsNonnullInteger() { + JSONCompareResult result = compareJSON("[ null ]", "[ 3 ]", LENIENT); + assertThat(result, failsWithMessage(equalTo("[]\nExpected: null\n but none found\n ; " + + "[]\nUnexpected: 3\n"))); + } + + @Test + public void reportsStrictUnmatchedJSONArrayWhereExpectedContainsNonnullIntegerButActualContainsNullElement() { + JSONCompareResult result = compareJSON("[ 3 ]", "[ null ]", STRICT); + assertThat(result, failsWithMessage(equalTo("[0]\nExpected: 3\n got: null\n"))); + } + + @Test + public void reportsStrictUnmatchedJSONArrayWhereExpectedContainsNullButActualContainsNonnullInteger() { + JSONCompareResult result = compareJSON("[ null ]", "[ 3 ]", STRICT); + assertThat(result, failsWithMessage(equalTo("[0]\nExpected: null\n got: 3\n"))); + } + private Matcher failsWithMessage(final Matcher expectedMessage) { return new TypeSafeMatcher() { @Override diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index 5f4c36ca..53546166 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -14,7 +14,6 @@ package org.skyscreamer.jsonassert; -import org.json.JSONException; import org.junit.Test; import org.skyscreamer.jsonassert.comparator.CustomComparator; import org.skyscreamer.jsonassert.comparator.JSONComparator; @@ -128,7 +127,7 @@ public boolean equal(Object o1, Object o2) { }; @Test - public void whenPathMatchesInCustomizationThenCallCustomMatcher() throws JSONException { + public void whenPathMatchesInCustomizationThenCallCustomMatcher() { JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("first", comparator)); JSONCompareResult result = compareJSON(expected, actual, jsonCmp); assertTrue(result.getMessage(), result.passed()); @@ -136,7 +135,7 @@ public void whenPathMatchesInCustomizationThenCallCustomMatcher() throws JSONExc } @Test - public void whenDeepPathMatchesCallCustomMatcher() throws JSONException { + public void whenDeepPathMatchesCallCustomMatcher() { JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("outer.inner.value", comparator)); JSONCompareResult result = compareJSON(deepExpected, deepActual, jsonCmp); assertTrue(result.getMessage(), result.passed()); @@ -144,7 +143,7 @@ public void whenDeepPathMatchesCallCustomMatcher() throws JSONException { } @Test - public void whenSimpleWildcardPathMatchesCallCustomMatcher() throws JSONException { + public void whenSimpleWildcardPathMatchesCallCustomMatcher() { JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("foo.*.baz", comparator)); JSONCompareResult result = compareJSON(simpleWildcardExpected, simpleWildcardActual, jsonCmp); assertTrue(result.getMessage(), result.passed()); @@ -152,7 +151,7 @@ public void whenSimpleWildcardPathMatchesCallCustomMatcher() throws JSONExceptio } @Test - public void whenDeepWildcardPathMatchesCallCustomMatcher() throws JSONException { + public void whenDeepWildcardPathMatchesCallCustomMatcher() { JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("root.**.baz", comparator)); JSONCompareResult result = compareJSON(deepWildcardExpected, deepWildcardActual, jsonCmp); assertTrue(result.getMessage(), result.passed()); @@ -160,7 +159,7 @@ public void whenDeepWildcardPathMatchesCallCustomMatcher() throws JSONException } @Test - public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONException { + public void whenRootDeepWildcardPathMatchesCallCustomMatcher() { JSONComparator jsonCmp = new CustomComparator(JSONCompareMode.STRICT, new Customization("**.baz", comparator)); JSONCompareResult result = compareJSON(rootDeepWildcardExpected, rootDeepWildcardActual, jsonCmp); assertTrue(result.getMessage(), result.passed()); diff --git a/src/test/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcherTest.java b/src/test/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcherTest.java index 0e591da8..18539bc0 100644 --- a/src/test/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcherTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/RegularExpressionValueMatcherTest.java @@ -16,10 +16,7 @@ import org.junit.Assert; -import org.json.JSONException; import org.junit.Test; -import org.skyscreamer.jsonassert.Customization; -import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.comparator.CustomComparator; /** @@ -34,52 +31,52 @@ public class RegularExpressionValueMatcherTest { private static final String CONSTANT_URI_REGEX_EXPECTED_JSON = "{d:{results:[{__metadata:{uri:X}}]}}"; private void doTest(String jsonPath, String regex, String expectedJSON, - String actualJSON) throws JSONException { + String actualJSON) { JSONAssert.assertEquals(expectedJSON, actualJSON, new CustomComparator( JSONCompareMode.STRICT_ORDER, new Customization(jsonPath, new RegularExpressionValueMatcher(regex)))); } @Test - public void constantRegexWithSimplePathMatchsStringAttribute() throws JSONException { + public void constantRegexWithSimplePathMatchsStringAttribute() { doTest("a", "v.", "{a:x}", "{a:v1}"); } @Test - public void constantRegexWithThreeLevelPathMatchsStringAttribute() throws JSONException { + public void constantRegexWithThreeLevelPathMatchsStringAttribute() { doTest("a.b.c", ".*Is.*", "{a:{b:{c:x}}}", "{a:{b:{c:thisIsAString}}}"); } @Test - public void dynamicRegexWithSimplePathMatchsStringAttribute() throws JSONException { + public void dynamicRegexWithSimplePathMatchsStringAttribute() { doTest("a", null, "{a:\"v.\"}", "{a:v1}"); } @Test - public void dynamicRegexWithThreeLevelPathMatchsStringAttribute() throws JSONException { + public void dynamicRegexWithThreeLevelPathMatchsStringAttribute() { doTest("a.b.c", null, "{a:{b:{c:\".*Is.*\"}}}", "{a:{b:{c:thisIsAString}}}"); } @Test - public void constantRegexMatchesStringAttributeInsideArray() throws JSONException { + public void constantRegexMatchesStringAttributeInsideArray() { doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\('\\d+'\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY); } @Test - public void dynamicRegexMatchesStringAttributeInsideArray() throws JSONException { + public void dynamicRegexMatchesStringAttributeInsideArray() { doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY); } @Test - public void dynamicRegexMatchesStringAttributeInsideArrayWithNoArgConstructor() throws JSONException { + public void dynamicRegexMatchesStringAttributeInsideArrayWithNoArgConstructor() { JSONAssert.assertEquals("{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY, new CustomComparator( JSONCompareMode.STRICT_ORDER, new Customization(ARRAY_ELEMENT_PREFIX, new RegularExpressionValueMatcher()))); } @Test - public void failsWhenDynamicRegexInvalid() throws JSONException { + public void failsWhenDynamicRegexInvalid() { try { doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY); } @@ -89,7 +86,7 @@ public void failsWhenDynamicRegexInvalid() throws JSONException { } @Test - public void failsWhenDynamicRegexDoesNotMatchStringAttributeInsideArray() throws JSONException { + public void failsWhenDynamicRegexDoesNotMatchStringAttributeInsideArray() { try { doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\w+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY); } @@ -99,7 +96,7 @@ public void failsWhenDynamicRegexDoesNotMatchStringAttributeInsideArray() throws } @Test - public void failsWhenConstantRegexInvalid() throws JSONException { + public void failsWhenConstantRegexInvalid() { try { doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\\\['\\\\d+'\\\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY); } @@ -109,7 +106,7 @@ public void failsWhenConstantRegexInvalid() throws JSONException { } @Test - public void failsWhenConstantRegexDoesNotMatchStringAttributeInsideArray() throws JSONException { + public void failsWhenConstantRegexDoesNotMatchStringAttributeInsideArray() { try { doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\\\('\\\\w+'\\\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY); } diff --git a/src/test/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparatorTest.java index c949e8b8..7af97ee5 100644 --- a/src/test/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparatorTest.java @@ -19,7 +19,6 @@ import java.text.MessageFormat; -import org.json.JSONException; import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; @@ -33,12 +32,12 @@ public class ArraySizeComparatorTest { private static final String twoElementArray = "{a:[b,c]}"; - private void doTest(String expectedJSON, String actualJSON) throws JSONException + private void doTest(String expectedJSON, String actualJSON) { JSONAssert.assertEquals(expectedJSON, actualJSON, new ArraySizeComparator(JSONCompareMode.STRICT_ORDER)); } - private void doFailingMatchTest(String expectedJSON, String actualJSON, String expectedMessagePattern) throws JSONException { + private void doFailingMatchTest(String expectedJSON, String actualJSON, String expectedMessagePattern) { try { doTest(expectedJSON, actualJSON); } @@ -51,77 +50,77 @@ private void doFailingMatchTest(String expectedJSON, String actualJSON, String e } @Test - public void succeedsWhenExactSizeExpected() throws JSONException { + public void succeedsWhenExactSizeExpected() { doTest("{a:[2]}", twoElementArray); } @Test - public void succeedsWhenSizeWithinExpectedRange() throws JSONException { + public void succeedsWhenSizeWithinExpectedRange() { doTest("{a:[1,3]}", twoElementArray); } @Test - public void succeedsWhenSizeIsMinimumOfExpectedRange() throws JSONException { + public void succeedsWhenSizeIsMinimumOfExpectedRange() { doTest("{a:[2,4]}", twoElementArray); } @Test - public void succeedsWhenSizeIsMaximumOfExpectedRange() throws JSONException { + public void succeedsWhenSizeIsMaximumOfExpectedRange() { doTest("{a:[1,2]}", twoElementArray); } @Test - public void failsWhenExpectedArrayTooShort() throws JSONException { + public void failsWhenExpectedArrayTooShort() { doFailingMatchTest("{a:[]}", twoElementArray, "a\\[\\]: invalid expectation: expected array should contain either 1 or 2 elements but contains 0 elements"); } @Test - public void failsWhenExpectedArrayTooLong() throws JSONException { + public void failsWhenExpectedArrayTooLong() { doFailingMatchTest("{a:[1,2,3]}", twoElementArray, "a\\[\\]: invalid expectation: expected array should contain either 1 or 2 elements but contains 3 elements"); } @Test - public void failsWhenExpectedNotAllSimpleTypes() throws JSONException { + public void failsWhenExpectedNotAllSimpleTypes() { doFailingMatchTest("{a:[{y:1},2]}", twoElementArray, "a\\[\\]: invalid expectation: minimum expected array size '\\{\"y\":1\\}' not a number"); } @Test - public void failsWhenExpectedMinimumTooSmall() throws JSONException { + public void failsWhenExpectedMinimumTooSmall() { doFailingMatchTest("{a:[-1,6]}", twoElementArray, "a\\[\\]: invalid expectation: minimum expected array size '-1' negative"); } @Test - public void failsWhenExpectedMaximumTooSmall() throws JSONException { + public void failsWhenExpectedMaximumTooSmall() { doFailingMatchTest("{a:[8,6]}", twoElementArray, "a\\[\\]: invalid expectation: maximum expected array size '6' less than minimum expected array size '8'"); } @Test - public void failsWhenExpectedArraySizeNotANumber() throws JSONException { + public void failsWhenExpectedArraySizeNotANumber() { doFailingMatchTest("{a:[X]}", twoElementArray, "a\\[\\]: invalid expectation: expected array size 'X' not a number"); } @Test - public void failsWhenFirstExpectedArrayElementNotANumber() throws JSONException { + public void failsWhenFirstExpectedArrayElementNotANumber() { doFailingMatchTest("{a:[MIN,6]}", twoElementArray, "a\\[\\]: invalid expectation: minimum expected array size 'MIN' not a number"); } @Test - public void failsWhenSecondExpectedArrayElementNotANumber() throws JSONException { + public void failsWhenSecondExpectedArrayElementNotANumber() { doFailingMatchTest("{a:[8,MAX]}", twoElementArray, "a\\[\\]: invalid expectation: maximum expected array size 'MAX' not a number"); } @Test - public void failsWhenActualArrayTooShort() throws JSONException { + public void failsWhenActualArrayTooShort() { doFailingMatchTest("{a:[3]}", twoElementArray, "a\\[\\]\\s*Expected:\\s*array size of 3 elements\\s*got:\\s*2 elements\\s*"); } @Test - public void failsWhenActualArrayLongerThanExpectedLength() throws JSONException { + public void failsWhenActualArrayLongerThanExpectedLength() { doFailingMatchTest("{a:[1]}", twoElementArray, "a\\[\\]\\s*Expected:\\s*array size of 1 elements\\s*got:\\s*2 elements\\s*"); } @Test - public void failsWhenActualArrayLongerThanMaxOfExpectedRange() throws JSONException { + public void failsWhenActualArrayLongerThanMaxOfExpectedRange() { doFailingMatchTest("{a:[0,1]}", twoElementArray, "a\\[\\]\\s*Expected:\\s*array size of 0 to 1 elements\\s*got:\\s*2 elements\\s*"); } @@ -130,12 +129,12 @@ public void failsWhenActualArrayLongerThanMaxOfExpectedRange() throws JSONExcept */ @Test - public void succeedsWhenActualArrayContainsExactly3Elements() throws JSONException { + public void succeedsWhenActualArrayContainsExactly3Elements() { JSONAssert.assertEquals("{a:[3]}", "{a:[7, 8, 9]}", new ArraySizeComparator(JSONCompareMode.LENIENT)); } @Test - public void succeedsWhenActualArrayContainsBetween2And6Elements() throws JSONException { + public void succeedsWhenActualArrayContainsBetween2And6Elements() { JSONAssert.assertEquals("{a:[2,6]}", "{a:[7, 8, 9]}", new ArraySizeComparator(JSONCompareMode.LENIENT)); } diff --git a/src/test/java/org/skyscreamer/jsonassert/comparator/CustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/comparator/CustomComparatorTest.java index 651e6a87..b48ffb59 100644 --- a/src/test/java/org/skyscreamer/jsonassert/comparator/CustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/comparator/CustomComparatorTest.java @@ -16,7 +16,6 @@ import junit.framework.Assert; import org.json.JSONArray; -import org.json.JSONException; import org.junit.Test; import org.skyscreamer.jsonassert.JSONCompare; import org.skyscreamer.jsonassert.JSONCompareMode; @@ -34,7 +33,7 @@ public ArrayOfJsonObjectsComparator(JSONCompareMode mode) { } @Override - public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { + public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) { compareJSONArrayOfJsonObjects(prefix, expected, actual, result); } }
    + * Behavior of JSONCompareMode + *
     ExtensibleStrict Ordering
    STRICTnoyes
    LENIENTyesno