Skip to content

Commit 30f1a60

Browse files
jbescoslukasj
authored andcommitted
TCK tests
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
1 parent 2808816 commit 30f1a60

File tree

1 file changed

+46
-1
lines changed
  • tck/tck-tests/src/main/java/jakarta/jsonp/tck/api/jsongeneratortests

1 file changed

+46
-1
lines changed

tck/tck-tests/src/main/java/jakarta/jsonp/tck/api/jsongeneratortests/ClientTests.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -2139,4 +2139,49 @@ public void jsonGeneratorDocumentRootTest() throws Fault {
21392139
result.eval();
21402140
}
21412141

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+
}
21422187
}

0 commit comments

Comments
 (0)