1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.hbase.regionserver;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
24 
25 import java.util.Map;
26 
27 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
28 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
30 import org.apache.hadoop.hbase.testclassification.MetricsTests;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33 
34 @Category({MetricsTests.class, SmallTests.class})
35 public class TestMetricsRegionSourceImpl {
36 
37   @Test
testCompareToHashCodeEquals()38   public void testCompareToHashCodeEquals() throws Exception {
39     MetricsRegionServerSourceFactory fact = CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class);
40 
41     MetricsRegionSource one = fact.createRegion(new RegionWrapperStub("TEST"));
42     MetricsRegionSource oneClone = fact.createRegion(new RegionWrapperStub("TEST"));
43     MetricsRegionSource two = fact.createRegion(new RegionWrapperStub("TWO"));
44 
45     assertEquals(0, one.compareTo(oneClone));
46     assertEquals(one.hashCode(), oneClone.hashCode());
47     assertNotEquals(one, two);
48 
49     assertTrue( one.compareTo(two) != 0);
50     assertTrue( two.compareTo(one) != 0);
51     assertTrue( two.compareTo(one) != one.compareTo(two));
52     assertTrue( two.compareTo(two) == 0);
53   }
54 
55 
56   @Test(expected = RuntimeException.class)
testNoGetRegionServerMetricsSourceImpl()57   public void testNoGetRegionServerMetricsSourceImpl() throws Exception {
58     // This should throw an exception because MetricsRegionSourceImpl should only
59     // be created by a factory.
60     CompatibilitySingletonFactory.getInstance(MetricsRegionSource.class);
61   }
62 
63   static class RegionWrapperStub implements MetricsRegionWrapper {
64 
65     private String regionName;
66 
RegionWrapperStub(String regionName)67     public RegionWrapperStub(String regionName) {
68       this.regionName = regionName;
69     }
70 
71     @Override
getTableName()72     public String getTableName() {
73       return null;
74     }
75 
76     @Override
getNamespace()77     public String getNamespace() {
78       return null;
79     }
80 
81     @Override
getRegionName()82     public String getRegionName() {
83       return this.regionName;
84     }
85 
86     @Override
getNumStores()87     public long getNumStores() {
88       return 0;
89     }
90 
91     @Override
getNumStoreFiles()92     public long getNumStoreFiles() {
93       return 0;
94     }
95 
96     @Override
getMemstoreSize()97     public long getMemstoreSize() {
98       return 0;
99     }
100 
101     @Override
getStoreFileSize()102     public long getStoreFileSize() {
103       return 0;
104     }
105 
106     @Override
getReadRequestCount()107     public long getReadRequestCount() {
108       return 0;
109     }
110 
111     @Override
getWriteRequestCount()112     public long getWriteRequestCount() {
113       return 0;
114     }
115 
116     @Override
getNumFilesCompacted()117     public long getNumFilesCompacted() {
118       return 0;
119     }
120 
121     @Override
getNumBytesCompacted()122     public long getNumBytesCompacted() {
123       return 0;
124     }
125 
126     @Override
getNumCompactionsCompleted()127     public long getNumCompactionsCompleted() {
128       return 0;
129     }
130 
131     @Override
getRegionHashCode()132     public int getRegionHashCode() {
133       return regionName.hashCode();
134     }
135 
136     /**
137      * Always return 0 for testing
138      */
139     @Override
getReplicaId()140     public int getReplicaId() {
141       return 0;
142     }
143   }
144 }
145