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.HBaseTestingUtil;
027import org.apache.hadoop.hbase.MockRegionServerServices;
028import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost;
029import org.apache.hadoop.hbase.regionserver.RegionServerServices;
030import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.After;
033import org.junit.Before;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039
040/**
041 * Test CoreCoprocessor Annotation works giving access to facility not usually available. Test
042 * RegionServerCoprocessor.
043 */
044@Category({ CoprocessorTests.class, SmallTests.class })
045public class TestCoreRegionServerCoprocessor {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestCoreRegionServerCoprocessor.class);
050
051  @Rule
052  public TestName name = new TestName();
053  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
054  private RegionServerServices rss;
055  private RegionServerCoprocessorHost rsch;
056
057  @Before
058  public void before() throws IOException {
059    String methodName = this.name.getMethodName();
060    this.rss = new MockRegionServerServices(HTU.getConfiguration());
061    this.rsch = new RegionServerCoprocessorHost(this.rss, HTU.getConfiguration());
062  }
063
064  @After
065  public void after() throws IOException {
066    this.rsch.preStop("Stopping", null);
067  }
068
069  /**
070   * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a
071   * RegionServerServices instance after some gymnastics.
072   */
073  public static class NotCoreRegionServerCoprocessor implements RegionServerCoprocessor {
074    public NotCoreRegionServerCoprocessor() {
075    }
076  }
077
078  /**
079   * Annotate with CoreCoprocessor. This should make it so I can get at instance of a
080   * RegionServerServices instance after some gymnastics.
081   */
082  @CoreCoprocessor
083  public static class CoreRegionServerCoprocessor implements RegionServerCoprocessor {
084    public CoreRegionServerCoprocessor() {
085    }
086  }
087
088  /**
089   * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to access
090   * a RegionServerServices instance. Assert the opposite too. Do it to RegionServerCoprocessors.
091   */
092  @Test
093  public void testCoreRegionCoprocessor() throws IOException {
094    RegionServerCoprocessorEnvironment env =
095      rsch.load(null, NotCoreRegionServerCoprocessor.class.getName(), 0, HTU.getConfiguration());
096    assertFalse(env instanceof HasRegionServerServices);
097    env = rsch.load(null, CoreRegionServerCoprocessor.class.getName(), 1, HTU.getConfiguration());
098    assertTrue(env instanceof HasRegionServerServices);
099    assertEquals(this.rss, ((HasRegionServerServices) env).getRegionServerServices());
100  }
101}