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.
BusinessListBaseTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusinessListBaseTests.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 System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using UnitDriven;
13using Csla.TestHelpers;
14
15
16#if NUNIT
17using NUnit.Framework;
18using TestClass = NUnit.Framework.TestFixtureAttribute;
19using TestInitialize = NUnit.Framework.SetUpAttribute;
20using TestCleanup = NUnit.Framework.TearDownAttribute;
21using TestMethod = NUnit.Framework.TestAttribute;
22#elif MSTEST
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24#endif
25
27{
28 [TestClass]
30 {
31 private static TestDIContext _testDIContext;
32
34 public static void ClassInitialize(TestContext context)
35 {
36 _testDIContext = TestDIContextFactory.CreateDefaultContext();
37 }
38
39 [TestMethod]
40 public void CreateList()
41 {
42 var obj = CreateRootList();
43 Assert.IsNotNull(obj);
44
45 var obj2 = CreateRoot();
46 Assert.IsNotNull(obj2.Children);
47 }
48
49 [TestMethod]
50 public void RootAddNewCore()
51 {
52 bool changed = false;
53 var obj = CreateRootList();
54 obj.CollectionChanged += (o, e) =>
55 {
56 changed = true;
57 };
58 var child = obj.AddNew();
59 Assert.IsTrue(changed);
60 Assert.AreEqual(child, obj[0]);
61 }
62
63 [TestMethod]
64
65 public void ChildAddNewCore()
66 {
67 bool childChanged = false;
68 bool changed = false;
69 var obj = CreateRoot();
70 obj.ChildChanged += (o, e) =>
71 {
72 childChanged = true;
73 };
74 obj.Children.CollectionChanged += (o, e) =>
75 {
76 changed = true;
77 };
78 var child = obj.Children.AddNew();
79 Assert.IsTrue(childChanged, "ChildChanged should be true");
80 Assert.IsTrue(changed, "Collection changed should be true");
81 Assert.AreEqual(child, obj.Children[0]);
82 }
83
84 [TestMethod]
86 {
87 var oldSetting = Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"];
88 try
89 {
90 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", "MobileFormatter");
91
92 var rootList = CreateRootList();
93 rootList.BeginEdit();
94 var child = rootList.AddNew();
95
96 rootList = rootList.Clone();
97
98 rootList.ApplyEdit();
99
100 Assert.IsTrue(rootList.IsDirty);
101 rootList = rootList.Save();
102 Assert.IsFalse(rootList.IsDirty);
103 }
104 finally
105 {
106 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", oldSetting);
107 }
108 }
109
110 [TestMethod]
111 public void UndoRootAcceptAdd()
112 {
113 var obj = CreateRootList();
114 obj.BeginEdit();
115 obj.AddNew();
116 obj.ApplyEdit();
117
118 Assert.IsTrue(obj.IsDirty);
119
120 obj = obj.Save();
121 Assert.IsFalse(obj.IsDirty);
122 }
123
124 [TestMethod]
125 public void UndoRootCancelAdd()
126 {
127 var obj = CreateRootList();
128 obj.BeginEdit();
129 obj.AddNew();
130 Assert.IsTrue(obj.IsDirty);
131 Assert.AreEqual(1, obj.Count);
132
133 obj.CancelEdit();
134
135 Assert.IsFalse(obj.IsDirty);
136 Assert.AreEqual(0, obj.Count);
137 }
138
139 [TestMethod]
140 public void UndoChildAcceptAdd()
141 {
142 var obj = CreateRoot();
143 obj.BeginEdit();
144 obj.Children.AddNew();
145 obj.ApplyEdit();
146
147 Assert.IsTrue(obj.Children.IsDirty);
148
149 obj = obj.Save();
150 Assert.IsFalse(obj.Children.IsDirty);
151 }
152
153 [TestMethod]
154 public void UndoChildCancelAdd()
155 {
156 var obj = CreateRoot();
157 obj.BeginEdit();
158 obj.Children.AddNew();
159 Assert.IsTrue(obj.Children.IsDirty);
160 Assert.AreEqual(1, obj.Children.Count);
161
162 obj.CancelEdit();
163
164 Assert.IsFalse(obj.Children.IsDirty);
165 Assert.AreEqual(0, obj.Children.Count);
166 }
167
168 [TestMethod]
169 public void InsertChild()
170 {
171
172 bool changed = false;
173 var obj = CreateChildList();
174 obj.CollectionChanged += (o, e) =>
175 {
176 changed = true;
177 };
178 var child = CreateChild(); // object is marked as child
179 obj.Insert(0, child);
180 Assert.IsTrue(changed);
181 Assert.AreEqual(child, obj[0]);
182 }
183
184 [TestMethod]
185 [ExpectedException(typeof(InvalidOperationException))] // thrown by BusinessListBase.InsertItem
187 {
188 bool changed = false;
189 var obj = CreateChildList();
190 obj.CollectionChanged += (o, e) =>
191 {
192 changed = true;
193 };
194 var nonChild = new Child(); // object is not marked as child
195 obj.Insert(0, nonChild);
196 Assert.IsTrue(changed);
197 Assert.AreEqual(nonChild, obj[0]);
198 }
199
200 [TestMethod]
202 {
203
204 bool changed = false;
205 var obj = CreateChildList();
206 obj.CollectionChanged += (o, e) =>
207 {
208 changed = true;
209 };
210 var child = CreateChild(); // object is marked as child
211
212 Assert.IsTrue(obj.RaiseListChangedEvents);
213 using (obj.SuppressListChangedEvents)
214 {
215 Assert.IsFalse(obj.RaiseListChangedEvents);
216
217 obj.Insert(0, child);
218 }
219 Assert.IsFalse(changed, "Should not raise ListChanged event");
220 Assert.IsTrue(obj.RaiseListChangedEvents);
221 Assert.AreEqual(child, obj[0]);
222 }
223
224 private Root CreateRoot()
225 {
226 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
227
228 return dataPortal.Create();
229 }
230
231 private RootList CreateRootList()
232 {
233 IDataPortal<RootList> dataPortal = _testDIContext.CreateDataPortal<RootList>();
234
235 return dataPortal.Create();
236 }
237
238 private ChildList CreateChildList()
239 {
240 IChildDataPortal<ChildList> childDataPortal = _testDIContext.CreateChildDataPortal<ChildList>();
241
242 return childDataPortal.CreateChild();
243 }
244
245 private Child CreateChild()
246 {
247 IChildDataPortal<Child> childDataPortal = _testDIContext.CreateChildDataPortal<Child>();
248
249 return childDataPortal.CreateChild();
250 }
251
252 }
253}
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
object CreateChild(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
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...