Skip to content

Commit 0844691

Browse files
committed
add a test for #177
1 parent 4792735 commit 0844691

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/test/java/com/fasterxml/jackson/core/json/TestJsonGenerator.java

+42-9
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@ public void testDupFieldNameWrites() throws Exception
234234
_testDupFieldNameWrites(f, true);
235235
}
236236

237+
// [core#177]
238+
// Also: should not try writing JSON String if field name expected
239+
// (in future maybe take one as alias... but not yet)
240+
/*
241+
public void testFailOnWritingStringNotFieldName() throws Exception
242+
{
243+
JsonFactory f = new JsonFactory();
244+
_testFailOnWritingStringNotFieldName(f, false);
245+
_testFailOnWritingStringNotFieldName(f, true);
246+
}
247+
*/
248+
249+
/*
250+
/**********************************************************
251+
/* Internal methods
252+
/**********************************************************
253+
*/
254+
237255
private void _testDupFieldNameWrites(JsonFactory f, boolean useReader) throws Exception
238256
{
239257
JsonGenerator gen;
@@ -250,21 +268,36 @@ private void _testDupFieldNameWrites(JsonFactory f, boolean useReader) throws Ex
250268
gen.writeFieldName("b");
251269
gen.flush();
252270
String json = bout.toString("UTF-8");
253-
fail("Should not have let two consequtive field name writes succeed: output = "+json);
271+
fail("Should not have let two consecutive field name writes succeed: output = "+json);
254272
} catch (JsonProcessingException e) {
255273
verifyException(e, "can not write a field name, expecting a value");
256274
}
257275
gen.close();
258276
}
259277

260-
/*
261-
/**********************************************************
262-
/* Internal methods
263-
/**********************************************************
264-
*/
265-
266-
private void doTestIntWrite(boolean pad)
267-
throws Exception
278+
private void _testFailOnWritingStringNotFieldName(JsonFactory f, boolean useReader) throws Exception
279+
{
280+
JsonGenerator gen;
281+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
282+
if (useReader) {
283+
gen = f.createGenerator(new OutputStreamWriter(bout, "UTF-8"));
284+
} else {
285+
gen = f.createGenerator(bout, JsonEncoding.UTF8);
286+
}
287+
gen.writeStartObject();
288+
289+
try {
290+
gen.writeString("a");
291+
gen.flush();
292+
String json = bout.toString("UTF-8");
293+
fail("Should not have let 'writeString()' be used in place of 'writeFieldName()': output = "+json);
294+
} catch (JsonProcessingException e) {
295+
verifyException(e, "can not write a field name, expecting a value");
296+
}
297+
gen.close();
298+
}
299+
300+
private void doTestIntWrite(boolean pad) throws Exception
268301
{
269302
int[] VALUES = new int[] {
270303
0, 1, -9, 32, -32, 57, 189, 2017, -9999, 13240, 123456,

0 commit comments

Comments
 (0)