Mercurial > pub > bltoolkit
comparison UnitTests/CS/EditableObjects/EditableObjectTest.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Reflection; | |
3 using System.Xml; | |
4 | |
5 using NUnit.Framework; | |
6 | |
7 using BLToolkit.EditableObjects; | |
8 using BLToolkit.Mapping; | |
9 using BLToolkit.Reflection; | |
10 using BLToolkit.TypeBuilder; | |
11 | |
12 namespace EditableObjects | |
13 { | |
14 [TestFixture] | |
15 public class EditableObjectTest | |
16 { | |
17 public class Source | |
18 { | |
19 public int ID = 10; | |
20 public string Name = "20"; | |
21 public XmlDocument Xml; | |
22 | |
23 public Source() | |
24 { | |
25 Xml = new XmlDocument(); | |
26 Xml.LoadXml("<test/>"); | |
27 } | |
28 } | |
29 | |
30 public abstract class Dest: EditableObject | |
31 { | |
32 public string ChangedPropertyName; | |
33 | |
34 public abstract int ID { get; set; } | |
35 public abstract string Name { get; set; } | |
36 public abstract XmlDocument Xml { get; set; } | |
37 | |
38 protected override void OnPropertyChanged(string propertyName) | |
39 { | |
40 ChangedPropertyName = propertyName; | |
41 if (propertyName == "ID") | |
42 Assert.That(ID, Is.Not.EqualTo(0)); | |
43 else if (propertyName == "Xml") | |
44 Assert.That(Xml.InnerXml, Is.Not.EqualTo("<test />")); | |
45 | |
46 } | |
47 } | |
48 | |
49 [Test] | |
50 public void Notification() | |
51 { | |
52 Dest o = Map.ObjectToObject<Dest>(new Source()); | |
53 | |
54 Assert.AreEqual("", o.ChangedPropertyName); | |
55 | |
56 o.ID = 1; | |
57 Assert.AreEqual("ID", o.ChangedPropertyName); | |
58 | |
59 o.Xml.DocumentElement.AppendChild(o.Xml.CreateElement("el")); | |
60 Assert.AreEqual("Xml", o.ChangedPropertyName); | |
61 } | |
62 | |
63 public abstract class Object1: EditableObject<Object1> | |
64 { | |
65 [MapField("ObjectId")] | |
66 public abstract int ID { get; } | |
67 public abstract short Field1 { get; set; } | |
68 | |
69 [MapValue(true, "Y")] | |
70 [MapValue(false, "N")] | |
71 public abstract bool Field2 { get; set; } | |
72 [Parameter(2, 2, 2)] | |
73 public abstract DateTime? Field3 { get; set; } | |
74 [Parameter(2L)] | |
75 public abstract long Field4 { get; set; } | |
76 public abstract byte Field5 { get; set; } | |
77 public abstract char Field6 { get; set; } | |
78 public abstract ushort Field7 { get; set; } | |
79 public abstract uint Field8 { get; set; } | |
80 public abstract ulong Field9 { get; set; } | |
81 public abstract sbyte Field10 { get; set; } | |
82 public abstract float Field11 { get; set; } | |
83 public abstract double Field12 { get; set; } | |
84 [Parameter(3.08)] | |
85 public abstract decimal? Field13 { get; set; } | |
86 public abstract string Field14 { get; set; } | |
87 public abstract Guid Field15 { get; set; } | |
88 public abstract DayOfWeek Field16 { get; set; } | |
89 public abstract ulong? Field17 { get; set; } | |
90 public abstract XmlDocument XmlField { get; set; } | |
91 } | |
92 | |
93 [Test] | |
94 public void TestCreate() | |
95 { | |
96 Object1 o = Object1.CreateInstance(); | |
97 | |
98 Assert.That(o.Field4, Is.EqualTo(2L)); | |
99 Assert.That(o.Field3, Is.EqualTo(new DateTime(2,2,2))); | |
100 Assert.That(o.Field13, Is.EqualTo(3.08m)); | |
101 | |
102 Assert.IsFalse(o.IsDirty); | |
103 | |
104 TypeAccessor<Object1>.Instance["ID"].SetValue(o, 1); | |
105 | |
106 Assert.AreEqual(1, o.ID); | |
107 Assert.IsTrue (o.IsDirty); | |
108 Assert.IsTrue (o.IsDirtyMember("ID")); | |
109 o.AcceptChanges(); | |
110 Assert.IsFalse (o.IsDirty); | |
111 | |
112 o.Field16 = DayOfWeek.Saturday; | |
113 | |
114 Assert.AreEqual(DayOfWeek.Saturday, o.Field16); | |
115 Assert.IsTrue (o.IsDirty); | |
116 Assert.IsTrue (o.IsDirtyMember("Field16")); | |
117 o.AcceptChanges(); | |
118 Assert.IsFalse (o.IsDirty); | |
119 | |
120 o.Field17 = 5; | |
121 | |
122 Assert.AreEqual(5, o.Field17); | |
123 Assert.IsTrue (o.IsDirty); | |
124 Assert.IsTrue (o.IsDirtyMember("Field17")); | |
125 o.AcceptChanges(); | |
126 Assert.IsFalse (o.IsDirty); | |
127 | |
128 o.XmlField.LoadXml(@"<root><element attribute=""value""/></root>"); | |
129 Assert.IsTrue (o.IsDirty); | |
130 o.AcceptChanges(); | |
131 | |
132 o.XmlField.SelectSingleNode("/root/element/@attribute").Value = "changed"; | |
133 Assert.IsTrue (o.IsDirty); | |
134 Assert.IsTrue (o.IsDirtyMember("XmlField")); | |
135 o.AcceptChanges(); | |
136 Assert.IsFalse (o.IsDirty); | |
137 | |
138 o.XmlField.SelectSingleNode("/root/element/@attribute").Value = "once again"; | |
139 o.XmlField = new XmlDocument(); | |
140 Assert.IsTrue (o.IsDirty); | |
141 } | |
142 | |
143 | |
144 [Test] | |
145 public void TestRejectChangesNotification() | |
146 { | |
147 Object1 o = Object1.CreateInstance(); | |
148 | |
149 Console.WriteLine("o is dirty: " + o.IsDirty); | |
150 | |
151 o.PropertyChanged += object_PropertyChanged; | |
152 | |
153 Console.WriteLine("Changing 3 fields"); | |
154 | |
155 o.Field1 = 10; | |
156 o.Field2 = !o.Field2; | |
157 o.Field3 = DateTime.Now; | |
158 o.XmlField.LoadXml("<root foo=\"bar\"/>"); | |
159 o.XmlField.DocumentElement.Attributes.RemoveAll(); | |
160 o.XmlField.DocumentElement.Attributes.Append(o.XmlField.CreateAttribute("attr")); | |
161 | |
162 Console.WriteLine("Dirty Members"); | |
163 | |
164 PropertyInfo[] dirtyMembers = o.GetDirtyMembers(); | |
165 | |
166 Assert.AreEqual(4, dirtyMembers.Length); | |
167 | |
168 foreach (PropertyInfo dirtyMember in dirtyMembers) | |
169 Console.WriteLine(dirtyMember.Name); | |
170 | |
171 Console.WriteLine("Rejecting field 1"); | |
172 | |
173 o.RejectMemberChanges("Field1"); | |
174 o.RejectMemberChanges("XmlField"); | |
175 | |
176 Console.WriteLine("Dirty Members"); | |
177 | |
178 dirtyMembers = o.GetDirtyMembers(); | |
179 Assert.AreEqual(2, dirtyMembers.Length); | |
180 | |
181 foreach (PropertyInfo dirtyMember in dirtyMembers) | |
182 Console.WriteLine(dirtyMember.Name); | |
183 | |
184 Console.WriteLine("Rejecting all changes"); | |
185 | |
186 o.RejectChanges(); | |
187 | |
188 Console.WriteLine("Dirty Members"); | |
189 | |
190 dirtyMembers = o.GetDirtyMembers(); | |
191 Assert.AreEqual(dirtyMembers.Length, 0); | |
192 | |
193 foreach (PropertyInfo dirtyMember in dirtyMembers) | |
194 Console.WriteLine(dirtyMember.Name); | |
195 } | |
196 | |
197 private static void object_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) | |
198 { | |
199 Console.WriteLine("Property Changed: " + e.PropertyName); | |
200 } | |
201 | |
202 [Test] | |
203 public void IsDirtyTest() | |
204 { | |
205 Object1 o = Object1.CreateInstance(); | |
206 | |
207 o.Field1 = 10; | |
208 o.Field2 = !o.Field2; | |
209 o.Field3 = DateTime.Now; | |
210 o.AcceptChanges(); | |
211 | |
212 Object1 c = (Object1)((ICloneable)o).Clone(); | |
213 | |
214 Assert.IsFalse(o.IsDirty); | |
215 Assert.IsFalse(c.IsDirty); | |
216 | |
217 o.Field1 = 100; | |
218 c = (Object1)((ICloneable)o).Clone(); | |
219 | |
220 Assert.IsTrue(o.IsDirty); | |
221 Assert.IsTrue(c.IsDirty); | |
222 | |
223 Assert.IsTrue(o.IsDirtyMember("Field1")); | |
224 Assert.IsTrue(c.IsDirtyMember("Field1")); | |
225 | |
226 Assert.IsFalse(o.IsDirtyMember("Field2")); | |
227 Assert.IsFalse(c.IsDirtyMember("Field2")); | |
228 } | |
229 | |
230 public class TestClass | |
231 { | |
232 public int ID; | |
233 public string Str; | |
234 } | |
235 | |
236 public struct TestStruct | |
237 { | |
238 public int ID; | |
239 public string Str; | |
240 } | |
241 | |
242 public abstract class TestEditableObject : EditableObject | |
243 { | |
244 public abstract int ID { get; set; } | |
245 | |
246 public static TestEditableObject CreateInstance() | |
247 { | |
248 return (TestEditableObject) | |
249 TypeAccessor.CreateInstanceEx(typeof(TestEditableObject), null); | |
250 } | |
251 } | |
252 | |
253 [Test] | |
254 public void EqualsTest() | |
255 { | |
256 TestClass classInst1 = new TestClass(); | |
257 TestClass classInst2 = new TestClass(); | |
258 | |
259 TestStruct structInst1 = new TestStruct(); | |
260 TestStruct structInst2 = new TestStruct(); | |
261 | |
262 TestEditableObject editableInst1 = TestEditableObject.CreateInstance(); | |
263 TestEditableObject editableInst2 = TestEditableObject.CreateInstance(); | |
264 TestEditableObject editableInst3 = editableInst1; | |
265 | |
266 classInst1 .ID = classInst2 .ID = 1; | |
267 structInst1 .ID = structInst2 .ID = 1; | |
268 editableInst1.ID = editableInst2.ID = 1; | |
269 | |
270 TestStruct structInst3 = structInst1; | |
271 TestClass classInst3 = classInst1; | |
272 | |
273 Assert.IsTrue(Equals(structInst1, structInst2)); | |
274 Assert.IsTrue(Equals(structInst1, structInst3)); | |
275 Assert.IsTrue(Equals(structInst2, structInst3)); | |
276 | |
277 Assert.IsFalse(Equals(classInst1, classInst2)); | |
278 Assert.IsFalse(Equals(editableInst1, editableInst2)); | |
279 | |
280 Assert.IsTrue (Equals(classInst1, classInst3)); | |
281 Assert.IsTrue (Equals(editableInst1, editableInst3)); | |
282 | |
283 Assert.IsFalse(Equals(classInst2, classInst3)); | |
284 Assert.IsFalse(Equals(editableInst2, editableInst3)); | |
285 } | |
286 | |
287 [Test] | |
288 public void EqualsSpeedTest() | |
289 { | |
290 TestClass eo1Inst1 = new TestClass(); | |
291 TestClass eo1Inst3 = eo1Inst1; | |
292 | |
293 TestEditableObject eo2Inst1 = TestEditableObject.CreateInstance(); | |
294 TestEditableObject eo2Inst3 = eo2Inst1; | |
295 | |
296 eo1Inst1.ID = 1; eo1Inst1.Equals(eo1Inst3); | |
297 eo2Inst1.ID = 1; eo2Inst1.Equals(eo2Inst3); | |
298 | |
299 long startTicks = DateTime.Now.Ticks; | |
300 for (int i = 0; i < 100000; i++) | |
301 eo1Inst1.Equals(eo1Inst3); | |
302 Console.WriteLine(".NET: {0}", DateTime.Now.Ticks - startTicks); | |
303 | |
304 startTicks = DateTime.Now.Ticks; | |
305 for (int i = 0; i < 100000; i++) | |
306 eo2Inst1.Equals(eo2Inst3); | |
307 Console.WriteLine("BLT: {0}", DateTime.Now.Ticks - startTicks); | |
308 } | |
309 | |
310 [Test] | |
311 public void GetHashCodeSpeedTest() | |
312 { | |
313 TestClass eo1Inst1 = new TestClass(); | |
314 TestEditableObject eo2Inst1 = TestEditableObject.CreateInstance(); | |
315 | |
316 eo1Inst1.ID = 1; eo1Inst1.GetHashCode(); | |
317 eo2Inst1.ID = 1; eo2Inst1.GetHashCode(); | |
318 | |
319 long startTicks = DateTime.Now.Ticks; | |
320 for (int i = 0; i < 100000; i++) | |
321 eo1Inst1.GetHashCode(); | |
322 Console.WriteLine(".NET: {0}", DateTime.Now.Ticks - startTicks); | |
323 | |
324 startTicks = DateTime.Now.Ticks; | |
325 for (int i = 0; i < 100000; i++) | |
326 eo2Inst1.GetHashCode(); | |
327 Console.WriteLine("BLT: {0}", DateTime.Now.Ticks - startTicks); | |
328 } | |
329 | |
330 } | |
331 } |