|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials are made available under the
|
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at
|
@@ -2139,4 +2139,49 @@ public void jsonGeneratorDocumentRootTest() throws Fault {
|
2139 | 2139 | result.eval();
|
2140 | 2140 | }
|
2141 | 2141 |
|
| 2142 | + /* |
| 2143 | + * @testName: jsonGeneratorStreamNotClosedTest |
| 2144 | + */ |
| 2145 | + @Test |
| 2146 | + public void jsonGeneratorStreamNotClosedTest() throws Fault { |
| 2147 | + ByteArrayOutputStreamCloseChecker stream = new ByteArrayOutputStreamCloseChecker(); |
| 2148 | + JsonGenerator gen = Json.createGenerator(stream); |
| 2149 | + try { |
| 2150 | + gen.writeStartObject(); |
| 2151 | + gen.write("foo", "bar"); |
| 2152 | + // no end object |
| 2153 | + gen.close(); |
| 2154 | + throw new Fault("It is expected a JsonGenerationException"); |
| 2155 | + } catch (JsonGenerationException e) { |
| 2156 | + if (stream.closed) { |
| 2157 | + // Stream should not be closed |
| 2158 | + throw new Fault("The underlying stream is closed but it shouldn't because JSON object was not completed"); |
| 2159 | + } |
| 2160 | + } |
| 2161 | + } |
| 2162 | + |
| 2163 | + /* |
| 2164 | + * @testName: jsonGeneratorStreamClosedTest |
| 2165 | + */ |
| 2166 | + @Test |
| 2167 | + public void jsonGeneratorStreamClosedTest() throws Fault { |
| 2168 | + ByteArrayOutputStreamCloseChecker stream = new ByteArrayOutputStreamCloseChecker(); |
| 2169 | + JsonGenerator gen = Json.createGenerator(stream); |
| 2170 | + gen.writeStartObject(); |
| 2171 | + gen.write("foo", "bar"); |
| 2172 | + gen.writeEnd(); |
| 2173 | + gen.close(); |
| 2174 | + if (!stream.closed) { |
| 2175 | + // Stream should be closed |
| 2176 | + throw new Fault("The underlying stream has to be closed because JSON object was completed"); |
| 2177 | + } |
| 2178 | + } |
| 2179 | + |
| 2180 | + private static class ByteArrayOutputStreamCloseChecker extends ByteArrayOutputStream { |
| 2181 | + private boolean closed = false; |
| 2182 | + @Override |
| 2183 | + public void close() throws IOException { |
| 2184 | + closed = true; |
| 2185 | + } |
| 2186 | + } |
2142 | 2187 | }
|
0 commit comments