From c3ecf920b69b1e32fdbcb0e54b480d5a2e3a0928 Mon Sep 17 00:00:00 2001 From: Yueh Chun Wu Date: Wed, 13 Mar 2024 01:28:26 -0700 Subject: [PATCH] updating file's name --- README.md | 10 +- .../java/org/json/junit/MileStone3Test.java | 45 +++++ .../java/org/json/junit/MileStone4Test.java | 168 ++++++++++++++---- .../java/org/json/junit/MileStone5Test.java | 144 --------------- 4 files changed, 183 insertions(+), 184 deletions(-) create mode 100644 src/test/java/org/json/junit/MileStone3Test.java delete mode 100644 src/test/java/org/json/junit/MileStone5Test.java diff --git a/README.md b/README.md index cfb58b0..d5586d2 100644 --- a/README.md +++ b/README.md @@ -512,7 +512,7 @@ The `addNodes` function effectively explores the entire JSON structure, creating

Junit Test

-* I created a new test file called `MileStone4Test` for testing `toStream` method's functionalities. +* I created a new test file called `MileStone3Test` for testing `toStream` method's functionalities. ``` obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node); @@ -520,11 +520,11 @@ List titles = obj.toStream().map(node -> extract value for key "title"). obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation); ``` -* When you run `mvn clean test -Dtest=MileStone4Test` in the root dir, you can check each test case's printed result. +* When you run `mvn clean test -Dtest=MileStone3Test` in the root dir, you can check each test case's printed result. ζˆͺεœ– 2024-02-28 δΈ‹εˆ5 55 07 -

MileStone5

+

MileStone4

* Add asynchronous methods to the library that allow the client code to proceed, while specifying what to do when the JSONObject becomes available. This is useful for when reading very large files. For example: @@ -566,7 +566,7 @@ The `toJSONObject` static function acts as an interface to this functionality, h

Junit Test

-* I created a new test file called `MileStone5Test` for testing `toJSONObject` method's functionalities. +* I created a new test file called `MileStone4Test` for testing `toJSONObject` method's functionalities. * Inside the testing file, `testTransformJSONObjectKeyAsyn` verify that the XML to JSON conversion correctly applies the key transformation function to all keys in the resulting JSON object. @@ -615,4 +615,4 @@ public void testTransformJSONObjectKeyAsynExceptionHandler() { } ``` -* When you run `mvn clean test -Dtest=MileStone5Test` in the root dir, you can check each test case's printed result. \ No newline at end of file +* When you run `mvn clean test -Dtest=MileStone4Test` in the root dir, you can check each test case's printed result. \ No newline at end of file diff --git a/src/test/java/org/json/junit/MileStone3Test.java b/src/test/java/org/json/junit/MileStone3Test.java new file mode 100644 index 0000000..dcd6394 --- /dev/null +++ b/src/test/java/org/json/junit/MileStone3Test.java @@ -0,0 +1,45 @@ +import org.json.JSONObject; +import org.json.XML; +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; + +import static junit.framework.Assert.assertEquals; + +public class MileStone3Test { + @Test + public void testMileStone3() { + JSONObject obj = XML.toJSONObject("AAAASmithBBBBSmith"); + + //obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node); + obj.toStream().forEach(node -> { + String path = node.getPath(); + Object value = node.getValue(); + String transformedKey = node.getKey(); + String transformedValue = value.toString(); + + if (path.contains("author")) { + node.setKey(transformedKey.toUpperCase()); + node.setValue(transformedValue.toUpperCase()); + System.out.println(node.toString()); + } + }); + System.out.println("------------------"); + + //List titles = obj.toStream().map(node -> extract value for key "title").collect(Collectors.toList()); + List titles = obj.toStream().filter(node -> node.getPath().contains("title")) + .map(node -> node.getValue().toString()) + .collect(Collectors.toList()); + assertEquals(titles.size(), 2); + System.out.println(titles.get(0)); + System.out.println(titles.get(1)); + System.out.println("------------------"); + + //obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation); + obj.toStream().filter(node -> node.getKey().equals("author")) + .forEach(node -> { + System.out.println(node.getValue().toString().replace("Smith", "Kevin")); + }); + } +} diff --git a/src/test/java/org/json/junit/MileStone4Test.java b/src/test/java/org/json/junit/MileStone4Test.java index b609e83..8bf8ec1 100644 --- a/src/test/java/org/json/junit/MileStone4Test.java +++ b/src/test/java/org/json/junit/MileStone4Test.java @@ -1,46 +1,144 @@ -import org.json.JSONNode; +import org.json.JSONArray; import org.json.JSONObject; import org.json.XML; import org.junit.Test; -import java.util.List; -import java.util.stream.Collectors; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.function.Consumer; +import java.util.function.Function; -import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; public class MileStone4Test { + + private JSONObject convertValuesToString(JSONObject jsonObject) { + JSONObject result = new JSONObject(); + for (String key : jsonObject.keySet()) { + Object value = jsonObject.get(key); + if (value instanceof JSONObject) { + value = convertValuesToString((JSONObject) value); + } else if (value instanceof JSONArray) { + JSONArray array = (JSONArray) value; + JSONArray newArray = new JSONArray(); + for (int i = 0; i < array.length(); i++) { + Object element = array.get(i); + if (element instanceof JSONObject) { + newArray.put(convertValuesToString((JSONObject) element)); + } else { + newArray.put(String.valueOf(element)); + } + } + value = newArray; + } else { + value = String.valueOf(value); + } + result.put(key, value); + } + return result; + } + + @Test + public void testTransformJSONObjectKeyAsyn(){ + try { + FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); + Function keyTransformer = (key) -> "swe262_" + key; + Consumer exceptionHandler = (e) -> e.printStackTrace(); + + Future futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler); + String expect = "{\n" + + " \"swe262_library\": {\n" + + " \"swe262_book\": [\n" + + " {\n" + + " \"swe262_title\": \"Programming Languages\",\n" + + " \"swe262_author\": \"Robert W. Sebesta\",\n" + + " \"swe262_isbn\": \"9780133943023\",\n" + + " \"swe262_published\": \"2016\"\n" + + " },\n" + + " {\n" + + " \"swe262_title\": \"Clean Code: A Handbook of Agile Software Craftsmanship\",\n" + + " \"swe262_author\": \"Robert C. Martin\",\n" + + " \"swe262_isbn\": \"9780132350884\",\n" + + " \"swe262_published\": \"2008\"\n" + + " },\n" + + " {\n" + + " \"swe262_title\": \"Design Patterns: Elements of Reusable Object-Oriented Software\",\n" + + " \"swe262_author\": \"Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides\",\n" + + " \"swe262_isbn\": \"9780201633610\",\n" + + " \"swe262_published\": \"1994\"\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; + + // block the execution until the task is complete + while(!futureActual.isDone()) { + System.out.println("Calculating..."); + Thread.sleep(300); + } + + JSONObject actual = futureActual.get(); + + actual = convertValuesToString(actual); + + org.json.junit.Util.compareActualVsExpectedJsonObjects(actual, new JSONObject(expect)); + reader.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + e.printStackTrace(); + } catch (IOException e) { + System.out.println("Fail to close file."); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println("Something wrong while blockign futureActual.isDone()."); + e.printStackTrace(); + } catch (ExecutionException e) { + System.out.println("error found when executing futureActual.get()."); + e.printStackTrace(); + } + } + + @Test - public void testMileStone4() { - JSONObject obj = XML.toJSONObject("AAAASmithBBBBSmith"); - - //obj.toStream().forEach(node -> do some transformation, possibly based on the path of the node); - obj.toStream().forEach(node -> { - String path = node.getPath(); - Object value = node.getValue(); - String transformedKey = node.getKey(); - String transformedValue = value.toString(); - - if (path.contains("author")) { - node.setKey(transformedKey.toUpperCase()); - node.setValue(transformedValue.toUpperCase()); - System.out.println(node.toString()); + public void testTransformJSONObjectKeyAsynReturnType() { + try { + FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); + Function keyTransformer = (key) -> "swe262_" + key; + Consumer exceptionHandler = (e) -> e.printStackTrace(); + + Future futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler); + + while (!futureActual.isDone()) { + System.out.println("Calculating..."); + Thread.sleep(300); } - }); - System.out.println("------------------"); - - //List titles = obj.toStream().map(node -> extract value for key "title").collect(Collectors.toList()); - List titles = obj.toStream().filter(node -> node.getPath().contains("title")) - .map(node -> node.getValue().toString()) - .collect(Collectors.toList()); - assertEquals(titles.size(), 2); - System.out.println(titles.get(0)); - System.out.println(titles.get(1)); - System.out.println("------------------"); - - //obj.toStream().filter(node -> node with certain properties).forEach(node -> do some transformation); - obj.toStream().filter(node -> node.getKey().equals("author")) - .forEach(node -> { - System.out.println(node.getValue().toString().replace("Smith", "Kevin")); - }); + + JSONObject actual = futureActual.get(); + + assertTrue(actual instanceof JSONObject); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + @Test + public void testTransformJSONObjectKeyAsynExceptionHandler() { + try { + FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); + Consumer exceptionHandler = (e) -> System.out.println("OMG ERROR!!"); + + Future futureActual = XML.toJSONObject(reader, null, exceptionHandler); + assertNull(futureActual); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } } } diff --git a/src/test/java/org/json/junit/MileStone5Test.java b/src/test/java/org/json/junit/MileStone5Test.java deleted file mode 100644 index bb5ab85..0000000 --- a/src/test/java/org/json/junit/MileStone5Test.java +++ /dev/null @@ -1,144 +0,0 @@ -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.XML; -import org.junit.Test; - -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.function.Consumer; -import java.util.function.Function; - -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; - -public class MileStone5Test { - - private JSONObject convertValuesToString(JSONObject jsonObject) { - JSONObject result = new JSONObject(); - for (String key : jsonObject.keySet()) { - Object value = jsonObject.get(key); - if (value instanceof JSONObject) { - value = convertValuesToString((JSONObject) value); - } else if (value instanceof JSONArray) { - JSONArray array = (JSONArray) value; - JSONArray newArray = new JSONArray(); - for (int i = 0; i < array.length(); i++) { - Object element = array.get(i); - if (element instanceof JSONObject) { - newArray.put(convertValuesToString((JSONObject) element)); - } else { - newArray.put(String.valueOf(element)); - } - } - value = newArray; - } else { - value = String.valueOf(value); - } - result.put(key, value); - } - return result; - } - - @Test - public void testTransformJSONObjectKeyAsyn(){ - try { - FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); - Function keyTransformer = (key) -> "swe262_" + key; - Consumer exceptionHandler = (e) -> e.printStackTrace(); - - Future futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler); - String expect = "{\n" + - " \"swe262_library\": {\n" + - " \"swe262_book\": [\n" + - " {\n" + - " \"swe262_title\": \"Programming Languages\",\n" + - " \"swe262_author\": \"Robert W. Sebesta\",\n" + - " \"swe262_isbn\": \"9780133943023\",\n" + - " \"swe262_published\": \"2016\"\n" + - " },\n" + - " {\n" + - " \"swe262_title\": \"Clean Code: A Handbook of Agile Software Craftsmanship\",\n" + - " \"swe262_author\": \"Robert C. Martin\",\n" + - " \"swe262_isbn\": \"9780132350884\",\n" + - " \"swe262_published\": \"2008\"\n" + - " },\n" + - " {\n" + - " \"swe262_title\": \"Design Patterns: Elements of Reusable Object-Oriented Software\",\n" + - " \"swe262_author\": \"Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides\",\n" + - " \"swe262_isbn\": \"9780201633610\",\n" + - " \"swe262_published\": \"1994\"\n" + - " }\n" + - " ]\n" + - " }\n" + - "}"; - - // block the execution until the task is complete - while(!futureActual.isDone()) { - System.out.println("Calculating..."); - Thread.sleep(300); - } - - JSONObject actual = futureActual.get(); - - actual = convertValuesToString(actual); - - org.json.junit.Util.compareActualVsExpectedJsonObjects(actual, new JSONObject(expect)); - reader.close(); - } catch (FileNotFoundException e) { - System.out.println("File not found."); - e.printStackTrace(); - } catch (IOException e) { - System.out.println("Fail to close file."); - e.printStackTrace(); - } catch (InterruptedException e) { - System.out.println("Something wrong while blockign futureActual.isDone()."); - e.printStackTrace(); - } catch (ExecutionException e) { - System.out.println("error found when executing futureActual.get()."); - e.printStackTrace(); - } - } - - - @Test - public void testTransformJSONObjectKeyAsynReturnType() { - try { - FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); - Function keyTransformer = (key) -> "swe262_" + key; - Consumer exceptionHandler = (e) -> e.printStackTrace(); - - Future futureActual = XML.toJSONObject(reader, keyTransformer, exceptionHandler); - - while (!futureActual.isDone()) { - System.out.println("Calculating..."); - Thread.sleep(300); - } - - JSONObject actual = futureActual.get(); - - assertTrue(actual instanceof JSONObject); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - @Test - public void testTransformJSONObjectKeyAsynExceptionHandler() { - try { - FileReader reader = new FileReader("src/test/java/org/json/junit/data/exampleXML.xml"); - Consumer exceptionHandler = (e) -> System.out.println("OMG ERROR!!"); - - Future futureActual = XML.toJSONObject(reader, null, exceptionHandler); - assertNull(futureActual); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } -}