[tests] Disable on Android due to environment differences
[mono.git] / mcs / class / corlib / Test / System.Reflection / AssemblyTest.cs
index b2a0a4a88fffab77263e5fe417beed35dbedbd20..186eac581ed7475437517964c8d3d9dfb5b41d90 100644 (file)
@@ -35,12 +35,14 @@ using System.Configuration.Assemblies;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
-#if !TARGET_JVM && !MONOTOUCH
+#if !MONOTOUCH
 using System.Reflection.Emit;
 #endif
 using System.Threading;
 using System.Runtime.Serialization;
 using System.Security;
+using System.Linq;
+using System.Resources;
 
 namespace MonoTests.System.Reflection
 {
@@ -87,12 +89,8 @@ namespace MonoTests.System.Reflection
                }
 
                [Test] // bug #49114
-#if NET_2_0
                [Category ("NotWorking")]
                [ExpectedException (typeof (ArgumentException))]
-#else
-               [ExpectedException (typeof (TypeLoadException))]
-#endif
                public void GetType_TypeName_Invalid () 
                {
                        typeof (int).Assembly.GetType ("&blabla", true, true);
@@ -103,7 +101,6 @@ namespace MonoTests.System.Reflection
                {
                        Assembly a = typeof (int).Assembly;
                        string typeName = typeof (string).AssemblyQualifiedName;
-#if NET_2_0
                        try {
                                a.GetType (typeName, true, false);
                                Assert.Fail ("#A1");
@@ -113,17 +110,6 @@ namespace MonoTests.System.Reflection
                                Assert.IsNotNull (ex.Message, "#A4");
                                Assert.IsNull (ex.ParamName, "#A5");
                        }
-#else
-                       try {
-                               a.GetType (typeName, true, false);
-                               Assert.Fail ("#A1");
-                       } catch (TypeLoadException ex) {
-                               Assert.AreEqual (typeof (TypeLoadException), ex.GetType (), "#A2");
-                               Assert.IsNull (ex.InnerException, "#A3");
-                               Assert.IsNotNull (ex.Message, "#A4");
-                               Assert.IsTrue (ex.Message.IndexOf (typeName) != -1, "#A5");
-                       }
-#endif
 
                        Type type = a.GetType (typeName, false);
                        Assert.IsNull (type, "#B1");
@@ -137,22 +123,24 @@ namespace MonoTests.System.Reflection
                        // note: only available in default appdomain
                        // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
                        // Not sure we should emulate this behavior.
+#if !MONODROID
                        string fname = AppDomain.CurrentDomain.FriendlyName;
                        if (fname.EndsWith (".dll")) { // nunit-console
                                Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
-#if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
                                Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
-#endif
                        } else { // gnunit
                                Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
-#if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
                                Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
-#endif
                        }
+#else
+                       Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
+                       Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
+#endif
                }
 
-#if !TARGET_JVM && !MONOTOUCH // Reflection.Emit is not supported.
+#if !MONOTOUCH // Reflection.Emit is not supported.
                [Test]
+               [Category("AndroidNotWorking")] // Missing Mono.CompilerServices.SymbolWriter
                public void GetModules_MissingFile ()
                {
                        AssemblyName newName = new AssemblyName ();
@@ -176,7 +164,6 @@ namespace MonoTests.System.Reflection
                }
 #endif
 
-#if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
                [Category ("NotWorking")]
                [Test]
                public void Corlib () 
@@ -203,7 +190,10 @@ namespace MonoTests.System.Reflection
                public void Corlib_test ()
                {
                        Assembly corlib_test = Assembly.GetExecutingAssembly ();
-#if MOBILE
+#if MONODROID
+                       Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
+                       Assert.IsNull (corlib_test.Evidence, "Evidence");
+#elif MOBILE
                        Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
                        Assert.IsNull (corlib_test.Evidence, "Evidence");
 #else
@@ -214,16 +204,11 @@ namespace MonoTests.System.Reflection
 
                        Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
                        Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
-#if NET_4_0 && !MOBILE
                        Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
-#else
-                       Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
-#endif
 
                        Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
                        Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
                }
-#endif
 
                [Test]
                public void GetAssembly ()
@@ -233,7 +218,6 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
-               [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
                public void GetFile_Null ()
                {
                        try {
@@ -248,7 +232,6 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
-               [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
                public void GetFile_Empty ()
                {
                        try {
@@ -264,7 +247,7 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
-               [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
+               [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
                public void GetFiles_False ()
                {
                        Assembly corlib = typeof (int).Assembly;
@@ -277,7 +260,7 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
-               [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
+               [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
                public void GetFiles_True ()
                {
                        Assembly corlib = typeof (int).Assembly;
@@ -363,9 +346,6 @@ namespace MonoTests.System.Reflection
                }
 
                [Test] // bug #78517
-#if ONLY_1_1
-               [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
-#endif
                public void LoadFrom_Empty_Assembly ()
                {
                        string tempFile = Path.GetTempFileName ();
@@ -426,7 +406,6 @@ namespace MonoTests.System.Reflection
                        Assert.Fail ("Was not able to load any corlib test");
                }
 
-#if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
                [Test]
                public void GetObjectData_Info_Null ()
                {
@@ -443,7 +422,6 @@ namespace MonoTests.System.Reflection
                                Assert.AreEqual ("info", ex.ParamName, "#6");
                        }
                }
-#endif // TARGET_JVM
 
                [Test]
                public void GetReferencedAssemblies ()
@@ -465,7 +443,7 @@ namespace MonoTests.System.Reflection
                        }
                }
 
-#if !TARGET_JVM && !MONOTOUCH // Reflection.Emit is not supported.
+#if !MONOTOUCH // Reflection.Emit is not supported.
                [Test]
                public void Location_Empty() {
                        string assemblyFileName = Path.Combine (
@@ -494,6 +472,63 @@ namespace MonoTests.System.Reflection
                        }
                }
 
+               [Test]
+               public void SateliteAssemblyForInMemoryAssembly ()
+               {
+                       string assemblyFileName = Path.Combine (
+                               Path.GetTempPath (), "AssemblyLocation1.dll");
+
+                       try {
+                               AssemblyName assemblyName = new AssemblyName ();
+                               assemblyName.Name = "AssemblyLocation1";
+
+                               AssemblyBuilder ab = AppDomain.CurrentDomain
+                                       .DefineDynamicAssembly (assemblyName,
+                                               AssemblyBuilderAccess.Save,
+                                               Path.GetTempPath ());
+
+                               ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
+                               TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
+
+                               MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
+                               ILGenerator gen = methodBuilder.GetILGenerator ();
+
+                               //
+                               //      var resourceManager = new ResourceManager (typeof (Program));
+                               //      resourceManager.GetString ("test");
+                               //
+                               gen.Emit (OpCodes.Ldtoken, typeBuilder);
+                               gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
+                               gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
+                               gen.Emit (OpCodes.Ldstr, "test");
+                               gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
+                               gen.Emit (OpCodes.Pop);
+                               gen.Emit (OpCodes.Ret);
+
+                               typeBuilder.CreateType ();
+
+                               ab.Save (Path.GetFileName (assemblyFileName));
+
+                               using (FileStream fs = File.OpenRead (assemblyFileName)) {
+                                       byte[] buffer = new byte[fs.Length];
+                                       fs.Read (buffer, 0, buffer.Length);
+                                       Assembly assembly = Assembly.Load (buffer);
+
+                                       var mm = assembly.GetType ("Program").GetMethod ("TestCall");
+                                       try {
+                                               mm.Invoke (null, null);
+                                               Assert.Fail ();
+                                       } catch (TargetInvocationException e) {
+                                               Assert.IsTrue (e.InnerException is MissingManifestResourceException);
+                                       }
+
+                                       fs.Close ();
+                               }
+                       } finally {
+                               File.Delete (assemblyFileName);
+                       }
+               }
+
                [Test]
                [Category ("NotWorking")]
                public void bug78464 ()
@@ -596,15 +631,11 @@ namespace MonoTests.System.Reflection
                                Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
                                Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
                                Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
-#if NET_2_0
                                try {
                                        assembly.GetManifestResourceStream ("read");
                                        Assert.Fail ("#A9");
                                } catch (FileNotFoundException) {
                                }
-#else
-                               Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
-#endif
                                try {
                                        assembly.GetFile ("readme.txt");
                                        Assert.Fail ("#A10");
@@ -628,7 +659,6 @@ namespace MonoTests.System.Reflection
                        }
                }
 
-#if NET_2_0
                [Test]
                [Category ("NotWorking")]
                public void ReflectionOnlyLoad ()
@@ -640,6 +670,7 @@ namespace MonoTests.System.Reflection
                }
 
                [Test]
+               [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be directly as files
                public void ReflectionOnlyLoadFrom ()
                {
                        string loc = typeof (AssemblyTest).Assembly.Location;
@@ -657,7 +688,6 @@ namespace MonoTests.System.Reflection
                        Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
                        assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
                }
-#endif
 
                [Test]
                [Category ("NotWorking")] // patch for bug #79720 must be committed first
@@ -767,11 +797,7 @@ namespace MonoTests.System.Reflection
                }
 
                [Test] // bug #79712
-#if NET_2_0
                [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
-#else
-               [Category ("NotWorking")]
-#endif
                public void Load_Culture_Mismatch ()
                {
                        string tempDir = Path.Combine (Path.GetTempPath (),
@@ -794,11 +820,7 @@ namespace MonoTests.System.Reflection
                                aname = new AssemblyName ();
                                aname.Name = "bug79712a";
                                aname.CultureInfo = CultureInfo.InvariantCulture;
-#if NET_2_0
                                Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
-#else
-                               Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
-#endif
 
                                // PART B
 
@@ -810,11 +832,7 @@ namespace MonoTests.System.Reflection
                                aname = new AssemblyName ();
                                aname.Name = "bug79712b";
                                aname.CultureInfo = new CultureInfo ("en-US");
-#if NET_2_0
                                Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
-#else
-                               Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
-#endif
                        } finally {
                                AppDomain.Unload (ad);
                                if (Directory.Exists (tempDir))
@@ -1084,7 +1102,7 @@ namespace MonoTests.System.Reflection
 
                        Directory.Delete (outdir, true);
                }
-#endif // TARGET_JVM
+#endif
 
                [Test]
                public void ManifestModule ()
@@ -1169,5 +1187,19 @@ namespace MonoTests.System.Reflection
                        } catch (ArgumentException) {}
                }
 
+#if NET_4_5
+               [Test]
+               public void DefinedTypes_Equality ()
+               {
+                       var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
+                       var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
+
+                       Assert.AreSame (x1, x2, "#1");
+               }
+#endif
+       }
+
+       public class TestDefinedTypes
+       {
        }
 }