1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.thrift.protocol; 20 21 import java.nio.charset.StandardCharsets; 22 23 import junit.framework.TestCase; 24 25 import org.apache.thrift.Fixtures; 26 import org.apache.thrift.TException; 27 import org.apache.thrift.transport.TMemoryBuffer; 28 29 import thrift.test.CompactProtoTestStruct; 30 import thrift.test.HolyMoley; 31 32 public class TestTSimpleJSONProtocol extends TestCase { 33 private TMemoryBuffer buf; 34 private TSimpleJSONProtocol proto; 35 36 @Override setUp()37 protected void setUp() throws Exception { 38 buf = new TMemoryBuffer(1000); 39 proto = new TSimpleJSONProtocol(buf); 40 } 41 bufToString()42 private String bufToString() { 43 return buf.toString(StandardCharsets.UTF_8); 44 } 45 testHolyMoley()46 public void testHolyMoley() throws TException { 47 final HolyMoley holyMoley = Fixtures.holyMoley.deepCopy(); 48 // unset sets that produce inconsistent ordering between JDK7/8 49 holyMoley.unsetBonks(); 50 holyMoley.unsetContain(); 51 holyMoley.write(proto); 52 assertEquals("{\"big\":[{\"im_true\":1,\"im_false\":0,\"a_bite\":35,\"integer16\":27000,\"integer32\":16777216,\"integer64\":6000000000,\"double_precision\":3.141592653589793,\"some_characters\":\"JSON THIS! \\\"\\u0001\",\"zomg_unicode\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκ�‼\",\"what_who\":0,\"base64\":\"base64\",\"byte_list\":[1,2,3],\"i16_list\":[1,2,3],\"i64_list\":[1,2,3]},{\"im_true\":1,\"im_false\":0,\"a_bite\":-42,\"integer16\":27000,\"integer32\":16777216,\"integer64\":6000000000,\"double_precision\":3.141592653589793,\"some_characters\":\"JSON THIS! \\\"\\u0001\",\"zomg_unicode\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκ�‼\",\"what_who\":0,\"base64\":\"base64\",\"byte_list\":[1,2,3],\"i16_list\":[1,2,3],\"i64_list\":[1,2,3]}]}", bufToString()); 53 } 54 testNesting()55 public void testNesting() throws TException { 56 Fixtures.nesting.write(proto); 57 assertEquals("{\"my_bonk\":{\"type\":31337,\"message\":\"I am a bonk... xor!\"},\"my_ooe\":{\"im_true\":1,\"im_false\":0,\"a_bite\":-42,\"integer16\":27000,\"integer32\":16777216,\"integer64\":6000000000,\"double_precision\":3.141592653589793,\"some_characters\":\"JSON THIS! \\\"\\u0001\",\"zomg_unicode\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκ�‼\",\"what_who\":0,\"base64\":\"base64\",\"byte_list\":[1,2,3],\"i16_list\":[1,2,3],\"i64_list\":[1,2,3]}}", bufToString()); 58 } 59 testOneOfEach()60 public void testOneOfEach() throws TException { 61 Fixtures.oneOfEach.write(proto); 62 assertEquals("{\"im_true\":1,\"im_false\":0,\"a_bite\":-42,\"integer16\":27000,\"integer32\":16777216,\"integer64\":6000000000,\"double_precision\":3.141592653589793,\"some_characters\":\"JSON THIS! \\\"\\u0001\",\"zomg_unicode\":\"ӀⅮΝ Нοⅿоɡгаρℎ Αttαⅽκ�‼\",\"what_who\":0,\"base64\":\"base64\",\"byte_list\":[1,2,3],\"i16_list\":[1,2,3],\"i64_list\":[1,2,3]}", bufToString()); 63 } 64 testSanePartsOfCompactProtoTestStruct()65 public void testSanePartsOfCompactProtoTestStruct() throws TException { 66 // unset all the maps with container keys 67 CompactProtoTestStruct struct = Fixtures.compactProtoTestStruct.deepCopy(); 68 struct.unsetList_byte_map(); 69 struct.unsetSet_byte_map(); 70 struct.unsetMap_byte_map(); 71 // unset sets and maps that produce inconsistent ordering between JDK7/8 72 struct.unsetByte_set(); 73 struct.unsetI16_set(); 74 struct.unsetI64_set(); 75 struct.unsetDouble_set(); 76 struct.unsetString_set(); 77 struct.unsetI16_byte_map(); 78 struct.unsetI32_byte_map(); 79 struct.unsetI64_byte_map(); 80 struct.unsetDouble_byte_map(); 81 struct.unsetString_byte_map(); 82 struct.write(proto); 83 assertEquals("{\"a_byte\":127,\"a_i16\":32000,\"a_i32\":1000000000,\"a_i64\":1099511627775,\"a_double\":5.6789,\"a_string\":\"my string\",\"a_binary\":\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\",\"true_field\":1,\"false_field\":0,\"empty_struct_field\":{},\"byte_list\":[-127,-1,0,1,127],\"i16_list\":[-1,0,1,32767],\"i32_list\":[-1,0,255,65535,16777215,2147483647],\"i64_list\":[-1,0,255,65535,16777215,4294967295,1099511627775,281474976710655,72057594037927935,9223372036854775807],\"double_list\":[0.1,0.2,0.3],\"string_list\":[\"first\",\"second\",\"third\"],\"boolean_list\":[1,1,1,0,0,0],\"struct_list\":[{},{}],\"i32_set\":[1,2,3],\"boolean_set\":[0,1],\"struct_set\":[{}],\"byte_byte_map\":{\"1\":2},\"boolean_byte_map\":{\"0\":0,\"1\":1},\"byte_i16_map\":{\"1\":1,\"2\":-1,\"3\":32767},\"byte_i32_map\":{\"1\":1,\"2\":-1,\"3\":2147483647},\"byte_i64_map\":{\"1\":1,\"2\":-1,\"3\":9223372036854775807},\"byte_double_map\":{\"1\":0.1,\"2\":-0.1,\"3\":1000000.1},\"byte_string_map\":{\"1\":\"\",\"2\":\"blah\",\"3\":\"loooooooooooooong string\"},\"byte_boolean_map\":{\"1\":1,\"2\":0},\"byte_map_map\":{\"0\":{},\"1\":{\"1\":1},\"2\":{\"1\":1,\"2\":2}},\"byte_set_map\":{\"0\":[],\"1\":[1],\"2\":[1,2]},\"byte_list_map\":{\"0\":[],\"1\":[1],\"2\":[1,2]},\"field500\":500,\"field5000\":5000,\"field20000\":20000}", bufToString()); 84 } 85 testThrowsOnCollectionKeys()86 public void testThrowsOnCollectionKeys() throws TException { 87 try { 88 Fixtures.compactProtoTestStruct.write(proto); 89 fail("this should throw a CollectionMapKeyException"); 90 } catch (TSimpleJSONProtocol.CollectionMapKeyException e) { 91 // 92 } 93 } 94 } 95