|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.alibaba.com.caucho.hessian.io; |
| 18 | + |
| 19 | +import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase; |
| 20 | +import com.alibaba.com.caucho.hessian.io.beans.Hessian2StringShortType; |
| 21 | +import com.alibaba.com.caucho.hessian.io.beans.PersonType; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static junit.framework.TestCase.assertEquals; |
| 34 | +import static junit.framework.TestCase.assertTrue; |
| 35 | + |
| 36 | +public class Hessian1StringShortTest extends SerializeTestBase { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void serialize_string_short_map_then_deserialize() throws Exception { |
| 40 | + |
| 41 | + Hessian2StringShortType stringShort = new Hessian2StringShortType(); |
| 42 | + Map<String, Short> stringShortMap = new HashMap<String, Short>(); |
| 43 | + stringShortMap.put("first", (short)0); |
| 44 | + stringShortMap.put("last", (short)60); |
| 45 | + stringShort.stringShortMap = stringShortMap; |
| 46 | + |
| 47 | + Hessian2StringShortType deserialize = baseHessionSerialize(stringShort); |
| 48 | + assertTrue(deserialize.stringShortMap != null); |
| 49 | + assertTrue(deserialize.stringShortMap.size() == 2); |
| 50 | + assertTrue(deserialize.stringShortMap.get("last") instanceof Short); |
| 51 | + assertEquals(Short.valueOf((short)0), deserialize.stringShortMap.get("first")); |
| 52 | + assertEquals(Short.valueOf((short)60), deserialize.stringShortMap.get("last")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void serialize_string_byte_map_then_deserialize() throws Exception { |
| 57 | + |
| 58 | + Hessian2StringShortType stringShort = new Hessian2StringShortType(); |
| 59 | + Map<String, Byte> stringByteMap = new HashMap<String, Byte>(); |
| 60 | + stringByteMap.put("first", (byte)0); |
| 61 | + stringByteMap.put("last", (byte)60); |
| 62 | + stringShort.stringByteMap = stringByteMap; |
| 63 | + |
| 64 | + Hessian2StringShortType deserialize = baseHessionSerialize(stringShort); |
| 65 | + assertTrue(deserialize.stringByteMap != null); |
| 66 | + assertTrue(deserialize.stringByteMap.size() == 2); |
| 67 | + assertTrue(deserialize.stringByteMap.get("last") instanceof Byte); |
| 68 | + assertEquals(Byte.valueOf((byte)0), deserialize.stringByteMap.get("first")); |
| 69 | + assertEquals(Byte.valueOf((byte) 60), deserialize.stringByteMap.get("last")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void serialize_map_then_deserialize() throws Exception { |
| 74 | + |
| 75 | + Map<String, Short> stringShortMap = new HashMap<String, Short>(); |
| 76 | + stringShortMap.put("first", (short)0); |
| 77 | + stringShortMap.put("last", (short)60); |
| 78 | + |
| 79 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 80 | + HessianOutput out = new HessianOutput(bout); |
| 81 | + |
| 82 | + out.writeObject(stringShortMap); |
| 83 | + out.flush(); |
| 84 | + |
| 85 | + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
| 86 | + HessianInput input = new HessianInput(bin); |
| 87 | + Map deserialize = (Map) input.readObject(HashMap.class, String.class, Short.class); |
| 88 | + assertTrue(deserialize != null); |
| 89 | + assertTrue(deserialize.size() == 2); |
| 90 | + assertTrue(deserialize.get("last") instanceof Short); |
| 91 | + assertEquals(Short.valueOf((short)0), deserialize.get("first")); |
| 92 | + assertEquals(Short.valueOf((short)60), deserialize.get("last")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void serialize_map_then_deserialize0() throws Exception { |
| 97 | + |
| 98 | + Map<String, Short> stringShortMap = new HashMap<String, Short>(); |
| 99 | + stringShortMap.put("first", (short)0); |
| 100 | + stringShortMap.put("last", (short)60); |
| 101 | + |
| 102 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 103 | + HessianOutput out = new HessianOutput(bout); |
| 104 | + |
| 105 | + out.writeObject(stringShortMap); |
| 106 | + out.flush(); |
| 107 | + |
| 108 | + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
| 109 | + HessianInput input = new HessianInput(bin); |
| 110 | + List<Class<?>> keyValueType = new ArrayList<Class<?>>(); |
| 111 | + keyValueType.add(String.class); |
| 112 | + keyValueType.add(short.class); |
| 113 | + |
| 114 | + Map deserialize = (Map) input.readObject(keyValueType); |
| 115 | + assertTrue(deserialize != null); |
| 116 | + assertTrue(deserialize.size() == 2); |
| 117 | + assertTrue(deserialize.get("last") instanceof Short); |
| 118 | + assertEquals(Short.valueOf((short)0), deserialize.get("first")); |
| 119 | + assertEquals(Short.valueOf((short)60), deserialize.get("last")); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void serialize_string_person_map_then_deserialize() throws Exception { |
| 124 | + |
| 125 | + Hessian2StringShortType stringShort = new Hessian2StringShortType(); |
| 126 | + Map<String, PersonType> stringPersonTypeMap = new HashMap<String, PersonType>(); |
| 127 | + stringPersonTypeMap.put("first", new PersonType( |
| 128 | + "jason.shang", 26, (double) 0.1, (short)1, (byte)2, Arrays.asList((short)1,(short)1) |
| 129 | + )); |
| 130 | + stringPersonTypeMap.put("last", new PersonType( |
| 131 | + "jason.shang2", 52, (double) 0.2, (short)2, (byte)4, Arrays.asList((short)2,(short)2) |
| 132 | + )); |
| 133 | + stringShort.stringPersonTypeMap = stringPersonTypeMap; |
| 134 | + |
| 135 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 136 | + HessianOutput out = new HessianOutput(bout); |
| 137 | + |
| 138 | + out.writeObject(stringShort); |
| 139 | + out.flush(); |
| 140 | + |
| 141 | + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
| 142 | + HessianInput input = new HessianInput(bin); |
| 143 | + |
| 144 | + Hessian2StringShortType deserialize = (Hessian2StringShortType) input.readObject(); |
| 145 | + assertTrue(deserialize.stringPersonTypeMap != null); |
| 146 | + assertTrue(deserialize.stringPersonTypeMap.size() == 2); |
| 147 | + assertTrue(deserialize.stringPersonTypeMap.get("last") instanceof PersonType); |
| 148 | + |
| 149 | + |
| 150 | + assertEquals(new PersonType( |
| 151 | + "jason.shang", 26, (double) 0.1, (short)1, (byte)2, Arrays.asList((short)1,(short)1) |
| 152 | + ), deserialize.stringPersonTypeMap.get("first")); |
| 153 | + |
| 154 | + assertEquals(new PersonType( |
| 155 | + "jason.shang2", 52, (double) 0.2, (short)2, (byte)4, Arrays.asList((short)2,(short)2) |
| 156 | + ), deserialize.stringPersonTypeMap.get("last")); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void serialize_list_then_deserialize() throws Exception { |
| 162 | + |
| 163 | + List<Short> shortList = new ArrayList<Short>(); |
| 164 | + shortList.add((short)0); |
| 165 | + shortList.add((short)60); |
| 166 | + |
| 167 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 168 | + HessianOutput out = new HessianOutput(bout); |
| 169 | + |
| 170 | + out.writeObject(shortList); |
| 171 | + out.flush(); |
| 172 | + |
| 173 | + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
| 174 | + HessianInput input = new HessianInput(bin); |
| 175 | + List<Short> deserialize = (List) input.readObject(ArrayList.class, Short.class); |
| 176 | + assertTrue(deserialize != null); |
| 177 | + assertTrue(deserialize.size() == 2); |
| 178 | + assertTrue(deserialize.get(1) instanceof Short); |
| 179 | + assertEquals(Short.valueOf((short)0), deserialize.get(0)); |
| 180 | + assertEquals(Short.valueOf((short)60), deserialize.get(1)); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void serialize_list_then_deserialize0() throws Exception { |
| 185 | + |
| 186 | + List<Short> shortList = new ArrayList<Short>(); |
| 187 | + shortList.add((short)0); |
| 188 | + shortList.add((short)60); |
| 189 | + |
| 190 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 191 | + HessianOutput out = new HessianOutput(bout); |
| 192 | + |
| 193 | + out.writeObject(shortList); |
| 194 | + out.flush(); |
| 195 | + |
| 196 | + ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
| 197 | + HessianInput input = new HessianInput(bin); |
| 198 | + |
| 199 | + List<Class<?>> valueType = new ArrayList<Class<?>>(); |
| 200 | + valueType.add(short.class); |
| 201 | + |
| 202 | + List<Short> deserialize = (List) input.readObject(valueType); |
| 203 | + assertTrue(deserialize != null); |
| 204 | + assertTrue(deserialize.size() == 2); |
| 205 | + assertTrue(deserialize.get(1) instanceof Short); |
| 206 | + assertEquals(Short.valueOf((short)0), deserialize.get(0)); |
| 207 | + assertEquals(Short.valueOf((short)60), deserialize.get(1)); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments