[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Api / DefaultTestAssemblyBuilder.cs
1 // ***********************************************************************
2 // Copyright (c) 2012-2014 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using NUnit.Common;
34 using NUnit.Compatibility;
35 using NUnit.Framework.Interfaces;
36 using NUnit.Framework.Internal;
37 using NUnit.Framework.Internal.Builders;
38
39 namespace NUnit.Framework.Api
40 {
41     /// <summary>
42     /// DefaultTestAssemblyBuilder loads a single assembly and builds a TestSuite
43     /// containing test fixtures present in the assembly.
44     /// </summary>
45     public class DefaultTestAssemblyBuilder : ITestAssemblyBuilder
46     {
47         static Logger log = InternalTrace.GetLogger(typeof(DefaultTestAssemblyBuilder));
48
49         #region Instance Fields
50
51         /// <summary>
52         /// The default suite builder used by the test assembly builder.
53         /// </summary>
54         ISuiteBuilder _defaultSuiteBuilder;
55
56         #endregion
57
58         #region Constructor
59
60         /// <summary>
61         /// Initializes a new instance of the <see cref="DefaultTestAssemblyBuilder"/> class.
62         /// </summary>
63         public DefaultTestAssemblyBuilder()
64         {
65             _defaultSuiteBuilder = new DefaultSuiteBuilder();
66         }
67
68         #endregion
69
70         #region Build Methods
71
72         /// <summary>
73         /// Build a suite of tests from a provided assembly
74         /// </summary>
75         /// <param name="assembly">The assembly from which tests are to be built</param>
76         /// <param name="options">A dictionary of options to use in building the suite</param>
77         /// <returns>
78         /// A TestSuite containing the tests found in the assembly
79         /// </returns>
80         public ITest Build(Assembly assembly, IDictionary<string, object> options)
81         {
82 #if PORTABLE
83             log.Debug("Loading {0}", assembly.FullName);
84 #else
85             log.Debug("Loading {0} in AppDomain {1}", assembly.FullName, AppDomain.CurrentDomain.FriendlyName);
86 #endif
87
88 #if SILVERLIGHT
89             string assemblyPath = AssemblyHelper.GetAssemblyName(assembly).Name;
90 #elif PORTABLE
91             string assemblyPath = AssemblyHelper.GetAssemblyName(assembly).FullName;
92 #else
93             string assemblyPath = AssemblyHelper.GetAssemblyPath(assembly);
94 #endif
95
96             return Build(assembly, assemblyPath, options);
97         }
98
99         /// <summary>
100         /// Build a suite of tests given the filename of an assembly
101         /// </summary>
102         /// <param name="assemblyName">The filename of the assembly from which tests are to be built</param>
103         /// <param name="options">A dictionary of options to use in building the suite</param>
104         /// <returns>
105         /// A TestSuite containing the tests found in the assembly
106         /// </returns>
107         public ITest Build(string assemblyName, IDictionary<string, object> options)
108         {
109 #if PORTABLE
110             log.Debug("Loading {0}", assemblyName);
111 #else
112             log.Debug("Loading {0} in AppDomain {1}", assemblyName, AppDomain.CurrentDomain.FriendlyName);
113 #endif
114
115             TestSuite testAssembly = null;
116
117             try
118             {
119                 var assembly = AssemblyHelper.Load(assemblyName);
120
121                 if (assembly == null)
122                     throw new Exception("assembly is null");
123
124                 testAssembly = Build(assembly, assemblyName, options);
125             }
126             catch (Exception ex)
127             {
128                 testAssembly = new TestAssembly(assemblyName);
129                 testAssembly.RunState = RunState.NotRunnable;
130                 testAssembly.Properties.Set(PropertyNames.SkipReason, ex.Message);
131             }
132
133             return testAssembly;
134         }
135
136         private TestSuite Build(Assembly assembly, string assemblyPath, IDictionary<string, object> options)
137         {
138             TestSuite testAssembly = null;
139
140             try
141             {
142                 if (options.ContainsKey(PackageSettings.DefaultTestNamePattern))
143                     TestNameGenerator.DefaultTestNamePattern = options[PackageSettings.DefaultTestNamePattern] as string;
144
145                 if (options.ContainsKey(PackageSettings.TestParameters))
146                 {
147                     string parameters = options[PackageSettings.TestParameters] as string;
148                     if (!string.IsNullOrEmpty(parameters))
149                         foreach (string param in parameters.Split(new[] { ';' }))
150                         {
151                             int eq = param.IndexOf("=");
152
153                             if (eq > 0 && eq < param.Length - 1)
154                             {
155                                 var name = param.Substring(0, eq);
156                                 var val = param.Substring(eq + 1);
157
158                                 TestContext.Parameters.Add(name, val);
159                             }
160                         }
161                 }
162
163                 IList fixtureNames = null;
164                 if (options.ContainsKey(PackageSettings.LOAD))
165                     fixtureNames = options[PackageSettings.LOAD] as IList;
166                 var fixtures = GetFixtures(assembly, fixtureNames);
167
168                 testAssembly = BuildTestAssembly(assembly, assemblyPath, fixtures);
169             }
170             catch (Exception ex)
171             {
172                 testAssembly = new TestAssembly(assemblyPath);
173                 testAssembly.RunState = RunState.NotRunnable;
174                 testAssembly.Properties.Set(PropertyNames.SkipReason, ex.Message);
175             }
176
177             return testAssembly;
178         }
179
180         #endregion
181
182         #region Helper Methods
183
184         private IList<Test> GetFixtures(Assembly assembly, IList names)
185         {
186             var fixtures = new List<Test>();
187             log.Debug("Examining assembly for test fixtures");
188
189             var testTypes = GetCandidateFixtureTypes(assembly, names);
190
191             log.Debug("Found {0} classes to examine", testTypes.Count);
192 #if LOAD_TIMING
193             System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
194             timer.Start();
195 #endif
196             int testcases = 0;
197             foreach (Type testType in testTypes)
198             {
199                 var typeInfo = new TypeWrapper(testType);
200
201                 try
202                 {
203                     if (_defaultSuiteBuilder.CanBuildFrom(typeInfo))
204                     {
205                         Test fixture = _defaultSuiteBuilder.BuildFrom(typeInfo);
206                         fixtures.Add(fixture);
207                         testcases += fixture.TestCaseCount;
208                     }
209                 }
210                 catch (Exception ex)
211                 {
212                     log.Error(ex.ToString());
213                 }
214             }
215
216 #if LOAD_TIMING
217             log.Debug("Found {0} fixtures with {1} test cases in {2} seconds", fixtures.Count, testcases, timer.Elapsed);
218 #else
219             log.Debug("Found {0} fixtures with {1} test cases", fixtures.Count, testcases);
220 #endif
221
222             return fixtures;
223         }
224
225         private IList<Type> GetCandidateFixtureTypes(Assembly assembly, IList names)
226         {
227             var types = assembly.GetTypes();
228
229             if (names == null || names.Count == 0)
230                 return types;
231
232             var result = new List<Type>();
233
234             foreach (string name in names)
235             {
236                 Type fixtureType = assembly.GetType(name);
237                 if (fixtureType != null)
238                     result.Add(fixtureType);
239                 else
240                 {
241                     string prefix = name + ".";
242
243                     foreach (Type type in types)
244                         if (type.FullName.StartsWith(prefix))
245                             result.Add(type);
246                 }
247             }
248
249             return result;
250         }
251
252         private TestSuite BuildTestAssembly(Assembly assembly, string assemblyName, IList<Test> fixtures)
253         {
254             TestSuite testAssembly = new TestAssembly(assembly, assemblyName);
255
256             if (fixtures.Count == 0)
257             {
258                 testAssembly.RunState = RunState.NotRunnable;
259                 testAssembly.Properties.Set(PropertyNames.SkipReason, "Has no TestFixtures");
260             }
261             else
262             {
263                 NamespaceTreeBuilder treeBuilder =
264                     new NamespaceTreeBuilder(testAssembly);
265                 treeBuilder.Add(fixtures);
266                 testAssembly = treeBuilder.RootSuite;
267             }
268
269             testAssembly.ApplyAttributesToTest(assembly);
270
271 #if !PORTABLE
272 #if !SILVERLIGHT
273             testAssembly.Properties.Set(PropertyNames.ProcessID, System.Diagnostics.Process.GetCurrentProcess().Id);
274 #endif
275             testAssembly.Properties.Set(PropertyNames.AppDomain, AppDomain.CurrentDomain.FriendlyName);
276 #endif
277
278             // TODO: Make this an option? Add Option to sort assemblies as well?
279             // [tronghieu.d] - no need to sort tcs, to sync with tc order of tct-mgr
280             //testAssembly.Sort();
281
282             return testAssembly;
283         }
284         #endregion
285     }
286 }