Skip to content

Commit 04dbb30

Browse files
committed
Merge pull request msgpack#243 from msgpack/issue_230
Issue 230
2 parents f0c76cd + 1db7c3e commit 04dbb30

4 files changed

Lines changed: 165 additions & 27 deletions

File tree

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,16 @@ else if (rootStackItem instanceof StackItemForArray) {
350350
throw new IllegalStateException("Unexpected rootStackItem: " + rootStackItem);
351351
}
352352
rootStackItem = null;
353-
MessagePacker messagePacker = getMessagePacker();
354-
messagePacker.flush();
353+
flushMessagePacker();
355354
}
356355
}
357356

357+
private void flushMessagePacker() throws IOException
358+
{
359+
MessagePacker messagePacker = getMessagePacker();
360+
messagePacker.flush();
361+
}
362+
358363
@Override
359364
protected void _releaseBuffers() {
360365

@@ -395,11 +400,19 @@ private void addKeyToStackTop(String key) {
395400
getStackTop().addKey(key);
396401
}
397402

398-
private void addValueToStackTop(Object value) {
399-
getStackTop().addValue(value);
403+
private void addValueToStackTop(Object value) throws IOException
404+
{
405+
if (stack.isEmpty()) {
406+
packValue(value);
407+
flushMessagePacker();
408+
}
409+
else {
410+
getStackTop().addValue(value);
411+
}
400412
}
401413

402-
private void popStackAndStoreTheItemAsValue() {
414+
private void popStackAndStoreTheItemAsValue() throws IOException
415+
{
403416
StackItem child = stack.pop();
404417
if (stack.size() > 0) {
405418
addValueToStackTop(child);

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
import java.io.ByteArrayInputStream;
1313
import java.io.ByteArrayOutputStream;
14+
import java.io.File;
15+
import java.io.FileOutputStream;
1416
import java.io.IOException;
17+
import java.io.OutputStream;
1518
import java.math.BigInteger;
1619
import java.util.ArrayList;
1720
import java.util.Arrays;
@@ -175,4 +178,28 @@ public static class ChangingPropertyNamesPojo {
175178
public void setTheName(String n) { _name = n; }
176179
}
177180

181+
protected interface FileSetup {
182+
void setup(File f) throws Exception;
183+
}
184+
185+
protected File createTempFile() throws Exception
186+
{
187+
return createTempFile(null);
188+
}
189+
190+
protected File createTempFile(FileSetup fileSetup) throws Exception
191+
{
192+
File tempFile = File.createTempFile("test", "msgpack");
193+
tempFile.deleteOnExit();
194+
if (fileSetup != null) {
195+
fileSetup.setup(tempFile);
196+
}
197+
return tempFile;
198+
}
199+
200+
protected OutputStream createTempFileOutputStream() throws IOException {
201+
File tempFile = File.createTempFile("test", "msgpack");
202+
tempFile.deleteOnExit();
203+
return new FileOutputStream(tempFile);
204+
}
178205
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.FileInputStream;
2828
import java.io.FileOutputStream;
2929
import java.io.IOException;
30+
import java.io.OutputStream;
3031
import java.math.BigDecimal;
3132
import java.util.*;
3233

@@ -197,10 +198,9 @@ else if (key.equals("num")) {
197198
}
198199

199200
@Test
200-
public void testMessagePackGeneratorDirectly() throws IOException {
201+
public void testMessagePackGeneratorDirectly() throws Exception {
201202
MessagePackFactory messagePackFactory = new MessagePackFactory();
202-
File tempFile = File.createTempFile("msgpackTest", "msgpack");
203-
tempFile.deleteOnExit();
203+
File tempFile = createTempFile();
204204

205205
JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8);
206206
assertTrue(generator instanceof MessagePackGenerator);
@@ -222,6 +222,27 @@ public void testMessagePackGeneratorDirectly() throws IOException {
222222
assertFalse(unpacker.hasNext());
223223
}
224224

225+
@Test
226+
public void testWritePrimitives() throws Exception {
227+
MessagePackFactory messagePackFactory = new MessagePackFactory();
228+
File tempFile = createTempFile();
229+
230+
JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8);
231+
assertTrue(generator instanceof MessagePackGenerator);
232+
generator.writeNumber(0);
233+
generator.writeString("one");
234+
generator.writeNumber(2.0f);
235+
generator.flush();
236+
generator.close();
237+
238+
FileInputStream fileInputStream = new FileInputStream(tempFile);
239+
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(fileInputStream);
240+
assertEquals(0, unpacker.unpackInt());
241+
assertEquals("one", unpacker.unpackString());
242+
assertEquals(2.0f, unpacker.unpackFloat(), 0.001f);
243+
assertFalse(unpacker.hasNext());
244+
}
245+
225246
@Test
226247
public void testBigDecimal() throws IOException {
227248
ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
@@ -267,9 +288,7 @@ public void testBigDecimal() throws IOException {
267288

268289
@Test(expected = IOException.class)
269290
public void testEnableFeatureAutoCloseTarget() throws IOException {
270-
File tempFile = File.createTempFile("test", "msgpack");
271-
tempFile.deleteOnExit();
272-
FileOutputStream out = new FileOutputStream(tempFile);
291+
OutputStream out = createTempFileOutputStream();
273292
MessagePackFactory messagePackFactory = new MessagePackFactory();
274293
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
275294
List<Integer> integers = Arrays.asList(1);
@@ -278,10 +297,9 @@ public void testEnableFeatureAutoCloseTarget() throws IOException {
278297
}
279298

280299
@Test
281-
public void testDisableFeatureAutoCloseTarget() throws IOException {
282-
File tempFile = File.createTempFile("test", "msgpack");
283-
tempFile.deleteOnExit();
284-
FileOutputStream out = new FileOutputStream(tempFile);
300+
public void testDisableFeatureAutoCloseTarget() throws Exception {
301+
File tempFile = createTempFile();
302+
OutputStream out = new FileOutputStream(tempFile);
285303
MessagePackFactory messagePackFactory = new MessagePackFactory();
286304
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
287305
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
@@ -296,4 +314,26 @@ public void testDisableFeatureAutoCloseTarget() throws IOException {
296314
assertEquals(1, unpacker.unpackArrayHeader());
297315
assertEquals(1, unpacker.unpackInt());
298316
}
317+
318+
@Test
319+
public void testWritePrimitiveObjectViaObjectMapper() throws Exception {
320+
File tempFile = createTempFile();
321+
OutputStream out = new FileOutputStream(tempFile);
322+
323+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
324+
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
325+
objectMapper.writeValue(out, 1);
326+
objectMapper.writeValue(out, "two");
327+
objectMapper.writeValue(out, 3.14);
328+
objectMapper.writeValue(out, Arrays.asList(4));
329+
objectMapper.writeValue(out, 5L);
330+
331+
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new FileInputStream(tempFile));
332+
assertEquals(1, unpacker.unpackInt());
333+
assertEquals("two", unpacker.unpackString());
334+
assertEquals(3.14, unpacker.unpackFloat(), 0.0001);
335+
assertEquals(1, unpacker.unpackArrayHeader());
336+
assertEquals(4, unpacker.unpackInt());
337+
assertEquals(5, unpacker.unpackLong());
338+
}
299339
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,36 @@ public void testMessagePackParserDirectly() throws IOException {
318318
parser.close(); // Intentional
319319
}
320320

321+
@Test
322+
public void testReadPrimitives() throws Exception
323+
{
324+
MessagePackFactory messagePackFactory = new MessagePackFactory();
325+
File tempFile = createTempFile();
326+
327+
FileOutputStream out = new FileOutputStream(tempFile);
328+
MessagePacker packer = MessagePack.newDefaultPacker(out);
329+
packer.packString("foo");
330+
packer.packDouble(3.14);
331+
packer.packLong(Long.MAX_VALUE);
332+
byte[] bytes = {0x00, 0x11, 0x22};
333+
packer.packBinaryHeader(bytes.length);
334+
packer.writePayload(bytes);
335+
packer.close();
336+
337+
JsonParser parser = messagePackFactory.createParser(new FileInputStream(tempFile));
338+
assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
339+
assertEquals("foo", parser.getText());
340+
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
341+
assertEquals(3.14, parser.getDoubleValue(), 0.0001);
342+
assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
343+
assertEquals(Long.MAX_VALUE, parser.getLongValue());
344+
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
345+
assertEquals(bytes.length, parser.getBinaryValue().length);
346+
assertEquals(bytes[0], parser.getBinaryValue()[0]);
347+
assertEquals(bytes[1], parser.getBinaryValue()[1]);
348+
assertEquals(bytes[2], parser.getBinaryValue()[2]);
349+
}
350+
321351
@Test
322352
public void testBigDecimal() throws IOException {
323353
double d0 = 1.23456789;
@@ -343,19 +373,21 @@ public void testBigDecimal() throws IOException {
343373
assertEquals(BigDecimal.valueOf(Double.MIN_NORMAL), objects.get(idx++));
344374
}
345375

346-
private File createTestFile() throws IOException {
347-
File tempFile = File.createTempFile("test", "msgpack");
348-
tempFile.deleteOnExit();
349-
FileOutputStream out = new FileOutputStream(tempFile);
350-
MessagePack.newDefaultPacker(out)
351-
.packArrayHeader(1).packInt(1)
352-
.packArrayHeader(1).packInt(1)
353-
.close();
376+
private File createTestFile() throws Exception {
377+
File tempFile = createTempFile(new FileSetup() {
378+
@Override
379+
public void setup(File f) throws IOException {
380+
MessagePack.newDefaultPacker(new FileOutputStream(f))
381+
.packArrayHeader(1).packInt(1)
382+
.packArrayHeader(1).packInt(1)
383+
.close();
384+
}
385+
});
354386
return tempFile;
355387
}
356388

357389
@Test(expected = IOException.class)
358-
public void testEnableFeatureAutoCloseSource() throws IOException {
390+
public void testEnableFeatureAutoCloseSource() throws Exception {
359391
File tempFile = createTestFile();
360392
MessagePackFactory messagePackFactory = new MessagePackFactory();
361393
FileInputStream in = new FileInputStream(tempFile);
@@ -365,11 +397,10 @@ public void testEnableFeatureAutoCloseSource() throws IOException {
365397
}
366398

367399
@Test
368-
public void testDisableFeatureAutoCloseSource() throws IOException {
400+
public void testDisableFeatureAutoCloseSource() throws Exception {
369401
File tempFile = createTestFile();
370-
MessagePackFactory messagePackFactory = new MessagePackFactory();
371402
FileInputStream in = new FileInputStream(tempFile);
372-
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
403+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
373404
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
374405
objectMapper.readValue(in, new TypeReference<List<Integer>>() {});
375406
objectMapper.readValue(in, new TypeReference<List<Integer>>() {});
@@ -386,4 +417,31 @@ public void testParseBigDecimal() throws IOException {
386417
bytes, new TypeReference<ArrayList<BigDecimal>>() {});
387418
assertEquals(list, result);
388419
}
420+
421+
@Test
422+
public void testReadPrimitiveObjectViaObjectMapper() throws Exception {
423+
File tempFile = createTempFile();
424+
FileOutputStream out = new FileOutputStream(tempFile);
425+
426+
MessagePacker packer = MessagePack.newDefaultPacker(out);
427+
packer.packString("foo");
428+
packer.packLong(Long.MAX_VALUE);
429+
packer.packDouble(3.14);
430+
byte[] bytes = {0x00, 0x11, 0x22};
431+
packer.packBinaryHeader(bytes.length);
432+
packer.writePayload(bytes);
433+
packer.close();
434+
435+
FileInputStream in = new FileInputStream(tempFile);
436+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
437+
objectMapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
438+
assertEquals("foo", objectMapper.readValue(in, new TypeReference<String>() {}));
439+
assertEquals(Long.MAX_VALUE, objectMapper.readValue(in, new TypeReference<Long>() {}));
440+
assertEquals(3.14, objectMapper.readValue(in, new TypeReference<Double>() {}));
441+
byte[] bs = objectMapper.readValue(in, new TypeReference<byte []>() {});
442+
assertEquals(bytes.length, bs.length);
443+
assertEquals(bytes[0], bs[0]);
444+
assertEquals(bytes[1], bs[1]);
445+
assertEquals(bytes[2], bs[2]);
446+
}
389447
}

0 commit comments

Comments
 (0)