View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.mockito.Matchers.eq;
23  import static org.mockito.Mockito.any;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.when;
26  import com.google.common.reflect.TypeToken;
27  import java.io.PrintWriter;
28  import java.io.StringWriter;
29  import java.lang.reflect.Type;
30  import java.util.HashSet;
31  import java.util.Map;
32  import java.util.Set;
33  import javax.management.MBeanAttributeInfo;
34  import javax.management.MBeanInfo;
35  import javax.management.MBeanServer;
36  import javax.management.ObjectName;
37  import javax.management.QueryExp;
38  import org.apache.hadoop.hbase.testclassification.MiscTests;
39  import org.apache.hadoop.hbase.testclassification.SmallTests;
40  import org.apache.hbase.thirdparty.com.google.gson.Gson;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test {@link JSONBean}.
46   */
47  @Category({MiscTests.class, SmallTests.class})
48  public class TestJSONBean {
49    private MBeanServer getMockMBeanServer() throws Exception {
50      MBeanServer mbeanServer = mock(MBeanServer.class);
51      Set<ObjectName> names = new HashSet<>();
52      names.add(new ObjectName("test1:type=test2"));
53      when(mbeanServer.queryNames(any(ObjectName.class), any(QueryExp.class))).thenReturn(names);
54      MBeanInfo mbeanInfo = mock(MBeanInfo.class);
55      when(mbeanInfo.getClassName()).thenReturn("testClassName");
56      String[] attributeNames = new String[] {"intAttr", "nanAttr", "infinityAttr",
57        "strAttr", "boolAttr"};
58      MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length];
59      for (int i = 0; i < attributeInfos.length; i++) {
60        attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i],
61          null,
62          null,
63          true,
64          false,
65          false);
66      }
67      when(mbeanInfo.getAttributes()).thenReturn(attributeInfos);
68      when(mbeanServer.getMBeanInfo(any(ObjectName.class))).thenReturn(mbeanInfo);
69      when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(3);
70      when(mbeanServer.getAttribute(any(ObjectName.class), eq("nanAttr"))).thenReturn(Double.NaN);
71      when(mbeanServer.getAttribute(any(ObjectName.class), eq("infinityAttr"))).
72        thenReturn(Double.POSITIVE_INFINITY);
73      when(mbeanServer.getAttribute(any(ObjectName.class), eq("strAttr"))).thenReturn("aString");
74      when(mbeanServer.getAttribute(any(ObjectName.class), eq("boolAttr"))).thenReturn(true);
75      return mbeanServer;
76    }
77  
78    private String getExpectedJSON() {
79      StringWriter sw = new StringWriter();
80      PrintWriter pw = new PrintWriter(sw);
81      pw.println("{");
82      pw.println("  \"beans\": [");
83      pw.println("    {");
84      pw.println("      \"name\": \"test1:type=test2\",");
85      pw.println("      \"modelerType\": \"testClassName\",");
86      pw.println("      \"intAttr\": 3,");
87      pw.println("      \"nanAttr\": \"NaN\",");
88      pw.println("      \"infinityAttr\": \"Infinity\",");
89      pw.println("      \"strAttr\": \"aString\",");
90      pw.println("      \"boolAttr\": true");
91      pw.println("    }");
92      pw.println("  ]");
93      pw.print("}");
94      return sw.toString();
95    }
96  
97    @Test
98    public void testJSONBeanValueTypes() throws Exception {
99      JSONBean bean = new JSONBean();
100     StringWriter stringWriter = new StringWriter();
101     try (
102       PrintWriter printWriter = new PrintWriter(stringWriter);
103       JSONBean.Writer jsonWriter = bean.open(printWriter)) {
104       jsonWriter.write(getMockMBeanServer(), null, null, false);
105     }
106 
107     final Gson gson = GsonUtil.createGson().create();
108     Type typeOfHashMap = new TypeToken<Map<String, Object>>() {}.getType();
109     Map<String, Object> expectedJson = gson.fromJson(getExpectedJSON(), typeOfHashMap);
110     Map<String, Object> actualJson = gson.fromJson(stringWriter.toString(), typeOfHashMap);
111     assertEquals(expectedJson, actualJson);
112   }
113 }