Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void beginElement(String name) {
lntabs();
append("\"FM3\":\"");
append(name);
append("\",");
lntabs();
append("\"");
}

public void beginMultivalue(String name) {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/ch/akuhn/fame/internal/JSONPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void beginElement(String name) {
append("{");
append("\"FM3\":\"");
append(name);
append("\",");
append("\"");
}

public void beginMultivalue(String name) {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down
61 changes: 51 additions & 10 deletions lib/src/test/java/ch/akuhn/fame/test/JSONPrinterTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand All @@ -19,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();
Expand All @@ -28,46 +37,78 @@ 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()));
}

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()));
}

private static String removeWhiteSpaces(String input) {
return input.replaceAll("\\s+", "");
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());
}

}
Loading