abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ckopp...@apache.org
Subject svn commit: r1042356 - in /abdera/java/trunk: ./ extensions/json/ extensions/json/src/main/java/org/apache/abdera/ext/json/ extensions/json/src/test/java/org/apache/abdera/ext/json/
Date Sun, 05 Dec 2010 14:07:12 GMT
Author: ckoppelt
Date: Sun Dec  5 14:07:11 2010
New Revision: 1042356

URL: http://svn.apache.org/viewvc?rev=1042356&view=rev
Log:
fixed nullpointers; added unit tests - preparing a refactoring

Removed:
    abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/TestSuite.java
Modified:
    abdera/java/trunk/extensions/json/pom.xml
    abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONStream.java
    abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONUtil.java
    abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/JSONStreamTest.java
    abdera/java/trunk/pom.xml

Modified: abdera/java/trunk/extensions/json/pom.xml
URL: http://svn.apache.org/viewvc/abdera/java/trunk/extensions/json/pom.xml?rev=1042356&r1=1042355&r2=1042356&view=diff
==============================================================================
--- abdera/java/trunk/extensions/json/pom.xml (original)
+++ abdera/java/trunk/extensions/json/pom.xml Sun Dec  5 14:07:11 2010
@@ -38,5 +38,9 @@
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
     </dependency>
+      <dependency>
+        <groupId>org.mockito</groupId>
+        <artifactId>mockito-all</artifactId>
+      </dependency>
   </dependencies>
 </project>

Modified: abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONStream.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONStream.java?rev=1042356&r1=1042355&r2=1042356&view=diff
==============================================================================
--- abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONStream.java
(original)
+++ abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONStream.java
Sun Dec  5 14:07:11 2010
@@ -40,8 +40,9 @@ public class JSONStream {
 
     private boolean isStart() {
         boolean b = sepstack.peek();
-        if (b)
+        if (b) {
             sepstack.set(sepstack.size() - 1, false);
+        }
         return b;
     }
 
@@ -62,8 +63,9 @@ public class JSONStream {
     }
 
     private void writeIndent() throws IOException {
-        for (int n = 0; n < depth; n++)
+        for (int n = 0; n < depth; n++) {
             writer.write(' ');
+        }
         writer.flush();
     }
 
@@ -122,8 +124,9 @@ public class JSONStream {
     }
 
     public void writeField(String name) throws IOException {
-        if (!isStart())
+        if (!isStart()) {
             writeSeparator();
+        }
         writeNewLine();
         writeIndent();
         writeQuoted(name);
@@ -131,23 +134,27 @@ public class JSONStream {
     }
 
     public void writeField(String name, Date value) throws IOException {
-        if (value != null)
+        if (value != null) {
             writeField(name, AtomDate.format(value));
+        }
     }
 
     public void writeField(String name, IRI value) throws IOException {
-        if (value != null)
+        if (value != null) {
             writeField(name, value.toASCIIString());
+        }
     }
 
     public void writeField(String name, MimeType value) throws IOException {
-        if (value != null)
+        if (value != null) {
             writeField(name, value.toString());
+        }
     }
 
     public void writeField(String name, EntityTag value) throws IOException {
-        if (value != null)
+        if (value != null) {
             writeField(name, value.toString());
+        }
     }
 
     public void writeField(String name, String value) throws IOException {

Modified: abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONUtil.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONUtil.java?rev=1042356&r1=1042355&r2=1042356&view=diff
==============================================================================
--- abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONUtil.java
(original)
+++ abdera/java/trunk/extensions/json/src/main/java/org/apache/abdera/ext/json/JSONUtil.java
Sun Dec  5 14:07:11 2010
@@ -58,10 +58,11 @@ public class JSONUtil {
 
     public static void toJson(Base base, Writer writer) throws IOException {
         JSONStream jstream = new JSONStream(writer);
-        if (base instanceof Document)
+        if (base instanceof Document) {
             toJson((Document)base, jstream);
-        else if (base instanceof Element)
+        } else if (base instanceof Element) {
             toJson((Element)base, jstream);
+        }
         writer.flush();
     }
 
@@ -74,10 +75,12 @@ public class JSONUtil {
         }
         IRI base = element.getResolvedBaseUri();
 
-        if (parentbase == null && base != null)
+        if (parentbase == null && base != null) {
             return false;
-        if (parentbase == null && base == null)
+        }
+        if (parentbase == null && base == null) {
             return true;
+        }
         return parentbase.equals(element.getResolvedBaseUri());
     }
 
@@ -94,8 +97,9 @@ public class JSONUtil {
                 jstream.startObject();
                 jstream.writeField("type", texttype.name().toLowerCase());
                 writeLanguageFields(element, jstream);
-                if (!isSameAsParentBase(element))
+                if (!isSameAsParentBase(element)) {
                     jstream.writeField("xml:base", element.getResolvedBaseUri());
+                }
                 jstream.endObject();
                 jstream.writeField("children");
                 switch (text.getTextType()) {
@@ -105,11 +109,19 @@ public class JSONUtil {
                         jstream.endArray();
                         break;
                     case HTML:
-                        Div div = HtmlHelper.parse(text.getValue());
-                        writeElementValue(div, jstream);
+                        if (text.getValue() != null) {
+                            Div div = HtmlHelper.parse(text.getValue());
+                            writeElementValue(div, jstream);
+                        } else {
+                            jstream.writeQuoted("");
+                        }
                         break;
                     case XHTML:
-                        writeElementValue(text.getValueElement(), jstream);
+                        if (text.getValueElement() != null) {
+                            writeElementValue(text.getValueElement(), jstream);
+                        } else {
+                            jstream.writeQuoted("");
+                        }
                         break;
                 }
                 jstream.endObject();
@@ -193,7 +205,7 @@ public class JSONUtil {
                 jstream.writeField("href", collection.getResolvedHref());
                 writeElement("title", collection.getTitleElement(), jstream);
                 String[] accepts = collection.getAccept();
-                if (accepts != null || accepts.length > 0) {
+                if (accepts != null && accepts.length > 0) {
                     jstream.writeField("accept");
                     jstream.startArray();
                     for (int n = 0; n < accepts.length; n++) {
@@ -211,8 +223,9 @@ public class JSONUtil {
             } else if (element instanceof Control) {
                 jstream.startObject();
                 writeLanguageFields(element, jstream);
-                if (!isSameAsParentBase(element))
+                if (!isSameAsParentBase(element)) {
                     jstream.writeField("xml:base", element.getResolvedBaseUri());
+                }
                 Control control = (Control)element;
                 jstream.writeField("draft", control.isDraft() ? "true" : "false");
                 writeExtensions((ExtensibleElement)element, jstream);
@@ -220,8 +233,9 @@ public class JSONUtil {
             } else if (element instanceof Entry) {
                 jstream.startObject();
                 writeLanguageFields(element, jstream);
-                if (!isSameAsParentBase(element))
+                if (!isSameAsParentBase(element)){
                     jstream.writeField("xml:base", element.getResolvedBaseUri());
+                }
                 Entry entry = (Entry)element;
                 jstream.writeField("id", entry.getId());
                 writeElement("title", entry.getTitleElement(), jstream);
@@ -243,8 +257,9 @@ public class JSONUtil {
             } else if (element instanceof Generator) {
                 jstream.startObject();
                 writeLanguageFields(element, jstream);
-                if (!isSameAsParentBase(element))
+                if (!isSameAsParentBase(element)) {
                     jstream.writeField("xml:base", element.getResolvedBaseUri());
+                }
                 Generator generator = (Generator)element;
                 jstream.writeField("version", generator.getVersion());
                 jstream.writeField("uri", generator.getResolvedUri());
@@ -253,16 +268,18 @@ public class JSONUtil {
             } else if (element instanceof Link) {
                 jstream.startObject();
                 writeLanguageFields(element, jstream);
-                if (!isSameAsParentBase(element))
+                if (!isSameAsParentBase(element)) {
                     jstream.writeField("xml:base", element.getResolvedBaseUri());
+                }
                 Link link = (Link)element;
                 jstream.writeField("href", link.getResolvedHref());
                 jstream.writeField("rel", link.getRel());
                 jstream.writeField("title", link.getTitle());
                 jstream.writeField("type", link.getMimeType());
                 jstream.writeField("hreflang", link.getHrefLang());
-                if (link.getLength() > -1)
+                if (link.getLength() > -1) {
                     jstream.writeField("length", link.getLength());
+                }
                 writeExtensions((ExtensibleElement)element, jstream);
                 jstream.endObject();
             } else if (element instanceof Person) {

Modified: abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/JSONStreamTest.java
URL: http://svn.apache.org/viewvc/abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/JSONStreamTest.java?rev=1042356&r1=1042355&r2=1042356&view=diff
==============================================================================
--- abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/JSONStreamTest.java
(original)
+++ abdera/java/trunk/extensions/json/src/test/java/org/apache/abdera/ext/json/JSONStreamTest.java
Sun Dec  5 14:07:11 2010
@@ -17,18 +17,15 @@
  */
 package org.apache.abdera.ext.json;
 
+import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayOutputStream;
+
 import org.apache.abdera.Abdera;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.writer.Writer;
 import org.junit.Test;
 
-/**
- * JSONStreamTest
- * 
- * @author David Calavera
- * @since 11/01/08
- */
 public class JSONStreamTest {
 
     @Test
@@ -39,6 +36,11 @@ public class JSONStreamTest {
         entry.setContent(new IRI("http://example.org/xml"), "text/xml");
 
         Writer json = abdera.getWriterFactory().getWriter("json");
-        entry.writeTo(json, System.out);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        entry.writeTo(json, outputStream);
+        String output = outputStream.toString();
+        assertTrue(output.contains("\"type\":\"text/xml\""));
+        assertTrue(output.contains("\"src\":\"http://example.org/xml\""));
+        assertTrue(output.contains("\"content\":"));
     }
 }

Modified: abdera/java/trunk/pom.xml
URL: http://svn.apache.org/viewvc/abdera/java/trunk/pom.xml?rev=1042356&r1=1042355&r2=1042356&view=diff
==============================================================================
--- abdera/java/trunk/pom.xml (original)
+++ abdera/java/trunk/pom.xml Sun Dec  5 14:07:11 2010
@@ -460,6 +460,13 @@
         <version>2.5</version>  
       </dependency>
       
+      <dependency>
+        <groupId>org.mockito</groupId>
+        <artifactId>mockito-all</artifactId>
+        <version>1.8.5</version>
+        <scope>test</scope>
+      </dependency>
+      
     </dependencies>
   </dependencyManagement>
   



Mime
View raw message