Skip to content

Commit a6bca85

Browse files
committed
some testing files added for milestone 4
1 parent 1c833a7 commit a6bca85

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.*;
2727
import java.util.concurrent.atomic.AtomicInteger;
2828
import java.util.regex.Pattern;
29+
import java.util.stream.Collectors;
2930

3031
import org.json.CDL;
3132
import org.json.JSONArray;
@@ -57,6 +58,7 @@
5758
import org.json.junit.data.Singleton;
5859
import org.json.junit.data.SingletonEnum;
5960
import org.json.junit.data.WeirdList;
61+
import org.junit.Before;
6062
import org.junit.Ignore;
6163
import org.junit.Test;
6264

@@ -3817,4 +3819,60 @@ public static HashMap<String, Object> buildNestedMap(int maxDepth) {
38173819
return nestedMap;
38183820
}
38193821

3822+
/**
3823+
* Test to determine validy of the stream method.
3824+
*/
3825+
@Test
3826+
public void streamTest() {
3827+
JSONObject obj = XML.toJSONObject(
3828+
"<Books><book><title>AAA</title><author>ASmith</author></book><book><title>BBB</title><author>BSmith</author></book></Books>");
3829+
3830+
JSONObject[] allNodesExpected = new JSONObject[] {
3831+
new JSONObject(
3832+
"{\"Books\":{\"book\":[{\"author\":\"ASmith\",\"title\":\"AAA\"},{\"author\":\"BSmith\",\"title\":\"BBB\"}]}}"),
3833+
new JSONObject(
3834+
"{\"book\":[{\"author\":\"ASmith\",\"title\":\"AAA\"},{\"author\":\"BSmith\",\"title\":\"BBB\"}]}"),
3835+
new JSONObject("{\"author\":\"ASmith\",\"title\":\"AAA\"}"),
3836+
new JSONObject("{\"author\":\"BSmith\",\"title\":\"BBB\"}") };
3837+
3838+
// Get All the nodes
3839+
JSONObject[] allNodesActual = obj.toStream().filter(node -> node != null).toArray(JSONObject[]::new);
3840+
3841+
for (int i = 0; i < allNodesActual.length; i += 1) {
3842+
assertEquals(allNodesActual[i].toString(), allNodesExpected[i].toString());
3843+
}
3844+
}
3845+
3846+
@Test
3847+
// TEST TO SEE WE CAN GET TITLES
3848+
public void streamTest2(){
3849+
JSONObject obj = XML.toJSONObject(
3850+
"<Books><book><title>AAA</title><author>ASmith</author></book><book><title>BBB</title><author>BSmith</author></book></Books>");
3851+
3852+
List<String> titles = obj.toStream()
3853+
.filter(node -> node != null)
3854+
.map(node -> node.optString("title"))
3855+
.filter(x -> x.length() > 0)
3856+
.collect(Collectors.toList());
3857+
3858+
String expectedTitles[] = { "AAA", "BBB" };
3859+
3860+
for (int i = 0; i < titles.size(); i += 1) {
3861+
assertEquals(expectedTitles[i], titles.get(i));
3862+
}
3863+
}
3864+
@Test
3865+
// TEST TO SEE IF WE CAN GET A PARTICULAR VALUE
3866+
public void streamTest3() {
3867+
3868+
JSONObject obj = XML.toJSONObject(
3869+
"<Books><book><title>AAA</title><author>ASmith</author></book><book><title>BBB</title><author>BSmith</author></book></Books>");
3870+
3871+
JSONObject[] expected = obj.toStream().filter(node -> node != null)
3872+
.filter(node -> node.has("author") &&
3873+
"ASmith".equals(node.optString("author")))
3874+
.toArray(JSONObject[]::new);
3875+
3876+
assertEquals(expected[0].toString(), "{\"author\":\"ASmith\",\"title\":\"AAA\"}");
3877+
}
38203878
}

0 commit comments

Comments
 (0)