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