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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org
.apache
.hadoop
.hbase
.util
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import static org
.mockito
.ArgumentMatchers
.eq
;
23 import static org
.mockito
.Mockito
.any
;
24 import static org
.mockito
.Mockito
.mock
;
25 import static org
.mockito
.Mockito
.when
;
26 import java
.io
.PrintWriter
;
27 import java
.io
.StringWriter
;
28 import java
.lang
.reflect
.Type
;
29 import java
.util
.HashSet
;
32 import javax
.management
.MBeanAttributeInfo
;
33 import javax
.management
.MBeanInfo
;
34 import javax
.management
.MBeanServer
;
35 import javax
.management
.ObjectName
;
36 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
39 import org
.junit
.ClassRule
;
40 import org
.junit
.Test
;
41 import org
.junit
.experimental
.categories
.Category
;
42 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.reflect
.TypeToken
;
43 import org
.apache
.hbase
.thirdparty
.com
.google
.gson
.Gson
;
46 * Test {@link JSONBean}.
48 @Category({MiscTests
.class, SmallTests
.class})
49 public class TestJSONBean
{
51 public static final HBaseClassTestRule CLASS_RULE
=
52 HBaseClassTestRule
.forClass(TestJSONBean
.class);
54 private MBeanServer
getMockMBeanServer() throws Exception
{
55 MBeanServer mbeanServer
= mock(MBeanServer
.class);
56 Set
<ObjectName
> names
= new HashSet
<>();
57 names
.add(new ObjectName("test1:type=test2"));
58 when(mbeanServer
.queryNames(any(), any())).thenReturn(names
);
59 MBeanInfo mbeanInfo
= mock(MBeanInfo
.class);
60 when(mbeanInfo
.getClassName()).thenReturn("testClassName");
61 String
[] attributeNames
= new String
[] {"intAttr", "nanAttr", "infinityAttr",
62 "strAttr", "boolAttr", "test:Attr"};
63 MBeanAttributeInfo
[] attributeInfos
= new MBeanAttributeInfo
[attributeNames
.length
];
64 for (int i
= 0; i
< attributeInfos
.length
; i
++) {
65 attributeInfos
[i
] = new MBeanAttributeInfo(attributeNames
[i
],
72 when(mbeanInfo
.getAttributes()).thenReturn(attributeInfos
);
73 when(mbeanServer
.getMBeanInfo(any())).thenReturn(mbeanInfo
);
74 when(mbeanServer
.getAttribute(any(), eq("intAttr"))).thenReturn(3);
75 when(mbeanServer
.getAttribute(any(), eq("nanAttr"))).thenReturn(Double
.NaN
);
76 when(mbeanServer
.getAttribute(any(), eq("infinityAttr"))).
77 thenReturn(Double
.POSITIVE_INFINITY
);
78 when(mbeanServer
.getAttribute(any(), eq("strAttr"))).thenReturn("aString");
79 when(mbeanServer
.getAttribute(any(), eq("boolAttr"))).thenReturn(true);
80 when(mbeanServer
.getAttribute(any(), eq("test:Attr"))).thenReturn("aString");
84 private String
getExpectedJSON() {
85 StringWriter sw
= new StringWriter();
86 PrintWriter pw
= new PrintWriter(sw
);
88 pw
.println(" \"beans\": [");
90 pw
.println(" \"name\": \"test1:type=test2\",");
91 pw
.println(" \"modelerType\": \"testClassName\",");
92 pw
.println(" \"intAttr\": 3,");
93 pw
.println(" \"nanAttr\": \"NaN\",");
94 pw
.println(" \"infinityAttr\": \"Infinity\",");
95 pw
.println(" \"strAttr\": \"aString\",");
96 pw
.println(" \"boolAttr\": true,");
97 pw
.println(" \"test:Attr\": aString");
101 return sw
.toString();
105 public void testJSONBeanValueTypes() throws Exception
{
106 JSONBean bean
= new JSONBean();
107 StringWriter stringWriter
= new StringWriter();
109 PrintWriter printWriter
= new PrintWriter(stringWriter
);
110 JSONBean
.Writer jsonWriter
= bean
.open(printWriter
)) {
111 jsonWriter
.write(getMockMBeanServer(), null, null, false);
114 final Gson gson
= GsonUtil
.createGson().create();
115 Type typeOfHashMap
= new TypeToken
<Map
<String
, Object
>>() {}.getType();
116 Map
<String
, Object
> expectedJson
= gson
.fromJson(getExpectedJSON(), typeOfHashMap
);
117 Map
<String
, Object
> actualJson
= gson
.fromJson(stringWriter
.toString(), typeOfHashMap
);
118 assertEquals(expectedJson
, actualJson
);