1 /* 2 * Copyright (C) 2022 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 17 package com.android.bedstead.harrier; 18 19 import static com.google.common.truth.Truth.assertWithMessage; 20 21 import static org.junit.Assume.assumeTrue; 22 23 import com.android.bedstead.harrier.annotations.FailureMode; 24 25 import org.junit.AssumptionViolatedException; 26 27 /** 28 * Utilities for use by {@link AnnotationExecutor} subclasses. 29 */ 30 public final class AnnotationExecutorUtil { 31 AnnotationExecutorUtil()32 private AnnotationExecutorUtil() { 33 34 } 35 36 /** 37 * {@link #failOrSkip(String, FailureMode)} if {@code value} is true. 38 */ checkFailOrSkip(String message, boolean value, FailureMode failureMode)39 public static void checkFailOrSkip(String message, boolean value, FailureMode failureMode) { 40 if (failureMode.equals(FailureMode.FAIL)) { 41 assertWithMessage(message).that(value).isTrue(); 42 } else if (failureMode.equals(FailureMode.SKIP)) { 43 assumeTrue(message, value); 44 } else { 45 throw new IllegalStateException("Unknown failure mode: " + failureMode); 46 } 47 } 48 49 /** 50 * Either fail or skip the current test depending on the value of {@code failureMode}. 51 */ failOrSkip(String message, FailureMode failureMode)52 public static void failOrSkip(String message, FailureMode failureMode) { 53 switch (failureMode) { 54 case FAIL: 55 throw new AssertionError(message); 56 case SKIP: 57 throw new AssumptionViolatedException(message); 58 default: 59 throw new IllegalStateException("Unknown failure mode: " + failureMode); 60 } 61 } 62 } 63