| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertArrayEquals; |
| 4 |
|
| 5 |
import java.io.*; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.List; |
| 8 |
import java.util.Random; |
| 9 |
|
| 10 |
import com.fasterxml.jackson.core.*; |
| 11 |
|
| 12 |
import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 13 |
import com.fasterxml.jackson.dataformat.smile.SmileGenerator; |
| 14 |
import com.fasterxml.jackson.dataformat.smile.SmileParser; |
| 15 |
|
| 16 |
/** |
| 17 |
* Test to verify handling of "raw String value" write methods that by-pass |
| 18 |
* most encoding steps, for potential higher output speed (in cases where |
| 19 |
* input naturally comes as UTF-8 encoded byte arrays). |
| 20 |
* |
| 21 |
* @since 1.7 |
| 22 |
*/ |
| 23 |
public class TestGeneratorWithRawUtf8 extends SmileTestBase |
| 24 |
{ |
| 25 |
public void testUtf8RawStrings() throws Exception |
| 26 |
{ |
| 27 |
// Let's create set of Strings to output; no ctrl chars as we do raw |
| 28 |
List<byte[]> strings = generateStrings(new Random(28), 750000, false); |
| 29 |
ByteArrayOutputStream out = new ByteArrayOutputStream(16000); |
| 30 |
SmileFactory jf = new SmileFactory(); |
| 31 |
JsonGenerator jgen = jf.createGenerator(out, JsonEncoding.UTF8); |
| 32 |
jgen.writeStartArray(); |
| 33 |
for (byte[] str : strings) { |
| 34 |
jgen.writeRawUTF8String(str, 0, str.length); |
| 35 |
} |
| 36 |
jgen.writeEndArray(); |
| 37 |
jgen.close(); |
| 38 |
byte[] json = out.toByteArray(); |
| 39 |
|
| 40 |
// Ok: let's verify that stuff was written out ok |
| 41 |
JsonParser jp = jf.createParser(json); |
| 42 |
assertToken(JsonToken.START_ARRAY, jp.nextToken()); |
| 43 |
for (byte[] inputBytes : strings) { |
| 44 |
assertToken(JsonToken.VALUE_STRING, jp.nextToken()); |
| 45 |
String string = jp.getText(); |
| 46 |
byte[] outputBytes = string.getBytes("UTF-8"); |
| 47 |
assertEquals(inputBytes.length, outputBytes.length); |
| 48 |
assertArrayEquals(inputBytes, outputBytes); |
| 49 |
} |
| 50 |
assertToken(JsonToken.END_ARRAY, jp.nextToken()); |
| 51 |
jp.close(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Unit test for "JsonGenerator.writeUTF8String()", which needs |
| 56 |
* to handle escaping properly |
| 57 |
*/ |
| 58 |
public void testUtf8StringsWithEscaping() throws Exception |
| 59 |
{ |
| 60 |
// Let's create set of Strings to output; do include control chars too: |
| 61 |
List<byte[]> strings = generateStrings(new Random(28), 720000, true); |
| 62 |
ByteArrayOutputStream out = new ByteArrayOutputStream(16000); |
| 63 |
SmileFactory jf = new SmileFactory(); |
| 64 |
JsonGenerator jgen = jf.createGenerator(out, JsonEncoding.UTF8); |
| 65 |
jgen.writeStartArray(); |
| 66 |
for (byte[] str : strings) { |
| 67 |
jgen.writeUTF8String(str, 0, str.length); |
| 68 |
} |
| 69 |
jgen.writeEndArray(); |
| 70 |
jgen.close(); |
| 71 |
byte[] json = out.toByteArray(); |
| 72 |
|
| 73 |
// Ok: let's verify that stuff was written out ok |
| 74 |
JsonParser jp = jf.createParser(json); |
| 75 |
assertToken(JsonToken.START_ARRAY, jp.nextToken()); |
| 76 |
for (byte[] inputBytes : strings) { |
| 77 |
assertToken(JsonToken.VALUE_STRING, jp.nextToken()); |
| 78 |
String string = jp.getText(); |
| 79 |
byte[] outputBytes = string.getBytes("UTF-8"); |
| 80 |
assertEquals(inputBytes.length, outputBytes.length); |
| 81 |
assertArrayEquals(inputBytes, outputBytes); |
| 82 |
} |
| 83 |
assertToken(JsonToken.END_ARRAY, jp.nextToken()); |
| 84 |
jp.close(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Test to point out an issue with "raw" UTF-8 encoding |
| 89 |
* |
| 90 |
* @author David Yu |
| 91 |
*/ |
| 92 |
public void testIssue492() throws Exception |
| 93 |
{ |
| 94 |
doTestIssue492(false); |
| 95 |
doTestIssue492(true); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
/********************************************************** |
| 100 |
/* Helper methods |
| 101 |
/********************************************************** |
| 102 |
*/ |
| 103 |
|
| 104 |
private void doTestIssue492(boolean asUtf8String) throws Exception |
| 105 |
{ |
| 106 |
SmileFactory factory = new SmileFactory(); |
| 107 |
|
| 108 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 109 |
SmileGenerator generator = factory.createGenerator(out); |
| 110 |
|
| 111 |
generator.writeStartObject(); |
| 112 |
|
| 113 |
generator.writeFieldName("name"); |
| 114 |
|
| 115 |
if(asUtf8String) |
| 116 |
{ |
| 117 |
byte[] text = "PojoFoo".getBytes("ASCII"); |
| 118 |
generator.writeUTF8String(text, 0, text.length); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
generator.writeString("PojoFoo"); |
| 123 |
} |
| 124 |
|
| 125 |
generator.writeFieldName("collection"); |
| 126 |
|
| 127 |
generator.writeStartObject(); |
| 128 |
|
| 129 |
generator.writeFieldName("v"); |
| 130 |
|
| 131 |
generator.writeStartArray(); |
| 132 |
|
| 133 |
if(asUtf8String) |
| 134 |
{ |
| 135 |
byte[] text = "1".getBytes("ASCII"); |
| 136 |
generator.writeUTF8String(text, 0, text.length); |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
generator.writeString("1"); |
| 141 |
} |
| 142 |
|
| 143 |
generator.writeEndArray(); |
| 144 |
|
| 145 |
generator.writeEndObject(); |
| 146 |
|
| 147 |
generator.writeEndObject(); |
| 148 |
|
| 149 |
generator.close(); |
| 150 |
|
| 151 |
byte[] data = out.toByteArray(); |
| 152 |
|
| 153 |
ByteArrayInputStream in = new ByteArrayInputStream(data); |
| 154 |
SmileParser parser = factory.createParser(in); |
| 155 |
|
| 156 |
assertToken(parser.nextToken(), JsonToken.START_OBJECT); |
| 157 |
|
| 158 |
assertToken(parser.nextToken(), JsonToken.FIELD_NAME); |
| 159 |
assertEquals(parser.getCurrentName(), "name"); |
| 160 |
assertToken(parser.nextToken(), JsonToken.VALUE_STRING); |
| 161 |
assertEquals(parser.getText(), "PojoFoo"); |
| 162 |
|
| 163 |
assertToken(parser.nextToken(), JsonToken.FIELD_NAME); |
| 164 |
assertEquals(parser.getCurrentName(), "collection"); |
| 165 |
assertToken(parser.nextToken(), JsonToken.START_OBJECT); |
| 166 |
|
| 167 |
assertToken(parser.nextToken(), JsonToken.FIELD_NAME); |
| 168 |
assertEquals("Should have property with name 'v'", parser.getCurrentName(), "v"); |
| 169 |
assertToken(parser.nextToken(), JsonToken.START_ARRAY); |
| 170 |
|
| 171 |
assertToken(parser.nextToken(), JsonToken.VALUE_STRING); |
| 172 |
assertEquals("Should get String value '1'", parser.getText(), "1"); |
| 173 |
|
| 174 |
assertToken(parser.nextToken(), JsonToken.END_ARRAY); |
| 175 |
assertToken(parser.nextToken(), JsonToken.END_OBJECT); |
| 176 |
|
| 177 |
|
| 178 |
assertToken(parser.nextToken(), JsonToken.END_OBJECT); |
| 179 |
parser.close(); |
| 180 |
} |
| 181 |
|
| 182 |
private List<byte[]> generateStrings(Random rnd, int totalLength, boolean includeCtrlChars) |
| 183 |
throws IOException |
| 184 |
{ |
| 185 |
ArrayList<byte[]> strings = new ArrayList<byte[]>(); |
| 186 |
do { |
| 187 |
int len = 2; |
| 188 |
int bits = rnd.nextInt(14); |
| 189 |
while (--bits >= 0) { |
| 190 |
len += len; |
| 191 |
} |
| 192 |
len = 1 + ((len + len) / 3); |
| 193 |
String str = generateString(rnd, len, includeCtrlChars); |
| 194 |
byte[] bytes = str.getBytes("UTF-8"); |
| 195 |
strings.add(bytes); |
| 196 |
totalLength -= bytes.length; |
| 197 |
} while (totalLength > 0); |
| 198 |
return strings; |
| 199 |
} |
| 200 |
|
| 201 |
private String generateString(Random rnd, int length, boolean includeCtrlChars) |
| 202 |
{ |
| 203 |
StringBuilder sb = new StringBuilder(length); |
| 204 |
do { |
| 205 |
int i; |
| 206 |
switch (rnd.nextInt(3)) { |
| 207 |
case 0: // 3 byte one |
| 208 |
i = 2048 + rnd.nextInt(16383); |
| 209 |
break; |
| 210 |
case 1: // 2 byte |
| 211 |
i = 128 + rnd.nextInt(1024); |
| 212 |
break; |
| 213 |
default: // ASCII |
| 214 |
i = rnd.nextInt(192); |
| 215 |
if (!includeCtrlChars) { |
| 216 |
i += 32; |
| 217 |
// but also need to avoid backslash, double-quote |
| 218 |
if (i == '\\' || i == '"') { |
| 219 |
i = '@'; // just arbitrary choice |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
sb.append((char) i); |
| 224 |
} while (sb.length() < length); |
| 225 |
return sb.toString(); |
| 226 |
} |
| 227 |
} |