|
| 1 | +/* |
| 2 | + * This file is part of LaunchWrapperTestSuite, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.lwts.runner; |
| 26 | + |
| 27 | +import java.lang.annotation.ElementType; |
| 28 | +import java.lang.annotation.Inherited; |
| 29 | +import java.lang.annotation.Retention; |
| 30 | +import java.lang.annotation.RetentionPolicy; |
| 31 | +import java.lang.annotation.Target; |
| 32 | + |
| 33 | +import org.junit.runner.Description; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runner.Runner; |
| 36 | +import org.junit.runner.notification.RunNotifier; |
| 37 | +import org.junit.runners.model.InitializationError; |
| 38 | +import org.junit.runners.model.RunnerBuilder; |
| 39 | + |
| 40 | +import com.google.common.collect.Lists; |
| 41 | + |
| 42 | +/** |
| 43 | + * A runner that delegates to another runner. Note that this class does not do anything |
| 44 | + * to run in a Launchwrapper context; use {@link LaunchWrapperDelegateRunner} for that. |
| 45 | + * |
| 46 | + * When using this directly with {@link RunWith}, also specify a {@link DelegatedRunWith} |
| 47 | + * annotation to indicate the runner to delegate to. |
| 48 | + */ |
| 49 | +public class DelegateRunner extends Runner { |
| 50 | + |
| 51 | + /** |
| 52 | + * Specifies the runner to delegate to. |
| 53 | + */ |
| 54 | + @Retention(RetentionPolicy.RUNTIME) |
| 55 | + @Target(ElementType.TYPE) |
| 56 | + @Inherited |
| 57 | + public @interface DelegatedRunWith { |
| 58 | + /** |
| 59 | + * @return the runner to use |
| 60 | + */ |
| 61 | + public Class<? extends Runner> value(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Invoked by JUnit to initialize this runner. |
| 66 | + * |
| 67 | + * @param testClass The test class |
| 68 | + * @param builder The RunnerBuilder |
| 69 | + * @throws InitializationError If an error occurs during initialization |
| 70 | + */ |
| 71 | + public DelegateRunner(Class<?> testClass, RunnerBuilder builder) throws InitializationError { |
| 72 | + this(getDelegateClass(testClass), testClass, builder); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Initialize this runner with another runner to delegate to. |
| 77 | + * |
| 78 | + * @param runnerClass The runner to delegate to |
| 79 | + * @param testClass The test class |
| 80 | + * @param builder The RunnerBuilder |
| 81 | + * @throws InitializationError If an error occurs during initialization |
| 82 | + */ |
| 83 | + protected DelegateRunner(Class<? extends Runner> runnerClass, Class<?> testClass, RunnerBuilder builder) throws InitializationError { |
| 84 | + this.runner = constructRunner(runnerClass, testClass, builder); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Finds the delegate runner class using the DelegatedRunWith annotation. |
| 89 | + */ |
| 90 | + private static Class<? extends Runner> getDelegateClass(Class<?> testClass) throws InitializationError { |
| 91 | + DelegatedRunWith annotation = testClass.getAnnotation(DelegatedRunWith.class); |
| 92 | + if (annotation == null) { |
| 93 | + throw new InitializationError("class '" + testClass + "' must have a DelegateRunWith annotation"); |
| 94 | + } |
| 95 | + return annotation.value(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Constructs the runner instance, trying both a constructor that takes only |
| 100 | + * a class, and one that takes a class and a RunnerBuilder. (See |
| 101 | + * https://git.io/fNeAw) |
| 102 | + */ |
| 103 | + @SuppressWarnings("serial") |
| 104 | + private static Runner constructRunner(Class<? extends Runner> runnerClass, Class<?> testClass, RunnerBuilder builder) throws InitializationError { |
| 105 | + try { |
| 106 | + return runnerClass.getConstructor(Class.class).newInstance(testClass); |
| 107 | + } catch (Exception e) { |
| 108 | + try { |
| 109 | + return runnerClass.getConstructor(Class.class, RunnerBuilder.class).newInstance(testClass, builder); |
| 110 | + } catch (Exception e2) { |
| 111 | + throw new InitializationError(Lists.newArrayList( |
| 112 | + new Throwable("Failed to construct runner '" + runnerClass |
| 113 | + + "' for test '" + testClass + "'", null, false, false) { |
| 114 | + @Override public String toString() { return getMessage(); } |
| 115 | + }, |
| 116 | + e, e2)); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private final Runner runner; |
| 122 | + |
| 123 | + @Override |
| 124 | + public Description getDescription() { |
| 125 | + return runner.getDescription(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void run(RunNotifier notifier) { |
| 130 | + runner.run(notifier); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public int testCount() { |
| 135 | + return runner.testCount(); |
| 136 | + } |
| 137 | +} |
0 commit comments