001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.util; 020 021import static org.junit.Assert.assertEquals; 022import static org.mockito.ArgumentMatchers.eq; 023import static org.mockito.Mockito.any; 024import static org.mockito.Mockito.mock; 025import static org.mockito.Mockito.when; 026import java.io.PrintWriter; 027import java.io.StringWriter; 028import java.lang.reflect.Type; 029import java.util.HashSet; 030import java.util.Map; 031import java.util.Set; 032import javax.management.MBeanAttributeInfo; 033import javax.management.MBeanInfo; 034import javax.management.MBeanServer; 035import javax.management.ObjectName; 036import org.apache.hadoop.hbase.HBaseClassTestRule; 037import org.apache.hadoop.hbase.testclassification.MiscTests; 038import org.apache.hadoop.hbase.testclassification.SmallTests; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042import org.apache.hbase.thirdparty.com.google.common.reflect.TypeToken; 043import org.apache.hbase.thirdparty.com.google.gson.Gson; 044 045/** 046 * Test {@link JSONBean}. 047 */ 048@Category({MiscTests.class, SmallTests.class}) 049public class TestJSONBean { 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestJSONBean.class); 053 054 private MBeanServer getMockMBeanServer() throws Exception { 055 MBeanServer mbeanServer = mock(MBeanServer.class); 056 Set<ObjectName> names = new HashSet<>(); 057 names.add(new ObjectName("test1:type=test2")); 058 when(mbeanServer.queryNames(any(), any())).thenReturn(names); 059 MBeanInfo mbeanInfo = mock(MBeanInfo.class); 060 when(mbeanInfo.getClassName()).thenReturn("testClassName"); 061 String[] attributeNames = new String[] {"intAttr", "nanAttr", "infinityAttr", 062 "strAttr", "boolAttr"}; 063 MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length]; 064 for (int i = 0; i < attributeInfos.length; i++) { 065 attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i], 066 null, 067 null, 068 true, 069 false, 070 false); 071 } 072 when(mbeanInfo.getAttributes()).thenReturn(attributeInfos); 073 when(mbeanServer.getMBeanInfo(any())).thenReturn(mbeanInfo); 074 when(mbeanServer.getAttribute(any(), eq("intAttr"))).thenReturn(3); 075 when(mbeanServer.getAttribute(any(), eq("nanAttr"))).thenReturn(Double.NaN); 076 when(mbeanServer.getAttribute(any(), eq("infinityAttr"))). 077 thenReturn(Double.POSITIVE_INFINITY); 078 when(mbeanServer.getAttribute(any(), eq("strAttr"))).thenReturn("aString"); 079 when(mbeanServer.getAttribute(any(), eq("boolAttr"))).thenReturn(true); 080 return mbeanServer; 081 } 082 083 private String getExpectedJSON() { 084 StringWriter sw = new StringWriter(); 085 PrintWriter pw = new PrintWriter(sw); 086 pw.println("{"); 087 pw.println(" \"beans\": ["); 088 pw.println(" {"); 089 pw.println(" \"name\": \"test1:type=test2\","); 090 pw.println(" \"modelerType\": \"testClassName\","); 091 pw.println(" \"intAttr\": 3,"); 092 pw.println(" \"nanAttr\": \"NaN\","); 093 pw.println(" \"infinityAttr\": \"Infinity\","); 094 pw.println(" \"strAttr\": \"aString\","); 095 pw.println(" \"boolAttr\": true"); 096 pw.println(" }"); 097 pw.println(" ]"); 098 pw.print("}"); 099 return sw.toString(); 100 } 101 102 @Test 103 public void testJSONBeanValueTypes() throws Exception { 104 JSONBean bean = new JSONBean(); 105 StringWriter stringWriter = new StringWriter(); 106 try ( 107 PrintWriter printWriter = new PrintWriter(stringWriter); 108 JSONBean.Writer jsonWriter = bean.open(printWriter)) { 109 jsonWriter.write(getMockMBeanServer(), null, null, false); 110 } 111 112 final Gson gson = GsonUtil.createGson().create(); 113 Type typeOfHashMap = new TypeToken<Map<String, Object>>() {}.getType(); 114 Map<String, Object> expectedJson = gson.fromJson(getExpectedJSON(), typeOfHashMap); 115 Map<String, Object> actualJson = gson.fromJson(stringWriter.toString(), typeOfHashMap); 116 assertEquals(expectedJson, actualJson); 117 } 118}