incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1154150 - in /incubator/isis/trunk/framework/viewer/json/json-applib/src: main/java/org/apache/isis/viewer/json/applib/ test/java/org/apache/isis/viewer/json/applib/
Date Fri, 05 Aug 2011 09:50:17 GMT
Author: danhaywood
Date: Fri Aug  5 09:50:16 2011
New Revision: 1154150

URL: http://svn.apache.org/viewvc?rev=1154150&view=rev
Log:
ISIS-109: JsonRepresentation tests, added basic xpath support

Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLink.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_toXml.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java?rev=1154150&r1=1154149&r2=1154150&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/JsonRepresentation.java
Fri Aug  5 09:50:16 2011
@@ -1,20 +1,33 @@
 package org.apache.isis.viewer.json.applib;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import net.sf.json.JSON;
 import net.sf.json.JSONSerializer;
 import net.sf.json.xml.XMLSerializer;
+import nu.xom.Builder;
+import nu.xom.Document;
+import nu.xom.Element;
+import nu.xom.Node;
+import nu.xom.Nodes;
+import nu.xom.ParsingException;
+import nu.xom.Serializer;
+import nu.xom.ValidityException;
 
+import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.isis.viewer.json.applib.blocks.Link;
 import org.apache.isis.viewer.json.applib.util.JsonMapper;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.JsonMappingException;
 
+import com.google.common.base.Charsets;
 
 /**
- * A wrapper around {@link JsonNode} that provides some additional helper methods.
+ * A wrapper around {@link JsonNode} that provides some additional helper
+ * methods.
  */
 public class JsonRepresentation {
 
@@ -29,7 +42,7 @@ public class JsonRepresentation {
     }
 
     public int size() {
-        if(!jsonNode.isArray()) {
+        if (!jsonNode.isArray()) {
             throw new IllegalStateException("Is an array");
         }
         return jsonNode.size();
@@ -37,21 +50,21 @@ public class JsonRepresentation {
 
     public String getString(String key) {
         JsonNode subNode = jsonNode.get(key);
-        if(subNode == null) {
+        if (subNode == null) {
             return null;
         }
         ensureValue(key, subNode, "string");
-        if(!subNode.isTextual()) {
+        if (!subNode.isTextual()) {
             throw new IllegalArgumentException("'" + key + "' (" + subNode.toString() + ")
is not a string");
         }
         return subNode.getTextValue();
     }
 
     private void ensureValue(String key, JsonNode subNode, String requiredType) {
-        if(subNode.isValueNode() ) {
+        if (subNode.isValueNode()) {
             return;
         }
-        if(subNode.isArray()) {
+        if (subNode.isArray()) {
             throw new IllegalArgumentException("'" + key + "' (a list) is not a " + requiredType);
         } else {
             throw new IllegalArgumentException("'" + key + "' (a map) is not a " + requiredType);
@@ -61,7 +74,7 @@ public class JsonRepresentation {
     public Link getLink(String key) throws JsonMappingException {
         JsonNode subNode = jsonNode.get(key);
         // TODO: extra checking here required
-        
+
         try {
             // TODO: review, rather heavyweight
             return JsonMapper.instance().read(subNode.toString(), Link.class);
@@ -87,11 +100,59 @@ public class JsonRepresentation {
     /**
      * Requires XOM to be added as a dependency.
      */
-    public JsonRepresentation xpath(String xpathExpression) {
+    public JsonRepresentation xpath(String xpathExpression) throws ValidityException, ParsingException,
IOException {
         String xml = toXml();
-        // TODO: use XOM's xpath support on the xml to find subnodes, 
-        // then serialize back to JSON
-        return null;
+        Builder builder = new nu.xom.Builder();
+        Document document = builder.build(xml, ".");
+        // toXml() puts structure under a <o> root.
+        Nodes matchingNodes = document.query("/o" + xpathExpression);
+        Document doc;
+        switch (matchingNodes.size()) {
+        case 0:
+            return null;
+        case 1:
+            Node node = matchingNodes.get(0);
+            if (!(node instanceof Element)) {
+                return null;
+            }
+            Element el = (Element) node;
+            el.detach();
+            doc = new nu.xom.Document(el);
+            return asJsonRepresentation(doc);
+        default:
+            doc = new Document(new nu.xom.Element("o"));
+            for (int i = 0; i < matchingNodes.size(); i++) {
+                Node matchingNode = matchingNodes.get(i);
+                matchingNode.detach();
+                doc.getRootElement().appendChild(matchingNode);
+            }
+            return asJsonRepresentation(doc);
+        }
+    }
+
+    private JsonRepresentation asJsonRepresentation(nu.xom.Document document2) throws IOException,
JsonParseException, JsonMappingException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        Serializer serializer = new nu.xom.Serializer(baos);
+        serializer.write(document2);
+
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        XMLSerializer xmlSerializer = new XMLSerializer();
+        JSON json = xmlSerializer.readFromStream(bais);
+        String jsonStr = json.toString();
+
+        return JsonMapper.instance().read(jsonStr, JsonRepresentation.class);
+    }
+
+    public boolean isArray() {
+        return jsonNode.isArray();
+    }
+
+    
+    
+    @Override
+    public String toString() {
+        return jsonNode.toString();
     }
+    
 
 }

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLink.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLink.java?rev=1154150&r1=1154149&r2=1154150&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLink.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_getLink.java
Fri Aug  5 09:50:16 2011
@@ -2,9 +2,7 @@ package org.apache.isis.viewer.json.appl
 
 import static org.apache.isis.viewer.json.applib.JsonUtils.readJson;
 import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
@@ -13,7 +11,6 @@ import org.apache.isis.viewer.json.appli
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class JsonRepresentationTest_getLink {

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_toXml.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_toXml.java?rev=1154150&r1=1154149&r2=1154150&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_toXml.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/test/java/org/apache/isis/viewer/json/applib/JsonRepresentationTest_toXml.java
Fri Aug  5 09:50:16 2011
@@ -1,6 +1,7 @@
 package org.apache.isis.viewer.json.applib;
 
 import static org.apache.isis.viewer.json.applib.JsonUtils.readJson;
+import static org.hamcrest.CoreMatchers.*;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.not;
 import static org.hamcrest.CoreMatchers.nullValue;
@@ -8,6 +9,9 @@ import static org.junit.Assert.assertTha
 
 import java.io.IOException;
 
+import nu.xom.ParsingException;
+import nu.xom.ValidityException;
+
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.junit.Test;
@@ -16,7 +20,7 @@ public class JsonRepresentationTest_toXm
 
 
     @Test
-    public void xpath_forLink() throws JsonParseException, JsonMappingException, IOException
{
+    public void toXml() throws JsonParseException, JsonMappingException, IOException {
         JsonRepresentation jsonRepresentation = 
                 new JsonRepresentation(readJson("map.json"));
         
@@ -24,5 +28,39 @@ public class JsonRepresentationTest_toXm
         
         assertThat(xml, is(not(nullValue())));
     }
-    
+
+    @Test
+    public void xpath_matchingSingleElement() throws JsonParseException, JsonMappingException,
IOException, ValidityException, ParsingException {
+        JsonRepresentation jsonRepresentation = 
+                new JsonRepresentation(readJson("map.json"));
+
+        JsonRepresentation matching = jsonRepresentation.xpath("//*[rel='someRel']");
+        assertThat(matching, is(not(nullValue())));
+        assertThat(matching.isArray(), is(false));
+        assertThat(matching.getString("rel"), is("someRel"));
+    }
+
+    @Test
+    public void xpath_matchingMultipleElementsInMap() throws JsonParseException, JsonMappingException,
IOException, ValidityException, ParsingException {
+        JsonRepresentation jsonRepresentation = 
+                new JsonRepresentation(readJson("map.json"));
+
+        JsonRepresentation matching = jsonRepresentation.xpath("/*");
+        assertThat(matching, is(not(nullValue())));
+        assertThat(matching.isArray(), is(false));
+        
+        assertThat(matching.getString("aString"), is(equalTo(jsonRepresentation.getString("aString"))));
+        assertThat(matching.getLink("aLink"), is(equalTo(jsonRepresentation.getLink("aLink"))));
+    }
+
+    @Test
+    public void xpath_matchingNone() throws JsonParseException, JsonMappingException, IOException,
ValidityException, ParsingException {
+        JsonRepresentation jsonRepresentation = 
+                new JsonRepresentation(readJson("map.json"));
+
+        JsonRepresentation matching = jsonRepresentation.xpath("//*[nonExistent='nonExistent']");
+        assertThat(matching, is(nullValue()));
+    }
+
+
 }



Mime
View raw message