9using System.Collections.Generic;
11using System.Threading;
16using Microsoft.VisualStudio.TestTools.UnitTesting;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
35 _testDIContext = TestDIContextFactory.CreateDefaultContext();
64 Assert.AreEqual(
"Fetched",
TestResults.
GetResult(
"Root"),
"global context missing server value");
78 #region TestAppContext across different Threads
84 List<AppContextThread> AppContextThreadList =
new List<AppContextThread>();
85 List<Thread> ThreadList =
new List<Thread>();
89 for (
int x = 0; x < 10; x++)
92 AppContextThreadList.Add(act);
94 Thread t =
new Thread(
new ThreadStart(act.
Run));
95 t.Name =
"Thread: " + x;
107 Assert.AreEqual(
true, act.
Removed);
122 foreach (Thread t
in ThreadList)
127 if (ex !=
null)
throw ex;
132 #region ClientContext
144 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
145 ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext();
149 applicationContext.
ClientContext.Add(
"clientcontext",
"client context data");
150 Assert.AreEqual(
"client context data", applicationContext.
ClientContext[
"clientcontext"],
"Matching data not retrieved");
152 Basic.Root root = dataPortal.Create(
new Basic.Root.Criteria());
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");
161 Assert.IsNotNull(root,
"Root object should not be null");
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");
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");
176 #region Dataportal Events
193 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
201 Basic.Root root = dataPortal.Fetch(
new Basic.Root.Criteria(
"testing"));
208 Assert.AreEqual(
"global",
TestResults.
GetResult(
"ClientInvokeComplete"),
"Client invoke complete");
212 Assert.AreEqual(
"global",
TestResults.
GetResult(
"dpinvokecomplete"),
"Server invoke compelte incorrect");
227 #region FailCreateContext
243 Assert.Fail(
"Exception didn't occur");
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");
257 #region FailFetchContext
269 Assert.Fail(
"Exception didn't occur");
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");
279 Assert.Fail(
"Unexpected exception: " + ex.
ToString());
287 #region FailUpdateContext
292 TestDIContext testDIContext = TestDIContextFactory.CreateContext(opts => opts.DataPortal(cfg => cfg.DataPortalReturnObjectOnException(
true)));
303 Assert.Fail(
"Create exception didn't occur");
308 Assert.AreEqual(
"Fail create", ex.GetBaseException().Message,
"Base exception message incorrect");
309 Assert.IsTrue(ex.Message.StartsWith(
"DataPortal.Create failed"),
"Exception message incorrect");
317 Assert.Fail(
"Insert exception didn't occur");
322 Assert.AreEqual(
"Fail insert", ex.GetBaseException().Message,
"Base exception message incorrect");
323 Assert.IsTrue(ex.Message.StartsWith(
"DataPortal.Update failed"),
"Exception message incorrect");
326 Assert.AreEqual(
"boom", root.
Data,
"Business object not returned");
336 #region FailDeleteContext
347 dataPortal.
Delete(
"fail");
348 Assert.Fail(
"Exception didn't occur");
353 Assert.AreEqual(
"Fail delete", ex.GetBaseException().Message,
"Base exception message incorrect");
354 Assert.IsTrue(ex.Message.StartsWith(
"DataPortal.Delete failed"),
"Exception message incorrect");
356 Assert.IsNull(root,
"Business object returned");
362 private Basic.Root GetRoot(
string data)
364 IDataPortal<Basic.Root> dataPortal = _testDIContext.CreateDataPortal<Basic.Root>();
366 return dataPortal.Fetch(
new Basic.Root.Criteria(data));
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.
void ApplicationContextProperties()
void ClearContextsAfterEachTest()
static void ClassInitialize(TestContext context)
void ClientContext()
Test the Client Context
void DataPortalEvents()
Test the dataportal events
void FailCreateContext()
Test the FaileCreate Context
void TestAppContextAcrossDifferentThreads()
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
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
static void Add(string key, string value)
Add an item to the test results, to indicate an outcome of a particular operation
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
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...