1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
19 using System.Collections.Generic;
21 using Microsoft.VisualStudio.TestTools.UnitTesting;
22 using Thrift.Protocols;
23 using Thrift.Protocols.Entities;
24 using Thrift.Protocols.Utilities;
26 namespace Thrift.Tests.Protocols
29 public class TJSONProtocolHelperTests
32 public void GetTypeNameForTypeId_Test()
35 var sets = new List<Tuple<TType, byte[]>>
37 new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
38 new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
39 new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
40 new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
41 new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
42 new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
43 new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString),
44 new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
45 new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
46 new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
47 new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList),
50 foreach (var t in sets)
52 Assert.IsTrue(TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1) == t.Item2, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
57 [ExpectedException(typeof(TProtocolException))]
58 public void GetTypeNameForTypeId_TStop_Test()
60 TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop);
64 [ExpectedException(typeof(TProtocolException))]
65 public void GetTypeNameForTypeId_NonExistingTType_Test()
67 TJSONProtocolHelper.GetTypeNameForTypeId((TType)100);
71 public void GetTypeIdForTypeName_Test()
74 var sets = new List<Tuple<TType, byte[]>>
76 new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool),
77 new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte),
78 new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16),
79 new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32),
80 new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64),
81 new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble),
82 new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString),
83 new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct),
84 new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap),
85 new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet),
86 new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList),
89 foreach (var t in sets)
91 Assert.IsTrue(TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2) == t.Item1, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}");
96 [ExpectedException(typeof(TProtocolException))]
97 public void GetTypeIdForTypeName_TStopTypeName_Test()
99 TJSONProtocolHelper.GetTypeIdForTypeName(new []{(byte)TType.Stop, (byte)TType.Stop});
103 [ExpectedException(typeof(TProtocolException))]
104 public void GetTypeIdForTypeName_NonExistingTypeName_Test()
106 TJSONProtocolHelper.GetTypeIdForTypeName(new byte[]{100});
110 [ExpectedException(typeof(TProtocolException))]
111 public void GetTypeIdForTypeName_EmptyName_Test()
113 TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] {});
117 public void IsJsonNumeric_Test()
120 var correctJsonNumeric = "+-.0123456789Ee";
121 var incorrectJsonNumeric = "AaBcDd/*\\";
123 var sets = correctJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, true)).ToList();
124 sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, false)));
126 foreach (var t in sets)
128 Assert.IsTrue(TJSONProtocolHelper.IsJsonNumeric(t.Item1) == t.Item2, $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}");
133 public void ToHexVal_Test()
136 var chars = "0123456789abcdef";
137 var expectedHexValues = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
139 var sets = chars.Select((ch, i) => new Tuple<char, byte>(ch, expectedHexValues[i])).ToList();
141 foreach (var t in sets)
143 var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1);
144 Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}");
149 [ExpectedException(typeof(TProtocolException))]
150 public void ToHexVal_WrongInputChar_Test()
152 TJSONProtocolHelper.ToHexVal((byte)'s');
156 public void ToHexChar_Test()
159 var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
160 var expectedChars = "0123456789abcdef";
163 var sets = hexValues.Select((hv, i) => new Tuple<byte, char>(hv, expectedChars[i])).ToList();
165 foreach (var t in sets)
167 var actualResult = TJSONProtocolHelper.ToHexChar(t.Item1);
168 Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}");