|
| 1 | +package org.msgpack; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertArrayEquals; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.math.BigInteger; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import org.msgpack.packer.StreamPacker; |
| 12 | +import org.msgpack.unpacker.BufferUnpacker; |
| 13 | +import org.msgpack.unpacker.Converter; |
| 14 | +import org.msgpack.value.Value; |
| 15 | + |
| 16 | + |
| 17 | +public class TestStreamPackConvert extends TestSet { |
| 18 | + |
| 19 | + @Test @Override |
| 20 | + public void testBoolean() throws Exception { |
| 21 | + super.testBoolean(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void testBoolean(boolean v) throws Exception { |
| 26 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 27 | + StreamPacker packer = new StreamPacker(out); |
| 28 | + packer.writeBoolean(v); |
| 29 | + byte[] bytes = out.toByteArray(); |
| 30 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 31 | + unpacker.wrap(bytes); |
| 32 | + Value value = unpacker.readValue(); |
| 33 | + assertTrue(value.isBoolean()); |
| 34 | + boolean ret = new Converter(value).readBoolean(); |
| 35 | + assertEquals(v, ret); |
| 36 | + } |
| 37 | + |
| 38 | + @Test @Override |
| 39 | + public void testByte() throws Exception { |
| 40 | + super.testByte(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void testByte(byte v) throws Exception { |
| 45 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 46 | + StreamPacker packer = new StreamPacker(out); |
| 47 | + packer.writeByte(v); |
| 48 | + byte[] bytes = out.toByteArray(); |
| 49 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 50 | + unpacker.wrap(bytes); |
| 51 | + Value value = unpacker.readValue(); |
| 52 | + assertTrue(value.isInteger()); |
| 53 | + byte ret = new Converter(value).readByte(); |
| 54 | + assertEquals(v, ret); |
| 55 | + } |
| 56 | + |
| 57 | + @Test @Override |
| 58 | + public void testShort() throws Exception { |
| 59 | + super.testShort(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void testShort(short v) throws Exception { |
| 64 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 65 | + StreamPacker packer = new StreamPacker(out); |
| 66 | + packer.writeShort(v); |
| 67 | + byte[] bytes = out.toByteArray(); |
| 68 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 69 | + unpacker.wrap(bytes); |
| 70 | + Value value = unpacker.readValue(); |
| 71 | + assertTrue(value.isInteger()); |
| 72 | + short ret = new Converter(value).readShort(); |
| 73 | + assertEquals(v, ret); |
| 74 | + } |
| 75 | + |
| 76 | + @Test @Override |
| 77 | + public void testInteger() throws Exception { |
| 78 | + super.testInteger(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void testInteger(int v) throws Exception { |
| 83 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 84 | + StreamPacker packer = new StreamPacker(out); |
| 85 | + packer.writeInt(v); |
| 86 | + byte[] bytes = out.toByteArray(); |
| 87 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 88 | + unpacker.wrap(bytes); |
| 89 | + Value value = unpacker.readValue(); |
| 90 | + assertTrue(value.isInteger()); |
| 91 | + int ret = new Converter(value).readInt(); |
| 92 | + assertEquals(v, ret); |
| 93 | + } |
| 94 | + |
| 95 | + @Test @Override |
| 96 | + public void testLong() throws Exception { |
| 97 | + super.testLong(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void testLong(long v) throws Exception { |
| 102 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 103 | + StreamPacker packer = new StreamPacker(out); |
| 104 | + packer.writeLong(v); |
| 105 | + byte[] bytes = out.toByteArray(); |
| 106 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 107 | + unpacker.wrap(bytes); |
| 108 | + Value value = unpacker.readValue(); |
| 109 | + assertTrue(value.isInteger()); |
| 110 | + long ret = new Converter(value).readLong(); |
| 111 | + assertEquals(v, ret); |
| 112 | + } |
| 113 | + |
| 114 | + @Test @Override |
| 115 | + public void testFloat() throws Exception { |
| 116 | + super.testFloat(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void testFloat(float v) throws Exception { |
| 121 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 122 | + StreamPacker packer = new StreamPacker(out); |
| 123 | + packer.writeFloat(v); |
| 124 | + byte[] bytes = out.toByteArray(); |
| 125 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 126 | + unpacker.wrap(bytes); |
| 127 | + Value value = unpacker.readValue(); |
| 128 | + assertTrue(value.isFloat()); |
| 129 | + float ret = new Converter(value).readFloat(); |
| 130 | + assertEquals(v, ret, 10e-10); |
| 131 | + } |
| 132 | + |
| 133 | + @Test @Override |
| 134 | + public void testDouble() throws Exception { |
| 135 | + super.testDouble(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void testDouble(double v) throws Exception { |
| 140 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 141 | + StreamPacker packer = new StreamPacker(out); |
| 142 | + packer.writeDouble(v); |
| 143 | + byte[] bytes = out.toByteArray(); |
| 144 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 145 | + unpacker.wrap(bytes); |
| 146 | + Value value = unpacker.readValue(); |
| 147 | + assertTrue(value.isFloat()); |
| 148 | + double ret = new Converter(value).readDouble(); |
| 149 | + assertEquals(v, ret, 10e-10); |
| 150 | + } |
| 151 | + |
| 152 | + @Test @Override |
| 153 | + public void testNil() throws Exception { |
| 154 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 155 | + StreamPacker packer = new StreamPacker(out); |
| 156 | + packer.writeNil(); |
| 157 | + byte[] bytes = out.toByteArray(); |
| 158 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 159 | + unpacker.wrap(bytes); |
| 160 | + Value value = unpacker.readValue(); |
| 161 | + assertTrue(value.isNil()); |
| 162 | + new Converter(value).readNil(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test @Override |
| 166 | + public void testBigInteger() throws Exception { |
| 167 | + super.testBigInteger(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void testBigInteger(BigInteger v) throws Exception { |
| 172 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 173 | + StreamPacker packer = new StreamPacker(out); |
| 174 | + packer.writeBigInteger(v); |
| 175 | + byte[] bytes = out.toByteArray(); |
| 176 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 177 | + unpacker.wrap(bytes); |
| 178 | + Value value = unpacker.readValue(); |
| 179 | + assertTrue(value.isInteger()); |
| 180 | + BigInteger ret = new Converter(value).readBigInteger(); |
| 181 | + assertEquals(v, ret); |
| 182 | + } |
| 183 | + |
| 184 | + @Test @Override |
| 185 | + public void testString() throws Exception { |
| 186 | + super.testString(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void testString(String v) throws Exception { |
| 191 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 192 | + StreamPacker packer = new StreamPacker(out); |
| 193 | + packer.writeString(v); |
| 194 | + byte[] bytes = out.toByteArray(); |
| 195 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 196 | + unpacker.wrap(bytes); |
| 197 | + Value value = unpacker.readValue(); |
| 198 | + assertTrue(value.isRaw()); |
| 199 | + String ret = new Converter(value).readString(); |
| 200 | + assertEquals(v, ret); |
| 201 | + } |
| 202 | + |
| 203 | + @Test @Override |
| 204 | + public void testByteArray() throws Exception { |
| 205 | + super.testByteArray(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void testByteArray(byte[] v) throws Exception { |
| 210 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 211 | + StreamPacker packer = new StreamPacker(out); |
| 212 | + packer.writeByteArray(v); |
| 213 | + byte[] bytes = out.toByteArray(); |
| 214 | + BufferUnpacker unpacker = new BufferUnpacker(); |
| 215 | + unpacker.wrap(bytes); |
| 216 | + Value value = unpacker.readValue(); |
| 217 | + assertTrue(value.isRaw()); |
| 218 | + byte[] ret = new Converter(value).readByteArray(); |
| 219 | + assertArrayEquals(v, ret); |
| 220 | + } |
| 221 | +} |
0 commit comments