AssemblyTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // System.Reflection.Assembly Test Cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Philippe Lavoie ([email protected])
  7. // Sebastien Pouliot ([email protected])
  8. //
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using NUnit.Framework;
  32. using System;
  33. using System.Globalization;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Security;
  37. namespace MonoTests.System.Reflection
  38. {
  39. [TestFixture]
  40. public class AssemblyTest
  41. {
  42. [Test]
  43. public void CreateInstance()
  44. {
  45. Type type = typeof (AssemblyTest);
  46. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  47. Assert.IsNotNull (obj, "#01");
  48. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  49. }
  50. [Test]
  51. public void CreateInvalidInstance()
  52. {
  53. Type type = typeof (AssemblyTest);
  54. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  55. Assert.IsNull (obj, "#03");
  56. }
  57. [Test]
  58. #if NET_2_0
  59. [ExpectedException (typeof (ArgumentException))]
  60. #else
  61. [ExpectedException (typeof (TypeLoadException))]
  62. #endif
  63. public void TestGetType ()
  64. {
  65. // Bug #49114
  66. typeof (int).Assembly.GetType ("&blabla", true, true);
  67. }
  68. [Test][Category("NotWorking")]
  69. public void GetEntryAssembly ()
  70. {
  71. // note: only available in default appdomain
  72. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  73. //
  74. // Not sure we should emulate this behavior.
  75. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  76. #if NET_2_0
  77. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  78. #endif
  79. }
  80. [Test]
  81. public void Corlib ()
  82. {
  83. Assembly corlib = typeof (int).Assembly;
  84. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  85. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  86. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  87. Assert.IsNotNull (corlib.Evidence, "Evidence");
  88. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  89. // corlib doesn't reference anything
  90. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  91. #if NET_2_0
  92. Assert.AreEqual ("mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  93. // not really "true" but it's even more trusted so...
  94. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  95. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  96. Assert.AreEqual ("v2.0.40607", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  97. Assert.AreEqual (PortableExecutableKind.ILOnly | PortableExecutableKind.Required32Bit, corlib.PortableExecutableKind, "PortableExecutableKind");
  98. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  99. Assert.AreEqual (0x20000001, corlib.MetadataToken);
  100. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  101. #elif NET_1_1
  102. Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  103. Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  104. Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  105. #endif
  106. }
  107. [Test]
  108. public void Corlib_test ()
  109. {
  110. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  111. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  112. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  113. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  114. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  115. #if NET_2_0
  116. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  117. Assert.AreEqual ("v2.0.40607", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  118. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  119. Assert.AreEqual (PortableExecutableKind.ILOnly, corlib_test.PortableExecutableKind, "PortableExecutableKind");
  120. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  121. #elif NET_1_1
  122. Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  123. #endif
  124. }
  125. [Test]
  126. public void GetAssembly ()
  127. {
  128. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  129. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentNullException))]
  133. public void GetFile_Null ()
  134. {
  135. Assembly.GetExecutingAssembly ().GetFile (null);
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentException))]
  139. public void GetFile_Empty ()
  140. {
  141. Assembly.GetExecutingAssembly ().GetFile (String.Empty);
  142. }
  143. [Test]
  144. public void GetFiles_False ()
  145. {
  146. Assembly corlib = typeof (int).Assembly;
  147. FileStream[] fss = corlib.GetFiles ();
  148. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  149. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  150. fss = corlib_test.GetFiles ();
  151. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  152. }
  153. [Test]
  154. public void GetFiles_True ()
  155. {
  156. Assembly corlib = typeof (int).Assembly;
  157. FileStream[] fss = corlib.GetFiles ();
  158. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  159. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  160. fss = corlib_test.GetFiles ();
  161. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  162. }
  163. [Test]
  164. public void LoadWithPartialName ()
  165. {
  166. Assembly corlib = Assembly.LoadWithPartialName ("corlib_test_default");
  167. Assembly corlib2 = Assembly.LoadWithPartialName ("corlib_plattest");
  168. Assert.IsTrue (corlib != null || corlib2 != null);
  169. }
  170. }
  171. }