|
26 | 26 | import java.util.*; |
27 | 27 | import java.util.concurrent.atomic.AtomicInteger; |
28 | 28 | import java.util.regex.Pattern; |
| 29 | +import java.util.stream.Collectors; |
29 | 30 |
|
30 | 31 | import org.json.CDL; |
31 | 32 | import org.json.JSONArray; |
|
57 | 58 | import org.json.junit.data.Singleton; |
58 | 59 | import org.json.junit.data.SingletonEnum; |
59 | 60 | import org.json.junit.data.WeirdList; |
| 61 | +import org.junit.Before; |
60 | 62 | import org.junit.Ignore; |
61 | 63 | import org.junit.Test; |
62 | 64 |
|
@@ -3817,4 +3819,60 @@ public static HashMap<String, Object> buildNestedMap(int maxDepth) { |
3817 | 3819 | return nestedMap; |
3818 | 3820 | } |
3819 | 3821 |
|
| 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 | + } |
3820 | 3878 | } |
0 commit comments