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.assertTrue; 021 022import java.io.IOException; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.ipc.RpcControllerFactory; 028import org.apache.hadoop.hbase.testclassification.ClientTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.junit.ClassRule; 031import org.junit.Rule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034import org.junit.rules.TestName; 035 036@Category({SmallTests.class, ClientTests.class}) 037public class TestBufferedMutator { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestBufferedMutator.class); 042 043 @Rule 044 public TestName name = new TestName(); 045 046 /** 047 * My BufferedMutator. 048 * Just to prove that I can insert a BM other than default. 049 */ 050 public static class MyBufferedMutator extends BufferedMutatorImpl { 051 MyBufferedMutator(ClusterConnection conn, RpcRetryingCallerFactory rpcCallerFactory, 052 RpcControllerFactory rpcFactory, BufferedMutatorParams params) { 053 super(conn, rpcCallerFactory, rpcFactory, params); 054 } 055 } 056 057 @Test 058 public void testAlternateBufferedMutatorImpl() throws IOException { 059 BufferedMutatorParams params = new BufferedMutatorParams(TableName.valueOf(name.getMethodName())); 060 Configuration conf = HBaseConfiguration.create(); 061 conf.set(AsyncRegistryFactory.REGISTRY_IMPL_CONF_KEY, DoNothingAsyncRegistry.class.getName()); 062 try (Connection connection = ConnectionFactory.createConnection(conf)) { 063 BufferedMutator bm = connection.getBufferedMutator(params); 064 // Assert we get default BM if nothing specified. 065 assertTrue(bm instanceof BufferedMutatorImpl); 066 // Now try and set my own BM implementation. 067 params.implementationClassName(MyBufferedMutator.class.getName()); 068 bm = connection.getBufferedMutator(params); 069 assertTrue(bm instanceof MyBufferedMutator); 070 } 071 // Now try creating a Connection after setting an alterate BufferedMutator into 072 // the configuration and confirm we get what was expected. 073 conf.set(BufferedMutator.CLASSNAME_KEY, MyBufferedMutator.class.getName()); 074 try (Connection connection = ConnectionFactory.createConnection(conf)) { 075 BufferedMutator bm = connection.getBufferedMutator(params); 076 assertTrue(bm instanceof MyBufferedMutator); 077 } 078 } 079}