It is more effective to properly concatenate the string than to wrap streams in SequenceInputStream
1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java Sun Jun 14 22:11:56 2015 +0200
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java Wed Jun 24 22:42:35 2015 +0200
1.3 @@ -45,7 +45,6 @@
1.4 import java.io.ByteArrayInputStream;
1.5 import java.io.EOFException;
1.6 import java.io.InputStream;
1.7 -import java.io.SequenceInputStream;
1.8 import java.io.UnsupportedEncodingException;
1.9 import java.util.ArrayList;
1.10 import java.util.HashMap;
1.11 @@ -63,9 +62,12 @@
1.12 * @author Jaroslav Tulach
1.13 */
1.14 public final class ConvertTypesTest {
1.15 - private static InputStream createIS(boolean includeSex, boolean includeAddress, int array)
1.16 + private static InputStream createIS(String prefix, boolean includeSex, boolean includeAddress, int array, String suffix)
1.17 throws UnsupportedEncodingException {
1.18 StringBuilder sb = new StringBuilder();
1.19 + if (prefix != null) {
1.20 + sb.append(prefix);
1.21 + }
1.22 int repeat;
1.23 if (array != -1) {
1.24 sb.append("[\n");
1.25 @@ -90,6 +92,9 @@
1.26 if (array != -1) {
1.27 sb.append(']');
1.28 }
1.29 + if (suffix != null) {
1.30 + sb.append(suffix);
1.31 + }
1.32 return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
1.33 }
1.34 private static Object createJSON(boolean includeSex)
1.35 @@ -117,7 +122,7 @@
1.36 @KOTest
1.37 public void parseConvertToPeople() throws Exception {
1.38 final BrwsrCtx c = newContext();
1.39 - final InputStream o = createIS(true, false, -1);
1.40 + final InputStream o = createIS(null, true, false, -1, null);
1.41
1.42 Person p = Models.parse(c, Person.class, o);
1.43
1.44 @@ -129,7 +134,7 @@
1.45 @KOTest
1.46 public void parseConvertToPeopleWithAddress() throws Exception {
1.47 final BrwsrCtx c = newContext();
1.48 - final InputStream o = createIS(true, true, -1);
1.49 + final InputStream o = createIS(null, true, true, -1, null);
1.50
1.51 Person p = Models.parse(c, Person.class, o);
1.52
1.53 @@ -143,7 +148,7 @@
1.54 @KOTest
1.55 public void parseConvertToPeopleWithAddressIntoAnArray() throws Exception {
1.56 final BrwsrCtx c = newContext();
1.57 - final InputStream o = createIS(true, true, -1);
1.58 + final InputStream o = createIS(null, true, true, -1, null);
1.59
1.60 List<Person> arr = new ArrayList<Person>();
1.61 Models.parse(c, Person.class, o, arr);
1.62 @@ -207,7 +212,7 @@
1.63 @KOTest
1.64 public void parseConvertToPeopleWithoutSex() throws Exception {
1.65 final BrwsrCtx c = newContext();
1.66 - final InputStream o = createIS(false, false, -1);
1.67 + final InputStream o = createIS(null, false, false, -1, null);
1.68 Person p = Models.parse(c, Person.class, o);
1.69
1.70 assertEquals("son", p.getFirstName(), "First name: " + p.getFirstName());
1.71 @@ -218,7 +223,7 @@
1.72 @KOTest
1.73 public void parseConvertToPeopleWithAddressOnArray() throws Exception {
1.74 final BrwsrCtx c = newContext();
1.75 - final InputStream o = createIS(true, true, 1);
1.76 + final InputStream o = createIS(null, true, true, 1, null);
1.77
1.78 Person p = Models.parse(c, Person.class, o);
1.79
1.80 @@ -232,7 +237,7 @@
1.81 @KOTest
1.82 public void parseConvertToPeopleWithoutSexOnArray() throws Exception {
1.83 final BrwsrCtx c = newContext();
1.84 - final InputStream o = createIS(false, false, 1);
1.85 + final InputStream o = createIS(null, false, false, 1, null);
1.86 Person p = Models.parse(c, Person.class, o);
1.87
1.88 assertEquals("son", p.getFirstName(), "First name: " + p.getFirstName());
1.89 @@ -243,7 +248,7 @@
1.90 @KOTest
1.91 public void parseFirstElementFromAbiggerArray() throws Exception {
1.92 final BrwsrCtx c = newContext();
1.93 - final InputStream o = createIS(false, false, 5);
1.94 + final InputStream o = createIS(null, false, false, 5, null);
1.95 Person p = Models.parse(c, Person.class, o);
1.96
1.97 assertEquals("son", p.getFirstName(), "First name: " + p.getFirstName());
1.98 @@ -254,7 +259,7 @@
1.99 @KOTest
1.100 public void parseAllElementFromAbiggerArray() throws Exception {
1.101 final BrwsrCtx c = newContext();
1.102 - final InputStream o = createIS(false, false, 5);
1.103 + final InputStream o = createIS(null, false, false, 5, null);
1.104
1.105 List<Person> res = new ArrayList<Person>();
1.106 Models.parse(c, Person.class, o, res);
1.107 @@ -279,17 +284,10 @@
1.108 }
1.109 private void doParseInnerArray(int array, int expect) throws Exception {
1.110 final BrwsrCtx c = newContext();
1.111 - final InputStream o = createIS(false, false, array);
1.112 - SequenceInputStream is = new SequenceInputStream(
1.113 - new ByteArrayInputStream("{ \"info\" : ".getBytes("UTF-8")),
1.114 - new SequenceInputStream(
1.115 - o,
1.116 - new ByteArrayInputStream("}".getBytes("UTF-8"))
1.117 - )
1.118 - );
1.119 + final InputStream o = createIS("{ \"info\" : ", false, false, array, "}");
1.120
1.121 List<People> res = new ArrayList<People>();
1.122 - Models.parse(c, People.class, is, res);
1.123 + Models.parse(c, People.class, o, res);
1.124
1.125 assertEquals(res.size(), 1, "One people" + res);
1.126
1.127 @@ -307,7 +305,7 @@
1.128 @KOTest
1.129 public void parseOnEmptyArray() throws Exception {
1.130 final BrwsrCtx c = newContext();
1.131 - final InputStream o = createIS(false, false, 0);
1.132 + final InputStream o = createIS(null, false, false, 0, null);
1.133
1.134 try {
1.135 Models.parse(c, Person.class, o);