01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package db4ounit;
22:
23: import java.lang.reflect.*;
24:
25: /**
26: * Reflection based db4ounit.Test implementation.
27: */
28: public class TestMethod extends TestAdapter {
29:
30: public static LabelProvider DEFAULT_LABEL_PROVIDER = new LabelProvider() {
31: public String getLabel(TestMethod method) {
32: return method.getSubject().getClass().getName() + "."
33: + method.getMethod().getName();
34: }
35: };
36:
37: private final Object _subject;
38: private final Method _method;
39: private final LabelProvider _labelProvider;
40:
41: public TestMethod(Object instance, Method method) {
42: this (instance, method, DEFAULT_LABEL_PROVIDER);
43: }
44:
45: public TestMethod(Object instance, Method method,
46: LabelProvider labelProvider) {
47: if (null == instance)
48: throw new IllegalArgumentException("instance");
49: if (null == method)
50: throw new IllegalArgumentException("method");
51: if (null == labelProvider)
52: throw new IllegalArgumentException("labelProvider");
53: _subject = instance;
54: _method = method;
55: _labelProvider = labelProvider;
56: }
57:
58: public Object getSubject() {
59: return _subject;
60: }
61:
62: public Method getMethod() {
63: return _method;
64: }
65:
66: public String getLabel() {
67: return _labelProvider.getLabel(this );
68: }
69:
70: protected void runTest() throws Exception {
71: invoke();
72: }
73:
74: protected void invoke() throws Exception {
75: _method.invoke(_subject, new Object[0]);
76: }
77:
78: protected void tearDown() {
79: if (_subject instanceof TestLifeCycle) {
80: try {
81: ((TestLifeCycle) _subject).tearDown();
82: } catch (Exception e) {
83: throw new TearDownFailureException(e);
84: }
85: }
86: }
87:
88: protected void setUp() {
89: if (_subject instanceof TestLifeCycle) {
90: try {
91: ((TestLifeCycle) _subject).setUp();
92: } catch (Exception e) {
93: throw new SetupFailureException(e);
94: }
95: }
96: }
97: }
|