diff --git a/src/test/java/org/json/NumberConversionUtilTest.java b/src/test/java/org/json/NumberConversionUtilTest.java
index c6f07254d..ea2d4a162 100644
--- a/src/test/java/org/json/NumberConversionUtilTest.java
+++ b/src/test/java/org/json/NumberConversionUtilTest.java
@@ -1,173 +1,175 @@
package org.json;
-import org.junit.Test;
-
import java.math.BigDecimal;
import java.math.BigInteger;
-import static org.junit.Assert.*;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
-public class NumberConversionUtilTest {
+class NumberConversionUtilTest {
@Test
- public void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){
+ void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){
Number number = NumberConversionUtil.stringToNumber("00.10d");
- assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 0.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", 0, number.longValue(),0);
- assertEquals("Do not match", 0, number.intValue(),0);
+ assertEquals(0.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(0.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(0, number.longValue(),0,"Do not match");
+ assertEquals(0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){
+ void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){
Number number = NumberConversionUtil.stringToNumber("0.10d");
- assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 0.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", 0, number.longValue(),0);
- assertEquals("Do not match", 0, number.intValue(),0);
+ assertEquals(0.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(0.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(0, number.longValue(),0,"Do not match");
+ assertEquals(0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){
+ void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){
Number number = NumberConversionUtil.stringToNumber("0.010d");
- assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 0.010f, number.floatValue(),0.0f);
- assertEquals("Do not match", 0, number.longValue(),0);
- assertEquals("Do not match", 0, number.intValue(),0);
+ assertEquals(0.010d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(0.010f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(0, number.longValue(),0,"Do not match");
+ assertEquals(0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){
+ void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){
Number number = NumberConversionUtil.stringToNumber("00200.10d");
- assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 200.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", 200, number.longValue(),0);
- assertEquals("Do not match", 200, number.intValue(),0);
+ assertEquals(200.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(200.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(200, number.longValue(),0,"Do not match");
+ assertEquals(200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){
+ void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){
Number number = NumberConversionUtil.stringToNumber("200.10d");
- assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 200.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", 200, number.longValue(),0);
- assertEquals("Do not match", 200, number.intValue(),0);
+ assertEquals(200.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(200.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(200, number.longValue(),0,"Do not match");
+ assertEquals(200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){
+ void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){
Number number = NumberConversionUtil.stringToNumber("200.010d");
- assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 200.010f, number.floatValue(),0.0f);
- assertEquals("Do not match", 200, number.longValue(),0);
- assertEquals("Do not match", 200, number.intValue(),0);
+ assertEquals(200.010d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(200.010f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(200, number.longValue(),0,"Do not match");
+ assertEquals(200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){
+ void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){
Number number = NumberConversionUtil.stringToNumber("-00.10d");
- assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -0.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", -0, number.longValue(),0);
- assertEquals("Do not match", -0, number.intValue(),0);
+ assertEquals(-0.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-0.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-0, number.longValue(),0,"Do not match");
+ assertEquals(-0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){
+ void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){
Number number = NumberConversionUtil.stringToNumber("-0.10d");
- assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -0.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", -0, number.longValue(),0);
- assertEquals("Do not match", -0, number.intValue(),0);
+ assertEquals(-0.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-0.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-0, number.longValue(),0,"Do not match");
+ assertEquals(-0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){
+ void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){
Number number = NumberConversionUtil.stringToNumber("-0.010d");
- assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -0.010f, number.floatValue(),0.0f);
- assertEquals("Do not match", -0, number.longValue(),0);
- assertEquals("Do not match", -0, number.intValue(),0);
+ assertEquals(-0.010d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-0.010f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-0, number.longValue(),0,"Do not match");
+ assertEquals(-0, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){
+ void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){
Number number = NumberConversionUtil.stringToNumber("-00200.10d");
- assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -200.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", -200, number.longValue(),0);
- assertEquals("Do not match", -200, number.intValue(),0);
+ assertEquals(-200.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-200.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-200, number.longValue(),0,"Do not match");
+ assertEquals(-200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){
+ void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){
Number number = NumberConversionUtil.stringToNumber("-200.10d");
- assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -200.10f, number.floatValue(),0.0f);
- assertEquals("Do not match", -200, number.longValue(),0);
- assertEquals("Do not match", -200, number.intValue(),0);
+ assertEquals(-200.10d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-200.10f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-200, number.longValue(),0,"Do not match");
+ assertEquals(-200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){
+ void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){
Number number = NumberConversionUtil.stringToNumber("-200.010d");
- assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -200.010f, number.floatValue(),0.0f);
- assertEquals("Do not match", -200, number.longValue(),0);
- assertEquals("Do not match", -200, number.intValue(),0);
+ assertEquals(-200.010d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-200.010f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-200, number.longValue(),0,"Do not match");
+ assertEquals(-200, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNumbersWithExponents(){
+ void shouldParseNumbersWithExponents(){
Number number = NumberConversionUtil.stringToNumber("23.45e7");
- assertEquals("Do not match", 23.45e7d, number.doubleValue(),0.0d);
- assertEquals("Do not match", 23.45e7f, number.floatValue(),0.0f);
- assertEquals("Do not match", 2.345E8, number.longValue(),0);
- assertEquals("Do not match", 2.345E8, number.intValue(),0);
+ assertEquals(23.45e7d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(23.45e7f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(2.345E8, number.longValue(),0,"Do not match");
+ assertEquals(2.345E8, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseNegativeNumbersWithExponents(){
+ void shouldParseNegativeNumbersWithExponents(){
Number number = NumberConversionUtil.stringToNumber("-23.45e7");
- assertEquals("Do not match", -23.45e7d, number.doubleValue(),0.0d);
- assertEquals("Do not match", -23.45e7f, number.floatValue(),0.0f);
- assertEquals("Do not match", -2.345E8, number.longValue(),0);
- assertEquals("Do not match", -2.345E8, number.intValue(),0);
+ assertEquals(-23.45e7d, number.doubleValue(),0.0d,"Do not match");
+ assertEquals(-23.45e7f, number.floatValue(),0.0f,"Do not match");
+ assertEquals(-2.345E8, number.longValue(),0,"Do not match");
+ assertEquals(-2.345E8, number.intValue(),0,"Do not match");
}
@Test
- public void shouldParseBigDecimal(){
+ void shouldParseBigDecimal(){
Number number = NumberConversionUtil.stringToNumber("19007199254740993.35481234487103587486413587843213584");
assertTrue(number instanceof BigDecimal);
}
@Test
- public void shouldParseBigInteger(){
+ void shouldParseBigInteger(){
Number number = NumberConversionUtil.stringToNumber("1900719925474099335481234487103587486413587843213584");
assertTrue(number instanceof BigInteger);
}
@Test
- public void shouldIdentifyPotentialNumber(){
- assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112.123"));
- assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112e123"));
- assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112.123"));
- assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112e23"));
- assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("--112.123"));
- assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("-a112.123"));
- assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("a112.123"));
- assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("e112.123"));
+ void shouldIdentifyPotentialNumber(){
+ assertTrue(NumberConversionUtil.potentialNumber("112.123"), "Does not identify as number");
+ assertTrue(NumberConversionUtil.potentialNumber("112e123"), "Does not identify as number");
+ assertTrue(NumberConversionUtil.potentialNumber("-112.123"), "Does not identify as number");
+ assertTrue(NumberConversionUtil.potentialNumber("-112e23"), "Does not identify as number");
+ assertFalse(NumberConversionUtil.potentialNumber("--112.123"), "Does not identify as not number");
+ assertFalse(NumberConversionUtil.potentialNumber("-a112.123"), "Does not identify as not number");
+ assertFalse(NumberConversionUtil.potentialNumber("a112.123"), "Does not identify as not number");
+ assertFalse(NumberConversionUtil.potentialNumber("e112.123"), "Does not identify as not number");
}
- @Test(expected = NumberFormatException.class)
- public void shouldExpectExceptionWhenNumberIsNotFormatted(){
- NumberConversionUtil.stringToNumber("112.aa123");
+ @Test
+ void shouldExpectExceptionWhenNumberIsNotFormatted(){
+ assertThrows(NumberFormatException.class, () -> {
+ NumberConversionUtil.stringToNumber("112.aa123");
+ });
}
diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java
index f3364fbba..3cc275162 100644
--- a/src/test/java/org/json/junit/CDLTest.java
+++ b/src/test/java/org/json/junit/CDLTest.java
@@ -4,11 +4,11 @@
Public Domain.
*/
-import static org.junit.Assert.*;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.*;
import org.json.JSONException;
import org.json.JSONObject;
+import org.junit.jupiter.api.Test;
import org.json.JSONArray;
import org.json.CDL;
@@ -18,7 +18,7 @@
* reference app. To test it, strings will be converted to JSON-Java classes
* and then converted back.
*/
-public class CDLTest {
+class CDLTest {
/**
* String of lines where the column names are in the first row,
@@ -50,10 +50,12 @@ public class CDLTest {
* Attempts to create a JSONArray from a null string.
* Expect a NullPointerException.
*/
- @Test(expected=NullPointerException.class)
- public void exceptionOnNullString() {
- String nullStr = null;
- CDL.toJSONArray(nullStr);
+ @Test
+ void exceptionOnNullString() {
+ assertThrows(NullPointerException.class, () -> {
+ String nullStr = null;
+ CDL.toJSONArray(nullStr);
+ });
}
/**
@@ -61,32 +63,32 @@ public void exceptionOnNullString() {
* in column title line. Expects a JSONException.
*/
@Test
- public void unbalancedQuoteInName() {
+ void unbalancedQuoteInName() {
String badLine = "Col1, \"Col2\nVal1, Val2";
try {
CDL.toJSONArray(badLine);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Missing close quote '\"'. at 12 [character 0 line 2]",
- e.getMessage());
+ assertEquals("Missing close quote '\"'. at 12 [character 0 line 2]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
-
+
/**
* Attempts to create a JSONArray from a string with unbalanced quotes
* in value line. Expects a JSONException.
*/
@Test
- public void unbalancedQuoteInValue() {
+ void unbalancedQuoteInValue() {
String badLine = "Col1, Col2\n\"Val1, Val2";
try {
CDL.toJSONArray(badLine);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Missing close quote '\"'. at 22 [character 11 line 2]",
- e.getMessage());
+ assertEquals("Missing close quote '\"'. at 22 [character 11 line 2]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
@@ -96,33 +98,33 @@ public void unbalancedQuoteInValue() {
* in column title line. Expects a JSONException.
*/
@Test
- public void nullInName() {
+ void nullInName() {
String badLine = "C\0ol1, Col2\nVal1, Val2";
try {
CDL.toJSONArray(badLine);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Bad character 'o' (111). at 2 [character 3 line 1]",
- e.getMessage());
+ assertEquals("Bad character 'o' (111). at 2 [character 3 line 1]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
-
+
/**
* Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
* Expects a JSONException.
*/
@Test
- public void unbalancedEscapedQuote(){
+ void unbalancedEscapedQuote(){
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
try {
CDL.toJSONArray(badLine);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Missing close quote '\"'. at 26 [character 15 line 2]",
- e.getMessage());
+ assertEquals("Missing close quote '\"'. at 26 [character 15 line 2]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
@@ -131,7 +133,7 @@ public void unbalancedEscapedQuote(){
* Assert that there is no error for a single escaped quote within a properly embedded quote.
*/
@Test
- public void singleEscapedQuote(){
+ void singleEscapedQuote(){
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
@@ -141,13 +143,13 @@ public void singleEscapedQuote(){
assertTrue(cdlStr.contains("Val1"));
assertTrue(cdlStr.contains("\"Val2"));
}
-
+
/**
* Assert that there is no error for a single escaped quote within a properly
* embedded quote when not the last value.
*/
@Test
- public void singleEscapedQuoteMiddleString(){
+ void singleEscapedQuoteMiddleString(){
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
@@ -157,13 +159,13 @@ public void singleEscapedQuoteMiddleString(){
assertTrue(cdlStr.contains("Val1"));
assertTrue(cdlStr.contains("\"Val2"));
}
-
+
/**
* Attempt to create a JSONArray with an escape quote and no enclosing quotes.
* Expects a JSONException.
*/
@Test
- public void badEscapedQuote(){
+ void badEscapedQuote(){
String badLine = "Col1, Col2\nVal1, \"\"Val2";
try {
@@ -171,74 +173,76 @@ public void badEscapedQuote(){
fail("Expecting an exception");
} catch (JSONException e) {
//System.out.println("Message" + e.getMessage());
- assertEquals("Expecting an exception message",
- "Bad character 'V' (86). at 20 [character 9 line 2]",
- e.getMessage());
+ assertEquals("Bad character 'V' (86). at 20 [character 9 line 2]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
-
+
/**
* call toString with a null array
*/
- @Test(expected=NullPointerException.class)
- public void nullJSONArrayToString() {
- CDL.toString((JSONArray)null);
+ @Test
+ void nullJSONArrayToString() {
+ assertThrows(NullPointerException.class, () -> {
+ CDL.toString((JSONArray)null);
+ });
}
/**
* Create a JSONArray from an empty string
*/
@Test
- public void emptyString() {
+ void emptyString() {
String emptyStr = "";
JSONArray jsonArray = CDL.toJSONArray(emptyStr);
- assertTrue("CDL should return null when the input string is empty",
- jsonArray == null);
+ assertTrue(jsonArray == null,
+ "CDL should return null when the input string is empty");
}
/**
* Create a JSONArray with only 1 row
*/
@Test
- public void onlyColumnNames() {
+ void onlyColumnNames() {
String columnNameStr = "col1, col2, col3";
JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
- assertNull("CDL should return null when only 1 row is given",
- jsonArray);
+ assertNull(jsonArray,
+ "CDL should return null when only 1 row is given");
}
/**
* Create a JSONArray from string containing only whitespace and commas
*/
@Test
- public void emptyLinesToJSONArray() {
+ void emptyLinesToJSONArray() {
String str = " , , , \n , , , ";
JSONArray jsonArray = CDL.toJSONArray(str);
- assertNull("JSONArray should be null for no content",
- jsonArray);
+ assertNull(jsonArray,
+ "JSONArray should be null for no content");
}
/**
* call toString with a null array
*/
@Test
- public void emptyJSONArrayToString() {
+ void emptyJSONArrayToString() {
JSONArray jsonArray = new JSONArray();
String str = CDL.toString(jsonArray);
- assertNull("CDL should return null for toString(null)",
- str);
+ assertNull(str,
+ "CDL should return null for toString(null)");
}
/**
* call toString with a null arrays for names and values
*/
@Test
- public void nullJSONArraysToString() {
+ void nullJSONArraysToString() {
String str = CDL.toString(null, null);
- assertNull("CDL should return null for toString(null)",
- str);
+ assertNull(str,
+ "CDL should return null for toString(null)");
}
/**
@@ -246,7 +250,7 @@ public void nullJSONArraysToString() {
* found that would otherwise be filtered out by CDL.
*/
@Test
- public void checkSpecialChars() {
+ void checkSpecialChars() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonArray.put(jsonObject);
@@ -254,7 +258,7 @@ public void checkSpecialChars() {
jsonObject.put("Col \r1", "V1");
// \r will be filtered from value
jsonObject.put("Col 2", "V2\r");
- assertTrue("expected length should be 1",jsonArray.length() == 1);
+ assertEquals(1, jsonArray.length(), "expected length should be 1");
String cdlStr = CDL.toString(jsonArray);
jsonObject = jsonArray.getJSONObject(0);
assertTrue(cdlStr.contains("\"Col 1\""));
@@ -267,7 +271,7 @@ public void checkSpecialChars() {
* Create a JSONArray from a string of lines
*/
@Test
- public void textToJSONArray() {
+ void textToJSONArray() {
JSONArray jsonArray = CDL.toJSONArray(this.lines);
JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
@@ -278,7 +282,7 @@ public void textToJSONArray() {
* string of value lines
*/
@Test
- public void jsonArrayToJSONArray() {
+ void jsonArrayToJSONArray() {
String nameArrayStr = "[Col1, Col2]";
String values = "V1, V2";
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
@@ -292,7 +296,7 @@ public void jsonArrayToJSONArray() {
* then convert to string and then back to JSONArray
*/
@Test
- public void textToJSONArrayAndBackToString() {
+ void textToJSONArrayAndBackToString() {
JSONArray jsonArray = CDL.toJSONArray(this.lines);
String jsonStr = CDL.toString(jsonArray);
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
diff --git a/src/test/java/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java
index 0af96401b..cdc26d565 100644
--- a/src/test/java/org/json/junit/CookieListTest.java
+++ b/src/test/java/org/json/junit/CookieListTest.java
@@ -4,12 +4,12 @@
Public Domain.
*/
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.json.*;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.jayway.jsonpath.*;
@@ -30,16 +30,18 @@
* 'secure' name with no associated value
* - CookieList has no special handling for attribute name/value pairs.
*/
-public class CookieListTest {
+class CookieListTest {
/**
* Attempts to create a CookieList from a null string.
* Expects a NullPointerException.
*/
- @Test(expected=NullPointerException.class)
- public void nullCookieListException() {
- String cookieStr = null;
- CookieList.toJSONObject(cookieStr);
+ @Test
+ void nullCookieListException() {
+ assertThrows(NullPointerException.class, () -> {
+ String cookieStr = null;
+ CookieList.toJSONObject(cookieStr);
+ });
}
/**
@@ -47,7 +49,7 @@ public void nullCookieListException() {
* Expects a JSONException.
*/
@Test
- public void malFormedCookieListException() {
+ void malFormedCookieListException() {
String cookieStr = "thisCookieHasNoEqualsChar";
try {
CookieList.toJSONObject(cookieStr);
@@ -56,9 +58,9 @@ public void malFormedCookieListException() {
/**
* Not sure of the missing char, but full string compare fails
*/
- assertEquals("Expecting an exception message",
- "Expected '=' and instead saw '' at 25 [character 26 line 1]",
- e.getMessage());
+ assertEquals("Expected '=' and instead saw '' at 25 [character 26 line 1]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
@@ -66,7 +68,7 @@ public void malFormedCookieListException() {
* Creates a CookieList from an empty string.
*/
@Test
- public void emptyStringCookieList() {
+ void emptyStringCookieList() {
String cookieStr = "";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
assertTrue(jsonObject.isEmpty());
@@ -76,26 +78,26 @@ public void emptyStringCookieList() {
* CookieList with the simplest cookie - a name/value pair with no delimiter.
*/
@Test
- public void simpleCookieList() {
+ void simpleCookieList() {
String cookieStr = "SID=31d4d96e407aad42";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
- assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
+ assertEquals(1, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "Expected 1 top level item");
+ assertEquals("31d4d96e407aad42", jsonObject.query("/SID"), "expected 31d4d96e407aad42");
}
/**
* CookieList with a single a cookie which has a name/value pair and delimiter.
*/
@Test
- public void simpleCookieListWithDelimiter() {
+ void simpleCookieListWithDelimiter() {
String cookieStr = "SID=31d4d96e407aad42;";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
- assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
+ assertEquals(1, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "Expected 1 top level item");
+ assertEquals("31d4d96e407aad42", jsonObject.query("/SID"), "expected 31d4d96e407aad42");
}
/**
@@ -103,7 +105,7 @@ public void simpleCookieListWithDelimiter() {
* with delimiters.
*/
@Test
- public void multiPartCookieList() {
+ void multiPartCookieList() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=myCookieValue2;"+
@@ -114,31 +116,31 @@ public void multiPartCookieList() {
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
- assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
- assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
- assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
- assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
- assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
- assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ assertEquals(6, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "Expected 6 top level items");
+ assertEquals("myCookieValue1", jsonObject.query("/name1"), "expected myCookieValue1");
+ assertEquals("myCookieValue2", jsonObject.query("/name2"), "expected myCookieValue2");
+ assertEquals("myCookieValue3", jsonObject.query("/name3"), "expected myCookieValue3");
+ assertEquals("myCookieValue4", jsonObject.query("/name4"), "expected myCookieValue4");
+ assertEquals("myCookieValue5", jsonObject.query("/name5"), "expected myCookieValue5");
+ assertEquals("myCookieValue6", jsonObject.query("/name6"), "expected myCookieValue6");
}
/**
* CookieList from a JSONObject with valid key and null value
*/
@Test
- public void convertCookieListWithNullValueToString() {
+ void convertCookieListWithNullValueToString() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", JSONObject.NULL);
String cookieToStr = CookieList.toString(jsonObject);
- assertTrue("toString() should be empty", "".equals(cookieToStr));
+ assertEquals("", cookieToStr, "toString() should be empty");
}
/**
* CookieList with multiple entries converted to a JSON document.
*/
@Test
- public void convertCookieListToString() {
+ void convertCookieListToString() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=myCookieValue2;"+
@@ -154,21 +156,21 @@ public void convertCookieListToString() {
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
- assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
- assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
- assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
- assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
- assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
- assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ assertEquals(6, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "Expected 6 top level items");
+ assertEquals("myCookieValue1", jsonObject.query("/name1"), "expected myCookieValue1");
+ assertEquals("myCookieValue2", jsonObject.query("/name2"), "expected myCookieValue2");
+ assertEquals("myCookieValue3", jsonObject.query("/name3"), "expected myCookieValue3");
+ assertEquals("myCookieValue4", jsonObject.query("/name4"), "expected myCookieValue4");
+ assertEquals("myCookieValue5", jsonObject.query("/name5"), "expected myCookieValue5");
+ assertEquals("myCookieValue6", jsonObject.query("/name6"), "expected myCookieValue6");
}
/**
* CookieList with multiple entries and some '+' chars and URL-encoded
* values converted to a JSON document.
*/
- @Test
- public void convertEncodedCookieListToString() {
+ @Test
+ void convertEncodedCookieListToString() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=my+Cookie+Value+2;"+
@@ -179,12 +181,12 @@ public void convertEncodedCookieListToString() {
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
- assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
- assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2")));
- assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3")));
- assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4")));
- assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
- assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ assertEquals(6, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "Expected 6 top level items");
+ assertEquals("myCookieValue1", jsonObject.query("/name1"), "expected myCookieValue1");
+ assertEquals("my Cookie Value 2", jsonObject.query("/name2"), "expected my Cookie Value 2");
+ assertEquals("my+Cookie&Value;3=", jsonObject.query("/name3"), "expected my+Cookie&Value;3=");
+ assertEquals("my%CookieValue4", jsonObject.query("/name4"), "expected my%CookieValue4");
+ assertEquals("myCookieValue5", jsonObject.query("/name5"), "expected my%CookieValue5");
+ assertEquals("myCookieValue6", jsonObject.query("/name6"), "expected myCookieValue6");
}
}
diff --git a/src/test/java/org/json/junit/CookieTest.java b/src/test/java/org/json/junit/CookieTest.java
index edd8a7eeb..107486fab 100644
--- a/src/test/java/org/json/junit/CookieTest.java
+++ b/src/test/java/org/json/junit/CookieTest.java
@@ -4,10 +4,10 @@
Public Domain.
*/
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
import org.json.*;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
@@ -23,16 +23,18 @@
*
* A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values.
*/
-public class CookieTest {
+class CookieTest {
/**
* Attempts to create a JSONObject from a null string.
* Expects a NullPointerException.
*/
- @Test(expected=NullPointerException.class)
- public void nullCookieException() {
- String cookieStr = null;
- Cookie.toJSONObject(cookieStr);
+ @Test
+ void nullCookieException() {
+ assertThrows(NullPointerException.class, () -> {
+ String cookieStr = null;
+ Cookie.toJSONObject(cookieStr);
+ });
}
/**
@@ -41,15 +43,15 @@ public void nullCookieException() {
* Expects a JSONException.
*/
@Test
- public void malFormedNameValueException() {
+ void malFormedNameValueException() {
String cookieStr = "thisCookieHasNoEqualsChar";
try {
Cookie.toJSONObject(cookieStr);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Expected '=' and instead saw '' at 25 [character 26 line 1]",
- e.getMessage());
+ assertEquals("Expected '=' and instead saw '' at 25 [character 26 line 1]",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
@@ -59,12 +61,12 @@ public void malFormedNameValueException() {
* Expects a JSONException.
*/
@Test
- public void booleanAttribute() {
+ void booleanAttribute() {
String cookieStr = "this=Cookie;myAttribute";
JSONObject jo = Cookie.toJSONObject(cookieStr);
- assertTrue("has key 'name'", jo.has("name"));
- assertTrue("has key 'value'", jo.has("value"));
- assertTrue("has key 'myAttribute'", jo.has("myattribute"));
+ assertTrue(jo.has("name"), "has key 'name'");
+ assertTrue(jo.has("value"), "has key 'value'");
+ assertTrue(jo.has("myattribute"), "has key 'myAttribute'");
}
/**
@@ -73,17 +75,18 @@ public void booleanAttribute() {
* Expects a JSONException
*/
@Test
- public void emptyStringCookieException() {
+ void emptyStringCookieException() {
String cookieStr = "";
try {
Cookie.toJSONObject(cookieStr);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Cookies must have a 'name'",
- e.getMessage());
+ assertEquals("Cookies must have a 'name'",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
+
/**
*
* Attempts to create a JSONObject from an cookie string where the name is blank.
@@ -91,15 +94,15 @@ public void emptyStringCookieException() {
* Expects a JSONException
*/
@Test
- public void emptyNameCookieException() {
+ void emptyNameCookieException() {
String cookieStr = " = value ";
try {
Cookie.toJSONObject(cookieStr);
fail("Expecting an exception");
} catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "Cookies must have a 'name'",
- e.getMessage());
+ assertEquals("Cookies must have a 'name'",
+ e.getMessage(),
+ "Expecting an exception message");
}
}
@@ -107,7 +110,7 @@ public void emptyNameCookieException() {
* Cookie from a simple name/value pair with no delimiter
*/
@Test
- public void simpleCookie() {
+ void simpleCookie() {
String cookieStr = "SID=31d4d96e407aad42";
String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
@@ -121,7 +124,7 @@ public void simpleCookie() {
* as a boolean.
*/
@Test
- public void multiPartCookie() {
+ void multiPartCookie() {
String cookieStr =
"PH=deleted; "+
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
@@ -148,7 +151,7 @@ public void multiPartCookie() {
* This test confirms both behaviors.
*/
@Test
- public void convertCookieToString() {
+ void convertCookieToString() {
String cookieStr =
"PH=deleted; "+
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
@@ -188,7 +191,7 @@ public void convertCookieToString() {
* behavior.
*/
@Test
- public void convertEncodedCookieToString() {
+ void convertEncodedCookieToString() {
String cookieStr =
"PH=deleted; "+
" expires=Wed,+19-Mar-2014+17:53:53+GMT;"+
@@ -209,7 +212,7 @@ public void convertEncodedCookieToString() {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
}
-
+
/**
* A public API method performs a URL encoding for selected chars
* in a string. Control chars, '+', '%', '=', ';' are all encoded
@@ -217,12 +220,12 @@ public void convertEncodedCookieToString() {
* This test confirms that behavior.
*/
@Test
- public void escapeString() {
+ void escapeString() {
String str = " +%\r\n\t\b%=;;; ";
String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b";
String actualStr = Cookie.escape(str);
- assertTrue("expect escape() to encode correctly. Actual: " +actualStr+
- " expected: " +expectedStr, expectedStr.equals(actualStr));
+ assertEquals(expectedStr, actualStr, "expect escape() to encode correctly. Actual: " + actualStr +
+ " expected: " + expectedStr);
}
/**
@@ -232,11 +235,11 @@ public void escapeString() {
* This test confirms that behavior.
*/
@Test
- public void unescapeString() {
+ void unescapeString() {
String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ ";
String expectedStr = " +%\r\n\t\b%=;;; ";
String actualStr = Cookie.unescape(str);
- assertTrue("expect unescape() to decode correctly. Actual: " +actualStr+
- " expected: " +expectedStr, expectedStr.equals(actualStr));
+ assertEquals(expectedStr, actualStr, "expect unescape() to decode correctly. Actual: " + actualStr +
+ " expected: " + expectedStr);
}
}
diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java
index 1496a636a..5c16804f9 100644
--- a/src/test/java/org/json/junit/EnumTest.java
+++ b/src/test/java/org/json/junit/EnumTest.java
@@ -4,9 +4,7 @@
Public Domain.
*/
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
import java.util.EnumSet;
import java.util.List;
@@ -17,7 +15,7 @@
import org.json.junit.data.MyEnum;
import org.json.junit.data.MyEnumClass;
import org.json.junit.data.MyEnumField;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
@@ -27,7 +25,7 @@
* classes, all required behavior is already be present in some form.
* These tests explore how enum serialization works with JSON-Java.
*/
-public class EnumTest {
+class EnumTest {
/**
* To serialize an enum by its getters, use the JSONObject Object constructor.
@@ -35,11 +33,11 @@ public class EnumTest {
* is created whose entries are the getter name/value pairs.
*/
@Test
- public void jsonObjectFromEnum() {
+ void jsonObjectFromEnum() {
// If there are no getters then the object is empty.
MyEnum myEnum = MyEnum.VAL2;
JSONObject jsonObject = new JSONObject(myEnum);
- assertTrue("simple enum has no getters", jsonObject.isEmpty());
+ assertTrue(jsonObject.isEmpty(), "simple enum has no getters");
// enum with a getters should create a non-empty object
MyEnumField myEnumField = MyEnumField.VAL2;
@@ -48,9 +46,9 @@ public void jsonObjectFromEnum() {
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider()
.parse(jsonObject.toString());
- assertTrue("expecting 2 items in top level object", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expecting val 2", "val 2".equals(jsonObject.query("/value")));
- assertTrue("expecting 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expecting 2 items in top level object");
+ assertEquals("val 2", jsonObject.query("/value"), "expecting val 2");
+ assertEquals(Integer.valueOf(2), jsonObject.query("/intVal"), "expecting 2");
/**
* class which contains enum instances. Each enum should be stored
@@ -63,12 +61,12 @@ public void jsonObjectFromEnum() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected 2 myEnumField items", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
- assertTrue("expected 0 myEnum items", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals("VAL3", (JsonPath.read(doc, "$.myEnumField")), "expected 2 myEnumField items");
+ assertEquals("VAL1", (JsonPath.read(doc, "$.myEnum")), "expected 0 myEnum items");
- assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
- assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
+ assertEquals(MyEnumField.VAL3, jsonObject.query("/myEnumField"), "expecting MyEnumField.VAL3");
+ assertEquals(MyEnum.VAL1, jsonObject.query("/myEnum"), "expecting MyEnum.VAL1");
}
/**
@@ -76,7 +74,7 @@ public void jsonObjectFromEnum() {
* and the JSONObject Object with names constructor.
*/
@Test
- public void jsonObjectFromEnumWithNames() {
+ void jsonObjectFromEnumWithNames() {
String [] names;
JSONObject jsonObject;
@@ -87,59 +85,59 @@ public void jsonObjectFromEnumWithNames() {
// validate JSON object
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
- assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
- assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
+ assertEquals(3, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 3 top level items");
+ assertEquals(MyEnum.VAL1, jsonObject.query("/VAL1"), "expected VAL1");
+ assertEquals(MyEnum.VAL2, jsonObject.query("/VAL2"), "expected VAL2");
+ assertEquals(MyEnum.VAL3, jsonObject.query("/VAL3"), "expected VAL3");
MyEnumField myEnumField = MyEnumField.VAL3;
names = JSONObject.getNames(myEnumField);
// The values will be MyEnmField fields
jsonObject = new JSONObject(myEnumField, names);
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
- assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
- assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
- assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
+ assertEquals(3, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 3 top level items");
+ assertEquals(MyEnumField.VAL1, jsonObject.query("/VAL1"), "expected VAL1");
+ assertEquals(MyEnumField.VAL2, jsonObject.query("/VAL2"), "expected VAL2");
+ assertEquals(MyEnumField.VAL3, jsonObject.query("/VAL3"), "expected VAL3");
}
-
+
/**
* Verify that enums are handled consistently between JSONArray and JSONObject
*/
@Test
- public void verifyEnumConsistency(){
+ void verifyEnumConsistency(){
JSONObject jo = new JSONObject();
jo.put("value", MyEnumField.VAL2);
String expected="{\"value\":\"VAL2\"}";
String actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
jo.accumulate("value", MyEnumField.VAL1);
expected="{\"value\":[\"VAL2\",\"VAL1\"]}";
actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
jo.remove("value");
jo.append("value", MyEnumField.VAL1);
expected="{\"value\":[\"VAL1\"]}";
actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
jo.put("value", EnumSet.of(MyEnumField.VAL2));
expected="{\"value\":[\"VAL2\"]}";
actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
JSONArray ja = new JSONArray();
ja.put(MyEnumField.VAL2);
jo.put("value", ja);
actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
jo.put("value", new MyEnumField[]{MyEnumField.VAL2});
actual = jo.toString();
- assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+ assertEquals(expected, actual, "Expected " + expected + " but actual was " + actual);
}
@@ -148,7 +146,7 @@ public void verifyEnumConsistency(){
* will be stored as a enum type.
*/
@Test
- public void enumPut() {
+ void enumPut() {
JSONObject jsonObject = new JSONObject();
MyEnum myEnum = MyEnum.VAL2;
jsonObject.put("myEnum", myEnum);
@@ -157,9 +155,9 @@ public void enumPut() {
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level objects", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
- assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/myEnumField")));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level objects");
+ assertEquals(MyEnum.VAL2, jsonObject.query("/myEnum"), "expected VAL2");
+ assertEquals(MyEnumField.VAL1, jsonObject.query("/myEnumField"), "expected VAL1");
JSONArray jsonArray = new JSONArray();
jsonArray.put(myEnum);
@@ -167,16 +165,16 @@ public void enumPut() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
- assertTrue("expected 2 top level objects", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
- assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonArray.query("/1")));
+ assertEquals(2, ((List>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level objects");
+ assertEquals(MyEnum.VAL2, jsonArray.query("/0"), "expected VAL2");
+ assertEquals(MyEnumField.VAL1, jsonArray.query("/1"), "expected VAL1");
/**
* Leaving these tests because they exercise get, opt, and remove
*/
- assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
- assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
- assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
+ assertEquals(MyEnum.VAL2, jsonArray.get(0), "expecting myEnum value");
+ assertEquals(MyEnumField.VAL1, jsonArray.opt(1), "expecting myEnumField value");
+ assertEquals(MyEnumField.VAL1, jsonArray.remove(1), "expecting myEnumField value");
}
/**
@@ -184,7 +182,7 @@ public void enumPut() {
* For enums, this means the assigned value will be returned as a string.
*/
@Test
- public void enumValueToString() {
+ void enumValueToString() {
String expectedStr1 = "\"VAL1\"";
String expectedStr2 = "\"VAL1\"";
MyEnum myEnum = MyEnum.VAL1;
@@ -192,11 +190,9 @@ public void enumValueToString() {
MyEnumClass myEnumClass = new MyEnumClass();
String str1 = JSONObject.valueToString(myEnum);
- assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
- str1.equals(expectedStr1));
+ assertEquals(str1, expectedStr1, "actual myEnum: " + str1 + " expected: " + expectedStr1);
String str2 = JSONObject.valueToString(myEnumField);
- assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
- str2.equals(expectedStr2));
+ assertEquals(str2, expectedStr2, "actual myEnumField: " + str2 + " expected: " + expectedStr2);
/**
* However, an enum within another class will not be rendered
@@ -206,8 +202,8 @@ public void enumValueToString() {
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL1);
String str3 = JSONObject.valueToString(myEnumClass);
- assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
- str3.startsWith(expectedStr3));
+ assertTrue(str3.startsWith(expectedStr3),
+ "actual myEnumClass: "+str3+" expected: "+expectedStr3);
}
/**
@@ -215,20 +211,20 @@ public void enumValueToString() {
* json[Object|Array].toString should serialize it in a reasonable way.
*/
@Test
- public void enumToString() {
+ void enumToString() {
MyEnum myEnum = MyEnum.VAL2;
JSONObject jsonObject = new JSONObject(myEnum);
String expectedStr = "{}";
- assertTrue("myEnum toString() should be empty", expectedStr.equals(jsonObject.toString()));
+ assertEquals(expectedStr, jsonObject.toString(), "myEnum toString() should be empty");
MyEnumField myEnumField = MyEnumField.VAL2;
jsonObject = new JSONObject(myEnumField);
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value")));
- assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals("val 2", jsonObject.query("/value"), "expected val 2");
+ assertEquals(Integer.valueOf(2), jsonObject.query("/intVal"), "expected 2");
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
@@ -237,29 +233,29 @@ public void enumToString() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
- assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals("VAL3", (JsonPath.read(doc, "$.myEnumField")), "expected VAL3");
+ assertEquals("VAL1", (JsonPath.read(doc, "$.myEnum")), "expected VAL1");
String [] names = JSONObject.getNames(myEnum);
jsonObject = new JSONObject(myEnum, names);
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
- assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
- assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
+ assertEquals(3, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 3 top level items");
+ assertEquals(MyEnum.VAL1, jsonObject.query("/VAL1"), "expected VAL1");
+ assertEquals(MyEnum.VAL2, jsonObject.query("/VAL2"), "expected VAL2");
+ assertEquals(MyEnum.VAL3, jsonObject.query("/VAL3"), "expected VAL3");
names = JSONObject.getNames(myEnumField);
jsonObject = new JSONObject(myEnumField, names);
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
- assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
- assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
- assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
+ assertEquals(3, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 3 top level items");
+ assertEquals(MyEnumField.VAL1, jsonObject.query("/VAL1"), "expected VAL1");
+ assertEquals(MyEnumField.VAL2, jsonObject.query("/VAL2"), "expected VAL2");
+ assertEquals(MyEnumField.VAL3, jsonObject.query("/VAL3"), "expected VAL3");
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
jsonObject = new JSONObject();
@@ -268,9 +264,9 @@ public void enumToString() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
- assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/myEnumField")));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals(MyEnum.VAL2, jsonObject.query("/myEnum"), "expected VAL2");
+ assertEquals(MyEnumField.VAL2, jsonObject.query("/myEnumField"), "expected VAL2");
JSONArray jsonArray = new JSONArray();
jsonArray.put(myEnum);
@@ -278,17 +274,17 @@ public void enumToString() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
- assertTrue("expected 2 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
- assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonArray.query("/1")));
+ assertEquals(2, ((List>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals(MyEnum.VAL2, jsonArray.query("/0"), "expected VAL2");
+ assertEquals(MyEnumField.VAL2, jsonArray.query("/1"), "expected VAL2");
}
/**
* Wrap should handle enums exactly as a value type like Integer, Boolean, or String.
*/
@Test
- public void wrap() {
- assertTrue("simple enum has no getters", JSONObject.wrap(MyEnum.VAL2) instanceof MyEnum);
+ void wrap() {
+ assertTrue(JSONObject.wrap(MyEnum.VAL2) instanceof MyEnum, "simple enum has no getters");
MyEnumField myEnumField = MyEnumField.VAL2;
JSONObject jsonObject = new JSONObject();
@@ -296,8 +292,8 @@ public void wrap() {
// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 1 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
- assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/enum")));
+ assertEquals(1, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 1 top level items");
+ assertEquals(MyEnumField.VAL2, jsonObject.query("/enum"), "expected VAL2");
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
@@ -306,12 +302,12 @@ public void wrap() {
// validate JSON content
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
- assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
- assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
- assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+ assertEquals(2, ((Map, ?>)(JsonPath.read(doc, "$"))).size(), "expected 2 top level items");
+ assertEquals("VAL3", (JsonPath.read(doc, "$.myEnumField")), "expected VAL3");
+ assertEquals("VAL1", (JsonPath.read(doc, "$.myEnum")), "expected VAL1");
- assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
- assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
+ assertEquals(MyEnumField.VAL3, jsonObject.query("/myEnumField"), "expecting MyEnumField.VAL3");
+ assertEquals(MyEnum.VAL1, jsonObject.query("/myEnum"), "expecting MyEnum.VAL1");
}
/**
@@ -327,7 +323,7 @@ public void wrap() {
* Exercise these enum API methods on JSONObject and JSONArray
*/
@Test
- public void enumAPI() {
+ void enumAPI() {
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
MyEnumField myEnumField = MyEnumField.VAL2;
@@ -340,37 +336,37 @@ public void enumAPI() {
// get a plain old enum
MyEnumField actualEnum = jsonObject.getEnum(MyEnumField.class, "enumKey");
- assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "get myEnumField");
// try to get the wrong value
try {
actualEnum = jsonObject.getEnum(MyEnumField.class, "strKey");
- assertTrue("should throw an exception for wrong key", false);
+ assertTrue(false, "should throw an exception for wrong key");
} catch (Exception ignored) {}
// get a class that contains an enum
MyEnumClass actualEnumClass = (MyEnumClass)jsonObject.get("enumClassKey");
- assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+ assertTrue(actualEnumClass.getMyEnum() == MyEnum.VAL1, "get enum");
// opt a plain old enum
actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey");
- assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "opt myEnumField");
// opt the wrong value
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey");
- assertTrue("opt null", actualEnum == null);
+ assertTrue(actualEnum == null, "opt null");
// opt a class that contains an enum
actualEnumClass = (MyEnumClass)jsonObject.opt("enumClassKey");
- assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+ assertTrue(actualEnumClass.getMyEnum() == MyEnum.VAL1, "get enum");
// opt with default a plain old enum
actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey", null);
- assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "opt myEnumField");
// opt with default the wrong value
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null);
- assertNull("opt null", actualEnum);
+ assertNull(actualEnum, "opt null");
// opt with default the string value
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey2", null);
@@ -378,10 +374,10 @@ public void enumAPI() {
// opt with default an index that does not exist
actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null);
- assertNull("opt null", actualEnum);
+ assertNull(actualEnum, "opt null");
- assertNull("Expected Null when the enum class is null",
- jsonObject.optEnum(null, "enumKey"));
+ assertNull(jsonObject.optEnum(null, "enumKey"),
+ "Expected Null when the enum class is null");
/**
* Exercise the proposed enum API methods on JSONArray
@@ -393,41 +389,41 @@ public void enumAPI() {
// get a plain old enum
actualEnum = jsonArray.getEnum(MyEnumField.class, 1);
- assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "get myEnumField");
// try to get the wrong value
try {
actualEnum = jsonArray.getEnum(MyEnumField.class, 0);
- assertTrue("should throw an exception for wrong index", false);
+ assertTrue(false, "should throw an exception for wrong index");
} catch (Exception ignored) {}
// get a class that contains an enum
actualEnumClass = (MyEnumClass)jsonArray.get(2);
- assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+ assertTrue(actualEnumClass.getMyEnum() == MyEnum.VAL1, "get enum");
// opt a plain old enum
actualEnum = jsonArray.optEnum(MyEnumField.class, 1);
- assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "opt myEnumField");
// opt the wrong value
actualEnum = jsonArray.optEnum(MyEnumField.class, 0);
- assertTrue("opt null", actualEnum == null);
+ assertTrue(actualEnum == null, "opt null");
// opt a class that contains an enum
actualEnumClass = (MyEnumClass)jsonArray.opt(2);
- assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+ assertTrue(actualEnumClass.getMyEnum() == MyEnum.VAL1, "get enum");
// opt with default a plain old enum
actualEnum = jsonArray.optEnum(MyEnumField.class, 1, null);
- assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+ assertTrue(actualEnum == MyEnumField.VAL2, "opt myEnumField");
// opt with default the wrong value
actualEnum = jsonArray.optEnum(MyEnumField.class, 0, null);
- assertTrue("opt null", actualEnum == null);
+ assertTrue(actualEnum == null, "opt null");
// opt with default an index that does not exist
actualEnum = jsonArray.optEnum(MyEnumField.class, 3, null);
- assertTrue("opt null", actualEnum == null);
+ assertTrue(actualEnum == null, "opt null");
}
}
diff --git a/src/test/java/org/json/junit/HTTPTest.java b/src/test/java/org/json/junit/HTTPTest.java
index 703d5ad2f..55443b399 100644
--- a/src/test/java/org/json/junit/HTTPTest.java
+++ b/src/test/java/org/json/junit/HTTPTest.java
@@ -4,25 +4,27 @@
Public Domain.
*/
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.*;
import org.json.*;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Unit tests for JSON-Java HTTP.java. See RFC7230.
*/
-public class HTTPTest {
+class HTTPTest {
/**
* Attempt to call HTTP.toJSONObject() with a null string
* Expects a NUllPointerException.
*/
- @Test(expected=NullPointerException.class)
- public void nullHTTPException() {
- String httpStr = null;
- HTTP.toJSONObject(httpStr);
+ @Test
+ void nullHTTPException() {
+ assertThrows(NullPointerException.class, () -> {
+ String httpStr = null;
+ HTTP.toJSONObject(httpStr);
+ });
}
/**
@@ -30,15 +32,14 @@ public void nullHTTPException() {
* an empty object. Expects a JSONException.
*/
@Test
- public void notEnoughHTTPException() {
+ void notEnoughHTTPException() {
String httpStr = "{}";
JSONObject jsonObject = new JSONObject(httpStr);
try {
HTTP.toString(jsonObject);
- assertTrue("Expected to throw exception", false);
+ assertTrue(false, "Expected to throw exception");
} catch (JSONException e) {
- assertTrue("Expecting an exception message",
- "Not enough material for an HTTP header.".equals(e.getMessage()));
+ assertEquals("Not enough material for an HTTP header.", e.getMessage(), "Expecting an exception message");
}
}
@@ -48,7 +49,7 @@ public void notEnoughHTTPException() {
* and HTTP-Version.
*/
@Test
- public void emptyStringHTTPRequest() {
+ void emptyStringHTTPRequest() {
String httpStr = "";
String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
@@ -61,7 +62,7 @@ public void emptyStringHTTPRequest() {
* and HTTP-Version.
*/
@Test
- public void simpleHTTPRequest() {
+ void simpleHTTPRequest() {
String httpStr = "GET /hello.txt HTTP/1.1";
String expectedHTTPStr =
"{\"Request-URI\":\"/hello.txt\",\"Method\":\"GET\",\"HTTP-Version\":\"HTTP/1.1\"}";
@@ -75,7 +76,7 @@ public void simpleHTTPRequest() {
* HTTP-Version, Status-Code, and Reason.
*/
@Test
- public void simpleHTTPResponse() {
+ void simpleHTTPResponse() {
String httpStr = "HTTP/1.1 200 OK";
String expectedHTTPStr =
"{\"HTTP-Version\":\"HTTP/1.1\",\"Status-Code\":\"200\",\"Reason-Phrase\":\"OK\"}";
@@ -89,7 +90,7 @@ public void simpleHTTPResponse() {
* request headers.
*/
@Test
- public void extendedHTTPRequest() {
+ void extendedHTTPRequest() {
String httpStr =
"POST /enlighten/calais.asmx HTTP/1.1\n"+
"Host: api.opencalais.com\n"+
@@ -119,7 +120,7 @@ public void extendedHTTPRequest() {
* response headers.
*/
@Test
- public void extendedHTTPResponse() {
+ void extendedHTTPResponse() {
String httpStr =
"HTTP/1.1 200 OK\n"+
"Content-Type: text/xml; charset=utf-8\n"+
@@ -140,7 +141,7 @@ public void extendedHTTPResponse() {
* response headers, then convert it back into an HTTP string.
*/
@Test
- public void convertHTTPRequestToString() {
+ void convertHTTPRequestToString() {
String httpStr =
"POST /enlighten/calais.asmx HTTP/1.1\n"+
"Host: api.opencalais.com\n"+
@@ -173,7 +174,7 @@ public void convertHTTPRequestToString() {
* response headers, then convert it back into an HTTP string.
*/
@Test
- public void convertHTTPResponseToString() {
+ void convertHTTPResponseToString() {
String httpStr =
"HTTP/1.1 200 OK\n"+
"Content-Type: text/xml; charset=utf-8\n"+
diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java
index fd0137978..68f7460ec 100644
--- a/src/test/java/org/json/junit/JSONArrayTest.java
+++ b/src/test/java/org/json/junit/JSONArrayTest.java
@@ -4,12 +4,7 @@
Public Domain.
*/
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.io.InputStream;
@@ -34,8 +29,8 @@
import org.json.JSONTokener;
import org.json.ParserConfiguration;
import org.json.junit.data.MyJsonString;
-import org.junit.Ignore;
-import org.junit.Test;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
@@ -73,7 +68,7 @@ public class JSONArrayTest {
* Tests that the similar method is working as expected.
*/
@Test
- public void verifySimilar() {
+ void verifySimilar() {
final String string1 = "HasSameRef";
final String string2 = "HasDifferentRef";
JSONArray obj1 = new JSONArray()
@@ -101,19 +96,21 @@ public void verifySimilar() {
.put(2.0)
.put(new String(string2));
- assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
- assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
- assertFalse("obj4-obj5 Should eval to false", obj4.similar(obj5));
+ assertFalse(obj1.similar(obj2), "obj1-obj2 Should eval to false");
+ assertTrue(obj1.similar(obj3), "obj1-obj3 Should eval to true");
+ assertFalse(obj4.similar(obj5), "obj4-obj5 Should eval to false");
}
-
+
/**
* Attempt to create a JSONArray with a null string.
* Expects a NullPointerException.
*/
- @Test(expected=NullPointerException.class)
- public void nullException() {
- String str = null;
- assertNull("Should throw an exception", new JSONArray(str));
+ @Test
+ void nullException() {
+ assertThrows(NullPointerException.class, () -> {
+ String str = null;
+ assertNull(new JSONArray(str), "Should throw an exception");
+ });
}
/**
@@ -121,59 +118,59 @@ public void nullException() {
* Expects a JSONException.
*/
@Test
- public void emptyStr() {
+ void emptyStr() {
String str = "";
try {
- assertNull("Should throw an exception", new JSONArray(str));
+ assertNull(new JSONArray(str), "Should throw an exception");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "A JSONArray text must start with '[' at 0 [character 1 line 1]",
- e.getMessage());
+ assertEquals("A JSONArray text must start with '[' at 0 [character 1 line 1]",
+ e.getMessage(),
+ "Expected an exception message");
}
}
-
+
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
- public void unclosedArray() {
+ void unclosedArray() {
try {
- assertNull("Should throw an exception", new JSONArray("["));
+ assertNull(new JSONArray("["), "Should throw an exception");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "Expected a ',' or ']' at 1 [character 2 line 1]",
- e.getMessage());
+ assertEquals("Expected a ',' or ']' at 1 [character 2 line 1]",
+ e.getMessage(),
+ "Expected an exception message");
}
}
-
+
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
- public void unclosedArray2() {
+ void unclosedArray2() {
try {
- assertNull("Should throw an exception", new JSONArray("[\"test\""));
+ assertNull(new JSONArray("[\"test\""), "Should throw an exception");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "Expected a ',' or ']' at 7 [character 8 line 1]",
- e.getMessage());
+ assertEquals("Expected a ',' or ']' at 7 [character 8 line 1]",
+ e.getMessage(),
+ "Expected an exception message");
}
}
-
+
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
- public void unclosedArray3() {
+ void unclosedArray3() {
try {
- assertNull("Should throw an exception", new JSONArray("[\"test\","));
+ assertNull(new JSONArray("[\"test\","), "Should throw an exception");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "Expected a ',' or ']' at 8 [character 9 line 1]",
- e.getMessage());
+ assertEquals("Expected a ',' or ']' at 8 [character 9 line 1]",
+ e.getMessage(),
+ "Expected an exception message");
}
}
@@ -183,22 +180,20 @@ public void unclosedArray3() {
* Expects a JSONException.
*/
@Test
- public void badObject() {
+ void badObject() {
String str = "abc";
try {
- assertNull("Should throw an exception", new JSONArray((Object)str));
+ assertNull(new JSONArray((Object)str), "Should throw an exception");
} catch (JSONException e) {
- assertTrue("Expected an exception message",
- "JSONArray initial value should be a string or collection or array.".
- equals(e.getMessage()));
+ assertEquals("JSONArray initial value should be a string or collection or array.", e.getMessage(), "Expected an exception message");
}
}
-
+
/**
* Verifies that the constructor has backwards compatibility with RAW types pre-java5.
*/
@Test
- public void verifyConstructor() {
+ void verifyConstructor() {
final JSONArray expected = new JSONArray("[10]");
@@ -214,14 +209,14 @@ public void verifyConstructor() {
JSONArray jaObj = new JSONArray(myCObj);
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaRaw));
+ expected.similar(jaRaw),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaInt));
+ expected.similar(jaInt),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaObj));
+ expected.similar(jaObj),
+ "The RAW Collection should give me the same as the Typed Collection");
Util.checkJSONArrayMaps(expected);
Util.checkJSONArrayMaps(jaObj);
Util.checkJSONArrayMaps(jaRaw);
@@ -232,21 +227,21 @@ public void verifyConstructor() {
* Tests consecutive calls to putAll with array and collection.
*/
@Test
- public void verifyPutAll() {
+ void verifyPutAll() {
final JSONArray jsonArray = new JSONArray();
// array
int[] myInts = { 1, 2, 3, 4, 5 };
jsonArray.putAll(myInts);
- assertEquals("int arrays lengths should be equal",
- jsonArray.length(),
- myInts.length);
+ assertEquals(jsonArray.length(),
+ myInts.length,
+ "int arrays lengths should be equal");
for (int i = 0; i < myInts.length; i++) {
- assertEquals("int arrays elements should be equal",
- myInts[i],
- jsonArray.getInt(i));
+ assertEquals(myInts[i],
+ jsonArray.getInt(i),
+ "int arrays elements should be equal");
}
// collection
@@ -255,14 +250,14 @@ public void verifyPutAll() {
int len = myInts.length + myList.size();
- assertEquals("arrays lengths should be equal",
- jsonArray.length(),
- len);
+ assertEquals(jsonArray.length(),
+ len,
+ "arrays lengths should be equal");
for (int i = 0; i < myList.size(); i++) {
- assertEquals("collection elements should be equal",
- myList.get(i),
- jsonArray.getString(myInts.length + i));
+ assertEquals(myList.get(i),
+ jsonArray.getString(myInts.length + i),
+ "collection elements should be equal");
}
Util.checkJSONArrayMaps(jsonArray);
}
@@ -271,7 +266,7 @@ public void verifyPutAll() {
* Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
*/
@Test
- public void verifyPutCollection() {
+ void verifyPutCollection() {
final JSONArray expected = new JSONArray("[[10]]");
@@ -290,25 +285,25 @@ public void verifyPutCollection() {
jaInt.put(myCInt);
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaRaw));
+ expected.similar(jaRaw),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaObj));
+ expected.similar(jaObj),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaInt));
+ expected.similar(jaInt),
+ "The RAW Collection should give me the same as the Typed Collection");
Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
jaRaw, jaObj, jaInt
)));
}
-
+
/**
* Verifies that the put Map has backwards compatibility with RAW types pre-java5.
*/
@Test
- public void verifyPutMap() {
+ void verifyPutMap() {
final JSONArray expected = new JSONArray("[{\"myKey\":10}]");
@@ -333,17 +328,17 @@ public void verifyPutMap() {
jaObjObj.put(myCObjObj);
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaRaw));
+ expected.similar(jaRaw),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaStrObj));
+ expected.similar(jaStrObj),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaStrInt));
+ expected.similar(jaStrInt),
+ "The RAW Collection should give me the same as the Typed Collection");
assertTrue(
- "The RAW Collection should give me the same as the Typed Collection",
- expected.similar(jaObjObj));
+ expected.similar(jaObjObj),
+ "The RAW Collection should give me the same as the Typed Collection");
Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
expected, jaRaw, jaStrObj, jaStrInt, jaObjObj
)));
@@ -355,44 +350,32 @@ public void verifyPutMap() {
*/
@SuppressWarnings("boxing")
@Test
- public void getArrayValues() {
+ void getArrayValues() {
JSONArray jsonArray = new JSONArray(this.arrayStr);
// booleans
- assertTrue("Array true",
- true == jsonArray.getBoolean(0));
- assertTrue("Array false",
- false == jsonArray.getBoolean(1));
- assertTrue("Array string true",
- true == jsonArray.getBoolean(2));
- assertTrue("Array string false",
- false == jsonArray.getBoolean(3));
+ assertTrue(jsonArray.getBoolean(0), "Array true");
+ assertFalse(jsonArray.getBoolean(1), "Array false");
+ assertTrue(jsonArray.getBoolean(2), "Array string true");
+ assertFalse(jsonArray.getBoolean(3), "Array string false");
// strings
- assertTrue("Array value string",
- "hello".equals(jsonArray.getString(4)));
+ assertEquals("hello", jsonArray.getString(4), "Array value string");
// doubles
- assertTrue("Array double",
- Double.valueOf(23.45e-4).equals(jsonArray.getDouble(5)));
- assertTrue("Array string double",
- Double.valueOf(23.45).equals(jsonArray.getDouble(6)));
- assertTrue("Array double can be float",
- Float.valueOf(23.45e-4f).equals(jsonArray.getFloat(5)));
+ assertEquals(Double.valueOf(23.45e-4), jsonArray.getDouble(5), "Array double");
+ assertEquals(Double.valueOf(23.45), jsonArray.getDouble(6), "Array string double");
+ assertEquals(Float.valueOf(23.45e-4f), jsonArray.getFloat(5), "Array double can be float");
// ints
- assertTrue("Array value int",
- Integer.valueOf(42).equals(jsonArray.getInt(7)));
- assertTrue("Array value string int",
- Integer.valueOf(43).equals(jsonArray.getInt(8)));
+ assertEquals(Integer.valueOf(42), jsonArray.getInt(7), "Array value int");
+ assertEquals(Integer.valueOf(43), jsonArray.getInt(8), "Array value string int");
// nested objects
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
- assertTrue("Array value JSONArray", nestedJsonArray != null);
+ assertTrue(nestedJsonArray != null, "Array value JSONArray");
JSONObject nestedJsonObject = jsonArray.getJSONObject(10);
- assertTrue("Array value JSONObject", nestedJsonObject != null);
+ assertTrue(nestedJsonObject != null, "Array value JSONObject");
// longs
- assertTrue("Array value long",
- Long.valueOf(0).equals(jsonArray.getLong(11)));
- assertTrue("Array value string long",
- Long.valueOf(-1).equals(jsonArray.getLong(12)));
+ assertEquals(Long.valueOf(0), jsonArray.getLong(11), "Array value long");
+ assertEquals(Long.valueOf(-1), jsonArray.getLong(12), "Array value string long");
- assertTrue("Array value null", jsonArray.isNull(-1));
+ assertTrue(jsonArray.isNull(-1), "Array value null");
Util.checkJSONArrayMaps(jsonArray);
}
@@ -402,63 +385,55 @@ public void getArrayValues() {
* API methods result in JSONExceptions
*/
@Test
- public void failedGetArrayValues() {
+ void failedGetArrayValues() {
JSONArray jsonArray = new JSONArray(this.arrayStr);
try {
jsonArray.getBoolean(4);
- assertTrue("expected getBoolean to fail", false);
+ assertTrue(false, "expected getBoolean to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a boolean (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a boolean (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.get(-1);
- assertTrue("expected get to fail", false);
+ assertTrue(false, "expected get to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[-1] not found.",e.getMessage());
+ assertEquals("JSONArray[-1] not found.",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getDouble(4);
- assertTrue("expected getDouble to fail", false);
+ assertTrue(false, "expected getDouble to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a double (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a double (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getInt(4);
- assertTrue("expected getInt to fail", false);
+ assertTrue(false, "expected getInt to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a int (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a int (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getJSONArray(4);
- assertTrue("expected getJSONArray to fail", false);
+ assertTrue(false, "expected getJSONArray to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a JSONArray (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a JSONArray (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getJSONObject(4);
- assertTrue("expected getJSONObject to fail", false);
+ assertTrue(false, "expected getJSONObject to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a JSONObject (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a JSONObject (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getLong(4);
- assertTrue("expected getLong to fail", false);
+ assertTrue(false, "expected getLong to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[4] is not a long (class java.lang.String : hello).",e.getMessage());
+ assertEquals("JSONArray[4] is not a long (class java.lang.String : hello).",e.getMessage(),"Expected an exception message");
}
try {
jsonArray.getString(5);
- assertTrue("expected getString to fail", false);
+ assertTrue(false, "expected getString to fail");
} catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[5] is not a String (class java.math.BigDecimal : 0.002345).",e.getMessage());
+ assertEquals("JSONArray[5] is not a String (class java.math.BigDecimal : 0.002345).",e.getMessage(),"Expected an exception message");
}
Util.checkJSONArrayMaps(jsonArray);
}
@@ -470,7 +445,7 @@ public void failedGetArrayValues() {
* conforming JSON text.
*/
@Test
- public void unquotedText() {
+ void unquotedText() {
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
JSONArray jsonArray = new JSONArray(str);
List