| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.util.*; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.core.*; |
| 7 |
|
| 8 |
import com.fasterxml.jackson.dataformat.smile.SmileFactory; |
| 9 |
|
| 10 |
/* Test based on kimchy's issue (see https://gist.github.com/853232); |
| 11 |
* exhibits an issue with buffer recycling. |
| 12 |
*/ |
| 13 |
public class TestGeneratorBufferRecycle extends SmileTestBase |
| 14 |
{ |
| 15 |
public void testMaps() throws Exception |
| 16 |
{ |
| 17 |
SmileFactory factory = new SmileFactory(); |
| 18 |
|
| 19 |
Map<?,?> props1 = buildMap("", 65); |
| 20 |
Map<?,?> props2 = buildMap("", 1); |
| 21 |
|
| 22 |
writeMapAndParse(factory, props1); |
| 23 |
writeMapAndParse(factory, props2); |
| 24 |
writeMapAndParse(factory, props1); |
| 25 |
writeMapAndParse(factory, props2); |
| 26 |
} |
| 27 |
|
| 28 |
/* |
| 29 |
/********************************************************** |
| 30 |
/* Helper methods |
| 31 |
/********************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
private static void writeMapAndParse(SmileFactory factory, Map<?,?> map) throws Exception { |
| 35 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 36 |
|
| 37 |
// generate |
| 38 |
JsonGenerator generator = factory.createGenerator(os); |
| 39 |
writeMap(generator, map); |
| 40 |
generator.close(); |
| 41 |
|
| 42 |
// parse |
| 43 |
JsonParser parser = factory.createParser(os.toByteArray()); |
| 44 |
while (parser.nextToken() != null) { |
| 45 |
|
| 46 |
} |
| 47 |
parser.close(); |
| 48 |
} |
| 49 |
|
| 50 |
private static Map<?,?> buildMap(String prefix, int size) { |
| 51 |
HashMap<String,String> props = new HashMap<String, String>(); |
| 52 |
for (int it = 0; it < size; it++) { |
| 53 |
String key = prefix + "prop_" + it; |
| 54 |
props.put(key, "a"); |
| 55 |
} |
| 56 |
return props; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
// A sample utility to write a map |
| 61 |
|
| 62 |
public static void writeMap(JsonGenerator gen, Map<?,?> map) throws IOException { |
| 63 |
gen.writeStartObject(); |
| 64 |
|
| 65 |
for (Map.Entry<?,?> entry : map.entrySet()) { |
| 66 |
gen.writeFieldName((String) entry.getKey()); |
| 67 |
Object value = entry.getValue(); |
| 68 |
if (value == null) { |
| 69 |
gen.writeNull(); |
| 70 |
} else { |
| 71 |
gen.writeString(value.toString()); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
gen.writeEndObject(); |
| 76 |
} |
| 77 |
} |