| /*
|
| * Copyright 1999-2011 Alibaba Group.
|
| *
|
| * Licensed under the Apache License, Version 2.0 (the "License");
|
| * you may not use this file except in compliance with the License.
|
| * You may obtain a copy of the License at
|
| *
|
| * http://www.apache.org/licenses/LICENSE-2.0
|
| *
|
| * Unless required by applicable law or agreed to in writing, software
|
| * distributed under the License is distributed on an "AS IS" BASIS,
|
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| * See the License for the specific language governing permissions and
|
| * limitations under the License.
|
| */
|
| package com.alibaba.dubbo.common.serialize.dubbo; |
| |
| import java.io.IOException; |
| |
| import junit.framework.TestCase; |
| |
| import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream; |
| import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; |
| import com.alibaba.dubbo.common.serialize.DataInput; |
| import com.alibaba.dubbo.common.serialize.DataOutput; |
| import com.alibaba.dubbo.common.serialize.support.dubbo.GenericDataInput; |
| import com.alibaba.dubbo.common.serialize.support.dubbo.GenericDataOutput; |
| |
| public class DataInputOutputTest extends TestCase |
| { |
| private static final String SMALL_STRING = DataInputOutputTest.class.getName(), BIG_STREAM = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; |
| |
| private static final byte[] SMALL_BYTES = SMALL_STRING.getBytes(), BIG_BYTES = BIG_STREAM.getBytes(); |
| |
| public void testMain() throws Exception |
| { |
| // write. |
| UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); |
| DataOutput cos = new GenericDataOutput(os); |
| writeTest(cos); |
| |
| // read. |
| byte[] b = os.toByteArray(); |
| DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b)); |
| readTest(cis); |
| } |
| |
| private void writeTest(DataOutput out) throws IOException |
| { |
| out.writeShort((short)'a'); |
| out.writeShort((short)-1); |
| out.writeShort((short)1234); |
| out.writeInt(0x22); |
| out.writeInt(-0x22); |
| out.writeInt(0x2222); |
| out.writeInt(-0x2222); |
| out.writeInt(0x222222); |
| out.writeInt(-0x222222); |
| out.writeInt(0x22222222); |
| out.writeInt(-0x22222222); |
| out.writeLong(0x22); |
| out.writeLong(-0x22); |
| out.writeLong(0x2222); |
| out.writeLong(-0x2222); |
| out.writeLong(0x222222); |
| out.writeLong(-0x222222); |
| out.writeLong(0x22222222); |
| out.writeLong(-0x22222222); |
| out.writeLong(0x2222222222l); |
| out.writeLong(-0x2222222222l); |
| out.writeLong(0x222222222222l); |
| out.writeLong(-0x222222222222l); |
| out.writeLong(0x22222222222222l); |
| out.writeLong(-0x22222222222222l); |
| out.writeLong(0x2222222222222222l); |
| out.writeLong(-0x2222222222222222l); |
| out.writeDouble(1212.454); |
| out.writeBytes(SMALL_BYTES); |
| out.writeUTF(SMALL_STRING); |
| out.writeBytes(BIG_BYTES); |
| out.flushBuffer(); |
| } |
| |
| private void readTest(DataInput in) throws IOException |
| { |
| assertEquals(in.readShort(), 'a'); |
| assertEquals(in.readShort(), -1); |
| assertEquals(in.readShort(), 1234); |
| assertEquals(in.readInt(), 0x22); |
| assertEquals(in.readInt(), -0x22); |
| assertEquals(in.readInt(), 0x2222); |
| assertEquals(in.readInt(), -0x2222); |
| assertEquals(in.readInt(), 0x222222); |
| assertEquals(in.readInt(), -0x222222); |
| assertEquals(in.readInt(), 0x22222222); |
| assertEquals(in.readInt(), -0x22222222); |
| assertEquals(in.readLong(), 0x22); |
| assertEquals(in.readLong(), -0x22); |
| assertEquals(in.readLong(), 0x2222); |
| assertEquals(in.readLong(), -0x2222); |
| assertEquals(in.readLong(), 0x222222); |
| assertEquals(in.readLong(), -0x222222); |
| assertEquals(in.readLong(), 0x22222222); |
| assertEquals(in.readLong(), -0x22222222); |
| assertEquals(in.readLong(), 0x2222222222l); |
| assertEquals(in.readLong(), -0x2222222222l); |
| assertEquals(in.readLong(), 0x222222222222l); |
| assertEquals(in.readLong(), -0x222222222222l); |
| assertEquals(in.readLong(), 0x22222222222222l); |
| assertEquals(in.readLong(), -0x22222222222222l); |
| assertEquals(in.readLong(), 0x2222222222222222l); |
| assertEquals(in.readLong(), -0x2222222222222222l); |
| assertEquals(in.readDouble(), 1212.454); |
| assertSameArray(in.readBytes(), SMALL_BYTES); |
| assertEquals(in.readUTF(), SMALL_STRING); |
| assertSameArray(in.readBytes(), BIG_BYTES); |
| } |
| |
| private static void assertSameArray(byte[] b1, byte[] b2) |
| { |
| assertEquals(b1.length, b2.length); |
| for(int i=0;i<b1.length;i++) |
| assertEquals(b1[i], b2[i]); |
| } |
| } |