001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.http.gson;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Map;
023import java.util.TreeMap;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.Size;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034import org.apache.hbase.thirdparty.com.google.gson.Gson;
035
036@Category({ MasterTests.class, SmallTests.class })
037public class GsonFactoryTest {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(GsonFactoryTest.class);
042
043  private static Gson gson;
044
045  @BeforeClass
046  public static void beforeClass() {
047    gson = GsonFactory.buildGson();
048  }
049
050  @Test
051  public void testSerializeToLowerCaseUnderscores() {
052    final SomeBean input = new SomeBean(false, 57, "hello\n");
053    final String actual = gson.toJson(input);
054    final String expected = "{\"a_boolean\":false,\"an_int\":57,\"a_string\":\"hello\\n\"}";
055    assertEquals(expected, actual);
056  }
057
058  @Test
059  public void testSerializeMapWithSizeKeys() {
060    final Map<Size, String> input = new TreeMap<>();
061    input.put(new Size(10, Size.Unit.KILOBYTE), "10kb");
062    input.put(new Size(5, Size.Unit.MEGABYTE), "5mb");
063    final String actual = gson.toJson(input);
064    final String expected = "{\"10240.0\":\"10kb\",\"5242880.0\":\"5mb\"}";
065    assertEquals(expected, actual);
066  }
067
068  @Test
069  public void testSerializeNonPrintableByteArrays() {
070    final Map<byte[], byte[]> input = new TreeMap<>(Bytes.BYTES_COMPARATOR);
071    input.put(Bytes.toBytes("this is printable"), new byte[] { 0, 1, 2, 3, 4, 5 });
072    input.put(new byte[] { -127, -63, 0, 63, 127 }, Bytes.toBytes("test"));
073    final String actual = gson.toJson(input);
074    final String expected =
075      "{" + "\"this is printable\":\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\","
076        + "\"��\\u0000?\u007F\":\"test\"}";
077    assertEquals(expected, actual);
078  }
079
080  private static final class SomeBean {
081    private final boolean aBoolean;
082    private final int anInt;
083    private final String aString;
084
085    public SomeBean(final boolean aBoolean, final int anInt, final String aString) {
086      this.aBoolean = aBoolean;
087      this.anInt = anInt;
088      this.aString = aString;
089    }
090
091    public boolean isaBoolean() {
092      return aBoolean;
093    }
094
095    public int getAnInt() {
096      return anInt;
097    }
098
099    public String getaString() {
100      return aString;
101    }
102  }
103}