1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitoutil; 7 8 import org.assertj.core.api.Assertions; 9 import org.assertj.core.api.Condition; 10 import org.assertj.core.description.Description; 11 import org.assertj.core.description.TextDescription; 12 import org.hamcrest.CoreMatchers; 13 14 import java.lang.reflect.Method; 15 import java.util.Arrays; 16 17 @SuppressWarnings("unchecked") 18 public class Conditions { 19 onlyThoseClassesInStackTrace(final String... classes)20 public static Condition<Throwable> onlyThoseClassesInStackTrace(final String... classes) { 21 return new Condition<Throwable>() { 22 @Override 23 public boolean matches(Throwable traceElements) { 24 StackTraceElement[] trace = traceElements.getStackTrace(); 25 26 Assertions.assertThat(trace.length) 27 .describedAs("Number of classes does not match.\nExpected: %s\nGot: %s", 28 Arrays.toString(classes), 29 Arrays.toString(traceElements.getStackTrace())) 30 .isEqualTo(classes.length); 31 32 for (int i = 0; i < trace.length; i++) { 33 Assertions.assertThat(trace[i].getClassName()).isEqualTo(classes[i]); 34 } 35 36 return true; 37 } 38 }; 39 } 40 41 public static Condition<StackTraceElement[]> onlyThoseClasses(final String... classes) { 42 return new Condition<StackTraceElement[]>() { 43 44 @Override 45 public boolean matches(StackTraceElement[] traceElements) { 46 Assertions.assertThat(traceElements.length) 47 .describedAs("Number of classes does not match.\nExpected: %s\nGot: %s", 48 Arrays.toString(classes), 49 Arrays.toString(traceElements)) 50 .isEqualTo(classes.length); 51 52 for (int i = 0; i < traceElements.length; i++) { 53 Assertions.assertThat(traceElements[i].getClassName()).isEqualTo(classes[i]); 54 } 55 56 return true; 57 } 58 }; 59 } 60 61 public static Condition<Throwable> firstMethodInStackTrace(final String method) { 62 return methodInStackTraceAt(0, method); 63 } 64 65 public static Condition<Throwable> methodInStackTraceAt(final int stackTraceIndex, final String method) { 66 return new Condition<Throwable>() { 67 private String actualMethodAtIndex; 68 69 @Override 70 public boolean matches(Throwable throwable) { 71 actualMethodAtIndex = throwable.getStackTrace()[stackTraceIndex].getMethodName(); 72 73 return actualMethodAtIndex.equals(method); 74 } 75 76 @Override 77 public Description description() { 78 return new TextDescription("Method at index: %d\nexpected to be: %s\nbut is: %s", stackTraceIndex, method, actualMethodAtIndex); 79 } 80 }; 81 } 82 83 public static Condition<Object> bridgeMethod(final String methodName) { 84 return new Condition<Object>() { 85 86 public boolean matches(Object o) { 87 Class<?> clazz = null; 88 if (o instanceof Class) { 89 clazz = (Class<?>) o; 90 } else { 91 clazz = o.getClass(); 92 } 93 94 for (Method m : clazz.getMethods()) { 95 if (m.isBridge() && m.getName().equals(methodName)) { 96 return true; 97 } 98 } 99 100 Assertions.fail("Bridge method [" + methodName + "]\nnot found in:\n" + o); 101 return false; 102 } 103 }; 104 } 105 106 public static org.hamcrest.Matcher<Object> clazz(Class<?> type) { 107 return CoreMatchers.instanceOf(type); 108 } 109 110 public static Condition<Throwable> methodsInStackTrace(final String... methods) { 111 return new Condition<Throwable>() { 112 public boolean matches(Throwable value) { 113 StackTraceElement[] trace = value.getStackTrace(); 114 for (int i = 0; i < methods.length; i++) { 115 Assertions.assertThat(trace[i].getMethodName()).describedAs("Expected methods[%d] to be in the stack trace.", i).isEqualTo(methods[i]); 116 } 117 return true; 118 } 119 }; 120 } 121 122 123 } 124