KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > TestMethod


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 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.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * Reflection based db4ounit.Test implementation.
28  */

29 public class TestMethod implements Test {
30     
31     public interface LabelProvider {
32         String JavaDoc getLabel(TestMethod method);
33     }
34     
35     public static LabelProvider DEFAULT_LABEL_PROVIDER = new LabelProvider() {
36         public String JavaDoc getLabel(TestMethod method) {
37             return method.getSubject().getClass().getName() + "." + method.getMethod().getName();
38         }
39     };
40     
41     private final Object JavaDoc _subject;
42     private final Method JavaDoc _method;
43     private final LabelProvider _labelProvider;
44     
45     public TestMethod(Object JavaDoc instance, Method JavaDoc method) {
46         this(instance, method, DEFAULT_LABEL_PROVIDER);
47     }
48
49     public TestMethod(Object JavaDoc instance, Method JavaDoc method, LabelProvider labelProvider) {
50         if (null == instance) throw new IllegalArgumentException JavaDoc("instance");
51         if (null == method) throw new IllegalArgumentException JavaDoc("method");
52         if (null == labelProvider) throw new IllegalArgumentException JavaDoc("labelProvider");
53         _subject = instance;
54         _method = method;
55         _labelProvider = labelProvider;
56     }
57     
58     public Object JavaDoc getSubject() {
59         return _subject;
60     }
61     
62     public Method JavaDoc getMethod() {
63         return _method;
64     }
65
66     public String JavaDoc getLabel() {
67         return _labelProvider.getLabel(this);
68     }
69
70     public void run(TestResult result) {
71         try {
72             result.testStarted(this);
73             setUp();
74             invoke();
75         } catch (InvocationTargetException JavaDoc e) {
76             result.testFailed(this, e.getTargetException());
77         } catch (Exception JavaDoc e) {
78             result.testFailed(this, e);
79         } finally {
80             try {
81                 tearDown();
82             } catch (TestException e) {
83                 result.testFailed(this, e);
84             }
85         }
86     }
87
88     protected void invoke() throws Exception JavaDoc {
89         _method.invoke(_subject, new Object JavaDoc[0]);
90     }
91
92     protected void tearDown() {
93         if (_subject instanceof TestLifeCycle) {
94             try {
95                 ((TestLifeCycle)_subject).tearDown();
96             } catch (Exception JavaDoc e) {
97                 throw new TearDownFailureException(e);
98             }
99         }
100     }
101
102     protected void setUp() {
103         if (_subject instanceof TestLifeCycle) {
104             try {
105                 ((TestLifeCycle)_subject).setUp();
106             } catch (Exception JavaDoc e) {
107                 throw new SetupFailureException(e);
108             }
109         }
110     }
111 }
112
Popular Tags