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