1
1
package com .fasterxml .jackson .core .util ;
2
2
3
+ import java .nio .charset .StandardCharsets ;
4
+
3
5
import org .junit .Assert ;
4
6
7
+ import com .fasterxml .jackson .core .JsonFactory ;
8
+ import com .fasterxml .jackson .core .JsonGenerator ;
9
+ import com .fasterxml .jackson .core .base .GeneratorBase ;
10
+ import com .fasterxml .jackson .core .io .IOContext ;
11
+
5
12
public class ByteArrayBuilderTest extends com .fasterxml .jackson .core .BaseTest
6
13
{
7
14
public void testSimple () throws Exception
@@ -27,4 +34,37 @@ public void testSimple() throws Exception
27
34
b .release ();
28
35
b .close ();
29
36
}
37
+
38
+ // [core#1195]: Try to verify that BufferRecycler instance is indeed reused
39
+ public void testBufferRecyclerReuse () throws Exception
40
+ {
41
+ JsonFactory f = new JsonFactory ();
42
+ BufferRecycler br = new BufferRecycler ()
43
+ // need to link with some pool
44
+ .withPool (JsonRecyclerPools .newBoundedPool (3 ));
45
+
46
+ ByteArrayBuilder bab = new ByteArrayBuilder (br , 20 );
47
+ assertSame (br , bab .bufferRecycler ());
48
+
49
+ JsonGenerator g = f .createGenerator (bab );
50
+ IOContext ioCtxt = ((GeneratorBase ) g ).ioContext ();
51
+ assertSame (br , ioCtxt .bufferRecycler ());
52
+ assertTrue (ioCtxt .bufferRecycler ().isLinkedWithPool ());
53
+
54
+ g .writeStartArray ();
55
+ g .writeEndArray ();
56
+ g .close ();
57
+
58
+ // Generator.close() should NOT release buffer recycler
59
+ assertTrue (br .isLinkedWithPool ());
60
+
61
+ byte [] result = bab .getClearAndRelease ();
62
+ assertEquals ("[]" , new String (result , StandardCharsets .UTF_8 ));
63
+ // Nor accessing contents
64
+ assertTrue (br .isLinkedWithPool ());
65
+
66
+ // only explicit release does
67
+ br .releaseToPool ();
68
+ assertFalse (br .isLinkedWithPool ());
69
+ }
30
70
}
0 commit comments