summaryrefslogtreecommitdiff
path: root/libraries/health/rules/tests/src/android/platform/test/rule/DynamicRuleChainTest.java
blob: a866451d431a7f0cc811e10a0fa780dcf2193096 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.platform.test.rule;

import static com.google.common.truth.Truth.assertThat;

import android.os.Bundle;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

/** Unit tests for {@link DynamicRuleChain}. */
@RunWith(JUnit4.class)
public class DynamicRuleChainTest {
    private static final Description DESCRIPTION =
            Description.createTestDescription("class", "method");

    // A static variable is used so that the rules in this test can modify its state while being
    // static (and thus able to be created via an empty constructor). This route is taken as the
    // rules themselves are not directly observable due to being created via reflection from
    // DynamicRuleChain.
    private static final List<String> sLogs = new ArrayList<String>();

    private static final Statement mStatement =
            new Statement() {
                @Override
                public void evaluate() {
                    sLogs.add("Test execution");
                }
            };

    @Rule public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void setUp() {
        // Clear the logs between tests.
        sLogs.clear();
    }

    @Test
    public void testAppliesRuleBySimpleClassNameForRulesInKnownPackage() throws Throwable {
        DynamicRuleChain chain = createWithRuleNames("DynamicRuleChainTest$Rule1");
        Statement applied = chain.apply(mStatement, DESCRIPTION);
        applied.evaluate();
        assertThat(sLogs).containsExactly("Rule1 starting", "Test execution", "Rule1 finished");
    }

    @Test
    public void testAppliesRuleByFullyQualifiedClassName() throws Throwable {
        DynamicRuleChain chain =
                createWithRuleNames("android.platform.test.rule.DynamicRuleChainTest$Rule1");
        Statement applied = chain.apply(mStatement, DESCRIPTION);
        applied.evaluate();
        assertThat(sLogs).containsExactly("Rule1 starting", "Test execution", "Rule1 finished");
    }

    @Test
    public void testThrowsOnNonexistentSimpleClassName() throws Throwable {
        String badName = "NotARuleRule";
        expectedException.expectMessage(
                String.format(
                        "Failed to dynamically load rule with simple class name %s.", badName));
        DynamicRuleChain chain = createWithRuleNames(badName);
        chain.apply(mStatement, DESCRIPTION);
    }

    @Test
    public void testThrowsOnNonexistentFullyQualifiedClassName() throws Throwable {
        String badName = "not.a.rule.Rule";
        expectedException.expectMessage(
                String.format(
                        "Failed to dynamically load rule with fully qualified name %s.", badName));
        DynamicRuleChain chain = createWithRuleNames(badName);
        chain.apply(mStatement, DESCRIPTION);
    }

    @Test
    public void testMultipleRulesAreAppliedInCorrectOrder() throws Throwable {
        DynamicRuleChain chain =
                createWithRuleNames(
                        "DynamicRuleChainTest$Rule2",
                        "android.platform.test.rule.DynamicRuleChainTest$Rule1");
        Statement applied = chain.apply(mStatement, DESCRIPTION);
        applied.evaluate();
        // Rule2's logs should be outside, and Rule1's logs should be inside.
        assertThat(sLogs)
                .containsExactly(
                        "Rule2 starting",
                        "Rule1 starting",
                        "Test execution",
                        "Rule1 finished",
                        "Rule2 finished");
    }

    @Test
    public void testInvalidRuleNameInMultipleRulesAlsoThrows() throws Throwable {
        String badName = "not.a.rule.Rule";
        expectedException.expectMessage(
                String.format(
                        "Failed to dynamically load rule with fully qualified name %s.", badName));
        DynamicRuleChain chain =
                createWithRuleNames(
                        badName, "android.platform.test.rule.DynamicRuleChainTest$Rule1");
        chain.apply(mStatement, DESCRIPTION);
    }

    @Test
    public void testSupportsDuplicateSimpleRuleNames() throws Throwable {
        DynamicRuleChain chain =
                createWithRuleNames(
                        "DynamicRuleChainTest$Rule2",
                        "android.platform.test.rule.DynamicRuleChainTest$Rule1",
                        "DynamicRuleChainTest$Rule2");
        Statement applied = chain.apply(mStatement, DESCRIPTION);
        applied.evaluate();
        // Rule2's logs should be outside, and Rule1's logs should be inside.
        assertThat(sLogs)
                .containsExactly(
                        "Rule2 starting",
                        "Rule1 starting",
                        "Rule2 starting",
                        "Test execution",
                        "Rule2 finished",
                        "Rule1 finished",
                        "Rule2 finished");
    }

    @Test
    public void testSupportsDuplicateFullyQualifiedRuleNames() throws Throwable {
        DynamicRuleChain chain =
                createWithRuleNames(
                        "android.platform.test.rule.DynamicRuleChainTest$Rule1",
                        "DynamicRuleChainTest$Rule2",
                        "android.platform.test.rule.DynamicRuleChainTest$Rule1");
        Statement applied = chain.apply(mStatement, DESCRIPTION);
        applied.evaluate();
        // Rule2's logs should be outside, and Rule1's logs should be inside.
        assertThat(sLogs)
                .containsExactly(
                        "Rule1 starting",
                        "Rule2 starting",
                        "Rule1 starting",
                        "Test execution",
                        "Rule1 finished",
                        "Rule2 finished",
                        "Rule1 finished");
    }

    private DynamicRuleChain createWithRuleNames(String... ruleNames) {
        Bundle args = new Bundle();
        args.putString(DynamicRuleChain.DEFAULT_RULES_OPTION, String.join(",", ruleNames));
        return new DynamicRuleChain(args);
    }

    private DynamicRuleChain createWithRulesOptionNameAndRuleNames(
            String rulesOptionName, String... ruleNames) {
        Bundle args = new Bundle();
        args.putString(rulesOptionName, String.join(",", ruleNames));
        return new DynamicRuleChain(rulesOptionName, args);
    }

    public static class Rule1 extends TestWatcher {
        @Override
        public void starting(Description description) {
            sLogs.add("Rule1 starting");
        }

        @Override
        public void finished(Description description) {
            sLogs.add("Rule1 finished");
        }
    }

    public static class Rule2 extends TestWatcher {
        @Override
        public void starting(Description description) {
            sLogs.add("Rule2 starting");
        }

        @Override
        public void finished(Description description) {
            sLogs.add("Rule2 finished");
        }
    }
}