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
15 using NUnit.Framework;
17 using System.ComponentModel;
18 using System.ComponentModel.Design;
20 namespace MonoTests.System.ComponentModel
22 public class UnlicensedObject
26 [LicenseProvider (typeof (TestLicenseProvider))]
27 public class LicensedObject
31 [LicenseProvider (typeof (TestLicenseProvider))]
32 public class InvalidLicensedObject
36 [LicenseProvider (typeof (TestLicenseProvider))]
37 public class RuntimeLicensedObject
39 public RuntimeLicensedObject ()
41 LicenseManager.Validate (typeof (RuntimeLicensedObject));
43 public RuntimeLicensedObject (int a): this () {}
46 [LicenseProvider (typeof (TestLicenseProvider))]
47 public class DesigntimeLicensedObject
49 public DesigntimeLicensedObject ()
51 LicenseManager.Validate (typeof (DesigntimeLicensedObject));
55 public class TestLicenseProvider : LicenseProvider
58 private class TestLicense : License
60 public override void Dispose ()
64 public override string LicenseKey
66 get { return "YourLicenseKey"; }
70 public TestLicenseProvider () : base ()
74 public override License GetLicense (LicenseContext context,
79 if (type.Name.Equals ("RuntimeLicensedObject")) {
80 if (context.UsageMode != LicenseUsageMode.Runtime)
82 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
85 return new TestLicense ();
88 if (type.Name.Equals ("DesigntimeLicensedObject"))
90 if (context.UsageMode != LicenseUsageMode.Designtime)
92 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
95 return new TestLicense ();
98 if (type.Name.Equals ("LicensedObject"))
99 return new TestLicense ();
102 throw new LicenseException (type, instance, "License fails because of class name.");
110 public class LicenseManagerTests
113 public void GetReady ()
120 object lockObject = new object ();
121 //**DEFAULT CONTEXT & LicenseUsageMode**
122 //Get CurrentContext, check default type
123 Assert.AreEqual ("System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString(), "LicenseManager #1");
124 //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode
125 Assert.AreEqual (LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode, "LicenseManager #2");
127 //**CHANGING CONTEXT**
128 //Change the context and check it changes
129 LicenseContext oldcontext = LicenseManager.CurrentContext;
130 LicenseContext newcontext = new DesigntimeLicenseContext();
131 LicenseManager.CurrentContext = newcontext;
132 Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #3");
133 //Check the UsageMode changed too
134 Assert.AreEqual (newcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #4");
135 //Set Context back to original
136 LicenseManager.CurrentContext = oldcontext;
138 Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #5");
139 //Check the UsageMode changed too
140 Assert.AreEqual (oldcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #6");
142 //**CONTEXT LOCKING**
144 LicenseManager.LockContext(lockObject);
145 //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed.
146 bool exceptionThrown = false;
149 LicenseManager.CurrentContext=newcontext;
153 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(), "LicenseManager #7");
154 exceptionThrown = true;
156 //Check the exception was thrown
157 Assert.AreEqual (true, exceptionThrown, "LicenseManager #8");
158 //Check the context didn't change
159 Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #9");
161 LicenseManager.UnlockContext(lockObject);
162 //Now's unlocked, change it
163 LicenseManager.CurrentContext=newcontext;
164 Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #10");
166 LicenseManager.CurrentContext = oldcontext;
170 LicenseManager.LockContext(lockObject);
171 //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser.
172 object wrongLockObject = new object();
173 exceptionThrown = false;
176 LicenseManager.UnlockContext(wrongLockObject);
180 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "LicenseManager #11");
181 exceptionThrown = true;
183 Assert.AreEqual (true, exceptionThrown, "LicenseManager #12");
185 LicenseManager.UnlockContext(lockObject);
187 //** bool IsValid(Type);
188 Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (UnlicensedObject)), "LicenseManager #13");
189 Assert.AreEqual (true, LicenseManager.IsLicensed (typeof (LicensedObject)), "LicenseManager #14");
190 Assert.AreEqual (false, LicenseManager.IsLicensed (typeof (InvalidLicensedObject)), "LicenseManager #15");
192 Assert.AreEqual (true, LicenseManager.IsValid (typeof (UnlicensedObject)), "LicenseManager #16");
193 Assert.AreEqual (true, LicenseManager.IsValid (typeof (LicensedObject)), "LicenseManager #17");
194 Assert.AreEqual (false, LicenseManager.IsValid (typeof (InvalidLicensedObject)), "LicenseManager #18");
196 UnlicensedObject unlicensedObject = new UnlicensedObject ();
197 LicensedObject licensedObject = new LicensedObject ();
198 InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
200 //** bool IsValid(Type, object, License);
201 License license=null;
202 Assert.AreEqual (true, LicenseManager.IsValid (unlicensedObject.GetType (), unlicensedObject, out license), "LicenseManager #19");
203 Assert.AreEqual (null, license, "LicenseManager #20");
206 Assert.AreEqual (true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license), "LicenseManager #21");
207 Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #22");
210 Assert.AreEqual (false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license), "LicenseManager #23");
211 Assert.AreEqual (null, license, "LicenseManager #24");
213 //** void Validate(Type);
214 //Shouldn't throw exception
215 LicenseManager.Validate (typeof (UnlicensedObject));
216 //Shouldn't throw exception
217 LicenseManager.Validate (typeof (LicensedObject));
218 //Should throw exception
219 exceptionThrown=false;
222 LicenseManager.Validate(typeof(InvalidLicensedObject));
226 Assert.AreEqual (typeof(LicenseException), e.GetType(), "LicenseManager #25");
227 exceptionThrown=true;
229 //Check the exception was thrown
230 Assert.AreEqual (true, exceptionThrown, "LicenseManager #26");
232 //** License Validate(Type, object);
233 //Shouldn't throw exception, returns null license
234 license=LicenseManager.Validate (typeof (UnlicensedObject), unlicensedObject);
235 Assert.AreEqual (null, license, "LicenseManager #27");
237 //Shouldn't throw exception, returns TestLicense license
238 license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
239 Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #28");
241 //Should throw exception, returns null license
242 exceptionThrown=false;
246 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
250 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #29");
251 exceptionThrown=true;
253 //Check the exception was thrown
254 Assert.AreEqual (true, exceptionThrown, "LicenseManager #30");
255 Assert.AreEqual (null, license, "LicenseManager #31");
258 //** object CreateWithContext (Type, LicenseContext);
260 //Test we can create an unlicensed object with no context
261 cwc = LicenseManager.CreateWithContext (typeof (UnlicensedObject), null);
262 Assert.AreEqual ("UnlicensedObject", cwc.GetType ().Name, "LicenseManager #32");
263 //Test we can create RunTime with CurrentContext (runtime)
265 cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
266 LicenseManager.CurrentContext);
267 Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #33");
268 //Test we can't create DesignTime with CurrentContext (runtime)
269 exceptionThrown=false;
273 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
277 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #34");
278 exceptionThrown=true;
280 //Check the exception was thrown
281 Assert.AreEqual (true, exceptionThrown, "LicenseManager #35");
282 //Test we can create DesignTime with A new DesignTimeContext
284 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
285 new DesigntimeLicenseContext ());
286 Assert.AreEqual ("DesigntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #36");
288 //** object CreateWithContext(Type, LicenseContext, object[]);
289 //Test we can create RunTime with CurrentContext (runtime)
291 cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
292 LicenseManager.CurrentContext, new object [] { 7 });
293 Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #37");