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.
ObjectFactoryTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ObjectFactoryTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Always make sure to cleanup after each test </summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Configuration;
10using System.Collections.Generic;
11using System.Linq;
12using System.Text;
14using Csla.TestHelpers;
16
17#if !NUNIT
18using Microsoft.VisualStudio.TestTools.UnitTesting;
19#else
20using NUnit.Framework;
21using TestClass = NUnit.Framework.TestFixtureAttribute;
22using TestInitialize = NUnit.Framework.SetUpAttribute;
23using TestCleanup = NUnit.Framework.TearDownAttribute;
24using TestMethod = NUnit.Framework.TestAttribute;
25#endif
26
28{
29 [TestClass]
30 public class ObjectFactoryTests
31 {
32 private static TestDIContext _testDIContext;
33
35 public static void ClassInitialize(TestContext context)
36 {
37 _testDIContext = TestDIContextFactory.CreateContext(
38 options => options.DataPortal(dp => dp.AddServerSideDataPortal(
39 cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory>>())
40 ));
41 }
42
46 [TestCleanup]
47 public void Cleanup()
48 {
49 // TODO: Is any of this cleanup still required? Probably not
50 //Csla.ApplicationContext.DataPortalProxy = "Local";
51 //Csla.DataPortal.ResetProxyType();
53 }
54
55 [TestMethod]
56 [TestCategory("SkipWhenLiveUnitTesting")]
57 public void Create()
58 {
59 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
60 // TODO: What proxy can we use for this test? Old one was Remoting, now retired
61 // options => options.Services.AddTransient<DataPortalClient.IDataPortalProxy, Testing.Business.TestProxies.AppDomainProxy>(),
62 opts => opts.DataPortal(
63 dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory>>())),
64 new System.Security.Claims.ClaimsPrincipal());
65
66 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
67
68 var root = dataPortal.Create();
69 Assert.AreEqual("Create", root.Data, "Data should match");
70 Assert.AreEqual(Csla.ApplicationContext.ExecutionLocations.Server, root.Location, "Location should match");
71 Assert.IsTrue(root.IsNew, "Should be new");
72 Assert.IsTrue(root.IsDirty, "Should be dirty");
73 }
74
75 [TestMethod]
76 public void CreateLocal()
77 {
78 // TODO: What proxy can we use for this test? Old one was Remoting, now retired
79 //TestDIContext testDIContext = TestDIContextFactory.CreateContext(
80 // options => options.Services.AddTransient<DataPortalClient.IDataPortalProxy, Testing.Business.TestProxies.AppDomainProxy>(),
81 //);
82 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
83 opts => opts.DataPortal(dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactoryC>>()))
84 );
85
86 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
87
88 var root = dataPortal.Create("abc");
89 Assert.AreEqual("Create abc", root.Data, "Data should match");
90 Assert.AreEqual(Csla.ApplicationContext.ExecutionLocations.Client, root.Location, "Location should match");
91 Assert.IsTrue(root.IsNew, "Should be new");
92 Assert.IsTrue(root.IsDirty, "Should be dirty");
93 }
94
95 [TestMethod]
96 public void CreateWithParam()
97 {
98 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
99 // TODO: What proxy can we use for this test? Old one was Remoting, now retired
100 // options => options.Services.AddTransient<DataPortalClient.IDataPortalProxy, Testing.Business.TestProxies.AppDomainProxy>(),
101 opts => opts.DataPortal(dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory>>())),
102 new System.Security.Claims.ClaimsPrincipal());
103
104 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
105
106 var root = dataPortal.Create("abc");
107 Assert.AreEqual("Create abc", root.Data, "Data should match");
108 Assert.AreEqual(Csla.ApplicationContext.ExecutionLocations.Client, root.Location, "Location should match");
109 Assert.IsTrue(root.IsNew, "Should be new");
110 Assert.IsTrue(root.IsDirty, "Should be dirty");
111 }
112
113 // TODO: This test needs to be updated when the factory model is updated
114 // to use DI and multi-property criteria
115 [TestMethod]
116 [ExpectedException(typeof(MissingMethodException))]
117 public void CreateMissing()
118 {
119 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
120 // TODO: What proxy can we use for this test? Old one was Remoting, now retired
121 // options => options.Services.AddTransient<DataPortalClient.IDataPortalProxy, Testing.Business.TestProxies.AppDomainProxy>(),
122 opts => opts.DataPortal(dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory1>>())),
123 new System.Security.Claims.ClaimsPrincipal()
124 );
125
126 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
127
128 try
129 {
130 var root = dataPortal.Create("abc", 123);
131 }
132 catch (DataPortalException ex)
133 {
134 throw ex.BusinessException;
135 }
136 }
137
138 [TestMethod]
139 public void FetchNoCriteria()
140 {
141 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
142
143 var root = dataPortal.Fetch();
144 Assert.AreEqual("Fetch", root.Data, "Data should match");
145 Assert.IsFalse(root.IsNew, "Should not be new");
146 Assert.IsFalse(root.IsDirty, "Should not be dirty");
147 }
148
149 [TestMethod]
150 public void FetchCriteria()
151 {
152 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
153
154 var root = dataPortal.Fetch("abc");
155 Assert.AreEqual("abc", root.Data, "Data should match");
156 Assert.IsFalse(root.IsNew, "Should not be new");
157 Assert.IsFalse(root.IsDirty, "Should not be dirty");
158 }
159
160 [TestMethod]
161 public void Update()
162 {
163 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
164
165 var root = dataPortal.Fetch();
166 root.Data = "abc";
167
168 root = dataPortal.Update(root);
169 Assert.AreEqual(TransactionalTypes.Manual, root.TransactionalType, "Transactional type should match");
170 Assert.AreEqual("Update", root.Data, "Data should match");
171 Assert.IsFalse(root.IsNew, "Should not be new");
172 Assert.IsFalse(root.IsDirty, "Should not be dirty");
173 }
174
175 [TestMethod]
177 {
178 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
179 opts => opts.DataPortal(dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory1>>()))
180 );
181 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
182
183 var root = dataPortal.Fetch();
184 root.Data = "abc";
185
186 root = dataPortal.Update(root);
187 Assert.AreEqual(TransactionalTypes.TransactionScope, root.TransactionalType, "Transactional type should match");
188 Assert.AreEqual("Serializable", root.IsolationLevel, "Transactional isolation should match");
189 Assert.AreEqual(30, root.TransactionTimeout, "Transactional timeout should match");
190
191 Assert.AreEqual("Update", root.Data, "Data should match");
192 Assert.IsFalse(root.IsNew, "Should not be new");
193 Assert.IsFalse(root.IsDirty, "Should not be dirty");
194 }
195
196 [TestMethod]
198 {
199 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
200 options => options
201 .Data(
202 cfg => cfg
203 .DefaultTransactionIsolationLevel(TransactionIsolationLevel.RepeatableRead)
204 .DefaultTransactionTimeoutInSeconds(45)
205 )
206 .DataPortal(
207 dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory4>>())
208 )
209 );
210 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
211
212 var root = dataPortal.Create();
213 root.Data = "abc";
214
215
216 root = dataPortal.Update(root);
217 Assert.AreEqual(TransactionalTypes.TransactionScope, root.TransactionalType, "Transactional type should match");
218 Assert.AreEqual("ReadCommitted", root.IsolationLevel, "Transactional isolation should match");
219 Assert.AreEqual(100, root.TransactionTimeout, "Transactional timeout should match");
220
221 Assert.AreEqual("Update", root.Data, "Data should match");
222 Assert.IsFalse(root.IsNew, "Should not be new");
223 Assert.IsFalse(root.IsDirty, "Should not be dirty");
224 }
225
226
227 [TestMethod]
229 {
230 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
231 options => options
232 .Data(
233 cfg => cfg
234 .DefaultTransactionIsolationLevel(TransactionIsolationLevel.RepeatableRead)
235 .DefaultTransactionTimeoutInSeconds(45)
236 )
237 .DataPortal(
238 dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory5>>()))
239 );
240 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
241
242 var root = dataPortal.Create();
243 root.Data = "abc";
244
245
246 root = dataPortal.Update(root);
247 Assert.AreEqual(TransactionalTypes.TransactionScope, root.TransactionalType, "Transactional type should match");
248 Assert.AreEqual("RepeatableRead", root.IsolationLevel, "Transactional isolation should match");
249 Assert.AreEqual(45, root.TransactionTimeout, "Transactional timeout should match");
250
251 Assert.AreEqual("Update", root.Data, "Data should match");
252 Assert.IsFalse(root.IsNew, "Should not be new");
253 Assert.IsFalse(root.IsDirty, "Should not be dirty");
254 }
255
256 [TestMethod]
257 public void Delete()
258 {
259 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
260
262
263 dataPortal.Delete("abc");
264
265 Assert.AreEqual("Delete", TestResults.GetResult("ObjectFactory"), "Data should match");
266 }
267
268 [TestMethod]
269 public void FetchLoadProperty()
270 {
271 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
272 options => options.DataPortal(
273 dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterObjectFactoryLoader<ObjectFactoryLoader<RootFactory3>>()))
274 );
275 IDataPortal<Root> dataPortal = testDIContext.CreateDataPortal<Root>();
276
277 var root = dataPortal.Fetch();
278 Assert.AreEqual("Fetch", root.Data, "Data should match");
279 Assert.IsFalse(root.IsNew, "Should not be new");
280 Assert.IsFalse(root.IsDirty, "Should not be dirty");
281 }
282
283 [TestMethod]
285 {
286 TestDIContext testDIContext = TestDIContextFactory.CreateDefaultContext();
287 IDataPortal<CommandObject> dataPortal = testDIContext.CreateDataPortal<CommandObject>();
288
290
291 var test = CommandObject.Execute(dataPortal);
292 // return value is set in Execute method in CommandObjectFactory
293 Assert.IsTrue(test);
294 }
295
296 [TestMethod]
297 [ExpectedException(typeof(DataPortalException))]
299 {
300 TestDIContext testDIContext = TestDIContextFactory.CreateDefaultContext();
301 IDataPortal<CommandObjectMissingFactoryMethod> dataPortal = testDIContext.CreateDataPortal<CommandObjectMissingFactoryMethod>();
302
303 try
304 {
306
307 var test = CommandObjectMissingFactoryMethod.Execute(dataPortal);
308 }
309 catch (DataPortalException ex)
310 {
311 // inner exception should be System.NotImplementedException and mesaage should contain methodname
312 Assert.AreEqual(typeof(System.NotImplementedException), ex.InnerException.GetType());
313 Assert.IsTrue(ex.InnerException.Message.Contains("ExecuteMissingMethod"));
314 // rethrow exception
315 throw;
316 }
317 Assert.Fail("Should throw exception");
318 }
319 }
320}
Provides consistent context information between the client and server DataPortal objects.
ExecutionLocations
Enum representing the locations code can execute.
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Exception BusinessException
Gets the original server-side exception.
static bool Execute(IDataPortal< CommandObject > dataPortal)
static bool Execute(IDataPortal< CommandObjectMissingFactoryMethod > dataPortal)
static void ClassInitialize(TestContext context)
void DataPortalExecute_OnCommandObjectWithFalseExecuteMethod_ThrowsExeptionMehodNotFound()
void Cleanup()
Always make sure to cleanup after each test
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
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Update(object obj)
Insert, update or delete an object in the database.
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...
TransactionalTypes
Provides a list of possible transactional technologies to be used by the server-side DataPortal.
TransactionIsolationLevel
Specifies an isolation level for transactions controlled by TransactionalAttribute