2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 using NUnit.Framework;
14 using System;
15 using System.ComponentModel;
16 using System.ComponentModel.Design;
17
18 namespace MonoTests.System.ComponentModel 
19 {
20         public class UnlicensedObject 
21         {
22         }
23
24         [LicenseProvider (typeof (TestLicenseProvider))]
25         public class LicensedObject
26         {
27         }
28
29         [LicenseProvider (typeof (TestLicenseProvider))]
30         public class InvalidLicensedObject
31         {
32         }
33
34         [LicenseProvider (typeof (TestLicenseProvider))]
35         public class RuntimeLicensedObject
36         {
37                 public RuntimeLicensedObject () 
38                 {
39                         LicenseManager.Validate (typeof (RuntimeLicensedObject));
40                 }
41                 public RuntimeLicensedObject (int a): this () {}
42         }
43
44         [LicenseProvider (typeof (TestLicenseProvider))]
45         public class DesigntimeLicensedObject
46         {
47                 public DesigntimeLicensedObject () 
48                 {
49                         LicenseManager.Validate (typeof (DesigntimeLicensedObject));
50                 }
51         }
52
53         public class TestLicenseProvider : LicenseProvider 
54         {
55
56                 private class TestLicense : License 
57                 {
58                         public override void Dispose ()
59                         {
60                         }
61
62                         public override string LicenseKey 
63                         {
64                                 get { return "YourLicenseKey"; }
65                         }
66                 }
67
68                 public TestLicenseProvider () : base ()
69                 {
70                 }
71
72                 public override License GetLicense (LicenseContext context,
73                         Type type,
74                         object instance,
75                         bool allowExceptions)
76                 {
77                         if (type.Name.Equals ("RuntimeLicensedObject")) {
78                                 if (context.UsageMode != LicenseUsageMode.Runtime)
79                                         if (allowExceptions)
80                                                 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
81                                         else
82                                                 return null;
83                                 return new TestLicense ();
84                         }
85
86                         if (type.Name.Equals ("DesigntimeLicensedObject")) 
87                         {
88                                 if (context.UsageMode != LicenseUsageMode.Designtime)
89                                         if (allowExceptions)
90                                                 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
91                                         else
92                                                 return null;
93                                 return new TestLicense ();
94                         }
95
96                         if (type.Name.Equals ("LicensedObject"))
97                                 return new TestLicense ();
98
99                         if (allowExceptions)
100                                 throw new LicenseException (type, instance, "License fails because of class name.");
101                         else
102                                 return null;
103                 }
104         }
105
106
107         [TestFixture]
108         public class LicenseManagerTests
109         {
110                 [SetUp]
111                 public void GetReady ()
112                 {
113                 }
114
115                 [Test]
116                 public void Test () 
117                 {
118                         object lockObject = new object ();
119                         //**DEFAULT CONTEXT & LicenseUsageMode**
120                         //Get CurrentContext, check default type
121                         Assert.AreEqual ("System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString(), "LicenseManager #1");
122                         //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
123                         Assert.AreEqual (LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode, "LicenseManager #2");
124
125                         //**CHANGING CONTEXT**
126                         //Change the context and check it changes
127                         LicenseContext oldcontext = LicenseManager.CurrentContext;
128                         LicenseContext newcontext = new DesigntimeLicenseContext();
129                         LicenseManager.CurrentContext = newcontext;
130                         Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #3");
131                         //Check the UsageMode changed too
132                         Assert.AreEqual (newcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #4");
133                         //Set Context back to original
134                         LicenseManager.CurrentContext = oldcontext;
135                         //Check it went back
136                         Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #5");
137                         //Check the UsageMode changed too
138                         Assert.AreEqual (oldcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #6");
139
140                         //**CONTEXT LOCKING**
141                         //Lock the context
142                         LicenseManager.LockContext(lockObject);
143                         //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
144                         bool exceptionThrown = false;
145                         try 
146                         {
147                                 LicenseManager.CurrentContext=newcontext;
148                         } 
149                         catch (Exception e) 
150                         {
151                                 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(), "LicenseManager #7");
152                                 exceptionThrown = true;
153                         } 
154                         //Check the exception was thrown
155                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #8");
156                         //Check the context didn't change
157                         Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #9");
158                         //Unlock it
159                         LicenseManager.UnlockContext(lockObject);
160                         //Now's unlocked, change it
161                         LicenseManager.CurrentContext=newcontext;
162                         Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #10");
163                         //Change it back
164                         LicenseManager.CurrentContext = oldcontext;
165
166
167                         //Lock the context
168                         LicenseManager.LockContext(lockObject);
169                         //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
170                         object wrongLockObject = new object();
171                         exceptionThrown = false;
172                         try 
173                         {
174                                 LicenseManager.UnlockContext(wrongLockObject);
175                         } 
176                         catch (Exception e) 
177                         {
178                                 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "LicenseManager #11");
179                                 exceptionThrown = true;
180                         } 
181                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #12");
182                         //Unlock it
183                         LicenseManager.UnlockContext(lockObject);
184
185                         //** bool IsValid(Type);
186                         Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (UnlicensedObject)), "LicenseManager #13");
187                         Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (LicensedObject)), "LicenseManager #14");
188                         Assert.AreEqual (false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)), "LicenseManager #15");
189
190                         Assert.AreEqual (true, LicenseManager.IsValid (typeof (UnlicensedObject)), "LicenseManager #16");
191                         Assert.AreEqual (true, LicenseManager.IsValid (typeof (LicensedObject)), "LicenseManager #17");
192                         Assert.AreEqual (false, LicenseManager.IsValid (typeof (InvalidLicensedObject)), "LicenseManager #18");
193
194                         UnlicensedObject unlicensedObject = new UnlicensedObject ();
195                         LicensedObject licensedObject = new LicensedObject ();
196                         InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
197
198                         //** bool IsValid(Type, object, License);
199                         License license=null;
200                         Assert.AreEqual (true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license), "LicenseManager #19");
201                         Assert.AreEqual (null, license, "LicenseManager #20");
202
203                         license=null;
204                         Assert.AreEqual (true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license), "LicenseManager #21");
205                         Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #22");
206
207                         license=null;
208                         Assert.AreEqual (false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license), "LicenseManager #23");
209                         Assert.AreEqual (null, license, "LicenseManager #24");
210
211                         //** void Validate(Type);
212                         //Shouldn't throw exception
213                         LicenseManager.Validate (typeof (UnlicensedObject));
214                         //Shouldn't throw exception
215                         LicenseManager.Validate (typeof (LicensedObject));
216                         //Should throw exception
217                         exceptionThrown=false;
218                         try 
219                         {
220                                 LicenseManager.Validate(typeof(InvalidLicensedObject));
221                         } 
222                         catch (Exception e) 
223                         {
224                                 Assert.AreEqual (typeof(LicenseException), e.GetType(), "LicenseManager #25");
225                                 exceptionThrown=true;
226                         } 
227                         //Check the exception was thrown
228                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #26");
229
230                         //** License Validate(Type, object);
231                         //Shouldn't throw exception, returns null license
232                         license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
233                         Assert.AreEqual (null, license, "LicenseManager #27");
234
235                         //Shouldn't throw exception, returns TestLicense license
236                         license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
237                         Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #28");
238
239                         //Should throw exception, returns null license
240                         exceptionThrown=false;
241                         try 
242                         {
243                                 license=null;
244                                 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
245                         } 
246                         catch (Exception e) 
247                         {
248                                 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #29");
249                                 exceptionThrown=true;
250                         } 
251                         //Check the exception was thrown
252                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #30");
253                         Assert.AreEqual (null, license, "LicenseManager #31");
254
255
256                         //** object CreateWithContext (Type, LicenseContext);
257                         object cwc = null;
258                         //Test we can create an unlicensed object with no context
259                         cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
260                         Assert.AreEqual ("UnlicensedObject", cwc.GetType ().Name, "LicenseManager #32");
261                         //Test we can create RunTime with CurrentContext (runtime)
262                         cwc = null;
263                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
264                                 LicenseManager.CurrentContext);
265                         Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #33");
266                         //Test we can't create DesignTime with CurrentContext (runtime)
267                         exceptionThrown=false;
268                         try 
269                         {
270                                 cwc = null;
271                                 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
272                         } 
273                         catch (Exception e) 
274                         {
275                                 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #34");
276                                 exceptionThrown=true;
277                         } 
278                         //Check the exception was thrown
279                         Assert.AreEqual (true, exceptionThrown, "LicenseManager #35");
280                         //Test we can create DesignTime with A new DesignTimeContext 
281                         cwc = null;
282                         cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
283                                 new DesigntimeLicenseContext ());
284                         Assert.AreEqual ("DesigntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #36");
285
286                         //** object CreateWithContext(Type, LicenseContext, object[]);
287                         //Test we can create RunTime with CurrentContext (runtime)
288                         cwc = null;
289                         cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
290                                 LicenseManager.CurrentContext, new object [] { 7 });
291                         Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #37");
292
293                 }
294         }
295 }