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.regionserver; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotEquals; 022import static org.junit.jupiter.api.Assertions.assertThrows; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import org.apache.hadoop.hbase.CompatibilitySingletonFactory; 026import org.apache.hadoop.hbase.testclassification.MetricsTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.jupiter.api.Tag; 029import org.junit.jupiter.api.Test; 030 031@Tag(MetricsTests.TAG) 032@Tag(SmallTests.TAG) 033public class TestMetricsUserSourceImpl { 034 035 @SuppressWarnings("SelfComparison") 036 @Test 037 public void testCompareToHashCodeEquals() throws Exception { 038 MetricsRegionServerSourceFactory fact = 039 CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class); 040 041 MetricsUserSource one = fact.createUser("ONE"); 042 MetricsUserSource oneClone = fact.createUser("ONE"); 043 MetricsUserSource two = fact.createUser("TWO"); 044 045 assertEquals(0, one.compareTo(oneClone)); 046 assertEquals(one.hashCode(), oneClone.hashCode()); 047 assertNotEquals(one, two); 048 049 assertTrue(one.compareTo(two) != 0); 050 assertTrue(two.compareTo(one) != 0); 051 assertTrue(two.compareTo(one) != one.compareTo(two)); 052 assertTrue(two.compareTo(two) == 0); 053 } 054 055 @Test 056 public void testNoGetRegionServerMetricsSourceImpl() throws Exception { 057 // This should throw an exception because MetricsUserSourceImpl should only 058 // be created by a factory. 059 assertThrows(RuntimeException.class, () -> { 060 CompatibilitySingletonFactory.getInstance(MetricsUserSource.class); 061 }); 062 } 063 064 @Test 065 public void testGetUser() { 066 MetricsRegionServerSourceFactory fact = 067 CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class); 068 069 MetricsUserSource one = fact.createUser("ONE"); 070 assertEquals("ONE", one.getUser()); 071 } 072 073}