01: /*
02: * Copyright (C) The DNA Group. All rights reserved.
03: *
04: * This software is published under the terms of the DNA
05: * Software License version 1.1, a copy of which has been included
06: * with this distribution in the LICENSE.txt file.
07: */
08: package org.codehaus.dna.impl;
09:
10: import java.lang.reflect.InvocationHandler;
11: import java.lang.reflect.Method;
12: import java.util.ArrayList;
13: import java.util.Arrays;
14: import java.util.List;
15: import junit.framework.Assert;
16:
17: class MockInvocationRecorder implements InvocationHandler {
18: private List m_invocations = new ArrayList();
19: private int m_index;
20:
21: public void addInvocation(final Method method, final Object[] args,
22: final Object result) {
23: final InvocationRecord record = new InvocationRecord();
24: record.m_method = method;
25: record.m_args = args;
26: record.m_result = result;
27: m_invocations.add(record);
28: }
29:
30: public Object invoke(final Object proxy, final Method method,
31: final Object[] args) throws Throwable {
32: InvocationRecord record = (InvocationRecord) m_invocations
33: .get(m_index++);
34: if (null == record) {
35: Assert.fail("Unexpected invocation " + method.getName()
36: + " with args " + Arrays.asList(args)
37: + " at index " + m_index + " when expecting "
38: + m_invocations.size() + "invocations");
39: }
40: Assert.assertEquals("method", record.m_method, method);
41: if (args != null && record.m_args != null) {
42: Assert.assertEquals("args.length", record.m_args.length,
43: args.length);
44: } else if (args == null && 0 != record.m_args.length) {
45: Assert.fail("Got empty args but expected "
46: + Arrays.asList(record.m_args));
47: } else if (record.m_args == null && 0 != args.length) {
48: Assert.fail("Expected empty args but got "
49: + Arrays.asList(args));
50: }
51:
52: return record.m_result;
53: }
54: }
|