001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.modeshape.common.junit; 018 019import java.lang.annotation.Annotation; 020import org.junit.rules.TestRule; 021import org.junit.runner.Description; 022import org.junit.runners.model.Statement; 023import org.modeshape.common.util.StringUtil; 024 025/** 026 * JUnit rule that inspects the presence of the {@link SkipLongRunning} annotation either on a test method or on a test suite. If 027 * it finds the annotation, it will only run the test method/suite if the system property {@code skipLongRunningTests} has the 028 * value {@code true} 029 * 030 * @author Horia Chiorean (hchiorea@redhat.com) 031 */ 032public class SkipTestRule implements TestRule { 033 034 @Override 035 public Statement apply( Statement base, 036 Description description ) { 037 SkipLongRunning skipLongRunningAnnotation = hasAnnotation(description, SkipLongRunning.class); 038 if (skipLongRunningAnnotation != null) { 039 boolean skipLongRunning = Boolean.valueOf(System.getProperty(SkipLongRunning.SKIP_LONG_RUNNING_PROPERTY)); 040 if (skipLongRunning) { 041 return emptyStatement(skipLongRunningAnnotation.value(), description); 042 } 043 } 044 045 SkipOnOS skipOnOSAnnotation = hasAnnotation(description, SkipOnOS.class); 046 if (skipOnOSAnnotation != null) { 047 String[] oses = skipOnOSAnnotation.value(); 048 String osName = System.getProperty("os.name"); 049 if (!StringUtil.isBlank(osName)) { 050 for (String os : oses) { 051 if (osName.toLowerCase().startsWith(os.toLowerCase())) { 052 return emptyStatement(skipOnOSAnnotation.description(), description); 053 } 054 } 055 } 056 } 057 058 return base; 059 } 060 061 private <T extends Annotation> T hasAnnotation( Description description, Class<T> annotationClass ) { 062 T annotation = description.getAnnotation(annotationClass); 063 if (annotation != null) { 064 return annotation; 065 } else if (description.isTest() && description.getTestClass().isAnnotationPresent(annotationClass)) { 066 return description.getTestClass().getAnnotation(annotationClass); 067 } 068 return null; 069 } 070 071 private static Statement emptyStatement( final String reason, final Description description ) { 072 return new Statement() { 073 @Override 074 public void evaluate() throws Throwable { 075 StringBuilder messageBuilder = new StringBuilder(description.testCount()); 076 messageBuilder.append("Skipped ").append(description.toString()); 077 if (!StringUtil.isBlank(reason)) { 078 messageBuilder.append(" because: ").append(reason); 079 } 080 System.out.println(messageBuilder.toString()); 081 } 082 }; 083 } 084}