[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Common / AssemblyHelper.cs
1 // ***********************************************************************
2 // Copyright (c) 2008 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.IO;
31 using System.Reflection;
32
33 #if NUNIT_ENGINE
34 namespace NUnit.Engine.Internal
35 #elif NUNIT_FRAMEWORK
36 namespace NUnit.Framework.Internal
37 #else
38 namespace NUnit.Common
39 #endif
40 {
41     /// <summary>
42     /// AssemblyHelper provides static methods for working
43     /// with assemblies.
44     /// </summary>
45     public class AssemblyHelper
46     {
47         #region GetAssemblyPath
48
49         /// <summary>
50         /// Gets the path from which the assembly defining a type was loaded.
51         /// </summary>
52         /// <param name="type">The Type.</param>
53         /// <returns>The path.</returns>
54         public static string GetAssemblyPath(Type type)
55         {
56 #if PORTABLE
57             return GetAssemblyPath(type.GetTypeInfo().Assembly);
58 #else
59             return GetAssemblyPath(type.Assembly);
60 #endif
61         }
62
63         /// <summary>
64         /// Gets the path from which an assembly was loaded.
65         /// For builds where this is not possible, returns
66         /// the name of the assembly.
67         /// </summary>
68         /// <param name="assembly">The assembly.</param>
69         /// <returns>The path.</returns>
70         public static string GetAssemblyPath(Assembly assembly)
71         {
72 #if SILVERLIGHT
73             return GetAssemblyName(assembly).Name;
74 #elif NETCF || PORTABLE
75             return assembly.ManifestModule.FullyQualifiedName;
76 #else
77             string codeBase = assembly.CodeBase;
78
79             if (IsFileUri(codeBase))
80                 return GetAssemblyPathFromCodeBase(codeBase);
81
82             return assembly.Location;
83 #endif
84         }
85
86 #endregion
87
88 #region GetDirectoryName
89
90 #if !SILVERLIGHT && !PORTABLE
91         /// <summary>
92         /// Gets the path to the directory from which an assembly was loaded.
93         /// </summary>
94         /// <param name="assembly">The assembly.</param>
95         /// <returns>The path.</returns>
96         public static string GetDirectoryName(Assembly assembly)
97         {
98             return Path.GetDirectoryName(GetAssemblyPath(assembly));
99         }
100 #endif
101
102 #endregion
103
104 #region GetAssemblyName
105
106         /// <summary>
107         /// Gets the AssemblyName of an assembly.
108         /// </summary>
109         /// <param name="assembly">The assembly</param>
110         /// <returns>An AssemblyName</returns>
111         public static AssemblyName GetAssemblyName(Assembly assembly)
112         {
113 #if SILVERLIGHT || PORTABLE
114             return new AssemblyName(assembly.FullName);
115 #else
116             return assembly.GetName();
117 #endif
118         }
119
120         #endregion
121
122         #region Load
123
124 #if PORTABLE
125         /// <summary>
126         /// Loads an assembly given a string, which is the AssemblyName
127         /// </summary>
128         /// <param name="name"></param>
129         /// <returns></returns>
130         public static Assembly Load(string name)
131         {
132             var ext = Path.GetExtension(name);
133             if (ext == ".dll" || ext == ".exe")
134                 name = Path.GetFileNameWithoutExtension(name);
135
136             return Assembly.Load(new AssemblyName { Name = name });
137         }
138 #else
139
140         /// <summary>
141         /// Loads an assembly given a string, which may be the 
142         /// path to the assembly or the AssemblyName
143         /// </summary>
144         /// <param name="nameOrPath"></param>
145         /// <returns></returns>
146         public static Assembly Load(string nameOrPath)
147         {
148             var ext = Path.GetExtension(nameOrPath).ToLower();
149
150             // Handle case where this is the path to an assembly
151             if (ext == ".dll" || ext == ".exe")
152             {
153 #if SILVERLIGHT
154                 return Assembly.Load(Path.GetFileNameWithoutExtension(nameOrPath));
155 #elif NETCF
156                 return Assembly.LoadFrom(nameOrPath);
157 #else
158                 return Assembly.Load(AssemblyName.GetAssemblyName(nameOrPath));
159 #endif
160             }
161
162             // Assume it's the string representation of an AssemblyName
163             return Assembly.Load(nameOrPath);
164         }
165 #endif
166
167         #endregion
168
169 #region Helper Methods
170
171 #if !NETCF && !SILVERLIGHT && !PORTABLE
172         private static bool IsFileUri(string uri)
173         {
174             return uri.ToLower().StartsWith(Uri.UriSchemeFile);
175         }
176
177         /// <summary>
178         /// Gets the assembly path from code base.
179         /// </summary>
180         /// <remarks>Public for testing purposes</remarks>
181         /// <param name="codeBase">The code base.</param>
182         /// <returns></returns>
183         public static string GetAssemblyPathFromCodeBase(string codeBase)
184         {
185             // Skip over the file:// part
186             int start = Uri.UriSchemeFile.Length + Uri.SchemeDelimiter.Length;
187
188             if (codeBase[start] == '/') // third slash means a local path
189             {
190                 // Handle Windows Drive specifications
191                 if (codeBase[start + 2] == ':')
192                     ++start;
193                 // else leave the last slash so path is absolute
194             }
195             else // It's either a Windows Drive spec or a share
196             {
197                 if (codeBase[start + 1] != ':')
198                     start -= 2; // Back up to include two slashes
199             }
200
201             return codeBase.Substring(start);
202         }
203 #endif
204
205 #endregion
206     }
207 }