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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.fail;
022
023import java.lang.reflect.Field;
024import java.lang.reflect.Modifier;
025import java.util.concurrent.TimeUnit;
026
027import org.apache.hadoop.hbase.testclassification.IntegrationTests;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.junit.ClassRule;
030import org.junit.experimental.categories.Category;
031import org.junit.runner.Description;
032import org.junit.runner.notification.RunListener;
033import org.junit.runner.notification.RunListener.ThreadSafe;
034
035/**
036 * A RunListener to confirm that we have a {@link CategoryBasedTimeout} class rule for every test.
037 */
038@InterfaceAudience.Private
039@ThreadSafe
040public class HBaseClassTestRuleChecker extends RunListener {
041
042  @Override
043  public void testStarted(Description description) throws Exception {
044    Category[] categories = description.getTestClass().getAnnotationsByType(Category.class);
045    for (Class<?> c : categories[0].value()) {
046      if (c == IntegrationTests.class) {
047        return;
048      }
049    }
050    for (Field field : description.getTestClass().getFields()) {
051      if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class &&
052        field.isAnnotationPresent(ClassRule.class)) {
053        HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null);
054        assertEquals(
055          "The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() +
056            " is for " + timeout.getClazz().getName(),
057          description.getTestClass(), timeout.getClazz());
058        return;
059      }
060    }
061    fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName());
062  }
063}