diff options
Diffstat (limited to 'tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/JUnitUtils.java')
-rw-r--r-- | tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/JUnitUtils.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/JUnitUtils.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/JUnitUtils.java new file mode 100644 index 00000000000..17bfadc190c --- /dev/null +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/JUnitUtils.java @@ -0,0 +1,116 @@ +/***************************************************************************** + * Copyright (c) 2015 Christian W. Damus and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian W. Damus - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.junit.utils; + +import java.lang.annotation.Annotation; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; + +/** + * Utilities for working with the JUnit data model. + */ +public class JUnitUtils { + + /** + * Not instantiable by clients. + */ + private JUnitUtils() { + super(); + } + + /** + * Obtains the test class implied by a {@code description} that is supplied to a {@link TestRule}. + * + * @param description + * a rule's owning description, which generally would be a test method or a test class + * (as these are the contexts in which rules are invoked) + * + * @return the test class, or {@code null} in the unlikely event that none can be found + */ + public static Class<?> getTestClass(Description description) { + Class<?> result = description.getTestClass(); + + if (result == null) { + for (Description child : description.getChildren()) { + result = getTestClass(child); + if (result != null) { + break; + } + } + } + + return result; + } + + /** + * Obtains the annotation of the specified {@code type} applicable to a {@code description} that is supplied to a {@link TestRule}. + * If the description is for a test method, then if that method doesn't have the requested annotation, its owning class is searched + * for the annotation. + * + * @param description + * a rule's owning description, which generally would be a test method or a test class + * (as these are the contexts in which rules are invoked) + * @param type + * the annotation type to look for + * + * @return the requested annotation, or {@code null} if none was found + */ + public static <A extends Annotation> A getAnnotation(Description description, Class<A> type) { + A result = description.getAnnotation(type); + + if ((result == null) && (description.getTestClass() != null)) { + result = description.getTestClass().getAnnotation(type); + } + + return result; + } + + /** + * Obtains the annotation of any one of the specified {@code types} applicable to a {@code description} that is supplied to a {@link TestRule}. + * If the description is for a test method, then if that method doesn't have any of the requested annotations, its owning class is searched + * for the annotations. + * + * @param description + * a rule's owning description, which generally would be a test method or a test class + * (as these are the contexts in which rules are invoked) + * @param types + * the annotation types to look for + * + * @return the first available of the requested annotations, or {@code null} if none was found + */ + @SafeVarargs + public static Annotation getAnyAnnotation(Description description, final Class<? extends Annotation>... types) { + Annotation result = null; + + for (Class<? extends Annotation> next : types) { + result = description.getAnnotation(next); + if (result != null) { + break; + } + } + + if ((result == null) && (description.getTestClass() != null)) { + Class<?> testClass = description.getTestClass(); + for (Class<? extends Annotation> next : types) { + result = testClass.getAnnotation(next); + if (result != null) { + break; + } + } + } + + return result; + } +} |