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.Closeable;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Method;
025import java.lang.reflect.Modifier;
026import java.util.Arrays;
027import java.util.List;
028import java.util.stream.Collectors;
029import org.apache.hadoop.hbase.Abortable;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ ClientTests.class, SmallTests.class })
038public class TestInterfaceAlign {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestInterfaceAlign.class);
043
044  /**
045   * Test methods name match up
046   */
047  @Test
048  public void testAdminWithAsyncAdmin() {
049    List<String> adminMethodNames = getMethodNames(Admin.class);
050    List<String> asyncAdminMethodNames = getMethodNames(AsyncAdmin.class);
051
052    // Remove some special methods
053    adminMethodNames.remove("getOperationTimeout");
054    adminMethodNames.remove("getSyncWaitTimeout");
055    adminMethodNames.remove("getConnection");
056    adminMethodNames.remove("getConfiguration");
057    adminMethodNames.removeAll(getMethodNames(Abortable.class));
058    adminMethodNames.removeAll(getMethodNames(Closeable.class));
059
060    asyncAdminMethodNames.remove("coprocessorService");
061
062    adminMethodNames.forEach(method -> {
063      boolean contains = asyncAdminMethodNames.contains(method);
064      if (method.endsWith("Async")) {
065        contains = asyncAdminMethodNames.contains(method.replace("Async", ""));
066      }
067      assertTrue("Admin method " + method + " should in AsyncAdmin too", contains);
068    });
069    asyncAdminMethodNames.forEach(method -> {
070      boolean contains = adminMethodNames.contains(method);
071      if (!contains) {
072        contains = adminMethodNames.contains(method + "Async");
073      }
074      assertTrue("AsyncAdmin method " + method + " should in Admin too", contains);
075    });
076  }
077
078  private <T> List<String> getMethodNames(Class<T> c) {
079    // DON'T use the getDeclaredMethods as we want to check the Public APIs only.
080    return Arrays.asList(c.getMethods()).stream().filter(m -> !isDeprecated(m))
081      .filter(m -> !Modifier.isStatic(m.getModifiers())).map(Method::getName).distinct()
082      .collect(Collectors.toList());
083  }
084
085  private boolean isDeprecated(Method method) {
086    Annotation[] annotations = method.getDeclaredAnnotations();
087    for (Annotation annotation : annotations) {
088      if (annotation instanceof Deprecated) {
089        return true;
090      }
091    }
092    return false;
093  }
094}