content: /*
   * 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 com.alibaba.metrics.jvm;
  
  import com.alibaba.metrics.Gauge;
  import com.alibaba.metrics.MetricName;
  import org.junit.Before;
  import org.junit.Test;
  
  import java.lang.management.GarbageCollectorMXBean;
  import java.util.Arrays;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
  
  public class GarbageCollectorMetricSetTest {
      private final GarbageCollectorMXBean gc = mock(GarbageCollectorMXBean.class);
      private final GarbageCollectorMetricSet metrics = new GarbageCollectorMetricSet(Arrays.asList(gc));
  
      private static final MetricName PS_OLDGEN_TIME = MetricName.build("ps_oldgen.time");
      private static final MetricName PS_OLDGEN_COUNT = MetricName.build("ps_oldgen.count");
      private static final MetricName PS_OLDGEN_TIME_DELTA = MetricName.build("ps_oldgen.time.delta");
      private static final MetricName PS_OLDGEN_COUNT_DELTA = MetricName.build("ps_oldgen.count.delta");
  
      @Before
      public void setUp() throws Exception {
          when(gc.getName()).thenReturn("PS OldGen");
          when(gc.getCollectionCount()).thenReturn(1L);
          when(gc.getCollectionTime()).thenReturn(2L);
      }
  
      @Test
      public void hasGaugesForGcCountsAndElapsedTimes() throws Exception {
          assertThat(metrics.getMetrics().keySet())
                  .containsOnly(PS_OLDGEN_TIME, PS_OLDGEN_COUNT, PS_OLDGEN_TIME_DELTA, PS_OLDGEN_COUNT_DELTA);
      }
  
      @Test
      public void hasAGaugeForGcCounts() throws Exception {
          final Gauge gauge = (Gauge) metrics.getMetrics().get(PS_OLDGEN_COUNT);
          assertThat(gauge.getValue())
                  .isEqualTo(1L);
      }
  
      @Test
      public void hasAGaugeForGcTimes() throws Exception {
          final Gauge gauge = (Gauge) metrics.getMetrics().get(PS_OLDGEN_TIME);
          assertThat(gauge.getValue())
                  .isEqualTo(2L);
      }
  
      @Test
      public void autoDiscoversGCs() throws Exception {
          assertThat(new GarbageCollectorMetricSet().getMetrics().keySet())
                  .isNotEmpty();
      }
  }
  