| 1 | /* |
| 2 | * Created on Apr 24, 2004 |
| 3 | * |
| 4 | * To change the template for this generated file go to |
| 5 | * Window - Preferences - Java - Code Generation - Code and Comments |
| 6 | */ |
| 7 | package biz.xsoftware.mock.client; |
| 8 | |
| 9 | /** |
| 10 | * This Exception is thrown when something is expected to happen |
| 11 | * and doesn't. It is basically the assert failing. When |
| 12 | * MockSuperclass.expectEvent is called and the event doesn't come, |
| 13 | * this exception is thrown. |
| 14 | * |
| 15 | * @author Dean Hiller |
| 16 | */ |
| 17 | public class JsExpectFailedException extends RuntimeException { |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | */ |
| 22 | private static final long serialVersionUID = 1L; |
| 23 | /** |
| 24 | * Expect failed because an event never came and waiting finally timed out. |
| 25 | */ |
| 26 | public static final String TIMED_OUT = "timed out waiting for method"; |
| 27 | /** |
| 28 | * Expect failed because a method you did not list in expected methods was called after |
| 29 | * all your expected methods were called. |
| 30 | */ |
| 31 | public static final String UNEXPECTED_CALL_AFTER = |
| 32 | "Another method that was not expected nor ignored was called after all the expected method calls"; |
| 33 | /** |
| 34 | * Expect failed because a method you did not list in expected methods was called |
| 35 | * before or during the other methods you did expect. |
| 36 | */ |
| 37 | public static final String UNEXPECTED_CALL_BEFORE = |
| 38 | "Another method that was not expected nor ignored was called before all the expected method calls were called"; |
| 39 | /** |
| 40 | * Expect failed because you expected no methods to be called, but a method was called. |
| 41 | */ |
| 42 | public static final String UNEXPECTED_ON_NONE = "Another method was called when no methods should have been called"; |
| 43 | |
| 44 | private String reason; |
| 45 | private Object[] methods; |
| 46 | private String detailMessage; |
| 47 | |
| 48 | |
| 49 | public JsExpectFailedException() {} |
| 50 | |
| 51 | /** |
| 52 | * Constructor for Expects that fail with a reason and message. |
| 53 | * |
| 54 | * @param message |
| 55 | */ |
| 56 | public JsExpectFailedException(String message, Object[] methods, String reason) { |
| 57 | super(message); |
| 58 | this.detailMessage = message; |
| 59 | this.reason = reason; |
| 60 | this.methods = methods; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the reason expecting the call(s) failed. |
| 65 | * |
| 66 | * Returns either |
| 67 | * <ol> |
| 68 | * <li>{@link #TIMED_OUT}</li> |
| 69 | * <li>{@link #UNEXPECTED_CALL_AFTER}</li> |
| 70 | * <li>{@link #UNEXPECTED_CALL_BEFORE}</li> |
| 71 | * <li>{@link #UNEXPECTED_ON_NONE}</li> |
| 72 | * </ol> |
| 73 | * @return The reason for the failure. One of |
| 74 | */ |
| 75 | public String getReason() { |
| 76 | return reason; |
| 77 | } |
| 78 | |
| 79 | public void setReason(String reason) { |
| 80 | this.reason = reason; |
| 81 | } |
| 82 | |
| 83 | public int retrieveCalledMethodCount() { |
| 84 | return methods.length; |
| 85 | } |
| 86 | /** |
| 87 | * Gives you back the CalledMethods giving you access to the |
| 88 | * parameters that were passed in and the stack traces of how |
| 89 | * they were called. |
| 90 | * |
| 91 | */ |
| 92 | public JsCalledMethod retrieveCalledMethod(int index) { |
| 93 | return (JsCalledMethod)methods[index]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return |
| 98 | */ |
| 99 | Object[] getCalledMethods() |
| 100 | { |
| 101 | return methods; |
| 102 | } |
| 103 | |
| 104 | public void setCalledMethods(Object[] methods) { |
| 105 | this.methods = methods; |
| 106 | } |
| 107 | |
| 108 | public String toString() { |
| 109 | return super.toString()+" Subclass:"+detailMessage; |
| 110 | } |
| 111 | } |