CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BypassPropertyChecksTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BypassPropertyChecksTests.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//-----------------------------------------------------------------------
8using Csla;
10using System;
11using UnitDriven;
12using System.Security.Claims;
13using Csla.TestHelpers;
14
15#if NUNIT
16using NUnit.Framework;
17using TestClass = NUnit.Framework.TestFixtureAttribute;
18using TestInitialize = NUnit.Framework.SetUpAttribute;
19using TestCleanup = NUnit.Framework.TearDownAttribute;
20using TestMethod = NUnit.Framework.TestAttribute;
21using TestSetup = NUnit.Framework.SetUpAttribute;
22#elif MSTEST
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24#endif
25
26
28{
29 [TestClass]
31 {
32 private static ClaimsPrincipal GetPrincipal(params string[] roles)
33 {
34 var identity = new ClaimsIdentity();
35 foreach (var item in roles)
36 identity.AddClaim(new Claim(ClaimTypes.Role, item));
37 return new ClaimsPrincipal(identity);
38 }
39
40 private static TestDIContext _testDIContext;
41
43 public static void ClassInitialize(TestContext context)
44 {
45 _testDIContext = TestDIContextFactory.CreateDefaultContext();
46 }
47
48 [TestMethod]
50 {
51 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
52 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
53
54 UnitTestContext context = GetContext();
55 bool propertyChangedFired = false;
56 BypassBusinessBase testObj = dataPortal.Fetch();
57 testObj.PropertyChanged += (o, e) =>
58 {
59 propertyChangedFired = true;
60 };
61 testObj.LoadIdByPass(1);
62 context.Assert.AreEqual(1, testObj.ReadIdByPass());
63 context.Assert.AreEqual(false, propertyChangedFired);
64 context.Assert.AreEqual(false, testObj.IsDirty);
65
66 testObj.LoadIdByNestedPass(3);
67 context.Assert.AreEqual(3, testObj.ReadIdByPass());
68 context.Assert.AreEqual(false, propertyChangedFired);
69 context.Assert.AreEqual(false, testObj.IsDirty);
70
71 testObj.LoadId(2);
72 context.Assert.AreEqual(true, propertyChangedFired);
73 context.Assert.AreEqual(2, testObj.ReadId());
74 context.Assert.AreEqual(true, testObj.IsDirty);
75
76 context.Assert.Success();
77 context.Complete();
78 }
79
80 [TestMethod]
81 [ExpectedException(typeof(Csla.Security.SecurityException))]
83 {
84 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
85 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
86
87 UnitTestContext context = GetContext();
88 bool propertyChangedFired = false;
89
90 BypassBusinessBase testObj = dataPortal.Fetch();
91 testObj.PropertyChanged += (o, e) =>
92 {
93 propertyChangedFired = true;
94 };
95 testObj.LoadId2ByPass(1);
96 context.Assert.AreEqual(1, testObj.ReadId2ByPass());
97 context.Assert.AreEqual(false, propertyChangedFired);
98 context.Assert.AreEqual(false, testObj.IsDirty);
99 testObj.LoadId2(2);
100 context.Assert.AreEqual(1, testObj.ReadId2ByPass()); // still one becuase set failed
101 context.Assert.AreEqual(true, testObj.IsDirty);
102 context.Assert.Success();
103 context.Complete();
104 }
105
106
107 [TestMethod]
109 {
110 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
111 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
112
113 UnitTestContext context = GetContext();
114 bool propertyChangedFired = false;
115
116 BypassBusinessBase testObj = dataPortal.Fetch();
117 testObj.PropertyChanged += (o, e) =>
118 {
119 propertyChangedFired = true;
120 };
121 testObj.LoadId2ByPass(1);
122 context.Assert.AreEqual(1, testObj.ReadId2ByPass());
123 context.Assert.AreEqual(false, propertyChangedFired);
124 context.Assert.AreEqual(false, testObj.IsDirty);
125 context.Assert.Success();
126 context.Complete();
127 }
128
129 [TestMethod]
131 {
132 TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
133 IDataPortal<BypassBusinessBase> dataPortal = customDIContext.CreateDataPortal<BypassBusinessBase>();
134
135 UnitTestContext context = GetContext();
136 bool propertyChangedFired = false;
137 BypassBusinessBase testObj = dataPortal.Fetch();
138 testObj.PropertyChanged += (o, e) =>
139 {
140 propertyChangedFired = true;
141 };
142 testObj.LoadId(1);
143 context.Assert.AreEqual(1, testObj.ReadId());
144 context.Assert.AreEqual(true, propertyChangedFired);
145 context.Assert.AreEqual(true, testObj.IsDirty);
146 context.Assert.Success();
147 context.Complete();
148 }
149
150 [TestMethod]
151 [ExpectedException(typeof(Csla.Security.SecurityException))]
153 {
154 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
155 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
156
157 UnitTestContext context = GetContext();
158 BypassBusinessBase testObj = dataPortal.Fetch();
159 bool propertyChangedFired = false;
160 testObj.PropertyChanged += (o, e) =>
161 {
162 propertyChangedFired = true;
163 };
164 testObj.LoadId2(1);
165 context.Assert.AreEqual(1, testObj.ReadId2ByPass());
166 context.Assert.AreEqual(true, propertyChangedFired);
167 context.Assert.AreEqual(true, testObj.IsDirty);
168 context.Assert.Success();
169 context.Complete();
170 }
171
172 [TestMethod]
174 {
175 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
176 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
177
178 UnitTestContext context = GetContext();
179 bool propertyChangedFired = false;
180 BypassBusinessBase testObj = dataPortal.Fetch();
181 testObj.PropertyChanged += (o, e) =>
182 {
183 propertyChangedFired = true;
184 };
185 testObj.LoadId2ByPass(1);
186 context.Assert.AreEqual(false, testObj.IsDirty);
187 context.Assert.AreEqual(1, testObj.ReadId2ByPass());
188 context.Assert.AreEqual(0, testObj.ReadId2()); // 0 becuase we cannot read
189 context.Assert.AreEqual(false, propertyChangedFired);
190 context.Assert.Success();
191 context.Complete();
192 }
193
194
195
196 [TestMethod]
198 {
199 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
200 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
201
202 UnitTestContext context = GetContext();
203 bool propertyChangedFired = false;
204
205 BypassBusinessBase testObj = dataPortal.Fetch();
206 testObj.PropertyChanged += (o, e) =>
207 {
208 propertyChangedFired = true;
209 };
210 testObj.LoadId4ByPass(1);
211 context.Assert.AreEqual(false, testObj.IsDirty);
212 context.Assert.AreEqual(1, testObj.ReadId4ByPass());
213 context.Assert.AreEqual(false, propertyChangedFired);
214 context.Assert.Success();
215 context.Complete();
216 }
217
218 [TestMethod]
220 {
221 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
222 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
223
224 UnitTestContext context = GetContext();
225 bool propertyChangedFired = false;
226 BypassBusinessBase testObj = dataPortal.Fetch();
227 testObj.PropertyChanged += (o, e) =>
228 {
229 propertyChangedFired = true;
230 };
231 testObj.LoadId3(1);
232 context.Assert.AreEqual(true, testObj.IsDirty);
233 context.Assert.AreEqual(1, testObj.ReadId3());
234 context.Assert.AreEqual(true, propertyChangedFired);
235 context.Assert.Success();
236 context.Complete();
237 }
238
239 [TestMethod]
240 [ExpectedException(typeof(Csla.Security.SecurityException))]
242 {
243 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
244 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
245
246 UnitTestContext context = GetContext();
247 BypassBusinessBase testObj = dataPortal.Fetch();
248 bool propertyChangedFired = false;
249 testObj.PropertyChanged += (o, e) =>
250 {
251 propertyChangedFired = true;
252 };
253 testObj.LoadId4(1);
254 context.Assert.AreEqual(true, testObj.IsDirty);
255 context.Assert.AreEqual(1, testObj.ReadId4ByPass());
256 context.Assert.AreEqual(true, propertyChangedFired);
257 context.Assert.Success();
258 context.Complete();
259 }
260
261 [TestMethod]
263 {
264 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
265 IDataPortal<BypassBusinessBase> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBase>();
266
267 UnitTestContext context = GetContext();
268 bool propertyChangedFired = false;
269 BypassBusinessBase testObj = dataPortal.Fetch();
270 testObj.PropertyChanged += (o, e) =>
271 {
272 propertyChangedFired = true;
273 };
274 testObj.LoadId4ByPass(1);
275 context.Assert.AreEqual(false, testObj.IsDirty);
276 context.Assert.AreEqual(1, testObj.ReadId4ByPass());
277 context.Assert.AreEqual(0, testObj.ReadId4()); // 0 becuase we cannot read
278 context.Assert.AreEqual(false, propertyChangedFired);
279 context.Assert.Success();
280 context.Complete();
281 }
282
283 [TestMethod]
284 public void TestBypassFactory()
285 {
286 TestDIContext testDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin"));
287 IDataPortal<BypassBusinessBaseUsingFactory> dataPortal = testDIContext.CreateDataPortal<BypassBusinessBaseUsingFactory>();
288
289 UnitTestContext context = GetContext();
290
292 context.Assert.AreEqual(false, obj.IsDirty);
293 context.Assert.AreEqual(7, obj.ReadId2ByPass());
294 context.Assert.Success();
295 context.Complete();
296 }
297 }
298}
static BypassBusinessBaseUsingFactory GetObject(IDataPortal< BypassBusinessBaseUsingFactory > dataPortal)
Type to carry context information for DI in unit tests
UnitTestContext GetContext()
Definition: TestBase.cs:12
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...