New test.
[mono.git] / mcs / class / System / Test / System.ComponentModel / LicenseManagerTests.cs
1 //
2 // System.ComponentModel.LicenseManagerTests test cases
3 //
4 // Authors:
5 //      Ivan Hamilton (ivan@chimerical.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Martin Willemoes Hansen (mwh@sysrq.dk)
8 //
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2003 Martin Willemoes Hansen
11 // (c) 2004 Ivan Hamilton
12
13 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
14
15 #if NUNIT
16 using NUnit.Framework;
17 #else
18 using System.Reflection;
19 #endif
20
21 using System;
22 using System.ComponentModel;
23 using System.ComponentModel.Design;
24
25 namespace MonoTests.System.ComponentModel 
26 {
27         public class UnlicensedObject 
28         {
29         }
30
31         [LicenseProvider (typeof (TestLicenseProvider))]
32         public class LicensedObject
33         {
34         }
35
36         [LicenseProvider (typeof (TestLicenseProvider))]
37         public class InvalidLicensedObject
38         {
39         }
40
41         [LicenseProvider (typeof (TestLicenseProvider))]
42         public class RuntimeLicensedObject
43         {
44                 public RuntimeLicensedObject () 
45                 {
46                         LicenseManager.Validate (typeof (RuntimeLicensedObject));
47                 }
48                 public RuntimeLicensedObject (int a): this () {}
49         }
50
51         [LicenseProvider (typeof (TestLicenseProvider))]
52         public class DesigntimeLicensedObject
53         {
54                 public DesigntimeLicensedObject () 
55                 {
56                         LicenseManager.Validate (typeof (DesigntimeLicensedObject));
57                 }
58         }
59
60         public class TestLicenseProvider : LicenseProvider 
61         {
62
63                 private class TestLicense : License 
64                 {
65                         public override void Dispose ()
66                         {
67                         }
68
69                         public override string LicenseKey 
70                         {
71                                 get { return "YourLicenseKey"; }
72                         }
73                 }
74
75                 public TestLicenseProvider () : base ()
76                 {
77                 }
78
79                 public override License GetLicense (LicenseContext context,
80                         Type type,
81                         object instance,
82                         bool allowExceptions)
83                 {
84                         if (type.Name.Equals ("RuntimeLicensedObject")) {
85                                 if (context.UsageMode != LicenseUsageMode.Runtime)
86                                         if (allowExceptions)
87                                                 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
88                                         else
89                                                 return null;
90                                 return new TestLicense ();
91                         }
92
93                         if (type.Name.Equals ("DesigntimeLicensedObject")) 
94                         {
95                                 if (context.UsageMode != LicenseUsageMode.Designtime)
96                                         if (allowExceptions)
97                                                 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
98                                         else
99                                                 return null;
100                                 return new TestLicense ();
101                         }
102
103                         if (type.Name.Equals ("LicensedObject"))
104                                 return new TestLicense ();
105
106                         if (allowExceptions)
107                                 throw new LicenseException (type, instance, "License fails because of class name.");
108                         else
109                                 return null;
110                 }
111         }
112
113
114 #if NUNIT
115         [TestFixture]
116         public class LicenseManagerTests : Assertion
117         {
118                 [SetUp]
119                 public void GetReady ()
120                 {
121 #else
122         public class LicenseManagerTests
123         {
124                 static LicenseManagerTests ()
125                 {
126 #endif
127                 }
128
129 #if NUNIT
130                 [Test]
131 #endif
132                 public void Test () 
133                 {
134                         object lockObject = new object ();
135                         //**DEFAULT CONTEXT & LicenseUsageMode**
136                         //Get CurrentContext, check default type
137                         AssertEquals ("LicenseManager #1", "System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString());
138                         //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
139                         AssertEquals ("LicenseManager #2", LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode);
140
141                         //**CHANGING CONTEXT**
142                         //Change the context and check it changes
143                         LicenseContext oldcontext = LicenseManager.CurrentContext;
144                         LicenseContext newcontext = new DesigntimeLicenseContext();
145                         LicenseManager.CurrentContext = newcontext;
146                         AssertEquals ("LicenseManager #3", newcontext, LicenseManager.CurrentContext);
147                         //Check the UsageMode changed too
148                         AssertEquals ("LicenseManager #4", newcontext.UsageMode, LicenseManager.UsageMode);
149                         //Set Context back to original
150                         LicenseManager.CurrentContext = oldcontext;
151                         //Check it went back
152                         AssertEquals ("LicenseManager #5", oldcontext, LicenseManager.CurrentContext);
153                         //Check the UsageMode changed too
154                         AssertEquals ("LicenseManager #6", oldcontext.UsageMode, LicenseManager.UsageMode);
155
156                         //**CONTEXT LOCKING**
157                         //Lock the context
158                         LicenseManager.LockContext(lockObject);
159                         //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
160                         bool exceptionThrown = false;
161                         try 
162                         {
163                                 LicenseManager.CurrentContext=newcontext;
164                         } 
165                         catch (Exception e) 
166                         {
167                                 AssertEquals ("LicenseManager #7",typeof(InvalidOperationException), e.GetType());
168                                 exceptionThrown = true;
169                         } 
170                         //Check the exception was thrown
171                         AssertEquals ("LicenseManager #8", true, exceptionThrown);
172                         //Check the context didn't change
173                         AssertEquals ("LicenseManager #9", oldcontext, LicenseManager.CurrentContext);
174                         //Unlock it
175                         LicenseManager.UnlockContext(lockObject);
176                         //Now's unlocked, change it
177                         LicenseManager.CurrentContext=newcontext;
178                         AssertEquals ("LicenseManager #10", newcontext, LicenseManager.CurrentContext);
179                         //Change it back
180                         LicenseManager.CurrentContext = oldcontext;
181
182
183                         //Lock the context
184                         LicenseManager.LockContext(lockObject);
185                         //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
186                         object wrongLockObject = new object();
187                         exceptionThrown = false;
188                         try 
189                         {
190                                 LicenseManager.UnlockContext(wrongLockObject);
191                         } 
192                         catch (Exception e) 
193                         {
194                                 AssertEquals ("LicenseManager #11",typeof(ArgumentException), e.GetType());
195                                 exceptionThrown = true;
196                         } 
197                         AssertEquals ("LicenseManager #12",true, exceptionThrown);
198                         //Unlock it
199                         LicenseManager.UnlockContext(lockObject);
200
201                         //** bool IsValid(Type);
202                         AssertEquals ("LicenseManager #13", true, LicenseManager.IsLicensed (typeof (UnlicensedObject)));
203                         AssertEquals ("LicenseManager #14", true, LicenseManager.IsLicensed (typeof (LicensedObject)));
204                         AssertEquals ("LicenseManager #15", false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)));
205
206                         AssertEquals ("LicenseManager #16", true, LicenseManager.IsValid (typeof (UnlicensedObject)));
207                         AssertEquals ("LicenseManager #17", true, LicenseManager.IsValid (typeof (LicensedObject)));
208                         AssertEquals ("LicenseManager #18", false, LicenseManager.IsValid (typeof (InvalidLicensedObject)));
209
210                         UnlicensedObject unlicensedObject = new UnlicensedObject ();
211                         LicensedObject licensedObject = new LicensedObject ();
212                         InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
213
214                         //** bool IsValid(Type, object, License);
215                         License license=null;
216                         AssertEquals ("LicenseManager #19", true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license));
217                         AssertEquals ("LicenseManager #20", null, license);
218
219                         license=null;
220                         AssertEquals ("LicenseManager #21",true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license));
221                         AssertEquals ("LicenseManager #22", "TestLicense", license.GetType ().Name);
222
223                         license=null;
224                         AssertEquals ("LicenseManager #23",false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license));
225                         AssertEquals ("LicenseManager #24",null, license);
226
227                         //** void Validate(Type);
228                         //Shouldn't throw exception
229                         LicenseManager.Validate (typeof (UnlicensedObject));
230                         //Shouldn't throw exception
231                         LicenseManager.Validate (typeof (LicensedObject));
232                         //Should throw exception
233                         exceptionThrown=false;
234                         try 
235                         {
236                                 LicenseManager.Validate(typeof(InvalidLicensedObject));
237                         } 
238                         catch (Exception e) 
239                         {
240                                 AssertEquals ("LicenseManager #25",typeof(LicenseException), e.GetType());
241                                 exceptionThrown=true;
242                         } 
243                         //Check the exception was thrown
244                         AssertEquals ("LicenseManager #26",true, exceptionThrown);
245
246                         //** License Validate(Type, object);
247                         //Shouldn't throw exception, returns null license
248                         license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
249                         AssertEquals ("LicenseManager #27", null, license);
250
251                         //Shouldn't throw exception, returns TestLicense license
252                         license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
253                         AssertEquals ("LicenseManager #28", "TestLicense", license.GetType ().Name);
254
255                         //Should throw exception, returns null license
256                         exceptionThrown=false;
257                         try 
258                         {
259                                 license=null;
260                                 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
261                         } 
262                         catch (Exception e) 
263                         {
264                                 AssertEquals ("LicenseManager #29",typeof (LicenseException), e.GetType ());
265                                 exceptionThrown=true;
266                         } 
267                         //Check the exception was thrown
268                         AssertEquals ("LicenseManager #30",true, exceptionThrown);
269                         AssertEquals ("LicenseManager #31",null, license);
270
271
272                         //** object CreateWithContext (Type, LicenseContext);
273                         object cwc = null;
274                         //Test we can create an unlicensed object with no context
275                         cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
276                         AssertEquals ("LicenseManager #32", "UnlicensedObject", cwc.GetType ().Name);
277                         //Test we can create RunTime with CurrentContext (runtime)
278                         cwc = null;
279                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
280                                 LicenseManager.CurrentContext);
281                         AssertEquals ("LicenseManager #33", "RuntimeLicensedObject", cwc.GetType ().Name);
282                         //Test we can't create DesignTime with CurrentContext (runtime)
283                         exceptionThrown=false;
284                         try 
285                         {
286                                 cwc = null;
287                                 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
288                         } 
289                         catch (Exception e) 
290                         {
291                                 AssertEquals ("LicenseManager #34",typeof (LicenseException), e.GetType ());
292                                 exceptionThrown=true;
293                         } 
294                         //Check the exception was thrown
295                         AssertEquals ("LicenseManager #35",true, exceptionThrown);
296                         //Test we can create DesignTime with A new DesignTimeContext 
297                         cwc = null;
298                         cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
299                                 new DesigntimeLicenseContext ());
300                         AssertEquals ("LicenseManager #36", "DesigntimeLicensedObject", cwc.GetType ().Name);
301
302                         //** object CreateWithContext(Type, LicenseContext, object[]);
303                         //Test we can create RunTime with CurrentContext (runtime)
304                         cwc = null;
305                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
306                                 LicenseManager.CurrentContext, new object [] { 7 });
307                         AssertEquals ("LicenseManager #37", "RuntimeLicensedObject", cwc.GetType ().Name);
308
309                 }
310                 
311 #if !NUNIT
312                 void Assert (string msg, bool result)
313                 {
314                         if (!result)
315                                 Console.WriteLine (msg);
316                 }
317
318                 void AssertEquals (string msg, object expected, object real)
319                 {
320                         if (expected == null && real == null)
321                                 return;
322
323                         if (expected != null && expected.Equals (real))
324                                 return;
325
326                         Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
327                 }
328
329                 void Fail (string msg)
330                 {
331                         Console.WriteLine ("Failed: {0}", msg);
332                 }
333
334                 static void Main ()
335                 {
336                         LicenseManagerTests p = new LicenseManagerTests ();
337                         Type t = p.GetType ();
338                         MethodInfo [] methods = t.GetMethods ();
339                         foreach (MethodInfo m in methods) 
340                         {
341                                 if (m.Name.Substring (0, 4) == "Test") 
342                                 {
343                                         m.Invoke (p, null);
344                                 }
345                         }
346                 }
347 #endif
348         }
349 }