summaryrefslogtreecommitdiff
path: root/base/test/android/junit/src/org/chromium/base/test/util/SkipCheckTest.java
blob: 51c7516e713652e02b4044f3696747c6f73e7b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base.test.util;

import junit.framework.TestCase;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.robolectric.annotation.Config;

import org.chromium.base.test.BaseRobolectricTestRunner;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;

/** Unit tests for SkipCheck. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class SkipCheckTest {
    private static class TestableSkipCheck extends SkipCheck {
        public static <T extends Annotation> List<T> getAnnotationsForTesting(
                AnnotatedElement element, Class<T> annotationClass) {
            return AnnotationProcessingUtils.getAnnotations(element, annotationClass);
        }

        @Override
        public boolean shouldSkip(FrameworkMethod m) {
            return false;
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    private @interface TestAnnotation {}

    @TestAnnotation
    private class AnnotatedBaseClass {
        public void unannotatedMethod() {}
        @TestAnnotation public void annotatedMethod() {}
    }

    private class ExtendsAnnotatedBaseClass extends AnnotatedBaseClass {
        public void anotherUnannotatedMethod() {}
    }

    private class ExtendsTestCaseClass extends TestCase {
        public ExtendsTestCaseClass(String name) {
            super(name);
        }
        public void testMethodA() {}
    }

    private class UnannotatedBaseClass {
        public void unannotatedMethod() {}
        @TestAnnotation public void annotatedMethod() {}
    }

    @Test
    public void getAnnotationsForClassNone() {
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                UnannotatedBaseClass.class, TestAnnotation.class);
        Assert.assertEquals(0, annotations.size());
    }

    @Test
    public void getAnnotationsForClassOnClass() {
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                AnnotatedBaseClass.class, TestAnnotation.class);
        Assert.assertEquals(1, annotations.size());
    }

    @Test
    public void getAnnotationsForClassOnSuperclass() {
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                ExtendsAnnotatedBaseClass.class, TestAnnotation.class);
        Assert.assertEquals(1, annotations.size());
    }

    @Test
    public void getAnnotationsForMethodNone() throws NoSuchMethodException {
        Method testMethod = UnannotatedBaseClass.class.getMethod("unannotatedMethod",
                (Class[]) null);
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                testMethod, TestAnnotation.class);
        Assert.assertEquals(0, annotations.size());
    }

    @Test
    public void getAnnotationsForMethodOnMethod() throws NoSuchMethodException {
        Method testMethod = UnannotatedBaseClass.class.getMethod("annotatedMethod",
                (Class[]) null);
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                testMethod, TestAnnotation.class);
        Assert.assertEquals(1, annotations.size());
    }

    @Test
    public void getAnnotationsForMethodOnClass() throws NoSuchMethodException {
        Method testMethod = AnnotatedBaseClass.class.getMethod("unannotatedMethod",
                (Class[]) null);
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                testMethod, TestAnnotation.class);
        Assert.assertEquals(1, annotations.size());
    }

    @Test
    public void getAnnotationsForMethodOnSuperclass() throws NoSuchMethodException {
        Method testMethod = ExtendsAnnotatedBaseClass.class.getMethod("unannotatedMethod",
                (Class[]) null);
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                testMethod, TestAnnotation.class);
        Assert.assertEquals(1, annotations.size());
    }

    @Test
    public void getAnnotationsOverlapping() throws NoSuchMethodException {
        Method testMethod = AnnotatedBaseClass.class.getMethod("annotatedMethod",
                (Class[]) null);
        List<TestAnnotation> annotations = TestableSkipCheck.getAnnotationsForTesting(
                testMethod, TestAnnotation.class);
        Assert.assertEquals(2, annotations.size());
    }
}