|
| 1 | +package com.fasterxml.jackson.databind.util; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.*; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.util.BufferRecycler; |
| 10 | +import com.fasterxml.jackson.core.util.JsonRecyclerPools; |
| 11 | +import com.fasterxml.jackson.core.util.RecyclerPool; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.*; |
| 14 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 15 | + |
| 16 | +// For [databind#4321]: basic test |
| 17 | +public class BufferRecyclersDatabindTest |
| 18 | + extends BaseMapTest |
| 19 | +{ |
| 20 | + @JsonPropertyOrder({ "a", "b" }) |
| 21 | + static class Pojo4321 { |
| 22 | + public int a; |
| 23 | + public String b; |
| 24 | + |
| 25 | + public Pojo4321(int a, String b) { |
| 26 | + this.a = a; |
| 27 | + this.b = b; |
| 28 | + } |
| 29 | + protected Pojo4321() { } |
| 30 | + } |
| 31 | + |
| 32 | + // // Parsers with RecyclerPools: |
| 33 | + |
| 34 | + public void testParserWithThreadLocalPool() throws Exception { |
| 35 | + _testParser(JsonRecyclerPools.threadLocalPool()); |
| 36 | + } |
| 37 | + |
| 38 | + public void testParserWithNopLocalPool() throws Exception { |
| 39 | + _testParser(JsonRecyclerPools.nonRecyclingPool()); |
| 40 | + } |
| 41 | + |
| 42 | + public void testParserWithDequeuPool() throws Exception { |
| 43 | + _testParser(JsonRecyclerPools.newConcurrentDequePool()); |
| 44 | + _testParser(JsonRecyclerPools.sharedConcurrentDequePool()); |
| 45 | + } |
| 46 | + |
| 47 | + public void testParserWithLockFreePool() throws Exception { |
| 48 | + _testParser(JsonRecyclerPools.newLockFreePool()); |
| 49 | + _testParser(JsonRecyclerPools.sharedLockFreePool()); |
| 50 | + } |
| 51 | + |
| 52 | + public void testParserWithBoundedPool() throws Exception { |
| 53 | + _testParser(JsonRecyclerPools.newBoundedPool(5)); |
| 54 | + _testParser(JsonRecyclerPools.sharedBoundedPool()); |
| 55 | + } |
| 56 | + |
| 57 | + private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception |
| 58 | + { |
| 59 | + ObjectMapper mapper = JsonMapper.builder( |
| 60 | + JsonFactory.builder() |
| 61 | + .recyclerPool(pool) |
| 62 | + .build()).build(); |
| 63 | + final String DOC = "{\"a\":123,\"b\":\"foobar\"}"; |
| 64 | + |
| 65 | + // Let's first test using char-backed parser |
| 66 | + Pojo4321 value = mapper.readerFor(Pojo4321.class) |
| 67 | + .readValue(DOC); |
| 68 | + assertEquals(123, value.a); |
| 69 | + assertEquals("foobar", value.b); |
| 70 | + |
| 71 | + // and then byte-backed parser |
| 72 | + value = mapper.readerFor(Pojo4321.class) |
| 73 | + .readValue(utf8Bytes(DOC)); |
| 74 | + assertEquals(123, value.a); |
| 75 | + assertEquals("foobar", value.b); |
| 76 | + } |
| 77 | + |
| 78 | + // // Generators with RecyclerPools: |
| 79 | + |
| 80 | + public void testGeneratorWithThreadLocalPool() throws Exception { |
| 81 | + _testGenerator(JsonRecyclerPools.threadLocalPool()); |
| 82 | + } |
| 83 | + |
| 84 | + public void testGeneratorWithNopLocalPool() throws Exception { |
| 85 | + _testGenerator(JsonRecyclerPools.nonRecyclingPool()); |
| 86 | + } |
| 87 | + |
| 88 | + public void testGeneratorWithDequeuPool() throws Exception { |
| 89 | + _testGenerator(JsonRecyclerPools.newConcurrentDequePool()); |
| 90 | + _testGenerator(JsonRecyclerPools.sharedConcurrentDequePool()); |
| 91 | + } |
| 92 | + |
| 93 | + public void testGeneratorWithLockFreePool() throws Exception { |
| 94 | + _testGenerator(JsonRecyclerPools.newLockFreePool()); |
| 95 | + _testGenerator(JsonRecyclerPools.sharedLockFreePool()); |
| 96 | + } |
| 97 | + |
| 98 | + public void testGeneratorWithBoundedPool() throws Exception { |
| 99 | + _testGenerator(JsonRecyclerPools.newBoundedPool(5)); |
| 100 | + _testGenerator(JsonRecyclerPools.sharedBoundedPool()); |
| 101 | + } |
| 102 | + |
| 103 | + private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception |
| 104 | + { |
| 105 | + ObjectMapper mapper = JsonMapper.builder( |
| 106 | + JsonFactory.builder() |
| 107 | + .recyclerPool(pool) |
| 108 | + .build()).build(); |
| 109 | + final String EXP = "{\"a\":-42,\"b\":\"bogus\"}"; |
| 110 | + |
| 111 | + // First write as String |
| 112 | + assertEquals(EXP, mapper.writeValueAsString(new Pojo4321(-42, "bogus"))); |
| 113 | + |
| 114 | + // and then as bytes |
| 115 | + assertEquals(EXP, new String(mapper.writeValueAsBytes(new Pojo4321(-42, "bogus")), |
| 116 | + StandardCharsets.UTF_8)); |
| 117 | + } |
| 118 | +} |
0 commit comments