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