Author: doogie
Date: Mon Mar 15 04:37:01 2010 New Revision: 923053 URL: http://svn.apache.org/viewvc?rev=923053&view=rev Log: 100% coverage during testing now. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/json/JSONWriter.java ofbiz/trunk/framework/base/src/org/ofbiz/base/json/test/JSONTests.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/json/JSONWriter.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/json/JSONWriter.java?rev=923053&r1=923052&r2=923053&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/json/JSONWriter.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/json/JSONWriter.java Mon Mar 15 04:37:01 2010 @@ -43,11 +43,11 @@ public class JSONWriter { } public JSONWriter(Writer writer) { - this(writer instanceof IndentingWriter ? (IndentingWriter) writer : new IndentingWriter(writer)); + this(IndentingWriter.makeIndentingWriter(writer)); } public JSONWriter(Writer writer, FallbackHandler fallbackHandler) { - this(writer instanceof IndentingWriter ? (IndentingWriter) writer : new IndentingWriter(writer), fallbackHandler); + this(IndentingWriter.makeIndentingWriter(writer), fallbackHandler); } public IndentingWriter getWriter() { @@ -108,13 +108,13 @@ public class JSONWriter { case '\r': writer.write("\\r"); continue; case '\t': writer.write("\\t"); continue; } - if (32 <= c && c >= 256) { + if (c >= 32 && c < 256) { + writer.write(c); + } else { writer.write("\\u"); String n = Integer.toString((int) c, 16); for (int j = 4 - n.length(); j > 0; j--) writer.write('0'); writer.write(n); - } else { - writer.write(c); } } writer.write('"'); Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/json/test/JSONTests.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/json/test/JSONTests.java?rev=923053&r1=923052&r2=923053&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/json/test/JSONTests.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/json/test/JSONTests.java Mon Mar 15 04:37:01 2010 @@ -19,8 +19,10 @@ package org.ofbiz.base.json.test; import java.io.IOException; +import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.io.StringWriter; +import java.io.OutputStreamWriter; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; @@ -32,6 +34,7 @@ import org.ofbiz.base.json.ParseExceptio import org.ofbiz.base.json.Token; import org.ofbiz.base.json.TokenMgrError; import org.ofbiz.base.test.GenericTestCaseBase; +import org.ofbiz.base.util.IndentingWriter; public class JSONTests extends GenericTestCaseBase { public JSONTests(String name) { @@ -58,6 +61,7 @@ public class JSONTests extends GenericTe } else { jsonWriter = new JSONWriter(writer); }; + assertTrue("writer is IndentingWriter", jsonWriter.getWriter() instanceof IndentingWriter); jsonWriter.write(object); return writer.toString(); } @@ -104,6 +108,54 @@ public class JSONTests extends GenericTe assertEquals("parse " + type, obj, parseJSON(json, true)); } + public void testClose() throws Exception { + JSONWriter writer = new JSONWriter(new OutputStreamWriter(new ByteArrayOutputStream())); + writer.close(); + IOException caught = null; + try { + writer.write(""); + } catch (IOException e) { + caught = e; + } finally { + assertNotNull("write after close", caught); + } + } + + public void testString() throws Exception { + StringBuilder wanted = new StringBuilder(); + StringBuilder json = new StringBuilder().append('"'); + for (int i = 0; i < 5120; i++) { + wanted.append((char) i); + if (i == '\b') { + json.append("\\b"); + } else if (i == '\f') { + json.append("\\f"); + } else if (i == '\t') { + json.append("\\t"); + } else if (i == '\n') { + json.append("\\n"); + } else if (i == '\r') { + json.append("\\r"); + } else if (i == '"') { + json.append("\\\""); + } else if (i == '\\') { + json.append("\\\\"); + } else if (i == '/') { + json.append("\\/"); + } else if (i < 32 || i >= 256) { + json.append("\\u"); + if (i < 16) json.append('0'); + if (i < 256) json.append('0'); + if (i < 4096) json.append('0'); + json.append(Integer.toString(i, 16)); + } else { + json.append((char) i); + } + } + json.append('"'); + assertSimpleJSON("string", wanted.toString(), json.toString()); + } + public void testParseBasicTypes() throws Exception { assertSimpleJSON("character", new Character('c'), "\"c\"", "c"); assertSimpleJSON("false", Boolean.FALSE, "false"); @@ -186,7 +238,7 @@ public class JSONTests extends GenericTe } public void testParseErrors() throws Exception { - for (char c = 1; c < 128; c++) { + for (char c = 1; c < 1024; c++) { if (c == '\t') { doWhitespaceExceptionTest(Character.toString(c), 8); } else if (c == '\n' || c == '\r' || c == ' ') { @@ -266,5 +318,13 @@ public class JSONTests extends GenericTe public void testResolve() throws Exception { assertResolveJSON("url", new URL("http://ofbiz.apache.org"), "resolve(\"java.net.URL:http:\\/\\/ofbiz.apache.org\")"); + IOException caught = null; + try { + getJSON(new URL("http://ofbiz.apache.org"), false); + } catch (IOException e) { + caught = e; + } finally { + assertNotNull("url not allowed", caught); + } } } |
Free forum by Nabble | Edit this page |