2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.codecs;
10 import static org.junit.Assert.assertEquals;
12 import org.junit.Test;
13 import org.opendaylight.yangtools.yang.data.api.codec.Int32Codec;
14 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 * Unit tests for Int32CodecString.
19 * @author Thomas Pantelis
21 public class Int32CodecStringTest {
22 @SuppressWarnings("unchecked")
24 public void testSerialize() {
25 Int32Codec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.int32Type(), Int32Codec.class);
26 assertEquals("serialize", "10", codec.serialize(Integer.valueOf(10)));
29 @SuppressWarnings("unchecked")
31 public void testDeserialize() {
32 final String hexa = "0x45FFFCDE";
33 final String negHexa = "-0x45FFFCDE";
34 final String octal = "010577776336";
35 final String negOctal = "-010577776336";
36 final String integer = "1174404318";
37 final String negInteger = "-1174404318";
39 Int32Codec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.int32Type(), Int32Codec.class);
41 assertEquals("deserialize", codec.deserialize(hexa), Integer.valueOf("+045FFFCDE", 16));
42 assertEquals("deserialize", codec.deserialize(negHexa), Integer.valueOf("-045FFFCDE", 16));
43 assertEquals("deserialize", codec.deserialize(octal), Integer.valueOf(octal, 8));
44 assertEquals("deserialize", codec.deserialize(negOctal), Integer.valueOf(negOctal, 8));
45 assertEquals("deserialize", codec.deserialize(integer), Integer.valueOf(integer, 10));
46 assertEquals("deserialize", codec.deserialize(negInteger), Integer.valueOf(negInteger, 10));
48 TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "1o");
49 TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "");