2 // System.ComponentModel.LicenseManagerTests test cases
5 // Ivan Hamilton (ivan@chimerical.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Martin Willemoes Hansen (mwh@sysrq.dk)
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2003 Martin Willemoes Hansen
11 // (c) 2004 Ivan Hamilton
13 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
16 using NUnit.Framework;
18 using System.Reflection;
22 using System.ComponentModel;
23 using System.ComponentModel.Design;
25 namespace MonoTests.System.ComponentModel
27 public class UnlicensedObject
31 [LicenseProvider (typeof (TestLicenseProvider))]
32 public class LicensedObject
36 [LicenseProvider (typeof (TestLicenseProvider))]
37 public class InvalidLicensedObject
41 [LicenseProvider (typeof (TestLicenseProvider))]
42 public class RuntimeLicensedObject
44 public RuntimeLicensedObject ()
46 LicenseManager.Validate (typeof (RuntimeLicensedObject));
48 public RuntimeLicensedObject (int a): this () {}
51 [LicenseProvider (typeof (TestLicenseProvider))]
52 public class DesigntimeLicensedObject
54 public DesigntimeLicensedObject ()
56 LicenseManager.Validate (typeof (DesigntimeLicensedObject));
60 public class TestLicenseProvider : LicenseProvider
63 private class TestLicense : License
65 public override void Dispose ()
69 public override string LicenseKey
71 get { return "YourLicenseKey"; }
75 public TestLicenseProvider () : base ()
79 public override License GetLicense (LicenseContext context,
84 if (type.Name.Equals ("RuntimeLicensedObject")) {
85 if (context.UsageMode != LicenseUsageMode.Runtime)
87 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
90 return new TestLicense ();
93 if (type.Name.Equals ("DesigntimeLicensedObject"))
95 if (context.UsageMode != LicenseUsageMode.Designtime)
97 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
100 return new TestLicense ();
103 if (type.Name.Equals ("LicensedObject"))
104 return new TestLicense ();
107 throw new LicenseException (type, instance, "License fails because of class name.");
116 public class LicenseManagerTests : Assertion
119 public void GetReady ()
122 public class LicenseManagerTests
124 static LicenseManagerTests ()
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);
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;
152 AssertEquals ("LicenseManager #5", oldcontext, LicenseManager.CurrentContext);
153 //Check the UsageMode changed too
154 AssertEquals ("LicenseManager #6", oldcontext.UsageMode, LicenseManager.UsageMode);
156 //**CONTEXT LOCKING**
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;
163 LicenseManager.CurrentContext=newcontext;
167 AssertEquals ("LicenseManager #7",typeof(InvalidOperationException), e.GetType());
168 exceptionThrown = true;
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);
175 LicenseManager.UnlockContext(lockObject);
176 //Now's unlocked, change it
177 LicenseManager.CurrentContext=newcontext;
178 AssertEquals ("LicenseManager #10", newcontext, LicenseManager.CurrentContext);
180 LicenseManager.CurrentContext = oldcontext;
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;
190 LicenseManager.UnlockContext(wrongLockObject);
194 AssertEquals ("LicenseManager #11",typeof(ArgumentException), e.GetType());
195 exceptionThrown = true;
197 AssertEquals ("LicenseManager #12",true, exceptionThrown);
199 LicenseManager.UnlockContext(lockObject);
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)));
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)));
210 UnlicensedObject unlicensedObject = new UnlicensedObject ();
211 LicensedObject licensedObject = new LicensedObject ();
212 InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
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);
220 AssertEquals ("LicenseManager #21",true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license));
221 AssertEquals ("LicenseManager #22", "TestLicense", license.GetType ().Name);
224 AssertEquals ("LicenseManager #23",false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license));
225 AssertEquals ("LicenseManager #24",null, license);
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;
236 LicenseManager.Validate(typeof(InvalidLicensedObject));
240 AssertEquals ("LicenseManager #25",typeof(LicenseException), e.GetType());
241 exceptionThrown=true;
243 //Check the exception was thrown
244 AssertEquals ("LicenseManager #26",true, exceptionThrown);
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);
251 //Shouldn't throw exception, returns TestLicense license
252 license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
253 AssertEquals ("LicenseManager #28", "TestLicense", license.GetType ().Name);
255 //Should throw exception, returns null license
256 exceptionThrown=false;
260 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
264 AssertEquals ("LicenseManager #29",typeof (LicenseException), e.GetType ());
265 exceptionThrown=true;
267 //Check the exception was thrown
268 AssertEquals ("LicenseManager #30",true, exceptionThrown);
269 AssertEquals ("LicenseManager #31",null, license);
272 //** object CreateWithContext (Type, LicenseContext);
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)
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;
287 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
291 AssertEquals ("LicenseManager #34",typeof (LicenseException), e.GetType ());
292 exceptionThrown=true;
294 //Check the exception was thrown
295 AssertEquals ("LicenseManager #35",true, exceptionThrown);
296 //Test we can create DesignTime with A new DesignTimeContext
298 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
299 new DesigntimeLicenseContext ());
300 AssertEquals ("LicenseManager #36", "DesigntimeLicensedObject", cwc.GetType ().Name);
302 //** object CreateWithContext(Type, LicenseContext, object[]);
303 //Test we can create RunTime with CurrentContext (runtime)
305 cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
306 LicenseManager.CurrentContext, new object [] { 7 });
307 AssertEquals ("LicenseManager #37", "RuntimeLicensedObject", cwc.GetType ().Name);
312 void Assert (string msg, bool result)
315 Console.WriteLine (msg);
318 void AssertEquals (string msg, object expected, object real)
320 if (expected == null && real == null)
323 if (expected != null && expected.Equals (real))
326 Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
329 void Fail (string msg)
331 Console.WriteLine ("Failed: {0}", msg);
336 LicenseManagerTests p = new LicenseManagerTests ();
337 Type t = p.GetType ();
338 MethodInfo [] methods = t.GetMethods ();
339 foreach (MethodInfo m in methods)
341 if (m.Name.Substring (0, 4) == "Test")