1 //
2 // EventBuilder.cs - NUnit Test Cases for the EventBuilder class
3 //
4 // Zoltan Varga (vargaz@freemail.hu)
5 //
6 // (C) Ximian, Inc.  http://www.ximian.com
7 
8 using System;
9 using System.Threading;
10 using System.Reflection;
11 using System.Reflection.Emit;
12 using System.Runtime.CompilerServices;
13 
14 using NUnit.Framework;
15 
16 namespace MonoTests.System.Reflection.Emit
17 {
18 
19 [TestFixture]
20 public class EventBuilderTest
21 {
AnEvent(object o)22 	public delegate void AnEvent (object o);
23 
24     private TypeBuilder tb;
25 
26 	private ModuleBuilder module;
27 
28 	private EventBuilder eb;
29 
30 	private MethodBuilder mb;
31 
32 	[SetUp]
SetUp()33 	protected void SetUp () {
34 		AssemblyName assemblyName = new AssemblyName();
35 		assemblyName.Name = GetType().FullName;
36 
37 		AssemblyBuilder assembly
38 			= Thread.GetDomain().DefineDynamicAssembly(
39 				assemblyName, AssemblyBuilderAccess.Run);
40 
41 		module = assembly.DefineDynamicModule("module1");
42 
43 	    tb = module.DefineType("class1",
44 							   TypeAttributes.Public);
45 
46 		eb = tb.DefineEvent ("event1", EventAttributes.None, typeof (AnEvent));
47 		mb =
48 			tb.DefineMethod ("OnAnEvent",
49 							 MethodAttributes.Public, typeof (void),
50 							 new Type [] { typeof (AnEvent) });
51 		ILGenerator ilgen = mb.GetILGenerator();
52 		ilgen.Emit (OpCodes.Ret);
53 
54 		// These two are required
55 		eb.SetAddOnMethod (mb);
56 		eb.SetRemoveOnMethod (mb);
57 	}
58 
59 	[Test]
60 	[ExpectedException (typeof (ArgumentNullException))]
TestSetAddOnMethod1()61 	public void TestSetAddOnMethod1 () {
62 		eb.SetAddOnMethod (null);
63 	}
64 
65 	[Test]
66 	[ExpectedException (typeof (InvalidOperationException))]
TestSetAddOnMethod2()67 	public void TestSetAddOnMethod2 () {
68 	  tb.CreateType ();
69 
70 	  eb.SetAddOnMethod (mb);
71 	}
72 
73 	[Test]
74 	[ExpectedException (typeof (ArgumentNullException))]
TestSetRaiseMethod1()75 	public void TestSetRaiseMethod1 () {
76 		eb.SetRaiseMethod (null);
77 	}
78 
79 	[Test]
80 	[ExpectedException (typeof (InvalidOperationException))]
TestSetRaiseMethod2()81 	public void TestSetRaiseMethod2 () {
82 	  tb.CreateType ();
83 
84 	  eb.SetRaiseMethod (mb);
85 	}
86 
87 	[Test]
88 	[ExpectedException (typeof (ArgumentNullException))]
TestSetRemoveAddOnMethod1()89 	public void TestSetRemoveAddOnMethod1 () {
90 		eb.SetRemoveOnMethod (null);
91 	}
92 
93 	[Test]
94 	[ExpectedException (typeof (InvalidOperationException))]
TestSetRemoveAddOnMethod2()95 	public void TestSetRemoveAddOnMethod2 () {
96 	  tb.CreateType ();
97 
98 	  eb.SetRemoveOnMethod (mb);
99 	}
100 
101 	[Test]
102 	[ExpectedException (typeof (ArgumentNullException))]
TestAddOtherMethod1()103 	public void TestAddOtherMethod1 () {
104 		eb.AddOtherMethod (null);
105 	}
106 
107 	[Test]
108 	[ExpectedException (typeof (InvalidOperationException))]
TestAddOtherMethod2()109 	public void TestAddOtherMethod2 () {
110 	  tb.CreateType ();
111 
112 	  eb.AddOtherMethod (mb);
113 	}
114 
115 	[Test]
116 	[ExpectedException (typeof (ArgumentNullException))]
TestSetCustomAttribute1()117 	public void TestSetCustomAttribute1 () {
118 		eb.SetCustomAttribute (null);
119 	}
120 
121 	[Test]
122 	[ExpectedException (typeof (ArgumentNullException))]
TestSetCustomAttribute2()123 	public void TestSetCustomAttribute2 () {
124 		eb.SetCustomAttribute (null, new byte [1]);
125 	}
126 
127 	[Test]
128 	[ExpectedException (typeof (ArgumentNullException))]
TestSetCustomAttribute3()129 	public void TestSetCustomAttribute3 () {
130 		ConstructorInfo con = typeof (String).GetConstructors ()[0];
131 		eb.SetCustomAttribute (con, null);
132 	}
133 
134 	[Test]
135 	[ExpectedException (typeof (InvalidOperationException))]
TestSetCustomAttribute4()136 	public void TestSetCustomAttribute4 () {
137 		tb.CreateType ();
138 
139 		byte[] custAttrData = { 1, 0, 0, 0, 0};
140 		Type attrType = Type.GetType
141 			("System.Reflection.AssemblyKeyNameAttribute");
142 		Type[] paramTypes = new Type[1];
143 		paramTypes[0] = typeof(String);
144 		ConstructorInfo ctorInfo =
145 			attrType.GetConstructor(paramTypes);
146 
147 		eb.SetCustomAttribute (ctorInfo, custAttrData);
148 	}
149 
150 	[Test]
151 	[ExpectedException (typeof (InvalidOperationException))]
TestSetCustomAttribute5()152 	public void TestSetCustomAttribute5 () {
153 		tb.CreateType ();
154 
155 		eb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
156 	}
157 
158 	[Test]
TestCreation()159 	public void TestCreation () {
160 		eb = tb.DefineEvent ("event2", EventAttributes.SpecialName, typeof (AnEvent));
161 
162 		eb.SetRaiseMethod (mb);
163 		eb.SetAddOnMethod (mb);
164 		eb.SetRemoveOnMethod (mb);
165 		eb.AddOtherMethod (mb);
166 		eb.AddOtherMethod (mb);
167 
168 		Type t = tb.CreateType ();
169 
170 		MethodInfo mi = t.GetMethod ("OnAnEvent");
171 
172 		EventInfo[] events = t.GetEvents ();
173 		Assert.AreEqual (2, events.Length);
174 
175 		{
176 			EventInfo ev = t.GetEvent ("event1");
177 			Assert.AreEqual ("event1", ev.Name);
178 			Assert.AreEqual (EventAttributes.None, ev.Attributes);
179 			Assert.AreEqual (t, ev.DeclaringType);
180 
181 			Assert.AreEqual (typeof (AnEvent), ev.EventHandlerType);
182 			Assert.AreEqual (true, ev.IsMulticast);
183 			Assert.AreEqual (false, ev.IsSpecialName);
184 
185 			Assert.AreEqual (mi, ev.GetAddMethod ());
186 			Assert.AreEqual (null, ev.GetRaiseMethod ());
187 			Assert.AreEqual (mi, ev.GetRemoveMethod ());
188 		}
189 
190 		{
191 			EventInfo ev = t.GetEvent ("event2");
192 			Assert.AreEqual ("event2", ev.Name);
193 			Assert.AreEqual (EventAttributes.SpecialName, ev.Attributes);
194 			Assert.AreEqual (t, ev.DeclaringType);
195 
196 			Assert.AreEqual (typeof (AnEvent), ev.EventHandlerType);
197 			Assert.AreEqual (true, ev.IsMulticast);
198 			Assert.AreEqual (true, ev.IsSpecialName);
199 
200 			Assert.AreEqual (mi, ev.GetAddMethod ());
201 			Assert.AreEqual (mi, ev.GetRaiseMethod ());
202 			Assert.AreEqual (mi, ev.GetRemoveMethod ());
203 		}
204 	}
205 
206 }
207 }
208