1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.adservices.shared.meta_testing;
17 
18 import com.android.adservices.shared.testing.Logger;
19 import com.android.adservices.shared.testing.StandardStreamsLogger;
20 
21 import org.junit.runners.model.Statement;
22 
23 /** A simple JUnit statement that provides methods to assert if was evaluated or not. */
24 public final class SimpleStatement extends Statement {
25 
26     private final Logger mLog =
27             new Logger(StandardStreamsLogger.getInstance(), SimpleStatement.class);
28 
29     private boolean mEvaluated;
30     private Throwable mThrowable;
31 
32     @Override
evaluate()33     public void evaluate() throws Throwable {
34         mLog.v("evaluate() called");
35         mEvaluated = true;
36         if (mThrowable != null) {
37             mLog.v("Throwing %s", mThrowable);
38             throw mThrowable;
39         }
40     }
41 
failWith(Throwable t)42     public void failWith(Throwable t) {
43         mThrowable = t;
44     }
45 
assertEvaluated()46     public void assertEvaluated() {
47         if (!mEvaluated) {
48             throw new AssertionError("test statement was not evaluated");
49         }
50     }
51 
assertNotEvaluated()52     public void assertNotEvaluated() {
53         if (mEvaluated) {
54             throw new AssertionError("test statement was evaluated");
55         }
56     }
57 }
58