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    adminMethodNames.forEach(method -> {
061      boolean contains = asyncAdminMethodNames.contains(method);
062      if (method.endsWith("Async")) {
063        contains = asyncAdminMethodNames.contains(method.replace("Async", ""));
064      }
065      assertTrue("Admin method " + method + " should in AsyncAdmin too", contains);
066    });
067    asyncAdminMethodNames.forEach(method -> {
068      boolean contains = adminMethodNames.contains(method);
069      if (!contains) {
070        contains = adminMethodNames.contains(method + "Async");
071      }
072      assertTrue("AsyncAdmin method " + method + " should in Admin too", contains);
073    });
074  }
075
076  private <T> List<String> getMethodNames(Class<T> c) {
077    // DON'T use the getDeclaredMethods as we want to check the Public APIs only.
078    return Arrays.asList(c.getMethods()).stream().filter(m -> !isDeprecated(m))
079      .filter(m -> !Modifier.isStatic(m.getModifiers())).map(Method::getName).distinct()
080      .collect(Collectors.toList());
081  }
082
083  private boolean isDeprecated(Method method) {
084    Annotation[] annotations = method.getDeclaredAnnotations();
085    for (Annotation annotation : annotations) {
086      if (annotation instanceof Deprecated) {
087        return true;
088      }
089    }
090    return false;
091  }
092}