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.coprocessor;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.MockRegionServerServices;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.RegionInfoBuilder;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.regionserver.HRegion;
036import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
037import org.apache.hadoop.hbase.regionserver.RegionServerServices;
038import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
039import org.apache.hadoop.hbase.testclassification.SmallTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.After;
042import org.junit.Before;
043import org.junit.ClassRule;
044import org.junit.Rule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.junit.rules.TestName;
048
049/**
050 * Test CoreCoprocessor Annotation works giving access to facility not usually available. Test
051 * RegionCoprocessor.
052 */
053@Category({ CoprocessorTests.class, SmallTests.class })
054public class TestCoreRegionCoprocessor {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestCoreRegionCoprocessor.class);
059
060  @Rule
061  public TestName name = new TestName();
062  HBaseTestingUtility HTU = HBaseTestingUtility.createLocalHTU();
063  private HRegion region = null;
064  private RegionServerServices rss;
065
066  @Before
067  public void before() throws IOException {
068    String methodName = this.name.getMethodName();
069    TableName tn = TableName.valueOf(methodName);
070    ColumnFamilyDescriptor cfd =
071      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(methodName)).build();
072    TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(cfd).build();
073    RegionInfo ri = RegionInfoBuilder.newBuilder(tn).build();
074    this.rss = new MockRegionServerServices(HTU.getConfiguration());
075    this.region = HRegion.openHRegion(ri, td, null, HTU.getConfiguration(), this.rss, null);
076  }
077
078  @After
079  public void after() throws IOException {
080    this.region.close();
081  }
082
083  /**
084   * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a
085   * RegionServerServices instance after some gymnastics.
086   */
087  public static class NotCoreRegionCoprocessor implements RegionCoprocessor {
088    public NotCoreRegionCoprocessor() {
089    }
090  }
091
092  /**
093   * Annotate with CoreCoprocessor. This should make it so I can get at instance of a
094   * RegionServerServices instance after some gymnastics.
095   */
096  @org.apache.hadoop.hbase.coprocessor.CoreCoprocessor
097  public static class CoreRegionCoprocessor implements RegionCoprocessor {
098    public CoreRegionCoprocessor() {
099    }
100  }
101
102  /**
103   * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to access
104   * a RegionServerServices instance. Assert the opposite too. Do it to RegionCoprocessors. n
105   */
106  @Test
107  public void testCoreRegionCoprocessor() throws IOException {
108    RegionCoprocessorHost rch = region.getCoprocessorHost();
109    RegionCoprocessorEnvironment env =
110      rch.load(null, NotCoreRegionCoprocessor.class.getName(), 0, HTU.getConfiguration());
111    assertFalse(env instanceof HasRegionServerServices);
112    env = rch.load(null, CoreRegionCoprocessor.class.getName(), 1, HTU.getConfiguration());
113    assertTrue(env instanceof HasRegionServerServices);
114    assertEquals(this.rss, ((HasRegionServerServices) env).getRegionServerServices());
115  }
116}