|
| 1 | +package com.fasterxml.jackson.dataformat.cbor.gen; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.*; |
| 4 | +import com.fasterxml.jackson.dataformat.cbor.CBORTestBase; |
| 5 | +import com.fasterxml.jackson.dataformat.cbor.CBORFactory; |
| 6 | +import com.fasterxml.jackson.dataformat.cbor.testutil.ByteOutputStreamForTesting; |
| 7 | + |
| 8 | +/** |
| 9 | + * Set of basic unit tests that verify aspect of closing a |
| 10 | + * {@link JsonGenerator} instance. This includes both closing |
| 11 | + * of physical resources (target), and logical content |
| 12 | + * (json content tree) |
| 13 | + *<p> |
| 14 | + * Specifically, features |
| 15 | + * <code>JsonGenerator.Feature#AUTO_CLOSE_TARGET</code> |
| 16 | + * and |
| 17 | + * <code>JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT</code> |
| 18 | + * are tested. |
| 19 | + */ |
| 20 | +public class TestGeneratorClosing extends CBORTestBase |
| 21 | +{ |
| 22 | + /* |
| 23 | + /********************************************************** |
| 24 | + /* Unit tests |
| 25 | + /********************************************************** |
| 26 | + */ |
| 27 | + |
| 28 | + /** |
| 29 | + * This unit test checks the default behaviour; with no auto-close, no |
| 30 | + * automatic closing should occur, nor explicit one unless specific |
| 31 | + * forcing method is used. |
| 32 | + */ |
| 33 | + public void testNoAutoCloseGenerator() throws Exception |
| 34 | + { |
| 35 | + JsonFactory f = cborFactory(); |
| 36 | + |
| 37 | + // Check the default settings |
| 38 | + assertTrue(f.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)); |
| 39 | + // then change |
| 40 | + f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); |
| 41 | + assertFalse(f.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)); |
| 42 | + |
| 43 | + try (final ByteOutputStreamForTesting output = new ByteOutputStreamForTesting()) { |
| 44 | + JsonGenerator g = f.createGenerator(output); |
| 45 | + |
| 46 | + // shouldn't be closed to begin with... |
| 47 | + assertFalse(output.isClosed()); |
| 48 | + g.writeNumber(39); |
| 49 | + // regular close won't close it either: |
| 50 | + g.close(); |
| 51 | + assertFalse(output.isClosed()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void testCloseGenerator() throws Exception |
| 56 | + { |
| 57 | + JsonFactory f = cborFactory(); |
| 58 | + f.enable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); |
| 59 | + @SuppressWarnings("resource") |
| 60 | + ByteOutputStreamForTesting output = new ByteOutputStreamForTesting(); |
| 61 | + JsonGenerator g = f.createGenerator(output); |
| 62 | + |
| 63 | + // shouldn't be closed to begin with... |
| 64 | + assertFalse(output.isClosed()); |
| 65 | + g.writeNumber(39); |
| 66 | + // but close() should now close the writer |
| 67 | + g.close(); |
| 68 | + assertTrue(output.isClosed()); |
| 69 | + } |
| 70 | + |
| 71 | + public void testNoAutoCloseOutputStream() throws Exception |
| 72 | + { |
| 73 | + JsonFactory f = cborFactory(); |
| 74 | + f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); |
| 75 | + @SuppressWarnings("resource") |
| 76 | + ByteOutputStreamForTesting output = new ByteOutputStreamForTesting(); |
| 77 | + JsonGenerator g = f.createGenerator(output, JsonEncoding.UTF8); |
| 78 | + |
| 79 | + assertFalse(output.isClosed()); |
| 80 | + g.writeNumber(39); |
| 81 | + g.close(); |
| 82 | + assertFalse(output.isClosed()); |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("resource") |
| 86 | + public void testAutoFlushOrNot() throws Exception |
| 87 | + { |
| 88 | + JsonFactory f = cborFactory(); |
| 89 | + |
| 90 | + ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting(); |
| 91 | + JsonGenerator g = f.createGenerator(bytes, JsonEncoding.UTF8); |
| 92 | + g.writeStartArray(); |
| 93 | + g.writeEndArray(); |
| 94 | + assertEquals(0, bytes.flushCount); |
| 95 | + g.flush(); |
| 96 | + assertEquals(1, bytes.flushCount); |
| 97 | + final int EXP_LENGTH = 2; |
| 98 | + assertEquals(EXP_LENGTH, bytes.toByteArray().length); |
| 99 | + g.close(); |
| 100 | + |
| 101 | + // then disable and we should not see flushing again... |
| 102 | + f = newFactoryBuilder() |
| 103 | + .disable(StreamWriteFeature.FLUSH_PASSED_TO_STREAM) |
| 104 | + // also need to disable this, to prevent implicit flush() on close() |
| 105 | + .disable(StreamWriteFeature.AUTO_CLOSE_TARGET) |
| 106 | + .build(); |
| 107 | + |
| 108 | + // and then with OutputStream |
| 109 | + bytes = new ByteOutputStreamForTesting(); |
| 110 | + g = f.createGenerator(bytes, JsonEncoding.UTF8); |
| 111 | + g.writeStartArray(); |
| 112 | + g.writeEndArray(); |
| 113 | + assertEquals(0, bytes.flushCount); |
| 114 | + g.flush(); |
| 115 | + assertEquals(0, bytes.flushCount); |
| 116 | + // and, as long as we won't be auto-closing, still no flush |
| 117 | + g.close(); |
| 118 | + assertEquals(0, bytes.flushCount); |
| 119 | + // and only direct `close()` will do it |
| 120 | + bytes.close(); |
| 121 | + assertEquals(EXP_LENGTH, bytes.toByteArray().length); |
| 122 | + } |
| 123 | + |
| 124 | + private TSFBuilder<?, ?> newFactoryBuilder() { |
| 125 | + return CBORFactory.builder(); |
| 126 | + } |
| 127 | +} |
0 commit comments