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.
InterceptorTests.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
7using Csla.TestHelpers;
8#if !NUNIT && !ANDROID
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10#elif !ANDROID
11using NUnit.Framework;
12using TestClass = NUnit.Framework.TestFixtureAttribute;
13using TestInitialize = NUnit.Framework.SetUpAttribute;
14using TestCleanup = NUnit.Framework.TearDownAttribute;
15using TestMethod = NUnit.Framework.TestAttribute;
16#endif
17
19{
20 [TestClass]
21 public class InterceptorTests
22 {
23 private static TestDIContext _testDIContext;
24
26 public static void ClassInitialize(TestContext context)
27 {
28 _testDIContext = TestDIContextFactory.CreateContext(
29 options => options
30 .DataPortal(dp => dp.AddServerSideDataPortal(config =>
31 {
32 config.AddInterceptorProvider<TestInterceptor>();
33 config.RegisterActivator<TestActivator>();
34 }
35 )));
36 }
37
38 [TestMethod]
39 public void CreateWithIntercept()
40 {
42
43 var obj = CreateInitializeRoot("abc");
44 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeRoot"), "Initialize should have run");
45 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeRoot"), "Complete should have run");
46 Assert.AreEqual("CreateInstance", TestResults.GetResult("Activate1+InitializeRoot"), "CreateInstance should have run");
47 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
48 Assert.AreEqual("ResolveType", TestResults.GetResult("Activate4+InitializeRoot"), "ResolveType should have run");
49 }
50
51 [TestMethod]
52 public void FetchWithIntercept()
53 {
55
56 var obj = GetInitializeRoot("abc");
57 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeRoot"), "Initialize should have run");
58 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeRoot"), "Complete should have run");
59 Assert.AreEqual("CreateInstance", TestResults.GetResult("Activate1+InitializeRoot"), "CreateInstance should have run");
60 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
61 }
62
63 [TestMethod]
65 {
67
68 var obj = GetInitializeListRoot();
69 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeListRoot"), "Initialize should have run");
70 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeListRoot"), "Complete should have run");
71 Assert.AreEqual("CreateInstance", TestResults.GetResult("Activate1+InitializeListRoot"), "CreateInstance (list) should have run");
72 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeListRoot"), "InitializeInstance (list) should have run");
73
74 Assert.AreEqual("CreateInstance", TestResults.GetResult("Activate1+InitializeRoot"), "CreateInstance should have run");
75 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
76 }
77
78 [TestMethod]
79 public void InterceptException()
80 {
82
83 try
84 {
85 var obj = GetInitializeRoot("boom");
86 }
87 catch
88 { }
89 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeRoot"), "Initialize should have run");
90 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeRoot"), "Complete should have run");
91 Assert.IsTrue(!string.IsNullOrEmpty(TestResults.GetResult("InterceptException+InitializeRoot")), "Complete should have exception");
92 }
93
94 [TestMethod]
95 public void UpdateWithIntercept()
96 {
98
99 var obj = GetInitializeRoot("abc");
101
102 obj.Name = "xyz";
103 obj = obj.Save();
104
105 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeRoot"), "Initialize should have run");
106 Assert.AreEqual("Update", TestResults.GetResult("InterceptOp1+InitializeRoot"), "Initialize op should be Update");
107 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeRoot"), "Complete should have run");
108 Assert.AreEqual("Update", TestResults.GetResult("InterceptOp2+InitializeRoot"), "Complete op should be Update");
109 Assert.IsFalse(TestResults.ContainsResult("Activate1+InitializeRoot"), "CreateInstance should not have run");
110 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
111 }
112
113 [TestMethod]
115 {
117
118 var obj = GetInitializeListRoot();
120
121 obj[0].Name = "xyz";
122 obj = obj.Save();
123
124 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InitializeListRoot"), "Initialize should have run");
125 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InitializeListRoot"), "Complete should have run");
126 Assert.IsFalse(TestResults.ContainsResult("Activate1+InitializeListRoot"), "CreateInstance (list) should not have run");
127 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeListRoot"), "InitializeInstance (list) should have run");
128
129 Assert.IsFalse(TestResults.ContainsResult("Activate1+InitializeRoot"), "CreateInstance should not have run");
130 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InitializeRoot"), "InitializeInstance should have run");
131 }
132
133 [TestMethod]
135 {
136 IDataPortal<InterceptorCommand> dataPortal = _testDIContext.CreateDataPortal<InterceptorCommand>();
137
139
140 var obj = dataPortal.Create();
142 obj = dataPortal.Execute(obj);
143
144 Assert.AreEqual("Execute", TestResults.GetResult("InterceptorCommand"), "Execute should have run");
145 Assert.AreEqual("Initialize", TestResults.GetResult("Intercept1+InterceptorCommand"), "Initialize should have run");
146 Assert.AreEqual("Execute", TestResults.GetResult("InterceptOp1+InterceptorCommand"), "Initialize op should be Execute");
147 Assert.AreEqual("Complete", TestResults.GetResult("Intercept2+InterceptorCommand"), "Complete should have run");
148 Assert.AreEqual("Execute", TestResults.GetResult("InterceptOp2+InterceptorCommand"), "Complete op should be Execute");
149 Assert.IsFalse(TestResults.ContainsResult("Activate1+InterceptorCommand"), "CreateInstance should not have run");
150 Assert.AreEqual("InitializeInstance", TestResults.GetResult("Activate2+InterceptorCommand"), "InitializeInstance should have run");
151 }
152
153 private InitializeRoot CreateInitializeRoot(string ident)
154 {
155 IDataPortal<InitializeRoot> dataPortal = _testDIContext.CreateDataPortal<InitializeRoot>();
156
157 return dataPortal.Create(ident);
158 }
159
160 private InitializeRoot GetInitializeRoot(string ident)
161 {
162 IDataPortal<InitializeRoot> dataPortal = _testDIContext.CreateDataPortal<InitializeRoot>();
163
164 return dataPortal.Fetch(ident);
165 }
166
167 private InitializeListRoot GetInitializeListRoot()
168 {
169 IDataPortal<InitializeListRoot> dataPortal = _testDIContext.CreateDataPortal<InitializeListRoot>();
170
171 return dataPortal.Fetch();
172 }
173
174 }
175
177 public class InitializeRoot : BusinessBase<InitializeRoot>
178 {
179 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
180 public string Name
181 {
182 get { return GetProperty(NameProperty); }
183 set { SetProperty(NameProperty, value); }
184 }
185
186 private void DataPortal_Create(string name)
187 {
188 Fetch(name);
189 }
190
191 private void DataPortal_Fetch(string name)
192 {
193 Fetch(name);
194 }
195
196 private void Child_Fetch(string name)
197 {
198 Fetch(name);
199 }
200
201 private void Fetch(string name)
202 {
203 if (name == "boom")
204 throw new Exception("boom");
205
206 using (BypassPropertyChecks)
207 {
208 Name = name;
209 }
210 }
211
212 [Update]
213 protected void DataPortal_Update()
214 {
215 }
216
217 private void Child_Update()
218 {
219 }
220 }
221
223 public class InitializeListRoot : BusinessListBase<InitializeListRoot, InitializeRoot>
224 {
225 private void DataPortal_Fetch([Inject] IChildDataPortal<InitializeRoot> childDataPortal)
226 {
227 using (SuppressListChangedEvents)
228 {
229 Add(childDataPortal.FetchChild("abc"));
230 }
231 }
232
233 [Update]
234 protected void DataPortal_Update()
235 {
236 base.Child_Update();
237 }
238 }
239
241 public class InterceptorCommand : CommandBase<InterceptorCommand>
242 {
243 [RunLocal]
244 [Create]
245 private void Create()
246 { }
247
248 [Execute]
249 protected void DataPortal_Execute()
250 {
251 TestResults.Add("InterceptorCommand", "Execute");
252 }
253 }
254
255 public class TestInterceptor : Csla.Server.IInterceptDataPortal
256 {
257 public void Initialize(Server.InterceptArgs e)
258 {
259 TestResults.Add("Intercept1+" + e.ObjectType.Name, "Initialize");
260 TestResults.Add("InterceptOp1+" + e.ObjectType.Name, e.Operation.ToString());
261 }
262
263 public void Complete(Server.InterceptArgs e)
264 {
265 TestResults.Add("Intercept2+" + e.ObjectType.Name, "Complete");
266 if (e.Exception != null)
267 TestResults.AddOrOverwrite("InterceptException+" + e.ObjectType.Name, e.Exception.Message);
268 TestResults.Add("InterceptOp2+" + e.ObjectType.Name, e.Operation.ToString());
269 }
270 }
271
272 public class TestActivator : Csla.Server.IDataPortalActivator
273 {
274 public object CreateInstance(Type requestedType)
275 {
276 TestResults.Add("Activate1+" + requestedType.Name, "CreateInstance");
277 return Activator.CreateInstance(requestedType);
278 }
279
280 public void InitializeInstance(object obj)
281 {
282 TestResults.Add("Activate2+" + obj.GetType().Name, "InitializeInstance");
283 }
284
285 public void FinalizeInstance(object obj)
286 {
287 TestResults.Add("Activate3+" + obj.GetType().Name, "FinalizeInstance");
288 }
289
290 public Type ResolveType(Type requestedType)
291 {
292 TestResults.Add("Activate4+" + requestedType.Name, "ResolveType");
293 return requestedType;
294 }
295 }
296}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
This is the base class from which most business collections or lists will be derived.
This is the base class from which command objects will be derived.
Definition: CommandBase.cs:51
Client side data portal used for making asynchronous data portal calls in .NET.
Definition: DataPortalT.cs:24
Maintains metadata about a property.
static readonly PropertyInfo< string > NameProperty
static void ClassInitialize(TestContext context)
void InitializeInstance(object obj)
Initializes an existing business object instance.
void FinalizeInstance(object obj)
Finalizes an existing business object instance.
Type ResolveType(Type requestedType)
Gets the actual business domain class type based on the requested type (which might be an interface).
object CreateInstance(Type requestedType)
Gets a new instance of the requested type.
void Initialize(Server.InterceptArgs e)
void Complete(Server.InterceptArgs e)
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 AddOrOverwrite(string key, string value)
Overwrite an item in the test results, to indicate an outcome of a particular operation
Definition: TestResults.cs:39
static bool ContainsResult(string key)
Get the existence of a result of an operation from the underlying results dictionary
Definition: TestResults.cs:61
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 child data portal type.
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...
object Execute(object obj)
Called to execute a Command object on the server.
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...
@ Serializable
Prevents updating or inserting until the transaction is complete.
@ Update
Update operation (includes insert, update and delete self).
@ Execute
Execute operation.