View Javadoc
1   /*
2    * Copyright 2011-2024 the original author or authors.
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    *         https://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.googlecode.catchexception.throwable.apis;
17  
18  import com.googlecode.catchexception.throwable.CatchThrowable;
19  import com.googlecode.catchexception.throwable.ThrowableNotThrownAssertionError;
20  
21  /**
22   * The Class CatchThrowableUtils.
23   */
24  final class CatchThrowableUtils {
25  
26      /**
27       * Prevent Instantiation of a new catch throable utils.
28       */
29      private CatchThrowableUtils() {
30          // Preventing instantiation
31      }
32  
33      /**
34       * Then thrown.
35       *
36       * @param actualThrowableClazz
37       *            the actual throwable clazz
38       */
39      @SuppressWarnings({ "unchecked", "rawtypes" })
40      public static void thenThrown(Class actualThrowableClazz) {
41          var e = CatchThrowable.caughtThrowable();
42          if (e == null) {
43              // no throwable caught -> assertion failed
44              throw new ThrowableNotThrownAssertionError(actualThrowableClazz);
45          }
46          if (!actualThrowableClazz.isAssignableFrom(CatchThrowable.caughtThrowable().getClass())) {
47              // caught throwable is of wrong type -> assertion failed
48              throw new ThrowableNotThrownAssertionError(actualThrowableClazz, e);
49          }
50      }
51  
52  }