1 //
2 // System.Reflection.ConstructorInfo Test Cases
3 //
4 // Authors:
5 //  Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using System;
30 using System.Reflection;
31 using System.Runtime.Serialization;
32 
33 using NUnit.Framework;
34 
35 namespace MonoTests.System.Reflection
36 {
37 	[TestFixture]
38 	public class ConstructorInfoTest
39 	{
40 		[Test]
Invoke()41 		public void Invoke ()
42 		{
43 			ConstructorInfo ctor = typeof (Foo).GetConstructor (
44 				BindingFlags.Public | BindingFlags.Instance, null,
45 				Type.EmptyTypes, null);
46 			Foo foo = ctor.Invoke (new object [0]) as Foo;
47 			Assert.IsNotNull (foo, "#1");
48 		}
49 
50 		[Test]
InvokeAbstract()51 		public void InvokeAbstract ()
52 		{
53 			object obj = FormatterServices.GetUninitializedObject (typeof (Foo));
54 			ConstructorInfo ctor = typeof (AbstractFoo).GetConstructor (
55 				BindingFlags.Instance | BindingFlags.NonPublic, null,
56 				Type.EmptyTypes, null);
57 			Assert.IsNotNull (ctor, "#A1");
58 			object ret = ctor.Invoke (obj, new object [0]);
59 			Assert.IsNull (ret, "#A2");
60 
61 			try {
62 				ctor.Invoke (null, new object [0]);
63 				Assert.Fail ("#B1");
64 			} catch (TargetException ex) {
65 				// Non-static method requires a target
66 				Assert.AreEqual (typeof (TargetException), ex.GetType (), "#B2");
67 				Assert.IsNull (ex.InnerException, "#B3");
68 				Assert.IsNotNull (ex.Message, "#B4");
69 			}
70 
71 			try {
72 				ctor.Invoke (new object [0]);
73 				Assert.Fail ("#C1");
74 			} catch (MemberAccessException ex) {
75 				// Cannot create an abstract class
76 				Assert.AreEqual (typeof (MemberAccessException), ex.GetType (), "#C2");
77 				Assert.IsNull (ex.InnerException, "#C3");
78 				Assert.IsNotNull (ex.Message, "#C4");
79 			}
80 		}
81 
82 		[Test]
InvokeOptionalArguments()83 		public void InvokeOptionalArguments ()
84 		{
85 			var constructor = typeof (Optional).GetConstructors () [0];
86 			try {
87 				constructor.Invoke (BindingFlags.Default, null, null, null);
88 				Assert.Fail ("#1");
89 			} catch (TargetParameterCountException)	{
90 			}
91 
92 			object[] parameters = new [] { Type.Missing, Type.Missing, Type.Missing };
93 			var instance = constructor.Invoke (BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, parameters, null);
94 			Assert.IsNotNull (instance, "#2a");
95 		}
96 
97 		abstract class AbstractFoo
98 		{
99 		}
100 
101 		class Foo : AbstractFoo
102 		{
103 		}
104 
105 		[Test]
106 		[ExpectedException (typeof (MemberAccessException))]
InvokeOpenGenericType()107 		public void InvokeOpenGenericType () {
108 			typeof (Gen<>).GetConstructor (Type.EmptyTypes).Invoke (null);
109 		}
110 
111 		public class Gen<T> {
Gen()112 			public Gen() {
113 			}
114 		}
115 
116 		class Optional
117 		{
Optional(string caption = null, string value = null, string layoutName = null)118 			public Optional (string caption = null, string value = null, string layoutName = null)
119 			{
120 			}
121 		}
122 
123 		[Test]
124 		[ExpectedException (typeof (TargetException))]
InvokeWithNullTarget()125 		public void InvokeWithNullTarget ()
126 		{
127 			typeof (Foo).GetConstructors ()[0].Invoke (null, BindingFlags.Default, null, null, null);
128 		}
129 
130 		[Test]
131 		[ExpectedException (typeof (TargetException))]
InvokeWithWrongTarget()132 		public void InvokeWithWrongTarget ()
133 		{
134 			typeof (Foo).GetConstructors ()[0].Invoke (new object (), BindingFlags.Default, null, null, null);
135 		}
136 
137 		[Test]
ContainsGenericParametersOnGenericType()138 		public void ContainsGenericParametersOnGenericType ()
139 		{
140 			var ctor = typeof (Gen<>).GetConstructor (Type.EmptyTypes);
141 			Assert.IsTrue (ctor.ContainsGenericParameters);
142 		}
143 
144 		[Test]
ConstructorInfoModule()145 		public void ConstructorInfoModule ()
146 		{
147 			Type type = typeof (Foo);
148 			ConstructorInfo co = type.GetConstructors ()[0];
149 
150 			Assert.AreEqual (type.Module, co.Module);
151 		}
152 	}
153 }
154