Skip to content

Commit b5e989b

Browse files
committed
add test cases and readme file
1 parent 7e5865e commit b5e989b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

README-M4.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Milestone 4 – Stream support for **JSONObject**
2+
3+
## What is Added
4+
5+
| Item | Description |
6+
|------|-------------|
7+
| `JSONObject.JSONNode` | Immutable leaf wrapper containing an absolute `path` and its `value`. |
8+
| `Stream<JSONNode> JSONObject.toStream()` | Depth-first, **lazy** flattening of any `JSONObject/JSONArray` into a `Stream`. Only leaf nodes are emitted (low memory footprint). |
9+
10+
Path conventions
11+
* Object keys: `/parent/child`
12+
* Array items: `/array[0]/child`
13+
14+
---
15+
16+
## Run the test class
17+
```bash
18+
mvn -Dtest=org.json.junit.milestone4.tests.JSONObjectStreamTest test
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.json.junit.milestone4.tests;
2+
3+
import org.json.JSONObject;
4+
import org.json.JSONObject.JSONNode;
5+
import org.json.XML;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.List;
12+
import java.util.Optional;
13+
import java.util.Scanner;
14+
import java.util.stream.Collectors;
15+
16+
import static org.junit.Assert.*;
17+
18+
/**
19+
* Unit tests for the JSONObject.toStream() extension (Milestone 4) – JUnit 4 / Java 8.
20+
*/
21+
public class JSONObjectStreamTest {
22+
23+
private static JSONObject catalog;
24+
25+
@BeforeClass
26+
public static void loadXml() throws Exception {
27+
try (InputStream in = JSONObjectStreamTest.class.getResourceAsStream("/books.xml")) {
28+
assertNotNull("books.xml must be on the class-path (src/test/resources)", in);
29+
30+
String xml;
31+
try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
32+
scanner.useDelimiter("\\A");
33+
xml = scanner.hasNext() ? scanner.next() : "";
34+
}
35+
36+
catalog = XML.toJSONObject(xml);
37+
}
38+
}
39+
40+
@Test
41+
public void extractTitles() {
42+
List<String> titles = catalog.toStream()
43+
.filter(n -> n.getPath().endsWith("/title"))
44+
.map(n -> n.getValue().toString())
45+
.collect(Collectors.toList());
46+
47+
assertEquals(12, titles.size());
48+
assertTrue(titles.contains("XML Developer's Guide"));
49+
assertTrue(titles.contains("Visual Studio 7: A Comprehensive Guide"));
50+
}
51+
52+
@Test
53+
public void findExactPath() {
54+
String wantedPath = "/catalog/book[0]/author";
55+
Optional<JSONNode> node = catalog.toStream()
56+
.filter(n -> n.getPath().equals(wantedPath))
57+
.findFirst();
58+
59+
assertTrue(node.isPresent());
60+
assertEquals("Gambardella, Matthew", node.get().getValue());
61+
}
62+
63+
@Test
64+
public void filterCheapBooks() {
65+
List<JSONNode> cheapPrices = catalog.toStream()
66+
.filter(n -> n.getPath().endsWith("/price"))
67+
.filter(n -> Double.parseDouble(n.getValue().toString()) < 10.0)
68+
.collect(Collectors.toList());
69+
70+
assertEquals(8, cheapPrices.size());
71+
assertTrue(cheapPrices.stream()
72+
.allMatch(n -> Double.parseDouble(n.getValue().toString()) < 10.0));
73+
}
74+
}

0 commit comments

Comments
 (0)