Skip to content

Commit 56aa2f8

Browse files
committed
ongoing unit test improvement
1 parent 9cf5328 commit 56aa2f8

1 file changed

Lines changed: 45 additions & 48 deletions

File tree

JSONObjectTest.java

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import static org.junit.Assert.*;
44
import static org.mockito.Mockito.*;
5+
56
import java.io.*;
67
import java.math.BigDecimal;
78
import java.math.BigInteger;
89
import java.util.*;
10+
911
import org.json.*;
1012
import org.junit.*;
1113

@@ -416,10 +418,7 @@ public void jsonObjectValues() {
416418
jsonObjectInner.get("myKey").equals("myVal"));
417419
}
418420

419-
// improving unit tests left off here
420-
421421
@Test
422-
423422
public void stringToValueNumbersTest() {
424423
// Check if library handles large or high precision numbers correctly
425424
assertTrue( "0.2 should be a Double!",
@@ -446,12 +445,12 @@ public void stringToValueNumbersTest() {
446445
JSONObject.stringToValue(str).equals("9223372036854775808"));
447446
}
448447

449-
@Test
450448
/**
451449
* This test documents numeric values which could be numerically
452450
* handled as BigDecimal or BigInteger. It helps determine what outputs
453451
* will change if those types are supported.
454452
*/
453+
@Test
455454
public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() {
456455
// Valid JSON Numbers, probably should return BigDecimal or BigInteger objects
457456
String str =
@@ -605,8 +604,16 @@ public void jsonObjectNonAndWrongValues() {
605604
exceptionCount == tryCount);
606605
}
607606

607+
/**
608+
* The purpose for the static method getNames() methods are not clear.
609+
* This method is not called from within JSON-Java. Most likely
610+
* uses are to prep names arrays for:
611+
* JSONObject(JSONObject jo, String[] names)
612+
* JSONObject(Object object, String names[]),
613+
*/
608614
@Test
609615
public void jsonObjectNames() {
616+
JSONObject jsonObject;
610617

611618
// getNames() from null JSONObject
612619
assertTrue("null names from null Object",
@@ -616,11 +623,17 @@ public void jsonObjectNames() {
616623
assertTrue("null names from Object with no fields",
617624
null == JSONObject.getNames(new MyJsonString()));
618625

626+
// getNames from new JSONOjbect
627+
jsonObject = new JSONObject();
628+
String [] names = JSONObject.getNames(jsonObject);
629+
assertTrue("names should be null", names == null);
630+
631+
619632
// getNames() from empty JSONObject
620633
String emptyStr = "{}";
621-
JSONObject emptyJsonObject = new JSONObject(emptyStr);
634+
jsonObject = new JSONObject(emptyStr);
622635
assertTrue("empty JSONObject should have null names",
623-
null == JSONObject.getNames(emptyJsonObject));
636+
null == JSONObject.getNames(jsonObject));
624637

625638
// getNames() from JSONObject
626639
String str =
@@ -630,9 +643,28 @@ public void jsonObjectNames() {
630643
"\"stringKey\":\"hello world!\","+
631644
"}";
632645
String [] expectedNames = {"trueKey", "falseKey", "stringKey"};
633-
JSONObject jsonObject = new JSONObject(str);
634-
String [] names = JSONObject.getNames(jsonObject);
646+
jsonObject = new JSONObject(str);
647+
names = JSONObject.getNames(jsonObject);
635648
Util.compareActualVsExpectedStringArrays(names, expectedNames);
649+
650+
/**
651+
* getNames() from an enum with properties has an interesting result.
652+
* It returns the enum values, not the selected enum properties
653+
*/
654+
MyEnumField myEnumField = MyEnumField.VAL1;
655+
String[] enumExpectedNames = {"VAL1", "VAL2", "VAL3"};
656+
names = JSONObject.getNames(myEnumField);
657+
Util.compareActualVsExpectedStringArrays(names, enumExpectedNames);
658+
659+
/**
660+
* A bean is also an object. But in order to test the static
661+
* method getNames(), this particular bean needs some public
662+
* data members, which have been added to the class.
663+
*/
664+
JSONObjectTest jsonObjectTest = new JSONObjectTest();
665+
String [] jsonObjectTestExpectedNames = {"publicString", "publicInt"};
666+
names = JSONObject.getNames(jsonObjectTest);
667+
Util.compareActualVsExpectedStringArrays(names, jsonObjectTestExpectedNames);
636668
}
637669

638670
@Test
@@ -668,19 +700,6 @@ public void jsonObjectNamesToJsonAray() {
668700
Util.compareActualVsExpectedStringArrays(names, expectedNames);
669701
}
670702

671-
@Test
672-
public void objectNames() {
673-
/**
674-
* A bean is also an object. But in order to test the static
675-
* method getNames(), this particular bean needs some public
676-
* data members, which have been added to the class.
677-
*/
678-
JSONObjectTest jsonObjectTest = new JSONObjectTest();
679-
String [] expectedNames = {"publicString", "publicInt"};
680-
String [] names = JSONObject.getNames(jsonObjectTest);
681-
Util.compareActualVsExpectedStringArrays(names, expectedNames);
682-
}
683-
684703
@Test
685704
public void jsonObjectIncrement() {
686705
String str =
@@ -693,8 +712,6 @@ public void jsonObjectIncrement() {
693712
"\"keyInt\":3,"+
694713
"\"keyLong\":9999999993,"+
695714
"\"keyDouble\":3.1,"+
696-
// TODO: not sure if this will work on other platforms
697-
698715
// Should work the same way on any platform! @see https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3
699716
// This is the effect of a float to double conversion and is inherent to the shortcomings of the IEEE 754 format, when
700717
// converting 32-bit into double-precision 64-bit.
@@ -773,26 +790,6 @@ public void jsonObjectIncrement() {
773790

774791
}
775792

776-
@Test
777-
public void emptyJsonObjectNamesToArray() {
778-
JSONObject jsonObject = new JSONObject();
779-
String [] names = JSONObject.getNames(jsonObject);
780-
assertTrue("names should be null", names == null);
781-
}
782-
783-
@Test
784-
public void jsonObjectNamesToArray() {
785-
String str =
786-
"{"+
787-
"\"trueKey\":true,"+
788-
"\"falseKey\":false,"+
789-
"\"stringKey\":\"hello world!\","+
790-
"}";
791-
String [] expectedNames = {"trueKey", "falseKey", "stringKey"};
792-
JSONObject jsonObject = new JSONObject(str);
793-
String [] names = JSONObject.getNames(jsonObject);
794-
Util.compareActualVsExpectedStringArrays(names, expectedNames);
795-
}
796793

797794
@Test
798795
public void jsonObjectNumberToString() {
@@ -907,6 +904,7 @@ public void jsonObjectToString() {
907904
}
908905

909906
@Test
907+
@SuppressWarnings("unchecked")
910908
public void jsonObjectToStringSuppressWarningOnCastToMap() {
911909
JSONObject jsonObject = new JSONObject();
912910
Map<String, String> map = new HashMap<String, String>();
@@ -921,8 +919,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() {
921919
* Can't do a Util compare because although they look the same
922920
* in the debugger, one is a map and the other is a JSONObject.
923921
*/
924-
// TODO: fix warnings
925-
map = (Map)jsonObject.get("key");
922+
map = (Map<String, String>)jsonObject.get("key");
926923
JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key");
927924
assertTrue("value size should be equal",
928925
map.size() == mapJsonObject.length() && map.size() == 1);
@@ -934,6 +931,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() {
934931
}
935932

936933
@Test
934+
@SuppressWarnings("unchecked")
937935
public void jsonObjectToStringSuppressWarningOnCastToCollection() {
938936
JSONObject jsonObject = new JSONObject();
939937
Collection<String> collection = new ArrayList<String>();
@@ -950,12 +948,11 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() {
950948
assertTrue("keys should be equal",
951949
jsonObject.keySet().iterator().next().equals(
952950
expectedJsonObject.keySet().iterator().next()));
953-
// TODO: fix warnings
954-
collection = (Collection)jsonObject.get("key");
951+
collection = (Collection<String>)jsonObject.get("key");
955952
JSONArray jsonArray = expectedJsonObject.getJSONArray("key");
956953
assertTrue("value size should be equal",
957954
collection.size() == jsonArray.length());
958-
Iterator it = collection.iterator();
955+
Iterator<String> it = collection.iterator();
959956
for (int i = 0; i < collection.size(); ++i) {
960957
assertTrue("items should be equal for index: "+i,
961958
jsonArray.get(i).toString().equals(it.next().toString()));

0 commit comments

Comments
 (0)