Skip to content

Commit cfe6851

Browse files
author
John J. Aylward
committed
Adds testing for -0 with optNumber
1 parent 1967bee commit cfe6851

File tree

1 file changed

+74
-36
lines changed

1 file changed

+74
-36
lines changed

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertNotEquals;
66
import static org.junit.Assert.assertNull;
77
import static org.junit.Assert.assertTrue;
8+
import static org.junit.Assert.fail;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.when;
1011

@@ -514,7 +515,7 @@ public void jsonObjectAccumulate() {
514515
// include an unsupported object for coverage
515516
try {
516517
jsonObject.accumulate("myArray", Double.NaN);
517-
assertTrue("Expected exception", false);
518+
fail("Expected exception");
518519
} catch (JSONException ignored) {}
519520

520521
// validate JSON
@@ -545,7 +546,7 @@ public void jsonObjectAppend() {
545546
// include an unsupported object for coverage
546547
try {
547548
jsonObject.append("myArray", Double.NaN);
548-
assertTrue("Expected exception", false);
549+
fail("Expected exception");
549550
} catch (JSONException ignored) {}
550551

551552
// validate JSON
@@ -595,6 +596,9 @@ public void jsonObjectValues() {
595596
"\"longStrKey\":\"987654321098765432\","+
596597
"\"doubleKey\":-23.45e7,"+
597598
"\"doubleStrKey\":\"00001.000\","+
599+
"\"BigDecimalStrKey\":\"19007199254740993.35481234487103587486413587843213584\","+
600+
"\"negZeroKey\":-0.0,"+
601+
"\"negZeroStrKey\":\"-0.0\","+
598602
"\"arrayKey\":[0,1,2],"+
599603
"\"objectKey\":{\"myKey\":\"myVal\"}"+
600604
"}";
@@ -611,10 +615,26 @@ public void jsonObjectValues() {
611615
jsonObject.getDouble("doubleKey") == -23.45e7);
612616
assertTrue("doubleStrKey should be double",
613617
jsonObject.getDouble("doubleStrKey") == 1);
618+
assertTrue("doubleKey can be float",
619+
jsonObject.getFloat("doubleKey") == -23.45e7f);
620+
assertTrue("doubleStrKey can be float",
621+
jsonObject.getFloat("doubleStrKey") == 1f);
614622
assertTrue("opt doubleKey should be double",
615623
jsonObject.optDouble("doubleKey") == -23.45e7);
616624
assertTrue("opt doubleKey with Default should be double",
617625
jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
626+
assertTrue("opt negZeroKey should be double",
627+
Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);
628+
assertTrue("opt negZeroStrKey with Default should be double",
629+
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
630+
assertTrue("optNumber negZeroKey should return Double",
631+
jsonObject.optNumber("negZeroKey") instanceof Double);
632+
assertTrue("optNumber negZeroStrKey should return Double",
633+
jsonObject.optNumber("negZeroStrKey") instanceof Double);
634+
assertTrue("optNumber negZeroKey should be -0.0",
635+
Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
636+
assertTrue("optNumber negZeroStrKey should be -0.0",
637+
Double.compare(jsonObject.optNumber("negZeroStrKey").doubleValue(), -0.0d) == 0);
618638
assertTrue("optFloat doubleKey should be float",
619639
jsonObject.optFloat("doubleKey") == -23.45e7f);
620640
assertTrue("optFloat doubleKey with Default should be float",
@@ -641,12 +661,14 @@ public void jsonObjectValues() {
641661
jsonObject.optNumber("longKey") instanceof Long);
642662
assertTrue("optNumber double should return Double",
643663
jsonObject.optNumber("doubleKey") instanceof Double);
644-
assertTrue("optNumber Str int should return BigDecimal",
645-
jsonObject.optNumber("intStrKey") instanceof BigDecimal);
646-
assertTrue("optNumber Str long should return BigDecimal",
647-
jsonObject.optNumber("longStrKey") instanceof BigDecimal);
648-
assertTrue("optNumber Str double should return BigDecimal",
649-
jsonObject.optNumber("doubleStrKey") instanceof BigDecimal);
664+
assertTrue("optNumber Str int should return Integer",
665+
jsonObject.optNumber("intStrKey") instanceof Integer);
666+
assertTrue("optNumber Str long should return Long",
667+
jsonObject.optNumber("longStrKey") instanceof Long);
668+
assertTrue("optNumber Str double should return Double",
669+
jsonObject.optNumber("doubleStrKey") instanceof Double);
670+
assertTrue("optNumber BigDecimalStrKey should return BigDecimal",
671+
jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
650672
assertTrue("xKey should not exist",
651673
jsonObject.isNull("xKey"));
652674
assertTrue("stringKey should exist",
@@ -804,110 +826,126 @@ public void jsonObjectNonAndWrongValues() {
804826
JSONObject jsonObject = new JSONObject(str);
805827
try {
806828
jsonObject.getBoolean("nonKey");
807-
assertTrue("Expected an exception", false);
829+
fail("Expected an exception");
808830
} catch (JSONException e) {
809831
assertTrue("expecting an exception message",
810832
"JSONObject[\"nonKey\"] not found.".equals(e.getMessage()));
811833
}
812834
try {
813835
jsonObject.getBoolean("stringKey");
814-
assertTrue("Expected an exception", false);
836+
fail("Expected an exception");
815837
} catch (JSONException e) {
816838
assertTrue("Expecting an exception message",
817839
"JSONObject[\"stringKey\"] is not a Boolean.".
818840
equals(e.getMessage()));
819841
}
820842
try {
821843
jsonObject.getString("nonKey");
822-
assertTrue("Expected an exception", false);
844+
fail("Expected an exception");
823845
} catch (JSONException e) {
824846
assertTrue("Expecting an exception message",
825847
"JSONObject[\"nonKey\"] not found.".
826848
equals(e.getMessage()));
827849
}
828850
try {
829851
jsonObject.getString("trueKey");
830-
assertTrue("Expected an exception", false);
852+
fail("Expected an exception");
831853
} catch (JSONException e) {
832854
assertTrue("Expecting an exception message",
833855
"JSONObject[\"trueKey\"] not a string.".
834856
equals(e.getMessage()));
835857
}
836858
try {
837859
jsonObject.getDouble("nonKey");
838-
assertTrue("Expected an exception", false);
860+
fail("Expected an exception");
839861
} catch (JSONException e) {
840862
assertTrue("Expecting an exception message",
841863
"JSONObject[\"nonKey\"] not found.".
842864
equals(e.getMessage()));
843865
}
844866
try {
845867
jsonObject.getDouble("stringKey");
846-
assertTrue("Expected an exception", false);
868+
fail("Expected an exception");
869+
} catch (JSONException e) {
870+
assertTrue("Expecting an exception message",
871+
"JSONObject[\"stringKey\"] is not a number.".
872+
equals(e.getMessage()));
873+
}
874+
try {
875+
jsonObject.getFloat("nonKey");
876+
fail("Expected an exception");
877+
} catch (JSONException e) {
878+
assertTrue("Expecting an exception message",
879+
"JSONObject[\"nonKey\"] not found.".
880+
equals(e.getMessage()));
881+
}
882+
try {
883+
jsonObject.getFloat("stringKey");
884+
fail("Expected an exception");
847885
} catch (JSONException e) {
848886
assertTrue("Expecting an exception message",
849887
"JSONObject[\"stringKey\"] is not a number.".
850888
equals(e.getMessage()));
851889
}
852890
try {
853891
jsonObject.getInt("nonKey");
854-
assertTrue("Expected an exception", false);
892+
fail("Expected an exception");
855893
} catch (JSONException e) {
856894
assertTrue("Expecting an exception message",
857895
"JSONObject[\"nonKey\"] not found.".
858896
equals(e.getMessage()));
859897
}
860898
try {
861899
jsonObject.getInt("stringKey");
862-
assertTrue("Expected an exception", false);
900+
fail("Expected an exception");
863901
} catch (JSONException e) {
864902
assertTrue("Expecting an exception message",
865903
"JSONObject[\"stringKey\"] is not an int.".
866904
equals(e.getMessage()));
867905
}
868906
try {
869907
jsonObject.getLong("nonKey");
870-
assertTrue("Expected an exception", false);
908+
fail("Expected an exception");
871909
} catch (JSONException e) {
872910
assertTrue("Expecting an exception message",
873911
"JSONObject[\"nonKey\"] not found.".
874912
equals(e.getMessage()));
875913
}
876914
try {
877915
jsonObject.getLong("stringKey");
878-
assertTrue("Expected an exception", false);
916+
fail("Expected an exception");
879917
} catch (JSONException e) {
880918
assertTrue("Expecting an exception message",
881919
"JSONObject[\"stringKey\"] is not a long.".
882920
equals(e.getMessage()));
883921
}
884922
try {
885923
jsonObject.getJSONArray("nonKey");
886-
assertTrue("Expected an exception", false);
924+
fail("Expected an exception");
887925
} catch (JSONException e) {
888926
assertTrue("Expecting an exception message",
889927
"JSONObject[\"nonKey\"] not found.".
890928
equals(e.getMessage()));
891929
}
892930
try {
893931
jsonObject.getJSONArray("stringKey");
894-
assertTrue("Expected an exception", false);
932+
fail("Expected an exception");
895933
} catch (JSONException e) {
896934
assertTrue("Expecting an exception message",
897935
"JSONObject[\"stringKey\"] is not a JSONArray.".
898936
equals(e.getMessage()));
899937
}
900938
try {
901939
jsonObject.getJSONObject("nonKey");
902-
assertTrue("Expected an exception", false);
940+
fail("Expected an exception");
903941
} catch (JSONException e) {
904942
assertTrue("Expecting an exception message",
905943
"JSONObject[\"nonKey\"] not found.".
906944
equals(e.getMessage()));
907945
}
908946
try {
909947
jsonObject.getJSONObject("stringKey");
910-
assertTrue("Expected an exception", false);
948+
fail("Expected an exception");
911949
} catch (JSONException e) {
912950
assertTrue("Expecting an exception message",
913951
"JSONObject[\"stringKey\"] is not a JSONObject.".
@@ -1004,18 +1042,18 @@ public void bigNumberOperations() {
10041042
*/
10051043
try {
10061044
jsonObject.getBigDecimal("bigInt");
1007-
assertTrue("expected an exeption", false);
1045+
fail("expected an exeption");
10081046
} catch (JSONException ignored) {}
10091047
obj = jsonObject.optBigDecimal("bigInt", BigDecimal.ONE);
10101048
assertTrue("expected BigDecimal", obj.equals(BigDecimal.ONE));
10111049
try {
10121050
jsonObject.getBigInteger("bigDec");
1013-
assertTrue("expected an exeption", false);
1051+
fail("expected an exeption");
10141052
} catch (JSONException ignored) {}
10151053
jsonObject.put("stringKey", "abc");
10161054
try {
10171055
jsonObject.getBigDecimal("stringKey");
1018-
assertTrue("expected an exeption", false);
1056+
fail("expected an exeption");
10191057
} catch (JSONException ignored) {}
10201058
obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE);
10211059
assertTrue("expected BigInteger", obj instanceof BigInteger);
@@ -1092,11 +1130,11 @@ public void bigNumberOperations() {
10921130
jsonArray.put(Boolean.TRUE);
10931131
try {
10941132
jsonArray.getBigInteger(2);
1095-
assertTrue("should not be able to get big int", false);
1133+
fail("should not be able to get big int");
10961134
} catch (Exception ignored) {}
10971135
try {
10981136
jsonArray.getBigDecimal(2);
1099-
assertTrue("should not be able to get big dec", false);
1137+
fail("should not be able to get big dec");
11001138
} catch (Exception ignored) {}
11011139
assertTrue("optBigInt is default", jsonArray.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE));
11021140
assertTrue("optBigDec is default", jsonArray.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE));
@@ -1851,7 +1889,7 @@ public void jsonObjectParsingErrors() {
18511889
String str = "{\"myKey\":true, \"myOtherKey\":false}";
18521890
JSONObject jsonObject = new JSONObject(str);
18531891
jsonObject.append("myKey", "hello");
1854-
assertTrue("Expected an exception", false);
1892+
fail("Expected an exception");
18551893
} catch (JSONException e) {
18561894
assertTrue("Expecting an exception message",
18571895
"JSONObject[myKey] is not a JSONArray.".
@@ -1862,7 +1900,7 @@ public void jsonObjectParsingErrors() {
18621900
String str = "{\"myKey\":true, \"myOtherKey\":false}";
18631901
JSONObject jsonObject = new JSONObject(str);
18641902
jsonObject.increment("myKey");
1865-
assertTrue("Expected an exception", false);
1903+
fail("Expected an exception");
18661904
} catch (JSONException e) {
18671905
assertTrue("Expecting an exception message",
18681906
"Unable to increment [\"myKey\"].".
@@ -1873,7 +1911,7 @@ public void jsonObjectParsingErrors() {
18731911
String str = "{\"myKey\":true, \"myOtherKey\":false}";
18741912
JSONObject jsonObject = new JSONObject(str);
18751913
jsonObject.get(null);
1876-
assertTrue("Expected an exception", false);
1914+
fail("Expected an exception");
18771915
} catch (JSONException e) {
18781916
assertTrue("Expecting an exception message",
18791917
"Null key.".
@@ -1882,7 +1920,7 @@ public void jsonObjectParsingErrors() {
18821920
try {
18831921
// invalid numberToString()
18841922
JSONObject.numberToString((Number)null);
1885-
assertTrue("Expected an exception", false);
1923+
fail("Expected an exception");
18861924
} catch (JSONException e) {
18871925
assertTrue("Expecting an exception message",
18881926
"Null pointer".
@@ -1892,29 +1930,29 @@ public void jsonObjectParsingErrors() {
18921930
// null put key
18931931
JSONObject jsonObject = new JSONObject("{}");
18941932
jsonObject.put(null, 0);
1895-
assertTrue("Expected an exception", false);
1933+
fail("Expected an exception");
18961934
} catch (NullPointerException ignored) {
18971935
}
18981936
try {
18991937
// multiple putOnce key
19001938
JSONObject jsonObject = new JSONObject("{}");
19011939
jsonObject.putOnce("hello", "world");
19021940
jsonObject.putOnce("hello", "world!");
1903-
assertTrue("Expected an exception", false);
1941+
fail("Expected an exception");
19041942
} catch (JSONException e) {
19051943
assertTrue("", true);
19061944
}
19071945
try {
19081946
// test validity of invalid double
19091947
JSONObject.testValidity(Double.NaN);
1910-
assertTrue("Expected an exception", false);
1948+
fail("Expected an exception");
19111949
} catch (JSONException e) {
19121950
assertTrue("", true);
19131951
}
19141952
try {
19151953
// test validity of invalid float
19161954
JSONObject.testValidity(Float.NEGATIVE_INFINITY);
1917-
assertTrue("Expected an exception", false);
1955+
fail("Expected an exception");
19181956
} catch (JSONException e) {
19191957
assertTrue("", true);
19201958
}
@@ -2294,7 +2332,7 @@ public void jsonObjectNullOperations() {
22942332
// assertTrue("should convert null to empty string", "".equals(string));
22952333
try {
22962334
value = jsonObjectNull.get("key");
2297-
assertTrue("get() null should throw exception", false);
2335+
fail("get() null should throw exception");
22982336
} catch (Exception ignored) {}
22992337

23002338
/**

0 commit comments

Comments
 (0)