11package ch .akuhn .fame .test ;
22
3+ import ch .akuhn .fame .MetaRepository ;
34import ch .akuhn .fame .Tower ;
45import ch .akuhn .fame .internal .JSONPrettyPrinter ;
6+ import ch .akuhn .fame .internal .JSONPrinter ;
57import ch .akuhn .fame .parser .InputSource ;
68import junit .framework .TestCase ;
79
@@ -10,6 +12,9 @@ public class JSONPrinterTest extends TestCase {
1012 private JSONPrettyPrinter printer ;
1113 private Appendable stream ;
1214
15+ /** FIXME
16+ * This is not a test but a method to export a meta-model
17+ */
1318 public void testExportJSON () {
1419 InputSource input = InputSource .fromResource ("ch/unibe/fame/resources/lib.mse" );
1520 Tower t = new Tower ();
@@ -19,6 +24,10 @@ public void testExportJSON() {
1924 System .out .println (stream );
2025 }
2126
27+ private static String removeWhiteSpaces (String input ) {
28+ return input .replaceAll ("\\ s+" , "" );
29+ }
30+
2231 @ Override
2332 public void setUp () throws Exception {
2433 super .setUp ();
@@ -28,46 +37,78 @@ public void setUp() throws Exception {
2837
2938 public void testBeginAttributeSimple () {
3039 printer .beginAttribute ("hello" );
31- assertEquals (stream . toString (), "\" hello\" :" );
40+ assertEquals ("\" hello\" :" , stream . toString () );
3241 }
3342
3443 public void testPrimitive () {
3544 printer .primitive ("value" );
36- assertEquals (stream . toString (), "\" value\" " );
45+ assertEquals ("\" value\" " , stream . toString () );
3746 }
3847
3948 public void testPrimitiveWithSpecialCharacter () {
4049 printer .primitive ("MySuper\" String" );
41- assertEquals (stream . toString (), "\" MySuper\\ \" String\" " );
50+ assertEquals ("\" MySuper\\ \" String\" " , stream . toString () );
4251 }
4352
4453 public void testPrimitiveWithSpecialCharacterAndActualExample () {
4554 printer .primitive ("print(\" Printer \" + name() + \" prints \" + thePacket.contents(),false)" );
46- assertEquals (stream . toString (), "\" print(\\ \" Printer \\ \" + name() + \\ \" prints \\ \" + thePacket.contents(),false)\" " );
55+ assertEquals ( "\" print(\\ \" Printer \\ \" + name() + \\ \" prints \\ \" + thePacket.contents(),false)\" " , stream . toString () );
4756 }
4857
4958 public void testReference () {
5059 printer .reference ("hello" );
51- assertEquals (removeWhiteSpaces ( stream . toString ()), "{\" ref\" :\" hello\" }" );
60+ assertEquals ("{\" ref\" :\" hello\" }" , removeWhiteSpaces ( stream . toString ()) );
5261 }
5362
5463 public void testReferenceIndex () {
5564 printer .reference (2 );
56- assertEquals (removeWhiteSpaces ( stream . toString ()), "{\" ref\" :2}" );
65+ assertEquals ("{\" ref\" :2}" , removeWhiteSpaces ( stream . toString ()) );
5766 }
5867
5968 public void testSerial () {
6069 printer .serial (2 );
61- assertEquals (removeWhiteSpaces ( stream . toString ()), " \" id\" :2," );
70+ assertEquals (", \" id\" :2" , removeWhiteSpaces ( stream . toString ()) );
6271 }
6372
6473 public void testBeginElement () {
6574 printer .beginElement ("Java.Class" );
66- assertEquals (removeWhiteSpaces ( stream . toString ()), "{\" FM3\" :\" Java.Class\" ," );
75+ assertEquals ("{\" FM3\" :\" Java.Class\" " , removeWhiteSpaces ( stream . toString ()) );
6776 }
6877
78+ public void testEmptyEntityPrettyPrinter () {
79+ String str = "((FM3.Package))" ;
80+ Tower t = new Tower ();
81+ t .getMetamodel ().importMSE (str );
82+ MetaRepository repo = t .getMetamodel ();
83+ repo .accept ( printer );
84+ assertEquals ("[{\" FM3\" :\" FM3.Package\" ,\" id\" :1}]" , removeWhiteSpaces (stream .toString ()));
85+ }
6986
70- private static String removeWhiteSpaces (String input ) {
71- return input .replaceAll ("\\ s+" , "" );
87+ public void testEmptyEntityJSONPrinter () {
88+ String str = "((FM3.Package))" ;
89+ Tower t = new Tower ();
90+ t .getMetamodel ().importMSE (str );
91+ MetaRepository repo = t .getMetamodel ();
92+ repo .accept ( new JSONPrinter (stream ));
93+ assertEquals ("[{\" FM3\" :\" FM3.Package\" ,\" id\" :1}]" , stream .toString ());
94+ }
95+
96+ public void testEntityWithAttributePrettyPrinter () {
97+ String str = "((FM3.Package (name 'Blah')))" ;
98+ Tower t = new Tower ();
99+ t .getMetamodel ().importMSE (str );
100+ MetaRepository repo = t .getMetamodel ();
101+ repo .accept ( printer );
102+ assertEquals ("[{\" FM3\" :\" FM3.Package\" ,\" id\" :1,\" name\" :\" Blah\" }]" , removeWhiteSpaces (stream .toString ()));
72103 }
104+
105+ public void testEntityWithAttributeJSONPrinter () {
106+ String str = "((FM3.Package (name 'Blah')))" ;
107+ Tower t = new Tower ();
108+ t .getMetamodel ().importMSE (str );
109+ MetaRepository repo = t .getMetamodel ();
110+ repo .accept ( new JSONPrinter (stream ));
111+ assertEquals ("[{\" FM3\" :\" FM3.Package\" ,\" id\" :1,\" name\" :\" Blah\" }]" , stream .toString ());
112+ }
113+
73114}
0 commit comments