~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitFramework/core/TemplateTestCase.cs

  • Committer: jnewkirk
  • Date: 2002-07-10 20:00:13 UTC
  • Revision ID: vcs-imports@canonical.com-20020710200013-j8us5mbi0usp7p02
initialĀ load

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (C) 2002. James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. All Rights Reserved.
 
3
//
 
4
namespace Nunit.Core
 
5
{
 
6
        using System;
 
7
        using System.Reflection;
 
8
 
 
9
        /// <summary>
 
10
        /// Summary description for TestCase.
 
11
        /// </summary>
 
12
        public abstract class TemplateTestCase : TestCase
 
13
        {
 
14
                private object fixture;
 
15
                private MethodInfo  method;
 
16
 
 
17
                public TemplateTestCase(object fixture, MethodInfo method) : base(fixture.GetType().FullName, method.Name)
 
18
                {
 
19
                        this.fixture = fixture;
 
20
                        this.method = method;
 
21
                }
 
22
 
 
23
                public override void Run(TestCaseResult testResult)
 
24
                {
 
25
                        if(ShouldRun)
 
26
                        {
 
27
                                DateTime start = DateTime.Now;
 
28
 
 
29
                                try 
 
30
                                {
 
31
                                        InvokeSetUp();
 
32
                                        InvokeTestCase();
 
33
                                        ProcessNoException(testResult);
 
34
                                }
 
35
                                catch(NunitException exception)
 
36
                                {
 
37
                                        ProcessException(exception.InnerException, testResult); 
 
38
                                }
 
39
                                catch(Exception exp)
 
40
                                {
 
41
                                        ProcessException(exp, testResult);
 
42
                                }
 
43
                                finally 
 
44
                                {
 
45
                                        try
 
46
                                        {
 
47
                                                InvokeTearDown();
 
48
                                        }
 
49
                                        catch(NunitException exception)
 
50
                                        {
 
51
                                                ProcessException(exception.InnerException, testResult); 
 
52
                                        }
 
53
                                        catch(Exception exp)
 
54
                                        {
 
55
                                                ProcessException(exp, testResult);
 
56
                                        }
 
57
                                        
 
58
                                        DateTime stop = DateTime.Now;
 
59
                                        TimeSpan span = stop.Subtract(start);
 
60
                                        testResult.Time = (double)span.Ticks / (double)TimeSpan.TicksPerSecond;
 
61
                                }
 
62
                        }
 
63
                        else
 
64
                        {
 
65
                                testResult.NotRun(this.IgnoreReason);
 
66
                        }
 
67
 
 
68
                        return;
 
69
                }
 
70
 
 
71
                private void InvokeTearDown()
 
72
                {
 
73
                        MethodInfo method = FindTearDownMethod(fixture);
 
74
                        if(method != null)
 
75
                        {
 
76
                                try
 
77
                                {
 
78
                                        method.Invoke(fixture, null);
 
79
                                }
 
80
                                catch(TargetInvocationException e)
 
81
                                {
 
82
                                        Exception inner = e.InnerException;
 
83
                                        throw new NunitException("Rethrown",inner);
 
84
                                }
 
85
                        }
 
86
                }
 
87
 
 
88
                private MethodInfo FindTearDownMethod(object fixture)
 
89
                {                       
 
90
                        return FindMethodByAttribute(fixture, typeof(Nunit.Framework.TearDownAttribute));
 
91
                }
 
92
 
 
93
                private void InvokeSetUp()
 
94
                {
 
95
                        MethodInfo method = FindSetUpMethod(fixture);
 
96
                        if(method != null)
 
97
                        {
 
98
                                try
 
99
                                {
 
100
                                        method.Invoke(fixture, null);
 
101
                                }
 
102
                                catch(TargetInvocationException e)
 
103
                                {
 
104
                                        Exception inner = e.InnerException;
 
105
                                        throw new NunitException("Rethrown",inner);
 
106
                                }
 
107
                        }
 
108
                }
 
109
 
 
110
                private MethodInfo FindSetUpMethod(object fixture)
 
111
                {
 
112
                        return FindMethodByAttribute(fixture, typeof(Nunit.Framework.SetUpAttribute));
 
113
                }
 
114
 
 
115
                private MethodInfo FindMethodByAttribute(object fixture, Type type)
 
116
                {
 
117
                        foreach(MethodInfo method in fixture.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic))
 
118
                        {
 
119
                                if(method.IsDefined(type,true)) 
 
120
                                {
 
121
                                        return method;
 
122
                                }
 
123
                        }
 
124
                        return null;
 
125
                }
 
126
 
 
127
                private void InvokeTestCase() 
 
128
                {
 
129
                        try
 
130
                        {
 
131
                                method.Invoke(fixture, null);
 
132
                        }
 
133
                        catch(TargetInvocationException e)
 
134
                        {
 
135
                                Exception inner = e.InnerException;
 
136
                                throw new NunitException("Rethrown",inner);
 
137
                        }
 
138
                }
 
139
 
 
140
                protected internal abstract void ProcessNoException(TestCaseResult testResult);
 
141
                
 
142
                protected internal abstract void ProcessException(Exception exception, TestCaseResult testResult);
 
143
        }
 
144
}