|
| 1 | +package com.fasterxml.jackson.core.util; |
| 2 | + |
| 3 | +import java.io.StringWriter; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.*; |
| 6 | + |
| 7 | +// Basic testing for [core#1064] wrt usage by `JsonParser` / `JsonGenerator` |
| 8 | +public class JsonBufferRecyclersTest extends BaseTest |
| 9 | +{ |
| 10 | + // // Parsers with RecyclerPools: |
| 11 | + |
| 12 | + public void testParserWithThreadLocalPool() throws Exception { |
| 13 | + _testParser(JsonRecyclerPools.threadLocalPool()); |
| 14 | + } |
| 15 | + |
| 16 | + public void testParserWithNopLocalPool() throws Exception { |
| 17 | + _testParser(JsonRecyclerPools.nonRecyclingPool()); |
| 18 | + } |
| 19 | + |
| 20 | + public void testParserWithDequeuPool() throws Exception { |
| 21 | + _testParser(JsonRecyclerPools.newConcurrentDequePool()); |
| 22 | + _testParser(JsonRecyclerPools.sharedConcurrentDequePool()); |
| 23 | + } |
| 24 | + |
| 25 | + public void testParserWithLockFreePool() throws Exception { |
| 26 | + _testParser(JsonRecyclerPools.newLockFreePool()); |
| 27 | + _testParser(JsonRecyclerPools.sharedLockFreePool()); |
| 28 | + } |
| 29 | + |
| 30 | + public void testParserWithBoundedPool() throws Exception { |
| 31 | + _testParser(JsonRecyclerPools.newBoundedPool(5)); |
| 32 | + _testParser(JsonRecyclerPools.sharedBoundedPool()); |
| 33 | + } |
| 34 | + |
| 35 | + private void _testParser(RecyclerPool<BufferRecycler> pool) throws Exception |
| 36 | + { |
| 37 | + JsonFactory jsonF = JsonFactory.builder() |
| 38 | + .recyclerPool(pool) |
| 39 | + .build(); |
| 40 | + |
| 41 | + JsonParser p = jsonF.createParser(a2q("{'a':123,'b':'foobar'}")); |
| 42 | + |
| 43 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 44 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 45 | + assertEquals("a", p.currentName()); |
| 46 | + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); |
| 47 | + assertEquals(123, p.getIntValue()); |
| 48 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 49 | + assertEquals("b", p.currentName()); |
| 50 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 51 | + assertEquals("foobar", p.getText()); |
| 52 | + assertToken(JsonToken.END_OBJECT, p.nextToken()); |
| 53 | + |
| 54 | + p.close(); |
| 55 | + } |
| 56 | + |
| 57 | + // // Generators with RecyclerPools: |
| 58 | + |
| 59 | + public void testGeneratorWithThreadLocalPool() throws Exception { |
| 60 | + _testGenerator(JsonRecyclerPools.threadLocalPool()); |
| 61 | + } |
| 62 | + |
| 63 | + public void testGeneratorWithNopLocalPool() throws Exception { |
| 64 | + _testGenerator(JsonRecyclerPools.nonRecyclingPool()); |
| 65 | + } |
| 66 | + |
| 67 | + public void testGeneratorWithDequeuPool() throws Exception { |
| 68 | + _testGenerator(JsonRecyclerPools.newConcurrentDequePool()); |
| 69 | + _testGenerator(JsonRecyclerPools.sharedConcurrentDequePool()); |
| 70 | + } |
| 71 | + |
| 72 | + public void testGeneratorWithLockFreePool() throws Exception { |
| 73 | + _testGenerator(JsonRecyclerPools.newLockFreePool()); |
| 74 | + _testGenerator(JsonRecyclerPools.sharedLockFreePool()); |
| 75 | + } |
| 76 | + |
| 77 | + public void testGeneratorWithBoundedPool() throws Exception { |
| 78 | + _testGenerator(JsonRecyclerPools.newBoundedPool(5)); |
| 79 | + _testGenerator(JsonRecyclerPools.sharedBoundedPool()); |
| 80 | + } |
| 81 | + |
| 82 | + private void _testGenerator(RecyclerPool<BufferRecycler> pool) throws Exception |
| 83 | + { |
| 84 | + JsonFactory jsonF = JsonFactory.builder() |
| 85 | + .recyclerPool(pool) |
| 86 | + .build(); |
| 87 | + |
| 88 | + StringWriter w = new StringWriter(); |
| 89 | + JsonGenerator g = jsonF.createGenerator(w); |
| 90 | + |
| 91 | + g.writeStartObject(); |
| 92 | + g.writeNumberField("a", -42); |
| 93 | + g.writeStringField("b", "barfoo"); |
| 94 | + g.writeEndObject(); |
| 95 | + |
| 96 | + g.close(); |
| 97 | + |
| 98 | + assertEquals(a2q("{'a':-42,'b':'barfoo'}"), w.toString()); |
| 99 | + } |
| 100 | +} |
0 commit comments