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 using NUnit.Framework;
15 using System.ComponentModel;
16 using System.ComponentModel.Design;
18 namespace MonoTests.System.ComponentModel
20 public class UnlicensedObject
24 [LicenseProvider (typeof (TestLicenseProvider))]
25 public class LicensedObject
29 [LicenseProvider (typeof (TestLicenseProvider))]
30 public class InvalidLicensedObject
34 [LicenseProvider (typeof (TestLicenseProvider))]
35 public class RuntimeLicensedObject
37 public RuntimeLicensedObject ()
39 LicenseManager.Validate (typeof (RuntimeLicensedObject));
41 public RuntimeLicensedObject (int a): this () {}
44 [LicenseProvider (typeof (TestLicenseProvider))]
45 public class DesigntimeLicensedObject
47 public DesigntimeLicensedObject ()
49 LicenseManager.Validate (typeof (DesigntimeLicensedObject));
53 public class TestLicenseProvider : LicenseProvider
56 private class TestLicense : License
58 public override void Dispose ()
62 public override string LicenseKey
64 get { return "YourLicenseKey"; }
68 public TestLicenseProvider () : base ()
72 public override License GetLicense (LicenseContext context,
77 if (type.Name.Equals ("RuntimeLicensedObject")) {
78 if (context.UsageMode != LicenseUsageMode.Runtime)
80 throw new LicenseException (type, instance, "License fails because this is a Runtime only license");
83 return new TestLicense ();
86 if (type.Name.Equals ("DesigntimeLicensedObject"))
88 if (context.UsageMode != LicenseUsageMode.Designtime)
90 throw new LicenseException (type, instance, "License fails because this is a Designtime only license");
93 return new TestLicense ();
96 if (type.Name.Equals ("LicensedObject"))
97 return new TestLicense ();
100 throw new LicenseException (type, instance, "License fails because of class name.");
108 public class LicenseManagerTests
111 public void GetReady ()
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");
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;
136 Assert.AreEqual (oldcontext, LicenseManager.CurrentContext, "LicenseManager #5");
137 //Check the UsageMode changed too
138 Assert.AreEqual (oldcontext.UsageMode, LicenseManager.UsageMode, "LicenseManager #6");
140 //**CONTEXT LOCKING**
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;
147 LicenseManager.CurrentContext=newcontext;
151 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(), "LicenseManager #7");
152 exceptionThrown = true;
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");
159 LicenseManager.UnlockContext(lockObject);
160 //Now's unlocked, change it
161 LicenseManager.CurrentContext=newcontext;
162 Assert.AreEqual (newcontext, LicenseManager.CurrentContext, "LicenseManager #10");
164 LicenseManager.CurrentContext = oldcontext;
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;
174 LicenseManager.UnlockContext(wrongLockObject);
178 Assert.AreEqual (typeof(ArgumentException), e.GetType(), "LicenseManager #11");
179 exceptionThrown = true;
181 Assert.AreEqual (true, exceptionThrown, "LicenseManager #12");
183 LicenseManager.UnlockContext(lockObject);
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");
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");
194 UnlicensedObject unlicensedObject = new UnlicensedObject ();
195 LicensedObject licensedObject = new LicensedObject ();
196 InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject ();
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");
204 Assert.AreEqual (true, LicenseManager.IsValid (licensedObject.GetType (), licensedObject,out license), "LicenseManager #21");
205 Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #22");
208 Assert.AreEqual (false, LicenseManager.IsValid (invalidLicensedObject.GetType (), invalidLicensedObject, out license), "LicenseManager #23");
209 Assert.AreEqual (null, license, "LicenseManager #24");
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;
220 LicenseManager.Validate(typeof(InvalidLicensedObject));
224 Assert.AreEqual (typeof(LicenseException), e.GetType(), "LicenseManager #25");
225 exceptionThrown=true;
227 //Check the exception was thrown
228 Assert.AreEqual (true, exceptionThrown, "LicenseManager #26");
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");
235 //Shouldn't throw exception, returns TestLicense license
236 license=LicenseManager.Validate(typeof(LicensedObject), licensedObject);
237 Assert.AreEqual ("TestLicense", license.GetType ().Name, "LicenseManager #28");
239 //Should throw exception, returns null license
240 exceptionThrown=false;
244 license=LicenseManager.Validate (typeof (InvalidLicensedObject), invalidLicensedObject);
248 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #29");
249 exceptionThrown=true;
251 //Check the exception was thrown
252 Assert.AreEqual (true, exceptionThrown, "LicenseManager #30");
253 Assert.AreEqual (null, license, "LicenseManager #31");
256 //** object CreateWithContext (Type, LicenseContext);
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)
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;
271 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject), LicenseManager.CurrentContext);
275 Assert.AreEqual (typeof (LicenseException), e.GetType (), "LicenseManager #34");
276 exceptionThrown=true;
278 //Check the exception was thrown
279 Assert.AreEqual (true, exceptionThrown, "LicenseManager #35");
280 //Test we can create DesignTime with A new DesignTimeContext
282 cwc = LicenseManager.CreateWithContext (typeof (DesigntimeLicensedObject),
283 new DesigntimeLicenseContext ());
284 Assert.AreEqual ("DesigntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #36");
286 //** object CreateWithContext(Type, LicenseContext, object[]);
287 //Test we can create RunTime with CurrentContext (runtime)
289 cwc = LicenseManager.CreateWithContext (typeof (RuntimeLicensedObject),
290 LicenseManager.CurrentContext, new object [] { 7 });
291 Assert.AreEqual ("RuntimeLicensedObject", cwc.GetType ().Name, "LicenseManager #37");