From 798ea6a3aac0f1ab1fa27481398ad51bdcb53b2b Mon Sep 17 00:00:00 2001 From: anquetil Date: Thu, 12 Dec 2024 18:33:48 +0100 Subject: [PATCH 1/2] modified printing of properties to avoid extra "," for entity with no property --- .../fame/internal/JSONPrettyPrinter.java | 6 +- .../ch/akuhn/fame/internal/JSONPrinter.java | 4 +- .../fame/internal/RepositoryVisitor.java | 5 +- .../ch/akuhn/fame/test/JSONPrinterTest.java | 58 ++++++++++++++++--- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java b/lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java index bd9b5b5..35dc43a 100644 --- a/lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java +++ b/lib/src/main/java/ch/akuhn/fame/internal/JSONPrettyPrinter.java @@ -39,8 +39,7 @@ public void beginElement(String name) { lntabs(); append("\"FM3\":\""); append(name); - append("\","); - lntabs(); + append("\""); } public void beginMultivalue(String name) { @@ -121,9 +120,10 @@ public void reference(String name, int index) { @Override public void serial(int index) { + printPropertySeparator(); + lntabs(); append("\"id\":"); append(String.valueOf(index)); - append(","); } public void printEntitySeparator() { diff --git a/lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java b/lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java index 5136ed4..997d6d0 100644 --- a/lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java +++ b/lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java @@ -30,7 +30,7 @@ public void beginElement(String name) { append("{"); append("\"FM3\":\""); append(name); - append("\","); + append("\""); } public void beginMultivalue(String name) { @@ -104,9 +104,9 @@ public void reference(String name, int index) { @Override public void serial(int index) { + printPropertySeparator(); append("\"id\":"); append(String.valueOf(index)); - append(","); } public void printEntitySeparator() { diff --git a/lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java b/lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java index 0ea2fe1..194d525 100644 --- a/lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java +++ b/lib/src/main/java/ch/akuhn/fame/internal/RepositoryVisitor.java @@ -79,6 +79,8 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col continue; }*/ if (!values.isEmpty()) { + visitor.printEntitySeparator(); + visitor.beginAttribute(property.getName()); if (property.isMultivalued()) { visitor.beginMultivalue(property.getName()); @@ -131,9 +133,6 @@ private void handleChildrenProperties(Object each, MetaDescription meta /**, Col visitor.endMultivalue(property.getName()); } visitor.endAttribute(property.getName()); - if (propertiesIterator.hasNext()) { - visitor.printEntitySeparator(); - } } } } diff --git a/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java b/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java index d36d76e..b3965d9 100644 --- a/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java +++ b/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java @@ -1,7 +1,9 @@ package ch.akuhn.fame.test; +import ch.akuhn.fame.MetaRepository; import ch.akuhn.fame.Tower; import ch.akuhn.fame.internal.JSONPrettyPrinter; +import ch.akuhn.fame.internal.JSONPrinter; import ch.akuhn.fame.parser.InputSource; import junit.framework.TestCase; @@ -10,6 +12,9 @@ public class JSONPrinterTest extends TestCase { private JSONPrettyPrinter printer; private Appendable stream; + /** FIXME + * This is not a test but a method to export a meta-model + */ public void testExportJSON() { InputSource input = InputSource.fromResource("ch/unibe/fame/resources/lib.mse"); Tower t = new Tower(); @@ -28,46 +33,83 @@ public void setUp() throws Exception { public void testBeginAttributeSimple() { printer.beginAttribute("hello"); - assertEquals(stream.toString(), "\"hello\":"); + assertEquals("\"hello\":", stream.toString()); } public void testPrimitive() { printer.primitive("value"); - assertEquals(stream.toString(), "\"value\""); + assertEquals("\"value\"", stream.toString()); } public void testPrimitiveWithSpecialCharacter() { printer.primitive("MySuper\"String"); - assertEquals(stream.toString(), "\"MySuper\\\"String\""); + assertEquals("\"MySuper\\\"String\"", stream.toString()); } public void testPrimitiveWithSpecialCharacterAndActualExample() { printer.primitive("print(\"Printer \" + name() + \" prints \"+ thePacket.contents(),false)"); - assertEquals(stream.toString(), "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\""); + assertEquals( "\"print(\\\"Printer \\\" + name() + \\\" prints \\\"+ thePacket.contents(),false)\"", stream.toString()); } public void testReference() { printer.reference("hello"); - assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":\"hello\"}"); + assertEquals("{\"ref\":\"hello\"}", removeWhiteSpaces(stream.toString())); } public void testReferenceIndex() { printer.reference(2); - assertEquals(removeWhiteSpaces(stream.toString()), "{\"ref\":2}"); + assertEquals("{\"ref\":2}", removeWhiteSpaces(stream.toString())); } public void testSerial() { printer.serial(2); - assertEquals(removeWhiteSpaces(stream.toString()), "\"id\":2,"); + assertEquals(",\"id\":2", removeWhiteSpaces(stream.toString())); } public void testBeginElement() { printer.beginElement("Java.Class"); - assertEquals(removeWhiteSpaces(stream.toString()), "{\"FM3\":\"Java.Class\","); + assertEquals("{\"FM3\":\"Java.Class\"", removeWhiteSpaces(stream.toString())); } private static String removeWhiteSpaces(String input) { return input.replaceAll("\\s+", ""); } + + public void testEmptyEntityPrettyPrinter() { + String str = "((FM3.Package))"; + Tower t = new Tower(); + t.getMetamodel().importMSE(str); + MetaRepository repo = t.getMetamodel(); + repo.accept( printer); + assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", removeWhiteSpaces(stream.toString())); + } + + public void testEmptyEntityJSONPrinter() { + String str = "((FM3.Package))"; + Tower t = new Tower(); + t.getMetamodel().importMSE(str); + MetaRepository repo = t.getMetamodel(); + repo.accept( new JSONPrinter(stream)); + assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1}]", stream.toString()); + } + + public void testEntityWithAttributePrettyPrinter() { + String str = "((FM3.Package (name 'Blah')))"; + Tower t = new Tower(); + t.getMetamodel().importMSE(str); + MetaRepository repo = t.getMetamodel(); + repo.accept( printer); + assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", removeWhiteSpaces(stream.toString())); + } + + public void testEntityWithAttributeJSONPrinter() { + String str = "((FM3.Package (name 'Blah')))"; + Tower t = new Tower(); + t.getMetamodel().importMSE(str); + MetaRepository repo = t.getMetamodel(); + repo.accept( new JSONPrinter(stream)); + assertEquals("[{\"FM3\":\"FM3.Package\",\"id\":1,\"name\":\"Blah\"}]", stream.toString()); + } + } \ No newline at end of file From 638d3340c4ae1d771eb09e75b6f7caa32fb8c19f Mon Sep 17 00:00:00 2001 From: anquetil Date: Thu, 12 Dec 2024 22:55:00 +0100 Subject: [PATCH 2/2] added test for MSEPrinter --- .../test/java/ch/akuhn/fame/test/JSONPrinterTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java b/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java index b3965d9..b0cf9bb 100644 --- a/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java +++ b/lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java @@ -24,6 +24,10 @@ public void testExportJSON() { System.out.println(stream); } + private static String removeWhiteSpaces(String input) { + return input.replaceAll("\\s+", ""); + } + @Override public void setUp() throws Exception { super.setUp(); @@ -71,11 +75,6 @@ public void testBeginElement() { assertEquals("{\"FM3\":\"Java.Class\"", removeWhiteSpaces(stream.toString())); } - - private static String removeWhiteSpaces(String input) { - return input.replaceAll("\\s+", ""); - } - public void testEmptyEntityPrettyPrinter() { String str = "((FM3.Package))"; Tower t = new Tower();