Author: etnu
Date: Thu Nov 20 10:31:57 2008
New Revision: 719318

URL: http://svn.apache.org/viewvc?rev=719318&view=rev
Log:
Fixed toJSONString for nested message bundles.


Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/MessageBundle.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/MessageBundleTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/MessageBundle.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/MessageBundle.java?rev=719318&r1=719317&r2=719318&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/MessageBundle.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/MessageBundle.java
 Thu Nov 20 10:31:57 2008
@@ -39,6 +39,7 @@
 
   private final Map<String, String> messages;
   private final String languageDirection;
+  private final String jsonString;
 
    /**
    * Constructs a message bundle from input xml (fetched from an external 
file).
@@ -56,6 +57,7 @@
           + ": " + e.getMessage());
     }
     messages = parseMessages(doc);
+    jsonString = new JSONObject(messages).toString();
     languageDirection = locale.getLanguageDirection();
   }
 
@@ -64,6 +66,7 @@
    */
   public MessageBundle(Element element) throws SpecParserException {
     messages = parseMessages(element);
+    jsonString = new JSONObject(messages).toString();
     languageDirection = XmlUtil.getAttribute(element, "language_direction", 
"ltr");
   }
 
@@ -85,11 +88,13 @@
       dir = child.languageDirection;
     }
     messages = Collections.unmodifiableMap(merged);
+    jsonString = new JSONObject(messages).toString();
     languageDirection = dir;
   }
 
   private MessageBundle() {
     this.messages = Collections.emptyMap();
+    jsonString = "{}";
     languageDirection = "ltr";
   }
 
@@ -111,9 +116,8 @@
   /**
    * Return the contents as a JSON encoded string
    */
-  private String jsonString;
   public String toJSONString() {
-    return jsonString;  
+    return jsonString;
   }
 
   /**
@@ -134,7 +138,6 @@
       }
       messages.put(name, msg.getTextContent().trim());
     }
-    jsonString = new JSONObject(messages).toString();
     return Collections.unmodifiableMap(messages);
   }
 

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/MessageBundleTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/MessageBundleTest.java?rev=719318&r1=719317&r2=719318&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/MessageBundleTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/MessageBundleTest.java
 Thu Nov 20 10:31:57 2008
@@ -26,6 +26,8 @@
 
 import com.google.common.collect.Maps;
 
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.junit.Before;
 import org.junit.Test;
 import org.w3c.dom.Element;
@@ -119,4 +121,40 @@
     MessageBundle b1 = new MessageBundle(locale, b0.toString());
     assertEquals(b0.getMessages(), b1.getMessages());
   }
+
+  private static void assertJsonEquals(JSONObject left, JSONObject right) 
throws JSONException {
+    assertEquals(left.length(), right.length());
+    for (String key : JSONObject.getNames(left)) {
+      assertEquals(left.get(key), right.get(key));
+    }
+  }
+
+  @Test
+  public void toJSONStringMatchesValues() throws Exception {
+    MessageBundle simple = new MessageBundle(XmlUtil.parse(PARENT_LOCALE));
+
+    JSONObject fromString = new JSONObject(simple.toJSONString());
+    JSONObject fromMap = new JSONObject(simple.getMessages());
+    assertJsonEquals(fromString, fromMap);
+  }
+
+  @Test
+  public void toJSONStringMatchesValuesLocaleCtor() throws Exception {
+    MessageBundle bundle = new MessageBundle(locale, XML);
+
+    JSONObject fromString = new JSONObject(bundle.toJSONString());
+    JSONObject fromMap = new JSONObject(bundle.getMessages());
+    assertJsonEquals(fromString, fromMap);
+  }
+
+  @Test
+  public void toJSONStringMatchesValuesWithChild() throws Exception {
+    MessageBundle parent = new MessageBundle(XmlUtil.parse(PARENT_LOCALE));
+    MessageBundle child = new MessageBundle(XmlUtil.parse(XML));
+    MessageBundle bundle = new MessageBundle(parent, child);
+
+    JSONObject fromString = new JSONObject(bundle.toJSONString());
+    JSONObject fromMap = new JSONObject(bundle.getMessages());
+    assertJsonEquals(fromString, fromMap);
+  }
 }


Reply via email to