| 1 | |
package biz.xsoftware.impl.mock; |
| 2 | |
|
| 3 | |
import java.lang.reflect.Method; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Set; |
| 6 | |
import java.util.logging.Logger; |
| 7 | |
|
| 8 | |
import biz.xsoftware.mock.CalledMethod; |
| 9 | |
|
| 10 | |
public final class MessageHelper { |
| 11 | |
|
| 12 | 0 | private MessageHelper() { |
| 13 | 0 | } |
| 14 | |
|
| 15 | |
public static String getMethodSignature(final String retVal, final Method method, final String postfix) { |
| 16 | 2 | String methodSig = "public "; |
| 17 | 2 | if (retVal == null) { |
| 18 | 2 | methodSig += method.getReturnType(); |
| 19 | 2 | } else { |
| 20 | 0 | methodSig += retVal; |
| 21 | |
} |
| 22 | 2 | methodSig += " " + method.getName(); |
| 23 | 2 | methodSig += postfix; |
| 24 | 2 | methodSig += "("; |
| 25 | 2 | boolean needChop = false; |
| 26 | 3 | for (Class<?> c : method.getParameterTypes()) { |
| 27 | 1 | methodSig += c.getName() + ", "; |
| 28 | 1 | needChop = true; |
| 29 | |
} |
| 30 | 2 | if (needChop) { |
| 31 | 1 | return methodSig.substring(0, methodSig.length() - 2) + ")"; |
| 32 | |
} else { |
| 33 | 1 | return methodSig + ")"; |
| 34 | |
} |
| 35 | |
} |
| 36 | |
|
| 37 | |
public static String putTogetherReason(final String[] methods, final Set<String> ignorables, |
| 38 | |
final List<CalledMethod> methodsCalled, final String lastLine) { |
| 39 | 11 | String reason = "\nMethods you expected...\n"; |
| 40 | 40 | for (int i = 0; i < methods.length; i++) { |
| 41 | 29 | reason += "method[" + i + "]=" + methods[i] + "\n"; |
| 42 | |
} |
| 43 | 11 | reason += "Methods you ignored...\n"; |
| 44 | 11 | String[] ignored = ignorables.toArray(new String[0]); |
| 45 | 11 | if (ignored.length <= 0) { |
| 46 | 11 | reason += "no ignored methods\n"; |
| 47 | |
} |
| 48 | 11 | for (int i = 0; i < ignored.length; i++) { |
| 49 | 0 | reason += "ignored[" + i + "]=" + ignored[i] + "\n"; |
| 50 | |
} |
| 51 | 11 | reason += "\nMethods that were called...\n"; |
| 52 | 34 | for (int i = 0; i < methodsCalled.size(); i++) { |
| 53 | 23 | if (ignorables.contains(methodsCalled.get(i).getMethodName())) { |
| 54 | 0 | reason += "method[" + i + "](ignored)="; |
| 55 | 0 | } else { |
| 56 | 23 | reason += "method[" + i + "]="; |
| 57 | |
} |
| 58 | 23 | reason += methodsCalled.get(i).getMethodName() + "\n"; |
| 59 | |
} |
| 60 | 11 | if (lastLine == null) { |
| 61 | 6 | reason += "(possibly more but we quit after finding unexpected methods)\n"; |
| 62 | 6 | } else { |
| 63 | 5 | reason += lastLine + "\n"; |
| 64 | |
} |
| 65 | |
|
| 66 | 11 | return reason; |
| 67 | |
} |
| 68 | |
|
| 69 | |
public static String getHowMethodWasCalled(CalledMethod method) { |
| 70 | 12 | Logger log = Logger.getLogger(MessageHelper.class.getName()); |
| 71 | 12 | Throwable t = method.getHowItWasCalled(); |
| 72 | 12 | StringBuilder retVal = new StringBuilder(); |
| 73 | 12 | retVal.append("\nThe last method was=").append(method.getMethodName()).append( |
| 74 | |
"\nHere is a stack trace showing ").append("you how it was called...\n").append("--------BEGIN=").append( |
| 75 | |
method.getMethodName()).append("---------------\n").append("--------END=").append( |
| 76 | |
method.getMethodName()).append("---------------\n"); |
| 77 | 12 | log.info(t.getMessage()); |
| 78 | 12 | return retVal.toString(); |
| 79 | |
} |
| 80 | |
} |