2 // InstanceDescriptorTest.cs - Unit tests for
3 // System.ComponentModel.Design.Serialization.InstanceDescriptor
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
33 using System.ComponentModel;
34 using System.ComponentModel.Design.Serialization;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Threading;
39 namespace MonoTests.System.ComponentModel.Design.Serialization {
42 public class InstanceDescriptorTest {
44 private const string url = "http://www.mono-project.com/";
45 private ConstructorInfo ci;
48 public void FixtureSetUp ()
50 ci = typeof (Uri).GetConstructor (new Type[1] { typeof (string) });
54 public void Constructor0_Arguments_Mismatch ()
57 new InstanceDescriptor (ci, null);
59 } catch (ArgumentException ex) {
61 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
62 Assert.IsNull (ex.InnerException, "#B3");
63 Assert.IsNotNull (ex.Message, "#B4");
64 Assert.IsNull (ex.ParamName, "#B5");
69 public void Constructor0_MemberInfo_Type ()
71 Type type = typeof (Uri);
72 InstanceDescriptor id = new InstanceDescriptor (type,
73 new object [] { url });
74 Assert.AreEqual (1, id.Arguments.Count, "#1");
75 Assert.IsTrue (id.IsComplete, "#2");
76 Assert.AreSame (type, id.MemberInfo, "#3");
77 Assert.IsNull (id.Invoke (), "#4");
81 public void Constructor_Null_ICollection ()
83 InstanceDescriptor id = new InstanceDescriptor (null, new object[] { });
84 Assert.AreEqual (0, id.Arguments.Count, "#1");
85 Assert.IsTrue (id.IsComplete, "#2");
86 Assert.IsNull (id.MemberInfo, "#3");
87 Assert.IsNull (id.Invoke (), "#4");
91 public void Constructor_MemberInfo_ICollection ()
93 InstanceDescriptor id = new InstanceDescriptor (ci, new object[] { url });
94 Assert.AreEqual (1, id.Arguments.Count, "Arguments");
95 Assert.IsTrue (id.IsComplete, "IsComplete");
96 Assert.AreSame (ci, id.MemberInfo, "MemberInfo");
97 Uri uri = (Uri) id.Invoke ();
98 Assert.AreEqual (url, uri.AbsoluteUri, "Invoke");
102 public void Constructor_Null_ICollection_Boolean ()
104 InstanceDescriptor id = new InstanceDescriptor (null, new object[] { }, true);
105 Assert.AreEqual (0, id.Arguments.Count, "#1");
106 Assert.IsTrue (id.IsComplete, "#2");
107 Assert.IsNull (id.MemberInfo, "#3");
108 Assert.IsNull (id.Invoke (), "#4");
112 [ExpectedException (typeof (ArgumentException))]
113 public void Constructor_MemberInfo_Null_Boolean ()
115 new InstanceDescriptor (ci, null, false);
116 // mismatch for required parameters
120 public void Constructor_MemberInfo_ICollection_Boolean ()
122 InstanceDescriptor id = new InstanceDescriptor (ci, new object[] { url }, false);
123 Assert.AreEqual (1, id.Arguments.Count, "Arguments");
124 Assert.IsFalse (id.IsComplete, "IsComplete");
125 Assert.AreSame (ci, id.MemberInfo, "MemberInfo");
126 Uri uri = (Uri) id.Invoke ();
127 Assert.AreEqual (url, uri.AbsoluteUri, "Invoke");
131 public void Field_Arguments_Empty ()
133 FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
135 InstanceDescriptor id = new InstanceDescriptor (fi, new object [0]);
136 Assert.AreEqual (0, id.Arguments.Count, "#1");
137 Assert.IsTrue (id.IsComplete, "#2");
138 Assert.AreSame (fi, id.MemberInfo, "#3");
139 Assert.IsNotNull (id.Invoke (), "#4");
143 public void Field_Arguments_Mismatch ()
145 FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
148 new InstanceDescriptor (fi, new object [] { url });
150 } catch (ArgumentException ex) {
151 // Parameter must be static
152 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
153 Assert.IsNull (ex.InnerException, "#3");
154 Assert.IsNotNull (ex.Message, "#4");
155 Assert.IsNull (ex.ParamName, "#5");
160 public void Field_Arguments_Null ()
162 FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
164 InstanceDescriptor id = new InstanceDescriptor (fi, null);
165 Assert.AreEqual (0, id.Arguments.Count, "#1");
166 Assert.IsTrue (id.IsComplete, "#2");
167 Assert.AreSame (fi, id.MemberInfo, "#3");
168 Assert.IsNotNull (id.Invoke (), "#4");
172 public void Field_MemberInfo_NonStatic ()
174 FieldInfo fi = typeof (InstanceField).GetField ("Name");
177 new InstanceDescriptor (fi, null);
179 } catch (ArgumentException ex) {
180 // Parameter must be static
181 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
182 Assert.IsNull (ex.InnerException, "#3");
183 Assert.IsNotNull (ex.Message, "#4");
184 Assert.IsNull (ex.ParamName, "#5");
189 public void Property_Arguments_Mismatch ()
192 // ensure the property is not linked out of the application since it make the test fails
193 Assert.NotNull (Thread.CurrentPrincipal, "pre-test");
195 PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal");
197 InstanceDescriptor id = new InstanceDescriptor (pi, new object [] { url });
198 Assert.AreEqual (1, id.Arguments.Count, "#1");
199 object [] arguments = new object [id.Arguments.Count];
200 id.Arguments.CopyTo (arguments, 0);
201 Assert.AreSame (url, arguments [0], "#2");
202 Assert.IsTrue (id.IsComplete, "#3");
203 Assert.AreSame (pi, id.MemberInfo, "#4");
207 } catch (TargetParameterCountException) {
212 public void Property_Arguments_Null ()
215 // ensure the property is not linked out of the application since it make the test fails
216 Assert.NotNull (Thread.CurrentPrincipal, "pre-test");
218 PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal");
220 InstanceDescriptor id = new InstanceDescriptor (pi, null);
221 Assert.AreEqual (0, id.Arguments.Count, "#1");
222 Assert.IsTrue (id.IsComplete, "#2");
223 Assert.AreSame (pi, id.MemberInfo, "#3");
224 Assert.IsNotNull (id.Invoke (), "#4");
228 public void Property_MemberInfo_NonStatic ()
230 PropertyInfo pi = typeof (Uri).GetProperty ("Host");
233 new InstanceDescriptor (pi, null);
235 } catch (ArgumentException ex) {
236 // Parameter must be static
237 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
238 Assert.IsNull (ex.InnerException, "#A3");
239 Assert.IsNotNull (ex.Message, "#A4");
240 Assert.IsNull (ex.ParamName, "#A5");
244 new InstanceDescriptor (pi, null, false);
246 } catch (ArgumentException ex) {
247 // Parameter must be static
248 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
249 Assert.IsNull (ex.InnerException, "#B3");
250 Assert.IsNotNull (ex.Message, "#B4");
251 Assert.IsNull (ex.ParamName, "#B5");
256 public void Property_MemberInfo_WriteOnly ()
258 PropertyInfo pi = typeof (WriteOnlyProperty).GetProperty ("Name");
261 new InstanceDescriptor (pi, null);
263 } catch (ArgumentException ex) {
264 // Parameter must be readable
265 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
266 Assert.IsNull (ex.InnerException, "#3");
267 Assert.IsNotNull (ex.Message, "#4");
268 Assert.IsNull (ex.ParamName, "#5");
272 class WriteOnlyProperty
274 public static string Name {