~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitCore/core/AddinManager.cs

  • Committer: charliepoole
  • Date: 2006-01-06 01:08:17 UTC
  • Revision ID: vcs-imports@canonical.com-20060106010817-z6xazs4kd89zj8j1
Move core from under NUnitFramework to NUnitCore directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
 
2
/************************************************************************************
 
3
'
 
4
' Copyright � 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
 
5
' Copyright � 2000-2003 Philip A. Craig
 
6
'
 
7
' This software is provided 'as-is', without any express or implied warranty. In no 
 
8
' event will the authors be held liable for any damages arising from the use of this 
 
9
' software.
 
10
 
11
' Permission is granted to anyone to use this software for any purpose, including 
 
12
' commercial applications, and to alter it and redistribute it freely, subject to the 
 
13
' following restrictions:
 
14
'
 
15
' 1. The origin of this software must not be misrepresented; you must not claim that 
 
16
' you wrote the original software. If you use this software in a product, an 
 
17
' acknowledgment (see the following) in the product documentation is required.
 
18
'
 
19
' Portions Copyright � 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
 
20
' or Copyright � 2000-2003 Philip A. Craig
 
21
'
 
22
' 2. Altered source versions must be plainly marked as such, and must not be 
 
23
' misrepresented as being the original software.
 
24
'
 
25
' 3. This notice may not be removed or altered from any source distribution.
 
26
'
 
27
'***********************************************************************************/
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Reflection;
 
32
 
 
33
namespace NUnit.Core
 
34
{
 
35
        public class AddinManager : ISuiteBuilder, ITestCaseBuilder
 
36
        {
 
37
                #region Static Fields
 
38
 
 
39
                private static readonly string SuiteBuilderAttributeType = typeof( SuiteBuilderAttribute ).FullName;
 
40
                private static readonly string SuiteBuilderInterfaceType = typeof( ISuiteBuilder ).FullName;
 
41
                private static readonly string TestCaseBuilderAttributeName = typeof( TestCaseBuilderAttribute ).FullName;
 
42
                private static readonly string TestCaseBuilderInterfaceName = typeof( ITestCaseBuilder ).FullName;
 
43
                
 
44
                #endregion
 
45
 
 
46
                #region Instance Fields
 
47
                private AddinManager priorState = null;
 
48
 
 
49
                private SuiteBuilderCollection suiteBuilders = new SuiteBuilderCollection();
 
50
                private TestCaseBuilderCollection testBuilders = new TestCaseBuilderCollection();
 
51
 
 
52
                #endregion
 
53
 
 
54
                #region Constructors
 
55
 
 
56
                public AddinManager() { }
 
57
 
 
58
                public AddinManager( AddinManager priorState )
 
59
                {
 
60
                        this.priorState = priorState;
 
61
                        this.suiteBuilders = new SuiteBuilderCollection( priorState.suiteBuilders );
 
62
                        this.testBuilders = new TestCaseBuilderCollection( priorState.testBuilders );
 
63
                }
 
64
 
 
65
                #endregion
 
66
 
 
67
                #region Properties
 
68
                public AddinManager PriorState
 
69
                {
 
70
                        get { return priorState; }
 
71
                }
 
72
                #endregion
 
73
 
 
74
                #region ISuiteBuilder Members
 
75
                public bool CanBuildFrom(Type type)
 
76
                {
 
77
                        return suiteBuilders.CanBuildFrom( type );
 
78
                }
 
79
 
 
80
                public TestSuite BuildFrom(Type type)
 
81
                {
 
82
                        return suiteBuilders.BuildFrom( type );
 
83
                }
 
84
                #endregion
 
85
 
 
86
                #region ITestCaseBuilder Members
 
87
                public bool CanBuildFrom(MethodInfo method)
 
88
                {
 
89
                        return testBuilders.CanBuildFrom( method );
 
90
                }
 
91
 
 
92
                public TestCase BuildFrom(MethodInfo method)
 
93
                {
 
94
                        return testBuilders.BuildFrom( method );
 
95
                }
 
96
                #endregion
 
97
 
 
98
                #region Addin Registration
 
99
                public void Register( Assembly assembly ) 
 
100
                {
 
101
                        foreach( Type type in assembly.GetExportedTypes() )
 
102
                        {
 
103
                                if ( Reflect.HasAttribute( type, SuiteBuilderAttributeType, false )
 
104
                                        && Reflect.HasInterface( type, SuiteBuilderInterfaceType ) )
 
105
                                {
 
106
                                        object builderObject = Reflect.Construct( type );
 
107
                                        ISuiteBuilder builder = builderObject as ISuiteBuilder;
 
108
                                        // May not be able to cast, if the builder uses an earlier
 
109
                                        // version of the interface.
 
110
                                        // TODO: Wrap the object and use reflection
 
111
                                        if ( builder != null )
 
112
                                                suiteBuilders.Add( builder );
 
113
                                        else 
 
114
                                                suiteBuilders.Add( new SuiteBuilderWrapper( builderObject ) );
 
115
                                        // TODO: Figure out when to unload - this is
 
116
                                        // not important now, since we use a different
 
117
                                        // appdomain for each load, but may be in future.
 
118
                                }
 
119
                                else if ( Reflect.HasAttribute( type, TestCaseBuilderAttributeName, false )
 
120
                                        && Reflect.HasInterface( type, TestCaseBuilderInterfaceName ) )
 
121
                                {
 
122
                                        object builderObject = Reflect.Construct( type );
 
123
                                        ITestCaseBuilder builder = (ITestCaseBuilder)builderObject;
 
124
                                        if ( builder != null )
 
125
                                                testBuilders.Add( builder );
 
126
                                }
 
127
                        }
 
128
                }
 
129
 
 
130
                public void Register( ISuiteBuilder builder )
 
131
                {
 
132
                        suiteBuilders.Add( builder );
 
133
                }
 
134
 
 
135
                public void Register( ITestCaseBuilder builder )
 
136
                {
 
137
                        testBuilders.Add( builder );
 
138
                }
 
139
 
 
140
                public void Clear()
 
141
                {
 
142
                        suiteBuilders.Clear();
 
143
                        testBuilders.Clear();
 
144
                }
 
145
 
 
146
                #endregion
 
147
 
 
148
                #region Nested SuiteBuilderWrapper Class
 
149
 
 
150
                private class SuiteBuilderWrapper : ISuiteBuilder
 
151
                {
 
152
                        private object builder;
 
153
                        private MethodInfo canBuildFromMethod;
 
154
                        private MethodInfo buildFromMethod;
 
155
 
 
156
                        public SuiteBuilderWrapper( object builder )
 
157
                        {
 
158
                                this.builder = builder;
 
159
                                this.canBuildFromMethod = Reflect.GetNamedMethod( 
 
160
                                        builder.GetType(), 
 
161
                                        "CanBuildFrom", 
 
162
                                        BindingFlags.Public | BindingFlags.Instance );
 
163
                                this.buildFromMethod = Reflect.GetNamedMethod(
 
164
                                        builder.GetType(),
 
165
                                        "BuildFrom",
 
166
                                        BindingFlags.Public | BindingFlags.Instance );
 
167
                                if ( buildFromMethod == null || canBuildFromMethod == null )
 
168
                                        throw new ArgumentException( "Invalid suite builder" );
 
169
                                // TODO: Check for proper signature - put in Reflect?
 
170
                        }
 
171
 
 
172
                        #region ISuiteBuilder Members
 
173
 
 
174
                        public bool CanBuildFrom(Type type)
 
175
                        {
 
176
                                return (bool)canBuildFromMethod.Invoke( builder, new object[] { type } );
 
177
                        }
 
178
 
 
179
                        public TestSuite BuildFrom(Type type)
 
180
                        {
 
181
                                return (TestSuite)buildFromMethod.Invoke( builder, new object[] { type } );
 
182
                        }
 
183
 
 
184
                        #endregion
 
185
 
 
186
                }
 
187
 
 
188
                #endregion
 
189
        }
 
190
}