OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.testing.local; | 5 package org.chromium.testing.local; |
6 | 6 |
7 import org.junit.rules.ExternalResource; | 7 import org.junit.rules.ExternalResource; |
8 import org.junit.runner.Description; | 8 import org.junit.runner.Description; |
9 import org.junit.runners.model.Statement; | 9 import org.junit.runners.model.Statement; |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... | |
30 * @Test | 30 * @Test |
31 * @Foo | 31 * @Foo |
32 * public void myTest() { ... } | 32 * public void myTest() { ... } |
33 * } | 33 * } |
34 * </pre> | 34 * </pre> |
35 * | 35 * |
36 * @param <T> type of the annotation to match on the test case. | 36 * @param <T> type of the annotation to match on the test case. |
37 */ | 37 */ |
38 public abstract class AnnotationProcessor<T extends Annotation> extends External Resource { | 38 public abstract class AnnotationProcessor<T extends Annotation> extends External Resource { |
39 private final Class<T> mAnnotationClass; | 39 private final Class<T> mAnnotationClass; |
40 private final boolean mAlwaysApply; | |
40 private Description mTestDescription; | 41 private Description mTestDescription; |
41 private T mAnnotation; | 42 private T mAnnotation; |
42 | 43 |
43 public AnnotationProcessor(Class<T> annotationClass) { | 44 /** |
45 * @param annotationClass The annotation to detect to trigger to rule. | |
46 * @param alwaysApply Whether the rule should trigger even when the annotati on is not found. | |
47 */ | |
48 public AnnotationProcessor(Class<T> annotationClass, boolean alwaysApply) { | |
Bernhard Bauer
2017/03/03 11:54:39
I find it a bit strange to have AnnotationProcesso
dgn
2017/03/03 13:48:30
Currently just adding @EnableFeatures at the class
| |
44 mAnnotationClass = annotationClass; | 49 mAnnotationClass = annotationClass; |
50 mAlwaysApply = alwaysApply; | |
45 } | 51 } |
46 | 52 |
47 @Override | 53 @Override |
48 public Statement apply(Statement base, Description description) { | 54 public Statement apply(Statement base, Description description) { |
49 mTestDescription = description; | 55 mTestDescription = description; |
50 mAnnotation = description.getAnnotation(mAnnotationClass); | 56 mAnnotation = description.getAnnotation(mAnnotationClass); |
Bernhard Bauer
2017/03/03 11:54:38
Maybe extract getting the annotation into a helper
dgn
2017/03/03 13:48:30
Done.
| |
51 if (mAnnotation == null) return base; | 57 if (mAnnotation != null) return super.apply(base, description); |
52 | 58 |
53 // Return the wrapped statement to execute before() and after(). | 59 mAnnotation = description.getTestClass().getAnnotation(mAnnotationClass) ; |
54 return super.apply(base, description); | 60 if (mAnnotation != null) return super.apply(base, description); |
61 | |
62 // No annotation, return the plain statement without executing before() and after(). | |
63 return mAlwaysApply ? super.apply(base, description) : base; | |
55 } | 64 } |
56 | 65 |
57 /** @return {@link Description} of the current test. */ | 66 /** @return {@link Description} of the current test. */ |
58 protected Description getTestDescription() { | 67 protected Description getTestDescription() { |
59 return mTestDescription; | 68 return mTestDescription; |
60 } | 69 } |
61 | 70 |
62 /** @return the annotation that caused the test to be processed. */ | 71 /** @return the annotation that caused the test to be processed. */ |
63 protected T getAnnotation() { | 72 protected T getAnnotation() { |
64 return mAnnotation; | 73 return mAnnotation; |
65 } | 74 } |
66 } | 75 } |
OLD | NEW |