Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / System / Test / System.ComponentModel.Design.Serialization / InstanceDescriptorTest.cs
1 //
2 // InstanceDescriptorTest.cs - Unit tests for 
3 //      System.ComponentModel.Design.Serialization.InstanceDescriptor
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31
32 using System;
33 using System.ComponentModel;
34 using System.ComponentModel.Design.Serialization;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Threading;
38
39 namespace MonoTests.System.ComponentModel.Design.Serialization {
40
41         [TestFixture]
42         public class InstanceDescriptorTest {
43
44                 private const string url = "http://www.mono-project.com/";
45                 private ConstructorInfo ci;
46
47                 [TestFixtureSetUp]
48                 public void FixtureSetUp ()
49                 {
50                         ci = typeof (Uri).GetConstructor (new Type[1] { typeof (string) });
51                 }
52
53                 [Test]
54                 public void Constructor0_Arguments_Mismatch ()
55                 {
56                         try {
57                                 new InstanceDescriptor (ci, null);
58                                 Assert.Fail ("#1");
59                         } catch (ArgumentException ex) {
60                                 // Length mismatch
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");
65                         }
66                 }
67
68                 [Test]
69                 public void Constructor0_MemberInfo_Type ()
70                 {
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");
78                 }
79
80                 [Test]
81                 public void Constructor_Null_ICollection ()
82                 {
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");
88                 }
89
90                 [Test]
91                 public void Constructor_MemberInfo_ICollection ()
92                 {
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");
99                 }
100
101                 [Test]
102                 public void Constructor_Null_ICollection_Boolean ()
103                 {
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");
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof (ArgumentException))]
113                 public void Constructor_MemberInfo_Null_Boolean ()
114                 {
115                         new InstanceDescriptor (ci, null, false);
116                         // mismatch for required parameters
117                 }
118
119                 [Test]
120                 public void Constructor_MemberInfo_ICollection_Boolean ()
121                 {
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");
128                 }
129
130                 [Test]
131                 public void Field_Arguments_Empty ()
132                 {
133                         FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
134
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");
140                 }
141
142                 [Test]
143                 public void Field_Arguments_Mismatch ()
144                 {
145                         FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
146
147                         try {
148                                 new InstanceDescriptor (fi, new object [] { url });
149                                 Assert.Fail ("#1");
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");
156                         }
157                 }
158
159                 [Test]
160                 public void Field_Arguments_Null ()
161                 {
162                         FieldInfo fi = typeof (Uri).GetField ("SchemeDelimiter");
163
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");
169                 }
170
171                 [Test]
172                 public void Field_MemberInfo_NonStatic ()
173                 {
174                         FieldInfo fi = typeof (InstanceField).GetField ("Name");
175
176                         try {
177                                 new InstanceDescriptor (fi, null);
178                                 Assert.Fail ("#1");
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");
185                         }
186                 }
187
188                 [Test]
189                 public void Property_Arguments_Mismatch ()
190                 {
191 #if MOBILE
192                         // ensure the property is not linked out of the application since it make the test fails
193                         Assert.NotNull (Thread.CurrentPrincipal, "pre-test");
194 #endif
195                         PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal");
196
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");
204                         try {
205                                 id.Invoke ();
206                                 Assert.Fail ("#5");
207                         } catch (TargetParameterCountException) {
208                         }
209                 }
210
211                 [Test]
212                 public void Property_Arguments_Null ()
213                 {
214 #if MOBILE
215                         // ensure the property is not linked out of the application since it make the test fails
216                         Assert.NotNull (Thread.CurrentPrincipal, "pre-test");
217 #endif
218                         PropertyInfo pi = typeof (Thread).GetProperty ("CurrentPrincipal");
219
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");
225                 }
226
227                 [Test]
228                 public void Property_MemberInfo_NonStatic ()
229                 {
230                         PropertyInfo pi = typeof (Uri).GetProperty ("Host");
231
232                         try {
233                                 new InstanceDescriptor (pi, null);
234                                 Assert.Fail ("#A1");
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");
241                         }
242
243                         try {
244                                 new InstanceDescriptor (pi, null, false);
245                                 Assert.Fail ("#B1");
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");
252                         }
253                 }
254
255                 [Test]
256                 public void Property_MemberInfo_WriteOnly ()
257                 {
258                         PropertyInfo pi = typeof (WriteOnlyProperty).GetProperty ("Name");
259
260                         try {
261                                 new InstanceDescriptor (pi, null);
262                                 Assert.Fail ("#1");
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");
269                         }
270                 }
271
272                 class WriteOnlyProperty
273                 {
274                         public static string Name {
275                                 set {
276                                 }
277                         }
278                 }
279
280                 class InstanceField
281                 {
282                         public string Name;
283                 }
284         }
285 }