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.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036
037@Category({ SmallTests.class, ClientTests.class })
038public class TestBufferedMutator {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestBufferedMutator.class);
043
044  @Rule
045  public TestName name = new TestName();
046
047  /**
048   * My BufferedMutator. 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 =
060      new BufferedMutatorParams(TableName.valueOf(name.getMethodName()));
061    Configuration conf = HBaseConfiguration.create();
062    conf.set(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
063      DoNothingConnectionRegistry.class.getName());
064    try (Connection connection = ConnectionFactory.createConnection(conf)) {
065      BufferedMutator bm = connection.getBufferedMutator(params);
066      // Assert we get default BM if nothing specified.
067      assertTrue(bm instanceof BufferedMutatorImpl);
068      // Now try and set my own BM implementation.
069      params.implementationClassName(MyBufferedMutator.class.getName());
070      bm = connection.getBufferedMutator(params);
071      assertTrue(bm instanceof MyBufferedMutator);
072    }
073    // Now try creating a Connection after setting an alterate BufferedMutator into
074    // the configuration and confirm we get what was expected.
075    conf.set(BufferedMutator.CLASSNAME_KEY, MyBufferedMutator.class.getName());
076    try (Connection connection = ConnectionFactory.createConnection(conf)) {
077      BufferedMutator bm = connection.getBufferedMutator(params);
078      assertTrue(bm instanceof MyBufferedMutator);
079    }
080  }
081}