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.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNull;
023
024import java.lang.management.GarbageCollectorMXBean;
025import java.lang.management.ManagementFactory;
026import java.util.Hashtable;
027import java.util.List;
028import java.util.Map;
029import javax.management.MalformedObjectNameException;
030import javax.management.ObjectName;
031import javax.management.openmbean.CompositeData;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({ MiscTests.class, SmallTests.class })
042public class TestJSONMetricUtil {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestJSONMetricUtil.class);
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestJSONMetricUtil.class);
049
050  @Test
051  public void testBuildHashtable() {
052    String[] keys = { "type", "name" };
053    String[] emptyKey = {};
054    String[] values = { "MemoryPool", "Par Eden Space" };
055    String[] values2 = { "MemoryPool", "Par Eden Space", "Test" };
056    String[] emptyValue = {};
057    Map<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values);
058    assertEquals(values[0], properties.get("type"));
059    assertEquals(values[1], properties.get("name"));
060
061    assertNull(JSONMetricUtil.buldKeyValueTable(keys, values2));
062    assertNull(JSONMetricUtil.buldKeyValueTable(keys, emptyValue));
063    assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, values2));
064    assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, emptyValue));
065  }
066
067  @Test
068  public void testBuildObjectName() throws MalformedObjectNameException {
069    String[] keys = { "type", "name" };
070    String[] values = { "MemoryPool", "Par Eden Space" };
071    Hashtable<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values);
072    ObjectName testObject =
073      JSONMetricUtil.buildObjectName(JSONMetricUtil.JAVA_LANG_DOMAIN, properties);
074    assertEquals(JSONMetricUtil.JAVA_LANG_DOMAIN, testObject.getDomain());
075    assertEquals(testObject.getKeyPropertyList(), properties);
076  }
077
078  @Test
079  public void testGetLastGCInfo() {
080    List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
081    for (GarbageCollectorMXBean bean : gcBeans) {
082      ObjectName on = bean.getObjectName();
083      Object value = JSONMetricUtil.getValueFromMBean(on, "LastGcInfo");
084      LOG.info("Collector Info: " + value);
085      if (value != null && value instanceof CompositeData) {
086        CompositeData cds = (CompositeData) value;
087        assertNotNull(cds.get("duration"));
088      }
089    }
090  }
091}