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.util.Arrays;
026import java.util.List;
027import java.util.stream.Collectors;
028import org.apache.hadoop.hbase.Abortable;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038@Category({ ClientTests.class, SmallTests.class })
039public class TestInterfaceAlign {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043      HBaseClassTestRule.forClass(TestInterfaceAlign.class);
044
045  private static final Logger LOG = LoggerFactory.getLogger(TestInterfaceAlign.class);
046
047  /**
048   * Test methods name match up
049   */
050  @Test
051  public void testAdminWithAsyncAdmin() {
052    List<String> adminMethodNames = getMethodNames(Admin.class);
053    List<String> asyncAdminMethodNames = getMethodNames(AsyncAdmin.class);
054
055    // Remove some special methods
056    adminMethodNames.remove("getOperationTimeout");
057    adminMethodNames.remove("getConnection");
058    adminMethodNames.remove("getConfiguration");
059    adminMethodNames.removeAll(getMethodNames(Abortable.class));
060    adminMethodNames.removeAll(getMethodNames(Closeable.class));
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        .map(Method::getName).distinct().collect(Collectors.toList());
082  }
083
084  private boolean isDeprecated(Method method) {
085    Annotation[] annotations = method.getDeclaredAnnotations();
086    for (Annotation annotation : annotations) {
087      if (annotation instanceof Deprecated) {
088        return true;
089      }
090    }
091    return false;
092  }
093}