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