You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hbase.apache.org by GitBox <gi...@apache.org> on 2020/06/30 07:54:51 UTC

[GitHub] [hbase] virajjasani commented on a change in pull request #1999: HBASE-24657 fix JsonBean.java from HBASE-23015 HBASE-20571

virajjasani commented on a change in pull request #1999:
URL: https://github.com/apache/hbase/pull/1999#discussion_r447476731



##########
File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestJSONBean.java
##########
@@ -0,0 +1,104 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.HashSet;
+import java.util.Set;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.QueryExp;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * Test {@link JSONBean}.
+ */
+@Category(SmallTests.class)
+public class TestJSONBean {
+  private MBeanServer getMockMBeanServer() throws Exception {
+    MBeanServer mbeanServer = mock(MBeanServer.class);
+    Set<ObjectName> names = new HashSet<>();
+    names.add(new ObjectName("test1:type=test2"));
+    when(mbeanServer.queryNames(any(ObjectName.class), any(QueryExp.class))).thenReturn(names);
+    MBeanInfo mbeanInfo = mock(MBeanInfo.class);
+    when(mbeanInfo.getClassName()).thenReturn("testClassName");
+    String[] attributeNames = new String[] {"intAttr", "nanAttr", "strAttr", "boolAttr"};
+    MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length];
+    for (int i = 0; i < attributeInfos.length; i++) {
+      attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i],
+        null,
+        null,
+        true,
+        false,
+        false);
+    }
+    when(mbeanInfo.getAttributes()).thenReturn(attributeInfos);
+    when(mbeanServer.getMBeanInfo(any(ObjectName.class))).thenReturn(mbeanInfo);
+    when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(3);

Review comment:
       Another test with infinity coverage here will also be good one to cover our code and that is supposed to print. 
   Basically we can do something like this:
   
   ```
     private MBeanServer getMockMBeanServer(Number number) throws Exception {
   ....
   ...
       when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(number);
     }
   ```
   ```
     private String getExpectedJSON(String str) {
   ...
   ...
       pw.println("      \"intAttr\": "+str+",");
     }
   ```
   
   and then call
   ```
       jsonWriter.write(getMockMBeanServer(10.0/0.0), null, null, false);
       assertEquals(mapper.readTree(getExpectedJSON("\"Infinity\"")), mapper.readTree(stringWriter.toString()));
   
       jsonWriter.write(getMockMBeanServer(3), null, null, false);
       assertEquals(mapper.readTree(getExpectedJSON("3")), mapper.readTree(stringWriter.toString()));
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org