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.client; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertSame; 022import static org.mockito.Mockito.mock; 023import static org.mockito.Mockito.mockConstruction; 024import static org.mockito.Mockito.mockStatic; 025 026import java.net.URI; 027import java.util.List; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseConfiguration; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.security.User; 033import org.apache.hadoop.hbase.testclassification.ClientTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.apache.hadoop.hbase.util.ReflectionUtils; 036import org.junit.After; 037import org.junit.Before; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.mockito.ArgumentCaptor; 042import org.mockito.MockedConstruction; 043import org.mockito.MockedStatic; 044 045/** 046 * Make sure we can successfully parse the URI component 047 */ 048@Category({ ClientTests.class, SmallTests.class }) 049public class TestConnectionRegistryUriParsing { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestConnectionRegistryUriParsing.class); 054 055 private Configuration conf; 056 057 private User user; 058 059 private MockedConstruction<RpcConnectionRegistry> mockedRpcRegistry; 060 061 private MockedConstruction<ZKConnectionRegistry> mockedZkRegistry; 062 063 private MockedStatic<ReflectionUtils> mockedReflectionUtils; 064 065 private List<?> args; 066 067 @Before 068 public void setUp() { 069 conf = HBaseConfiguration.create(); 070 user = mock(User.class); 071 args = null; 072 mockedRpcRegistry = mockConstruction(RpcConnectionRegistry.class, (mock, context) -> { 073 args = context.arguments(); 074 }); 075 mockedZkRegistry = mockConstruction(ZKConnectionRegistry.class, (mock, context) -> { 076 args = context.arguments(); 077 }); 078 mockedReflectionUtils = mockStatic(ReflectionUtils.class); 079 } 080 081 @After 082 public void tearDown() { 083 mockedRpcRegistry.closeOnDemand(); 084 mockedZkRegistry.closeOnDemand(); 085 mockedReflectionUtils.closeOnDemand(); 086 } 087 088 @Test 089 public void testParseRpcSingle() throws Exception { 090 ConnectionRegistryFactory.create(new URI("hbase+rpc://server1:123"), conf, user); 091 assertEquals(1, mockedRpcRegistry.constructed().size()); 092 assertSame(user, args.get(1)); 093 Configuration conf = (Configuration) args.get(0); 094 assertEquals("server1:123", conf.get(RpcConnectionRegistry.BOOTSTRAP_NODES)); 095 } 096 097 @Test 098 public void testParseRpcMultiple() throws Exception { 099 ConnectionRegistryFactory.create(new URI("hbase+rpc://server1:123,server2:456,server3:789"), 100 conf, user); 101 assertEquals(1, mockedRpcRegistry.constructed().size()); 102 assertSame(user, args.get(1)); 103 Configuration conf = (Configuration) args.get(0); 104 assertEquals("server1:123,server2:456,server3:789", 105 conf.get(RpcConnectionRegistry.BOOTSTRAP_NODES)); 106 } 107 108 @Test 109 public void testParseZkSingle() throws Exception { 110 ConnectionRegistryFactory.create(new URI("hbase+zk://server1:123/root"), conf, user); 111 assertEquals(1, mockedZkRegistry.constructed().size()); 112 assertSame(user, args.get(1)); 113 Configuration conf = (Configuration) args.get(0); 114 assertEquals("server1:123", conf.get(HConstants.CLIENT_ZOOKEEPER_QUORUM)); 115 assertEquals("/root", conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT)); 116 } 117 118 @Test 119 public void testParseZkMultiple() throws Exception { 120 ConnectionRegistryFactory 121 .create(new URI("hbase+zk://server1:123,server2:456,server3:789/root/path"), conf, user); 122 assertEquals(1, mockedZkRegistry.constructed().size()); 123 assertSame(user, args.get(1)); 124 Configuration conf = (Configuration) args.get(0); 125 assertEquals("server1:123,server2:456,server3:789", 126 conf.get(HConstants.CLIENT_ZOOKEEPER_QUORUM)); 127 assertEquals("/root/path", conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT)); 128 } 129 130 @Test 131 public void testFallbackNoScheme() throws Exception { 132 conf.setClass(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY, ZKConnectionRegistry.class, 133 ConnectionRegistry.class); 134 ConnectionRegistryFactory.create(new URI("server1:2181/path"), conf, user); 135 ArgumentCaptor<Class<?>> clazzCaptor = ArgumentCaptor.forClass(Class.class); 136 ArgumentCaptor<Object[]> argsCaptor = ArgumentCaptor.forClass(Object[].class); 137 mockedReflectionUtils 138 .verify(() -> ReflectionUtils.newInstance(clazzCaptor.capture(), argsCaptor.capture())); 139 assertEquals(ZKConnectionRegistry.class, clazzCaptor.getValue()); 140 assertSame(conf, argsCaptor.getValue()[0]); 141 assertSame(user, argsCaptor.getValue()[1]); 142 } 143 144 @Test 145 public void testFallbackNoCreator() throws Exception { 146 conf.setClass(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY, RpcConnectionRegistry.class, 147 ConnectionRegistry.class); 148 ConnectionRegistryFactory.create(new URI("hbase+tls://server1:123/path"), conf, user); 149 ArgumentCaptor<Class<?>> clazzCaptor = ArgumentCaptor.forClass(Class.class); 150 ArgumentCaptor<Object[]> argsCaptor = ArgumentCaptor.forClass(Object[].class); 151 mockedReflectionUtils 152 .verify(() -> ReflectionUtils.newInstance(clazzCaptor.capture(), argsCaptor.capture())); 153 assertEquals(RpcConnectionRegistry.class, clazzCaptor.getValue()); 154 assertSame(conf, argsCaptor.getValue()[0]); 155 assertSame(user, argsCaptor.getValue()[1]); 156 } 157}