10using System.Collections.Generic;
11using System.ComponentModel;
17using System.Threading.Tasks;
22using TestClass = NUnit.Framework.TestFixtureAttribute;
23using TestInitialize = NUnit.Framework.SetUpAttribute;
24using TestCleanup = NUnit.Framework.TearDownAttribute;
25using TestMethod = NUnit.Framework.TestAttribute;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
41 _testDIContext = TestDIContextFactory.CreateDefaultContext();
50 var root = await CreateHasRulesManagerAsync();
51 context.
Assert.AreEqual(
"<new>", root.Name);
52 context.
Assert.AreEqual(
true, root.IsValid,
"should be valid on create");
53 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
59 context.
Assert.AreEqual(
"<new>", root.Name);
60 context.
Assert.AreEqual(
true, root.IsValid,
"should be valid after CancelEdit");
61 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
67 context.
Assert.AreEqual(
"", root.Name);
68 context.
Assert.AreEqual(
false, root.IsValid);
69 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
70 context.
Assert.AreEqual(
"Name required", root.BrokenRulesCollection[0].Description);
81 var root = await CreateHasRulesManager2Async(
"<new>");
82 Assert.AreEqual(
"<new>", root.Name);
83 Assert.AreEqual(
true, root.IsValid,
"should be valid on create");
84 Assert.AreEqual(0, root.BrokenRulesCollection.Count);
90 Assert.AreEqual(
"<new>", root.Name);
91 Assert.AreEqual(
true, root.IsValid,
"should be valid after CancelEdit");
92 Assert.AreEqual(0, root.BrokenRulesCollection.Count);
98 Assert.AreEqual(
"", root.Name);
99 Assert.AreEqual(
false, root.IsValid);
100 Assert.AreEqual(1, root.BrokenRulesCollection.Count);
101 Assert.AreEqual(
"Name required", root.BrokenRulesCollection[0].Description);
112 var root = await CreateHasRulesManagerAsync();
113 context.
Assert.AreEqual(
"<new>", root.Name);
114 context.
Assert.AreEqual(
true, root.IsValid,
"should be valid on create");
115 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
117 bool validationComplete =
false;
118 root.ValidationComplete += (vo, ve) => { validationComplete =
true; };
122 context.
Assert.AreEqual(
"", root.Name);
123 context.
Assert.AreEqual(
false, root.IsValid);
124 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
125 context.
Assert.AreEqual(
"Name required", root.BrokenRulesCollection[0].Description);
126 context.
Assert.
IsTrue(validationComplete,
"ValidationComplete should have run");
128 root.Name =
"Begin 1";
129 context.
Assert.AreEqual(
"Begin 1", root.Name);
130 context.
Assert.AreEqual(
true, root.IsValid);
131 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
133 root.Name =
"Begin 2";
134 context.
Assert.AreEqual(
"Begin 2", root.Name);
135 context.
Assert.AreEqual(
true, root.IsValid);
136 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
138 root.Name =
"Begin 3";
139 context.
Assert.AreEqual(
"Begin 3", root.Name);
140 context.
Assert.AreEqual(
true, root.IsValid);
141 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
147 context.
Assert.AreEqual(
"Begin 2", root.Name);
148 context.
Assert.AreEqual(
true, root.IsValid);
149 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
150 hrmClone.CancelEdit();
151 context.
Assert.AreEqual(
"Begin 2", hrmClone.
Name);
152 context.
Assert.AreEqual(
true, hrmClone.IsValid);
153 context.
Assert.AreEqual(0, hrmClone.BrokenRulesCollection.Count);
155 context.
Assert.AreEqual(
"Begin 1", root.Name);
156 context.
Assert.AreEqual(
true, root.IsValid);
157 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
158 hrmClone.CancelEdit();
159 context.
Assert.AreEqual(
"Begin 1", hrmClone.
Name);
160 context.
Assert.AreEqual(
true, hrmClone.IsValid);
161 context.
Assert.AreEqual(0, hrmClone.BrokenRulesCollection.Count);
163 context.
Assert.AreEqual(
"", root.Name);
164 context.
Assert.AreEqual(
false, root.IsValid);
165 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
166 context.
Assert.AreEqual(
"Name required", root.BrokenRulesCollection[0].Description);
167 hrmClone.CancelEdit();
169 context.
Assert.AreEqual(
false, hrmClone.IsValid);
170 context.
Assert.AreEqual(1, hrmClone.BrokenRulesCollection.Count);
171 context.
Assert.AreEqual(
"Name required", hrmClone.BrokenRulesCollection[0].Description);
173 context.
Assert.AreEqual(
"<new>", root.Name);
174 context.
Assert.AreEqual(
true, root.IsValid);
175 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
176 hrmClone.CancelEdit();
177 context.
Assert.AreEqual(
"<new>", hrmClone.
Name);
178 context.
Assert.AreEqual(
true, hrmClone.IsValid);
179 context.
Assert.AreEqual(0, hrmClone.BrokenRulesCollection.Count);
192 var root = await CreateHasRulesManager2Async(
"test");
193 Assert.AreEqual(
true, root.IsValid);
198 Assert.AreEqual(
false, root.IsValid);
200 Assert.AreEqual(
false, rootClone.IsValid);
202 rootClone.
Name =
"something";
203 Assert.AreEqual(
true, rootClone.IsValid);
211 var root = await CreateHasRulesManagerAsync();
213 Assert.AreEqual(
false, root.IsValid,
"should not be valid");
214 Assert.AreEqual(1, root.BrokenRulesCollection.Count);
215 Assert.AreEqual(
"Name required", root.BrokenRulesCollection[0].Description);
224 var root = await CreateHasRulesManagerAsync();
225 root.Name =
"12345678901";
226 context.
Assert.AreEqual(
false, root.IsValid,
"should not be valid");
227 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
229 Assert.AreEqual(
"Name can not exceed 10 characters", root.BrokenRulesCollection[0].Description);
231 root.Name =
"1234567890";
232 context.
Assert.AreEqual(
true, root.IsValid,
"should be valid");
233 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
244 var root = await CreateHasRulesManagerAsync();
245 root.Name =
"12345678901";
246 context.
Assert.AreEqual(
false, root.IsValid,
"should not be valid before clone");
247 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
249 Assert.AreEqual(
"Name can not exceed 10 characters", root.BrokenRulesCollection[0].Description);
252 context.
Assert.AreEqual(
false, root.IsValid,
"should not be valid after clone");
253 context.
Assert.AreEqual(1, root.BrokenRulesCollection.Count);
255 context.
Assert.AreEqual(
"Name can not exceed 10 characters", root.BrokenRulesCollection[0].Description);
257 root.Name =
"1234567890";
258 context.
Assert.AreEqual(
true, root.IsValid,
"Should be valid");
259 context.
Assert.AreEqual(0, root.BrokenRulesCollection.Count);
270 HasRegEx root = CreateWithoutCriteria<HasRegEx>();
272 root.
Ssn =
"555-55-5555";
273 root.
Ssn2 =
"555-55-5555";
274 context.
Assert.
IsTrue(root.IsValid,
"Ssn should be valid");
276 root.
Ssn =
"555-55-5555d";
277 context.
Assert.
IsFalse(root.IsValid,
"Ssn should not be valid");
279 root.
Ssn =
"555-55-5555";
280 root.
Ssn2 =
"555-55-5555d";
281 context.
Assert.
IsFalse(root.IsValid,
"Ssn should not be valid");
291 var root = CreateWithoutCriteria<BrokenRulesMergeRoot>();
294 context.
Assert.AreEqual(2, list.Count,
"Should have 2 broken rules");
295 context.
Assert.AreEqual(
"rule://csla.test.validationrules.brokenrulesmergeroot-rulebroken/Test1", list[0].RuleName);
307 var root = await CreateHasRulesManager2Async();
308 string expected = root.Name;
312 rootClone.CancelEdit();
314 string actual = rootClone.
Name;
315 context.
Assert.AreEqual(expected, actual);
328 var root = await CreateWithoutCriteriaAsync<HasChildren>();
329 context.
Assert.AreEqual(
false, root.IsValid);
331 root.ChildList.Add(CreateChildWithoutCriteria<Child>());
332 context.
Assert.AreEqual(
true, root.IsValid);
335 context.
Assert.AreEqual(
false, root.IsValid);
345 var root = CreateWithoutCriteria<HasBadRule>();
348 context.
Assert.AreEqual(1, root.GetBrokenRules().Count);
350 context.
Assert.AreEqual(
"rule://csla.test.validationrules.hasbadrule-badrule/null:InvalidOperationException", root.GetBrokenRules()[0].Description);
352 context.
Assert.AreEqual(
"rule://csla.test.validationrules.hasbadrule-badrule/(object):Operation is not valid due to the current state of the object.", root.GetBrokenRules()[0].Description);
362 var root = CreateWithoutCriteria<HasPrivateFields>();
375 var root = CreateWithoutCriteria<UsesCommonRules>();
376 context.Assert.AreEqual(1, root.Data);
378 context.Assert.IsFalse(root.IsValid);
379 context.Assert.IsTrue(root.BrokenRulesCollection[0].Description.Length > 0);
383 context.Assert.IsFalse(root.IsValid);
386 context.Assert.IsFalse(root.IsValid);
389 context.Assert.IsTrue(root.IsValid);
391 context.Assert.Success();
399 var root = CreateWithoutCriteria<MinMaxNullableRules>();
400 context.Assert.IsNull(root.DataNullable);
402 context.Assert.IsFalse(root.IsValid);
403 context.Assert.IsTrue(root.BrokenRulesCollection[0].Description.Length > 0);
406 root.DataNullable = 0;
407 context.Assert.IsFalse(root.IsValid);
409 root.DataNullable = 20;
410 context.Assert.IsFalse(root.IsValid);
412 root.DataNullable = 15;
413 context.Assert.IsTrue(root.IsValid);
415 context.Assert.Success();
424 var root = CreateWithoutCriteria<UsesCommonRules>();
426 context.Assert.IsTrue(root.IsValid,
"Should start valid");
429 context.Assert.IsFalse(root.IsValid,
"Min too short");
431 root.MinCheck =
"123456";
432 context.Assert.IsTrue(root.IsValid,
"Min OK");
435 context.Assert.IsTrue(root.IsValid,
"Max OK");
437 root.MaxCheck =
"123456";
438 context.Assert.IsFalse(root.IsValid,
"Max too long");
440 context.Assert.Success();
452 var applicationContext = _testDIContext.CreateTestApplicationContext();
454 new Dictionary<Core.IPropertyInfo, object> {
459 context.Assert.AreEqual(0, ctx.Results.Count);
462 new Dictionary<Core.IPropertyInfo, object> {
467 context.Assert.AreEqual(1, ctx.Results.Count);
469 context.Assert.Success();
478 context.Assert.Try(() =>
485 var rootEI = (IDataErrorInfo)root;
488 context.Assert.AreEqual(
"PrimaryProperty does not exist.", broken);
489 var value = root.Value1;
493 context.Assert.AreEqual(
"PrimaryProperty has value.", broken);
495 context.Assert.Success();
509 var root = dataPortal.
Fetch();
510 context.Assert.IsFalse(root.IsDirty);
511 context.Assert.AreEqual(
"csla rocks", root.Value1);
513 context.Assert.IsTrue(root.IsDirty);
514 context.Assert.AreEqual(
"CSLA ROCKS", root.Value1);
515 context.Assert.Success();
526 var root = dataPortal.
Fetch(
"CSLA ROCKS");
527 context.Assert.IsFalse(root.IsDirty);
528 context.Assert.AreEqual(
"CSLA ROCKS", root.Value1);
530 context.Assert.IsFalse(root.IsDirty);
531 context.Assert.AreEqual(
"CSLA ROCKS", root.Value1);
532 context.Assert.Success();
536 private T CreateWithoutCriteria<T>()
540 return dataPortal.
Create();
543 private async Task<T> CreateWithoutCriteriaAsync<T>()
550 private T CreateChildWithoutCriteria<T>()
557 private async Task<HasRulesManager> CreateHasRulesManagerAsync()
559 IDataPortal<HasRulesManager> dataPortal = _testDIContext.CreateDataPortal<HasRulesManager>();
561 return await dataPortal.CreateAsync(
new HasRulesManager.Criteria());
564 private async Task<HasRulesManager2> CreateHasRulesManager2Async()
566 IDataPortal<HasRulesManager2> dataPortal = _testDIContext.CreateDataPortal<HasRulesManager2>();
568 return await dataPortal.CreateAsync();
571 private async Task<HasRulesManager2> CreateHasRulesManager2Async(
string ident)
573 IDataPortal<HasRulesManager2> dataPortal = _testDIContext.CreateDataPortal<HasRulesManager2>();
575 return await dataPortal.CreateAsync(
new HasRulesManager2.Criteria(ident));
602 base.AddBusinessRules();
608 protected override void Execute(Rules.IRuleContext context)
610 throw new InvalidOperationException();
615 private void Create()
636 protected override object ReadProperty(Core.IPropertyInfo propertyInfo)
641 return base.ReadProperty(propertyInfo);
646 base.AddBusinessRules();
651 private void Create()
659 private static PropertyInfo<int> DataProperty = RegisterProperty<int>(c => c.Data,
null, 1);
662 get {
return GetProperty(DataProperty); }
663 set { SetProperty(DataProperty, value); }
666 private static PropertyInfo<string> MinCheckProperty = RegisterProperty<string>(c => c.MinCheck,
null,
"123456");
669 get {
return GetProperty(MinCheckProperty); }
670 set { SetProperty(MinCheckProperty, value); }
673 private static PropertyInfo<string> MaxCheckProperty = RegisterProperty<string>(c => c.MaxCheck);
676 get {
return GetProperty(MaxCheckProperty); }
677 set { SetProperty(MaxCheckProperty, value); }
682 base.AddBusinessRules();
691 private void Create()
700 private static PropertyInfo<int?> DataNullableProperty = RegisterProperty<int?>(c => c.DataNullable);
703 get {
return GetProperty(DataNullableProperty); }
704 set { SetProperty(DataNullableProperty, value); }
709 base.AddBusinessRules();
716 private void Create()
741 base.AddBusinessRules();
751 : base(primaryProperty)
758 protected override void Execute(Rules.IRuleContext context)
762 if (
string.IsNullOrEmpty(v1) ||
string.IsNullOrEmpty(v2))
763 context.AddErrorResult(
string.Format(
"v1:{0}, v2:{1}", v1, v2));
785 base.AddBusinessRules();
810 base.AddBusinessRules();
817 : base(primaryProperty)
819 InputProperties =
new List<IPropertyInfo>(){primaryProperty};
825 context.
AddOutValue(PrimaryProperty, value.ToUpperInvariant());
841 private void Fetch(
string value)
843 using (BypassPropertyChecks)
854 : base(primaryProperty)
859 protected override void Execute(Rules.IRuleContext context)
863 context.AddErrorResult(
"PrimaryProperty has value.");
867 context.AddErrorResult(
"PrimaryProperty does not exist.");
883 _testDIContext = TestDIContextFactory.CreateDefaultContext();
887 [ExpectedException(typeof(ArgumentException))]
893 var applicationContext = _testDIContext.CreateTestApplicationContext();
895 new Dictionary<Core.IPropertyInfo, object>
901 Assert.Fail(
"Must throw exception.");
905 [ExpectedException(typeof(ArgumentException))]
911 var applicationContext = _testDIContext.CreateTestApplicationContext();
913 new Dictionary<Core.IPropertyInfo, object>
919 Assert.Fail(
"Must throw exception.");
923 [ExpectedException(typeof(ArgumentException))]
929 var applicationContext = _testDIContext.CreateTestApplicationContext();
931 new Dictionary<Core.IPropertyInfo, object>
937 Assert.Fail(
"Must throw exception.");
941 [ExpectedException(typeof(ArgumentException))]
947 var applicationContext = _testDIContext.CreateTestApplicationContext();
949 new Dictionary<Core.IPropertyInfo, object>
955 Assert.Fail(
"Must throw exception.");
959 [ExpectedException(typeof(ArgumentException))]
965 var applicationContext = _testDIContext.CreateTestApplicationContext();
967 new Dictionary<Core.IPropertyInfo, object>
973 Assert.Fail(
"Must throw exception.");
977 [ExpectedException(typeof(ArgumentException))]
983 var applicationContext = _testDIContext.CreateTestApplicationContext();
985 new Dictionary<Core.IPropertyInfo, object>
991 Assert.Fail(
"Must throw exception.");
1001 var applicationContext = _testDIContext.CreateTestApplicationContext();
1003 new Dictionary<Core.IPropertyInfo, object>
1009 Assert.IsTrue(
true,
"Must not fail.");
This is the base class from which most business objects will be derived.
T Clone()
Creates a clone of the object.
Maintains metadata about a property.
A collection of currently broken rules.
BrokenRulesCollection()
Creates a read-write instance of the collection.
virtual Csla.Core.IPropertyInfo PrimaryProperty
Gets or sets the primary property affected by this rule.
List< Csla.Core.IPropertyInfo > InputProperties
Gets a list of secondary property values to be supplied to the rule when it is executed.
List< Csla.Core.IPropertyInfo > AffectedProperties
Gets a list of properties affected by this rule.
Base class used to create business and validation rules.
Tracks the business rules for a business object.
List< string > CheckRules()
Invokes all rules for the business type.
void AddRule(IBusinessRuleBase rule)
Associates a business rule with the business object.
BrokenRulesCollection GetBrokenRules()
Gets the broken rules list.
Business rule for a maximum length string.
Business rule for a maximum value.
Business rule for a minimum length string.
Business rule for a minimum value.
Business rule for a required string.
Context information provided to a business rule when it is invoked.
void AddWarningResult(string description)
Add a Warning severity result to the Results list.
void AddSuccessResult(bool stopProcessing)
Add a Success severity result to the Results list.
void AddErrorResult(string description)
Add a Error severity result to the Results list.
void Complete()
Indicates that the rule processing is complete, so CSLA .NET will process the Results list.
void AddInformationResult(string description)
Add an Information severity result to the Results list.
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
override void AddBusinessRules()
DirtyAfterOutValueChangesProperty()
static PropertyInfo< string > Value1Property
static PropertyInfo< int > IdProperty
new Rules.BrokenRulesCollection GetBrokenRules()
override void AddBusinessRules()
override void AddBusinessRules()
static PropertyInfo< string > Value1Property
static PropertyInfo< string > NameProperty
override object ReadProperty(Core.IPropertyInfo propertyInfo)
override void AddBusinessRules()
static PropertyInfo< string > NameProperty
override void AddBusinessRules()
void AddWarningResultThrowsErrorWhenMessageIsEmpty()
void AddInformationResultOnPropertyThrowsErrorWhenMessageIsEmpty()
static void ClassInitialize(TestContext context)
void AddSuccessResultDoesNotFail()
void AddInformationResultThrowsErrorWhenMessageIsEmpty()
void AddWarningResultOnPropertyThrowsErrorWhenMessageIsEmpty()
void AddErrorResultOnPropertyThrowsErrorWhenMessageIsEmpty()
void AddErrorResultThrowsErrorWhenMessageIsEmpty()
override void AddBusinessRules()
static PropertyInfo< string > Value1Property
static PropertyInfo< string > Value2Property
Csla.Core.IPropertyInfo SecondaryProperty
TwoProps(Csla.Core.IPropertyInfo primaryProperty, Csla.Core.IPropertyInfo secondProperty)
override void Execute(Rules.IRuleContext context)
override void AddBusinessRules()
async Task BreakLengthRuleAndClone()
void CanHaveRuleOnLazyField()
async Task VerifyUndoableStateStackOnClone()
async Task TestValidationAfterEditCycle()
static void ClassInitialize(TestContext context)
void MinMaxNullableValue()
void ObjectDirtyWhenOutputValueChangesPropertyValue()
async Task TestValidationRulesWithPublicProperty()
async Task BreakLengthRule()
async Task TestValidationRulesAfterClone()
void RuleThrowsException()
async Task TestValidationRulesWithPrivateMember()
async Task BreakRequiredRule()
async Task ListChangedEventTrigger()
void ObjectNotDirtyWhenOutputValueDoNotChangePropertyValue()
Type to carry context information for DI in unit tests
void IsFalse(bool condition)
void IsTrue(bool condition)
UnitTestContext GetContext()
Maintains metadata about a property.
Interface defining the members of the child data portal type.
object CreateChild(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Interface defining the members of the data portal type.
Task< object > CreateAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Interface defining a business/validation rule implementation.
Context information provided to a business rule when it is invoked.
void AddOutValue(object value)
Add an outbound value to update the rule's primary property on the business object once the rule is c...
Dictionary< Csla.Core.IPropertyInfo, object > InputPropertyValues
Gets a dictionary containing copies of property values from the target business object.
RelationshipTypes
List of valid relationship types between a parent object and another object through a managed propert...
@ Serializable
Prevents updating or inserting until the transaction is complete.
@ Execute
Execute operation.