01: /*
02: * Copyright 2006-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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 org.kuali.test;
17:
18: import java.lang.reflect.Method;
19: import java.util.Arrays;
20: import java.util.HashMap;
21: import java.util.List;
22:
23: import junit.framework.AssertionFailedError;
24:
25: /**
26: * This class implements a method for a {@link MockService}.
27: */
28: public class MockMethod {
29:
30: private final String name;
31: private final Object noResultFallback;
32: private final HashMap<List<Object>, Object> argListToResultMap = new HashMap<List<Object>, Object>();
33:
34: /**
35: * Constructs a new instance.
36: *
37: * @param name the name of the method to mock
38: * @param noResultFallback the Object on which to actually invoke this method if no mock results are associated, or null if a
39: * {@link junit.framework.AssertionFailedError} should be thrown in this case instead.
40: */
41: public MockMethod(String name, Object noResultFallback) {
42: this .name = name;
43: this .noResultFallback = noResultFallback;
44: }
45:
46: public String getName() {
47: return name;
48: }
49:
50: /**
51: * Sets a result from this method for the given list of arguments.
52: *
53: * @param result the result to return for invoking with the given args
54: * @param args the arguments to associate with the given result. The given array is copied, but if the hashCode or equality of
55: * Objects in this array change, then the invoke method may fail to find the associated result.
56: */
57: public void setResult(Object result, Object[] args) {
58: argListToResultMap.put(Arrays.asList((Object[]) args.clone()),
59: result);
60: }
61:
62: /**
63: * Returns the result associated with the given list of arguments (regardless of the given method). If there are no associated
64: * results, invokes the given method on the noResultFallback Object. If the noResultFallback Object is null, throws a
65: * {@link junit.framework.AssertionFailedError}.
66: *
67: * @see java.lang.reflect.InvocationHandler#invoke
68: */
69: public Object invoke(Object proxy, Method method, Object[] args)
70: throws Throwable {
71: List<Object> key = Arrays.asList(args);
72: if (argListToResultMap.containsKey(key)) {
73: return argListToResultMap.get(key);
74: } else {
75: if (noResultFallback == null) {
76: throw new AssertionFailedError("method " + name
77: + " has no result for " + key);
78: }
79: return method.invoke(noResultFallback, args);
80: }
81: }
82: }
|