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.metrics.impl;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.metrics.Counter;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.Before;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030/**
031 * Test class for {@link CounterImpl}.
032 */
033@Category(SmallTests.class)
034public class TestCounterImpl {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestCounterImpl.class);
039
040  private Counter counter;
041
042  @Before public void setup() {
043    this.counter = new CounterImpl();
044  }
045
046  @Test public void testCounting() {
047    counter.increment();
048    assertEquals(1L, counter.getCount());
049    counter.increment();
050    assertEquals(2L, counter.getCount());
051    counter.increment(2L);
052    assertEquals(4L, counter.getCount());
053    counter.increment(-1L);
054    assertEquals(3L, counter.getCount());
055
056    counter.decrement();
057    assertEquals(2L, counter.getCount());
058    counter.decrement();
059    assertEquals(1L, counter.getCount());
060    counter.decrement(4L);
061    assertEquals(-3L, counter.getCount());
062    counter.decrement(-3L);
063    assertEquals(0L, counter.getCount());
064  }
065}