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. 051 * Test 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 public TestName name = new TestName(); 061 HBaseTestingUtility HTU = HBaseTestingUtility.createLocalHTU(); 062 private HRegion region = null; 063 private RegionServerServices rss; 064 065 @Before 066 public void before() throws IOException { 067 String methodName = this.name.getMethodName(); 068 TableName tn = TableName.valueOf(methodName); 069 ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(methodName)).build(); 070 TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(cfd).build(); 071 RegionInfo ri = RegionInfoBuilder.newBuilder(tn).build(); 072 this.rss = new MockRegionServerServices(HTU.getConfiguration()); 073 this.region = HRegion.openHRegion(ri, td, null, HTU.getConfiguration(), this.rss, null); 074 } 075 076 @After 077 public void after() throws IOException { 078 this.region.close(); 079 } 080 081 /** 082 * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a 083 * RegionServerServices instance after some gymnastics. 084 */ 085 public static class NotCoreRegionCoprocessor implements RegionCoprocessor { 086 public NotCoreRegionCoprocessor() {} 087 } 088 089 /** 090 * Annotate with CoreCoprocessor. This should make it so I can get at instance of a 091 * RegionServerServices instance after some gymnastics. 092 */ 093 @org.apache.hadoop.hbase.coprocessor.CoreCoprocessor 094 public static class CoreRegionCoprocessor implements RegionCoprocessor { 095 public CoreRegionCoprocessor() {} 096 } 097 098 /** 099 * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to 100 * access a RegionServerServices instance. Assert the opposite too. 101 * Do it to RegionCoprocessors. 102 * @throws IOException 103 */ 104 @Test 105 public void testCoreRegionCoprocessor() throws IOException { 106 RegionCoprocessorHost rch = region.getCoprocessorHost(); 107 RegionCoprocessorEnvironment env = 108 rch.load(null, NotCoreRegionCoprocessor.class.getName(), 0, HTU.getConfiguration()); 109 assertFalse(env instanceof HasRegionServerServices); 110 env = rch.load(null, CoreRegionCoprocessor.class.getName(), 1, HTU.getConfiguration()); 111 assertTrue(env instanceof HasRegionServerServices); 112 assertEquals(this.rss, ((HasRegionServerServices)env).getRegionServerServices()); 113 } 114}