Skip to content

Commit bee73b7

Browse files
Allow disabling scan for data annotations attributes (#4614)
* #4492 Allow disabling scan for data annotations attributes * Fix test broken due to rule caching * Fix comments * #4615 Ensure ApplicationContext isn't null during initialization * Fix broken test * Add missing test config * #4492 Make Options property internal * #4492 Revert breaking change * #4492 Fix comment * #4492 Don't cache options object * Suppress warnings about async * #4492 Skip test on CI server --------- Co-authored-by: Stefan Ossendorf <StefanOssendorf@users.noreply.github.com>
1 parent ab7ebf3 commit bee73b7

File tree

18 files changed

+215
-53
lines changed

18 files changed

+215
-53
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<RunConfiguration>
4+
<MaxCpuCount>1</MaxCpuCount>
5+
</RunConfiguration>
6+
</RunSettings>

Source/Csla.test/Basic/BasicTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ public void DeletedListTestWithCancel()
384384
public void SuppressListChangedEventsDoNotRaiseCollectionChanged()
385385
{
386386
bool changed = false;
387-
var obj = new RootList();
387+
var portal = _testDIContext.CreateDataPortal<RootList>();
388+
var obj = portal.Create();
388389
obj.ListChanged += (_, _) =>
389390
{
390391
changed = true;

Source/Csla.test/Basic/CollectionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public void SetLast()
4242
[TestMethod]
4343
public void RootListGetRuleDescriptions()
4444
{
45-
RootList list = new RootList();
45+
var portal = _testDIContext.CreateDataPortal<RootList>();
46+
var list = portal.Create();
4647
RootListChild child = list.AddNew();
4748
string[] rules = child.GetRuleDescriptions();
4849
}

Source/Csla.test/Basic/RootList.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ namespace Csla.Test.Basic
1111
[Serializable]
1212
public class RootList : BusinessBindingListBase<RootList, RootListChild>
1313
{
14-
public RootList()
15-
{
16-
AllowEdit = true;
17-
AllowNew = true;
18-
AllowRemove = true;
19-
}
20-
2114
protected override object AddNewCore()
2215
{
2316
RootListChild child = new RootListChild();
2417
Add(child);
2518
return child;
2619
}
20+
21+
[Create]
22+
private void Create()
23+
{
24+
AllowNew = true;
25+
AllowEdit = true;
26+
AllowRemove = true;
27+
}
2728
}
2829

2930
[Serializable]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="BusinessRuleTests.cs" company="Marimer LLC">
3+
// Copyright (c) Marimer LLC. All rights reserved.
4+
// Website: https://cslanet.com
5+
// </copyright>
6+
// <summary>no summary</summary>
7+
//-----------------------------------------------------------------------
8+
using System.ComponentModel.DataAnnotations;
9+
using Csla.Configuration;
10+
using Csla.TestHelpers;
11+
using FluentAssertions;
12+
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.VisualStudio.TestTools.UnitTesting;
14+
15+
namespace Csla.Test.BizRules
16+
{
17+
[TestClass]
18+
public class BusinessRuleTests
19+
{
20+
private static TestDIContext _testDIContext;
21+
22+
[ClassInitialize]
23+
public static void ClassInitialize(TestContext context)
24+
{
25+
_testDIContext = TestDIContextFactory.CreateDefaultContext();
26+
}
27+
28+
[TestInitialize]
29+
public void Initialize()
30+
{
31+
TestResults.Reinitialise();
32+
}
33+
34+
[TestMethod]
35+
public void DefaultDataAnnotationsScan()
36+
{
37+
var portal = _testDIContext.ServiceProvider.GetRequiredService<IDataPortal<TestBusinessRule>>();
38+
var obj = portal.Create();
39+
obj.Name = "";
40+
obj.IsValid.Should().BeFalse();
41+
}
42+
43+
[TestMethod]
44+
[TestCategory("SkipOnCIServer")]
45+
public void DisableDataAnnotationsScan()
46+
{
47+
var options = _testDIContext.ServiceProvider.GetRequiredService<CslaOptions>();
48+
options.ScanForDataAnnotations(false);
49+
50+
// use different type to avoid caching
51+
var portal = _testDIContext.ServiceProvider.GetRequiredService<IDataPortal<TestBusinessRule2>>();
52+
var obj = portal.Create();
53+
obj.Name = "";
54+
obj.IsValid.Should().BeTrue();
55+
}
56+
}
57+
58+
public class TestBusinessRule : BusinessBase<TestBusinessRule>
59+
{
60+
public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
61+
[Required]
62+
public string Name
63+
{
64+
get => GetProperty(NameProperty);
65+
set => SetProperty(NameProperty, value);
66+
}
67+
68+
[Create]
69+
private void Create()
70+
{
71+
BusinessRules.CheckRules();
72+
}
73+
}
74+
75+
public class TestBusinessRule2 : BusinessBase<TestBusinessRule2>
76+
{
77+
public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
78+
[Required]
79+
public string Name
80+
{
81+
get => GetProperty(NameProperty);
82+
set => SetProperty(NameProperty, value);
83+
}
84+
85+
[Create]
86+
private void Create()
87+
{
88+
BusinessRules.CheckRules();
89+
}
90+
}
91+
}

Source/Csla.test/ChildrenByInterface/ChildInterfaceTests.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,41 @@
66
// <summary>no summary</summary>
77
//-----------------------------------------------------------------------
88

9+
using Csla.TestHelpers;
910
using Microsoft.VisualStudio.TestTools.UnitTesting;
1011

1112
namespace Csla.Test.ChildrenByInterface
1213
{
1314
[TestClass]
1415
public class ChildInterfaceTests
1516
{
17+
private static TestDIContext _testDIContext;
18+
19+
[ClassInitialize]
20+
public static void ClassInitialize(TestContext context)
21+
{
22+
_testDIContext = TestDIContextFactory.CreateDefaultContext();
23+
}
24+
25+
[TestInitialize]
26+
public void Initialize()
27+
{
28+
TestResults.Reinitialise();
29+
}
30+
1631
[TestMethod]
1732
public void AddItems()
1833
{
19-
ItemList list =
34+
var portal = _testDIContext.CreateDataPortal<ItemList>();
35+
var child1Portal = _testDIContext.CreateDataPortal<Item1>();
36+
var child2Portal = _testDIContext.CreateDataPortal<Item2>();
37+
var list = portal.Create();
38+
list.AddRange(
2039
[
21-
new Item1(),
22-
new Item2()
40+
child1Portal.Create(),
41+
child2Portal.Create()
2342

24-
];
43+
]);
2544

2645
Assert.IsTrue(list[0] is Item1, "First element should be Item1");
2746
Assert.IsTrue(list[1] is Item2, "Second element should be Item2");
@@ -42,6 +61,11 @@ protected override object GetIdValue()
4261
{
4362
return 0;
4463
}
64+
65+
[Create]
66+
private void Create()
67+
{
68+
}
4569
}
4670

4771
[Serializable]
@@ -56,8 +80,22 @@ protected override object GetIdValue()
5680
{
5781
return 0;
5882
}
83+
84+
[Create]
85+
private void Create()
86+
{
87+
}
5988
}
6089

6190
[Serializable]
62-
public class ItemList : BusinessBindingListBase<ItemList, IItem>;
91+
public class ItemList : BusinessBindingListBase<ItemList, IItem>
92+
{
93+
[Create]
94+
private void Create()
95+
{
96+
AllowNew = true;
97+
AllowEdit = true;
98+
AllowRemove = true;
99+
}
100+
}
63101
}

Source/Csla.test/DataPortal/AsynchDataPortalTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public async Task CreateAsync_WithCriteria()
9999
[TestMethod]
100100
public void CreateAsync_WithException()
101101
{
102+
#pragma warning disable MSTEST0040 // Do not assert inside 'async void' contexts
102103
var lck = new AutoResetEvent(false);
103104
new Action(async () =>
104105
{
@@ -119,6 +120,7 @@ public void CreateAsync_WithException()
119120
}
120121
}).Invoke();
121122
lck.WaitOne();
123+
#pragma warning restore MSTEST0040 // Do not assert inside 'async void' contexts
122124
}
123125

124126
[TestMethod]
@@ -166,6 +168,7 @@ public async Task FetchAsync_WithCriteria()
166168
[TestMethod]
167169
public void FetchAsync_WithException()
168170
{
171+
#pragma warning disable MSTEST0040 // Do not assert inside 'async void' contexts
169172
var lck = new AutoResetEvent(false);
170173
new Action(async () =>
171174
{
@@ -186,6 +189,7 @@ public void FetchAsync_WithException()
186189
}
187190
}).Invoke();
188191
lck.WaitOne();
192+
#pragma warning restore MSTEST0040 // Do not assert inside 'async void' contexts
189193
}
190194

191195
[TestMethod]
@@ -226,6 +230,7 @@ public async Task SaveAsync()
226230
[TestMethod]
227231
public async Task SaveAsyncWithException()
228232
{
233+
#pragma warning disable MSTEST0040 // Do not assert inside 'async void' contexts
229234
var context = GetContext();
230235
await context.Assert.Try(async () =>
231236
{
@@ -257,6 +262,7 @@ await context.Assert.Try(async () =>
257262
context.Assert.Success();
258263
});
259264
context.Complete();
265+
#pragma warning restore MSTEST0040 // Do not assert inside 'async void' contexts
260266
}
261267

262268
#endif
@@ -287,6 +293,7 @@ public async Task ExecuteAsync()
287293
[TestMethod]
288294
public void ExecuteAsyncWithException()
289295
{
296+
#pragma warning disable MSTEST0040 // Do not assert inside 'async void' contexts
290297
var lck = new AutoResetEvent(false);
291298
new Action(async () =>
292299
{
@@ -308,6 +315,7 @@ public void ExecuteAsyncWithException()
308315
}
309316
}).Invoke();
310317
lck.WaitOne();
318+
#pragma warning restore MSTEST0040 // Do not assert inside 'async void' contexts
311319
}
312320
#endregion
313321

Source/Csla.test/Nullable/NullableObject.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ public static void DeleteNullableObject(string name, IDataPortal<NullableObject>
7373
dataPortal.Delete(new Criteria(name));
7474
}
7575

76-
public NullableObject()
77-
{
78-
AddBusinessRules();
79-
}
80-
76+
[Create]
8177
private void DataPortal_Create(object criteria)
8278
{
8379
Criteria crit = (Criteria)(criteria);
@@ -86,6 +82,7 @@ private void DataPortal_Create(object criteria)
8682
TestResults.Add("NullableObject", "Created");
8783
}
8884

85+
[Fetch]
8986
protected void DataPortal_Fetch(object criteria)
9087
{
9188
Criteria crit = (Criteria)(criteria);

Source/Csla/ApplicationContext.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.ComponentModel;
1414
using Csla.Server;
1515
using System.Diagnostics.CodeAnalysis;
16+
using Csla.Configuration;
1617

1718
namespace Csla
1819
{
@@ -30,6 +31,8 @@ public ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
3031
{
3132
ApplicationContextAccessor = applicationContextAccessor;
3233
ApplicationContextAccessor.GetContextManager().ApplicationContext = this;
34+
var options = GetRequiredService<CslaOptions>();
35+
PropertyChangedMode = options.BindingOptions.PropertyChangedMode;
3336
}
3437

3538
internal ApplicationContextAccessor ApplicationContextAccessor { get; set; }
@@ -183,26 +186,11 @@ public enum ExecutionLocations
183186
Server
184187
}
185188

186-
private PropertyChangedModes _propertyChangedMode;
187-
private bool _propertyChangedModeSet;
188-
189189
/// <summary>
190190
/// Gets or sets a value specifying how CSLA .NET should
191191
/// raise PropertyChanged events.
192192
/// </summary>
193-
public PropertyChangedModes PropertyChangedMode
194-
{
195-
get
196-
{
197-
if (!_propertyChangedModeSet)
198-
{
199-
var options = GetRequiredService<Configuration.CslaOptions>();
200-
_propertyChangedMode = options.BindingOptions.PropertyChangedMode;
201-
_propertyChangedModeSet = true;
202-
}
203-
return _propertyChangedMode;
204-
}
205-
}
193+
public PropertyChangedModes PropertyChangedMode { get; }
206194

207195
/// <summary>
208196
/// Enum representing the way in which CSLA .NET

Source/Csla/BusinessBindingListBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ ApplicationContext IUseApplicationContext.ApplicationContext
5555
set
5656
{
5757
ApplicationContext = value;
58+
if (ApplicationContext == null)
59+
throw new InvalidOperationException("ApplicationContext == null");
5860
InitializeIdentity();
5961
Initialize();
6062
AllowNew = true;

0 commit comments

Comments
 (0)