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