1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17package org.apache.geode.test.junit.rules;18import java.io.Serializable;19import java.text.DateFormat;20import java.text.ParseException;21import java.text.SimpleDateFormat;22import java.util.Calendar;23import org.junit.AssumptionViolatedException;24import org.junit.rules.TestRule;25import org.junit.runner.Description;26import org.junit.runners.model.Statement;27import org.apache.geode.test.junit.ConditionalIgnore;28import org.apache.geode.test.junit.IgnoreCondition;29import org.apache.geode.test.junit.support.IgnoreConditionEvaluationException;30/**31 * The ConditionalIgnoreRule class...32 *33 * @see org.junit.rules.TestRule34 * @see org.junit.runner.Description35 * @see org.junit.runners.model.Statement36 * @see org.apache.geode.test.junit.ConditionalIgnore37 * @see org.apache.geode.test.junit.IgnoreCondition38 */39@SuppressWarnings({ "serial", "unused" })40public class ConditionalIgnoreRule implements TestRule, Serializable {41 protected static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";42 protected static final String DEFAULT_MESSAGE = "Ignoring test case (%1$s) of test class (%2$s)!";43 protected static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_PATTERN);44 @Override45 public Statement apply(final Statement base, final Description description) {46 return new Statement() {47 @Override public void evaluate() throws Throwable {48 ConditionalIgnoreRule.this.evaluate(base, description);49 }50 };51 }52 public final void evaluate(Statement statement, Description description) throws Throwable {53 throwOnIgnoreTest(statement, description).evaluate();54 }55 protected Statement throwOnIgnoreTest(Statement statement, Description description) {56 if (isTest(description)) {57 boolean ignoreTest = false;58 String message = "";59 ConditionalIgnore testCaseAnnotation = description.getAnnotation(ConditionalIgnore.class);60 if (testCaseAnnotation != null) {61 ignoreTest = evaluate(testCaseAnnotation, description);62 message = testCaseAnnotation.value();63 }64 else if (description.getTestClass().isAnnotationPresent(ConditionalIgnore.class)) {65 ConditionalIgnore testClassAnnotation = description.getTestClass().getAnnotation(ConditionalIgnore.class);66 ignoreTest = evaluate(testClassAnnotation, description);67 message = testClassAnnotation.value();68 }69 if (ignoreTest) {70 throw new AssumptionViolatedException(format(message, description));71 }72 }73 return statement;74 }75 protected boolean isTest(final Description description) {76 return (description.isSuite() || description.isTest());77 }78 protected String format(String message, Description description) {79 message = (!message.isEmpty() ? message : DEFAULT_MESSAGE);80 return String.format(message, description.getMethodName(), description.getClassName());81 }82 protected boolean evaluate(ConditionalIgnore conditionalIgnoreAnnotation, Description description) {83 return (evaluateCondition(conditionalIgnoreAnnotation.condition(), description)84 || evaluateUntil(conditionalIgnoreAnnotation.until()));85 }86 protected boolean evaluateCondition(Class<? extends IgnoreCondition> ignoreConditionType, Description description) {87 try {88 return ignoreConditionType.newInstance().evaluate(description);89 }90 catch (Exception e) {91 throw new IgnoreConditionEvaluationException(String.format("failed to evaluate IgnoreCondition: %1$s",92 ignoreConditionType.getName()), e);93 }94 }95 protected boolean evaluateUntil(String timestamp) {96 try {97 return DATE_FORMAT.parse(timestamp).after(Calendar.getInstance().getTime());98 }99 catch (ParseException e) {100 return false;101 }102 }103}...