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.
AppContextTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AppContextTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Test to see if contexts get cleared out properly</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11using System.Threading;
13using Csla.TestHelpers;
14
15#if !NUNIT
16using Microsoft.VisualStudio.TestTools.UnitTesting;
17#else
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23#endif
24
26{
27 [TestClass()]
28 public class AppContextTests
29 {
30 private static TestDIContext _testDIContext;
31
33 public static void ClassInitialize(TestContext context)
34 {
35 _testDIContext = TestDIContextFactory.CreateDefaultContext();
36 }
37
38 [TestInitialize]
39 public void SetScopedSp()
40 {
42 }
43
44 [TestCleanup]
46 {
48 }
49
50 #region Simple Test
51
52 [TestMethod()]
53 public void SimpleTest()
54 {
55 IDataPortal<SimpleRoot> dataPortal = _testDIContext.CreateDataPortal<SimpleRoot>();
56
57 // TODO: How do we do this test in Csla 6?
59 //ApplicationContext.ClientContext["v1"] = "client";
60
61 SimpleRoot root = dataPortal.Fetch(new SimpleRoot.Criteria("data"));
62
63 //Assert.AreEqual("client", ApplicationContext.ClientContext["v1"], "client context didn't roundtrip");
64 Assert.AreEqual("Fetched", TestResults.GetResult("Root"), "global context missing server value");
65 }
66
67 #endregion
68
69 [TestMethod()]
71 {
72 // TODO: Is there any equivalent for this test in Csla 6?
73 //ApplicationContext.DataPortalProxy = null;
74 //Assert.AreEqual("Local", ApplicationContext.DataPortalProxy);
75 //Assert.AreEqual("Client", ApplicationContext.ExecutionLocation.ToString());
76 }
77
78 #region TestAppContext across different Threads
79
80 // TODO: Is this test relevant anymore? I can't work out how to do this test
81 [TestMethod]
83 {
84 List<AppContextThread> AppContextThreadList = new List<AppContextThread>();
85 List<Thread> ThreadList = new List<Thread>();
86
88
89 for (int x = 0; x < 10; x++)
90 {
91 AppContextThread act = new AppContextThread("Thread: " + x);
92 AppContextThreadList.Add(act);
93
94 Thread t = new Thread(new ThreadStart(act.Run));
95 t.Name = "Thread: " + x;
96 t.Start();
97 ThreadList.Add(t);
98 }
99
100 Exception ex = null;
101 try
102 {
103 foreach (AppContextThread act in AppContextThreadList)
104 {
105 //We are accessing the Client/GlobalContext via this thread, therefore
106 //it should be removed.
107 Assert.AreEqual(true, act.Removed);
108 }
109 //We are now accessing the shared value. If any other thread
110 //loses its Client/GlobalContext this will turn to true
111 //Assert.AreEqual(false, AppContextThread.StaticRemoved);
112 }
113 catch (Exception e)
114 {
115 ex = e;
116 }
117 finally
118 {
119 foreach (AppContextThread act in AppContextThreadList)
120 act.Stop();
121
122 foreach (Thread t in ThreadList)
123 {
124 t.Join();
125 }
126 }
127 if (ex != null) throw ex;
128 }
129
130 #endregion
131
132 #region ClientContext
133
141 [TestMethod()]
142 public void ClientContext()
143 {
144 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
145 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
146
148
149 applicationContext.ClientContext.Add("clientcontext", "client context data");
150 Assert.AreEqual("client context data", applicationContext.ClientContext["clientcontext"], "Matching data not retrieved");
151
152 Basic.Root root = dataPortal.Create(new Basic.Root.Criteria());
153 root.Data = "saved";
154 Assert.AreEqual("saved", root.Data, "Root data should be 'saved'");
155 Assert.AreEqual(true, root.IsDirty, "Object should be dirty");
156 Assert.AreEqual(true, root.IsValid, "Object should be valid");
157
159 root = root.Save();
160
161 Assert.IsNotNull(root, "Root object should not be null");
162 Assert.AreEqual("Inserted", TestResults.GetResult("Root"), "Object not inserted");
163 Assert.AreEqual("saved", root.Data, "Root data should be 'saved'");
164 Assert.AreEqual(false, root.IsNew, "Object should not be new");
165 Assert.AreEqual(false, root.IsDeleted, "Object should not be deleted");
166 Assert.AreEqual(false, root.IsDirty, "Object should not be dirty");
167
168 //TODO: Is there a modern equivalent of this?
169 //Assert.AreEqual("client context data", Csla.ApplicationContext.ClientContext["clientcontext"], "Client context data lost");
170 Assert.AreEqual("client context data", TestResults.GetResult("clientcontext"), "Global context data lost");
171 Assert.AreEqual("new global value", TestResults.GetResult("globalcontext"), "New global value lost");
172 }
173
174 #endregion
175
176 #region Dataportal Events
177
178 // TODO: Is this test relevant any more? These event handlers don't seem to be exposed
190 [TestMethod()]
191 public void DataPortalEvents()
192 {
193 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
194
196 TestResults.Add("global", "global");
197
198 //dataPortal.DataPortalInvoke += new Action<DataPortalEventArgs>(OnDataPortaInvoke);
199 //dataPortal.DataPortalInvokeComplete += new Action<DataPortalEventArgs>(OnDataPortalInvokeComplete);
200
201 Basic.Root root = dataPortal.Fetch(new Basic.Root.Criteria("testing"));
202
203 //dataPortal.DataPortalInvoke -= new Action<DataPortalEventArgs>(OnDataPortaInvoke);
204 //dataPortal.DataPortalInvokeComplete -= new Action<DataPortalEventArgs>(OnDataPortalInvokeComplete);
205
206 //Populated in the handlers below
207 Assert.AreEqual("global", TestResults.GetResult("ClientInvoke"), "Client invoke incorrect");
208 Assert.AreEqual("global", TestResults.GetResult("ClientInvokeComplete"), "Client invoke complete");
209
210 //populated in the Root Dataportal handlers.
211 Assert.AreEqual("global", TestResults.GetResult("dpinvoke"), "Server invoke incorrect");
212 Assert.AreEqual("global", TestResults.GetResult("dpinvokecomplete"), "Server invoke compelte incorrect");
213 }
214
215 private void OnDataPortaInvoke(DataPortalEventArgs e)
216 {
217 TestResults.Add("ClientInvoke", TestResults.GetResult("global"));
218 }
219
220 private void OnDataPortalInvokeComplete(DataPortalEventArgs e)
221 {
222 TestResults.Add("ClientInvokeComplete", TestResults.GetResult("global"));
223 }
224
225 #endregion
226
227 #region FailCreateContext
228
232 [TestMethod()]
233 public void FailCreateContext()
234 {
235 IDataPortal<ExceptionRoot> dataPortal = _testDIContext.CreateDataPortal<ExceptionRoot>();
236
238
239 ExceptionRoot root;
240 try
241 {
242 root = dataPortal.Create(new ExceptionRoot.Criteria());
243 Assert.Fail("Exception didn't occur");
244 }
245 catch (DataPortalException ex)
246 {
247 Assert.IsNull(ex.BusinessObject, "Business object shouldn't be returned");
248 Assert.AreEqual("Fail create", ex.GetBaseException().Message, "Base exception message incorrect");
249 Assert.IsTrue(ex.Message.StartsWith("DataPortal.Create failed"), "Exception message incorrect");
250 }
251
252 Assert.AreEqual("create", TestResults.GetResult("create"), "GlobalContext not preserved");
253 }
254
255 #endregion
256
257 #region FailFetchContext
258
259 [TestMethod()]
260 public void FailFetchContext()
261 {
262 IDataPortal<ExceptionRoot> dataPortal = _testDIContext.CreateDataPortal<ExceptionRoot>();
263
265 ExceptionRoot root = null;
266 try
267 {
268 root = dataPortal.Fetch(new ExceptionRoot.Criteria("fail"));
269 Assert.Fail("Exception didn't occur");
270 }
271 catch (DataPortalException ex)
272 {
273 Assert.IsNull(ex.BusinessObject, "Business object shouldn't be returned");
274 Assert.AreEqual("Fail fetch", ex.GetBaseException().Message, "Base exception message incorrect");
275 Assert.IsTrue(ex.Message.StartsWith("DataPortal.Fetch failed"), "Exception message incorrect");
276 }
277 catch (Exception ex)
278 {
279 Assert.Fail("Unexpected exception: " + ex.ToString());
280 }
281
282 Assert.AreEqual("create", TestResults.GetResult("create"), "GlobalContext not preserved");
283 }
284
285 #endregion
286
287 #region FailUpdateContext
288
289 [TestMethod()]
290 public void FailUpdateContext()
291 {
292 TestDIContext testDIContext = TestDIContextFactory.CreateContext(opts => opts.DataPortal(cfg => cfg.DataPortalReturnObjectOnException(true)));
293 IDataPortal<ExceptionRoot> dataPortal = testDIContext.CreateDataPortal<ExceptionRoot>();
294
295 try
296 {
298
299 ExceptionRoot root;
300 try
301 {
302 root = dataPortal.Create(new ExceptionRoot.Criteria());
303 Assert.Fail("Create exception didn't occur");
304 }
305 catch (DataPortalException ex)
306 {
307 root = (ExceptionRoot)ex.BusinessObject;
308 Assert.AreEqual("Fail create", ex.GetBaseException().Message, "Base exception message incorrect");
309 Assert.IsTrue(ex.Message.StartsWith("DataPortal.Create failed"), "Exception message incorrect");
310 }
311
312 root.Data = "boom";
313 try
314 {
315 root = root.Save();
316
317 Assert.Fail("Insert exception didn't occur");
318 }
319 catch (DataPortalException ex)
320 {
321 root = (ExceptionRoot)ex.BusinessObject;
322 Assert.AreEqual("Fail insert", ex.GetBaseException().Message, "Base exception message incorrect");
323 Assert.IsTrue(ex.Message.StartsWith("DataPortal.Update failed"), "Exception message incorrect");
324 }
325
326 Assert.AreEqual("boom", root.Data, "Business object not returned");
327 Assert.AreEqual("create", TestResults.GetResult("create"), "GlobalContext not preserved");
328 }
329 finally
330 {
331 }
332 }
333
334 #endregion
335
336 #region FailDeleteContext
337
338 [TestMethod()]
339 public void FailDeleteContext()
340 {
341 IDataPortal<ExceptionRoot> dataPortal = _testDIContext.CreateDataPortal<ExceptionRoot>();
343
344 ExceptionRoot root = null;
345 try
346 {
347 dataPortal.Delete("fail");
348 Assert.Fail("Exception didn't occur");
349 }
350 catch (DataPortalException ex)
351 {
352 root = (ExceptionRoot)ex.BusinessObject;
353 Assert.AreEqual("Fail delete", ex.GetBaseException().Message, "Base exception message incorrect");
354 Assert.IsTrue(ex.Message.StartsWith("DataPortal.Delete failed"), "Exception message incorrect");
355 }
356 Assert.IsNull(root, "Business object returned");
357 Assert.AreEqual("create", TestResults.GetResult("create"), "GlobalContext not preserved");
358 }
359
360 #endregion
361
362 private Basic.Root GetRoot(string data)
363 {
364 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
365
366 return dataPortal.Fetch(new Basic.Root.Criteria(data));
367 }
368
369 }
370}
Provides consistent context information between the client and server DataPortal objects.
ContextDictionary ClientContext
Returns the application-specific context data provided by the client.
T Save()
Saves the object to the database.
Provides information about the DataPortal call.
This exception is returned for any errors occurring during the server-side DataPortal invocation.
override string ToString()
Gets a string representation of this object.
object BusinessObject
Returns a reference to the business object from the server-side DataPortal.
static void ClassInitialize(TestContext context)
void ClientContext()
Test the Client Context
void DataPortalEvents()
Test the dataportal events
void FailCreateContext()
Test the FaileCreate Context
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
Definition: TestResults.cs:21
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Definition: TestResults.cs:69
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
Definition: TestResults.cs:49
static void Add(string key, string value)
Add an item to the test results, to indicate an outcome of a particular operation
Definition: TestResults.cs:29
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
void Delete(params object[] criteria)
Called by a Shared (static in C#) method in the business class to cause immediate deletion of a speci...
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...