1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
6 | using HeuristicLab.Persistence.Core;
|
---|
7 | using System.Collections;
|
---|
8 | using HeuristicLab.Persistence.Default.Xml;
|
---|
9 | using HeuristicLab.Persistence.Default.DebugString;
|
---|
10 | using System.IO;
|
---|
11 | using System.Reflection;
|
---|
12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
13 | using HeuristicLab.Persistence.Interfaces;
|
---|
14 | using HeuristicLab.Persistence.Default.Xml.Primitive;
|
---|
15 | using HeuristicLab.Persistence.Default.CompositeSerializers;
|
---|
16 | using HeuristicLab.Persistence.Auxiliary;
|
---|
17 | using System.Text.RegularExpressions;
|
---|
18 | using HeuristicLab.Persistence.Test;
|
---|
19 | using System.Drawing;
|
---|
20 |
|
---|
21 | namespace HeuristicLab.Persistence.UnitTest {
|
---|
22 |
|
---|
23 | public class NumberTest {
|
---|
24 | [Storable]
|
---|
25 | private bool _bool = true;
|
---|
26 | [Storable]
|
---|
27 | private byte _byte = 0xFF;
|
---|
28 | [Storable]
|
---|
29 | private sbyte _sbyte = 0xF;
|
---|
30 | [Storable]
|
---|
31 | private short _short = -123;
|
---|
32 | [Storable]
|
---|
33 | private ushort _ushort = 123;
|
---|
34 | [Storable]
|
---|
35 | private int _int = -123;
|
---|
36 | [Storable]
|
---|
37 | private uint _uint = 123;
|
---|
38 | [Storable]
|
---|
39 | private long _long = 123456;
|
---|
40 | [Storable]
|
---|
41 | private ulong _ulong = 123456;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public class NonDefaultConstructorClass {
|
---|
45 | [Storable]
|
---|
46 | int value;
|
---|
47 | public NonDefaultConstructorClass(int value) {
|
---|
48 | this.value = value;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public class IntWrapper {
|
---|
53 |
|
---|
54 | [Storable]
|
---|
55 | public int Value;
|
---|
56 |
|
---|
57 | private IntWrapper() { }
|
---|
58 |
|
---|
59 | public IntWrapper(int value) {
|
---|
60 | this.Value = value;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override bool Equals(object obj) {
|
---|
64 | if (obj as IntWrapper == null)
|
---|
65 | return false;
|
---|
66 | return Value.Equals(((IntWrapper)obj).Value);
|
---|
67 | }
|
---|
68 | public override int GetHashCode() {
|
---|
69 | return Value.GetHashCode();
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | public class PrimitivesTest : NumberTest {
|
---|
75 | [Storable]
|
---|
76 | private char c = 'e';
|
---|
77 | [Storable]
|
---|
78 | private long[,] _long_array =
|
---|
79 | new long[,] { { 123, 456, }, { 789, 123 } };
|
---|
80 | [Storable]
|
---|
81 | public List<int> list = new List<int> { 1, 2, 3, 4, 5 };
|
---|
82 | [Storable]
|
---|
83 | private object o = new object();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public enum TestEnum { va1, va2, va3, va8 } ;
|
---|
87 |
|
---|
88 | public class RootBase {
|
---|
89 | [Storable]
|
---|
90 | private string baseString = " Serial ";
|
---|
91 | [Storable]
|
---|
92 | public TestEnum myEnum = TestEnum.va3;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public class Root : RootBase {
|
---|
96 | [Storable]
|
---|
97 | public Stack<int> intStack = new Stack<int>();
|
---|
98 | [Storable]
|
---|
99 | public int[] i = new[] { 3, 4, 5, 6 };
|
---|
100 | [Storable(Name = "Test String")]
|
---|
101 | public string s;
|
---|
102 | [Storable]
|
---|
103 | public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 });
|
---|
104 | [Storable]
|
---|
105 | public List<int> intList = new List<int>(new[] { 321, 312, 321 });
|
---|
106 | [Storable]
|
---|
107 | public Custom c;
|
---|
108 | [Storable]
|
---|
109 | public List<Root> selfReferences;
|
---|
110 | [Storable]
|
---|
111 | public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
|
---|
112 | [Storable]
|
---|
113 | public bool boolean = true;
|
---|
114 | [Storable]
|
---|
115 | public DateTime dateTime;
|
---|
116 | [Storable]
|
---|
117 | public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123);
|
---|
118 | [Storable]
|
---|
119 | public Dictionary<string, int> dict = new Dictionary<string, int>();
|
---|
120 | [Storable(DefaultValue = "default")]
|
---|
121 | public string uninitialized;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public enum SimpleEnum { one, two, three }
|
---|
125 | public enum ComplexEnum { one = 1, two = 2, three = 3 }
|
---|
126 | [FlagsAttribute]
|
---|
127 | public enum TrickyEnum { zero = 0, one = 1, two = 2 }
|
---|
128 |
|
---|
129 | public class EnumTest {
|
---|
130 | [Storable]
|
---|
131 | public SimpleEnum simpleEnum = SimpleEnum.one;
|
---|
132 | [Storable]
|
---|
133 | public ComplexEnum complexEnum = (ComplexEnum)2;
|
---|
134 | [Storable]
|
---|
135 | public TrickyEnum trickyEnum = (TrickyEnum)15;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public class Custom {
|
---|
139 | [Storable]
|
---|
140 | public int i;
|
---|
141 | [Storable]
|
---|
142 | public Root r;
|
---|
143 | [Storable]
|
---|
144 | public string name = "<![CDATA[<![CDATA[Serial]]>]]>";
|
---|
145 | }
|
---|
146 |
|
---|
147 | public class Manager {
|
---|
148 |
|
---|
149 | public DateTime lastLoadTime;
|
---|
150 | [Storable]
|
---|
151 | private DateTime lastLoadTimePersistence {
|
---|
152 | get { return lastLoadTime; }
|
---|
153 | set { lastLoadTime = DateTime.Now; }
|
---|
154 | }
|
---|
155 | [Storable]
|
---|
156 | public double? dbl;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public class C {
|
---|
160 | [Storable]
|
---|
161 | public C[][] allCs;
|
---|
162 | [Storable]
|
---|
163 | public KeyValuePair<List<C>, C> kvpList;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public class NonSerializable {
|
---|
167 | int x;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | [TestClass]
|
---|
172 | public class UseCases {
|
---|
173 |
|
---|
174 | private string tempFile;
|
---|
175 |
|
---|
176 | [TestInitialize()]
|
---|
177 | public void CreateTempFile() {
|
---|
178 | tempFile = Path.GetTempFileName();
|
---|
179 | }
|
---|
180 |
|
---|
181 | [TestCleanup()]
|
---|
182 | public void ClearTempFile() {
|
---|
183 | StreamReader reader = new StreamReader(tempFile);
|
---|
184 | string s = reader.ReadToEnd();
|
---|
185 | reader.Close();
|
---|
186 | File.Delete(tempFile);
|
---|
187 | }
|
---|
188 |
|
---|
189 | [TestMethod]
|
---|
190 | public void ComplexStorable() {
|
---|
191 | Root r = new Root();
|
---|
192 | r.intStack.Push(1);
|
---|
193 | r.intStack.Push(2);
|
---|
194 | r.intStack.Push(3);
|
---|
195 | r.selfReferences = new List<Root> { r, r };
|
---|
196 | r.c = new Custom { r = r };
|
---|
197 | r.dict.Add("one", 1);
|
---|
198 | r.dict.Add("two", 2);
|
---|
199 | r.dict.Add("three", 3);
|
---|
200 | r.myEnum = TestEnum.va1;
|
---|
201 | r.i = new[] { 7, 5, 6 };
|
---|
202 | r.s = "new value";
|
---|
203 | r.intArray = new ArrayList { 3, 2, 1 };
|
---|
204 | r.intList = new List<int> { 9, 8, 7 };
|
---|
205 | r.multiDimArray = new double[,] { { 5, 4, 3 }, { 1, 4, 6 } };
|
---|
206 | r.boolean = false;
|
---|
207 | r.dateTime = DateTime.Now;
|
---|
208 | r.kvp = new KeyValuePair<string, int>("string key", 321);
|
---|
209 | r.uninitialized = null;
|
---|
210 | XmlGenerator.Serialize(r, tempFile);
|
---|
211 | Root newR = (Root)XmlParser.Deserialize(tempFile);
|
---|
212 | Assert.AreEqual(
|
---|
213 | DebugStringGenerator.Serialize(r),
|
---|
214 | DebugStringGenerator.Serialize(newR));
|
---|
215 | Assert.AreSame(newR, newR.selfReferences[0]);
|
---|
216 | Assert.AreNotSame(r, newR);
|
---|
217 | Assert.AreEqual(r.myEnum, TestEnum.va1);
|
---|
218 | Assert.AreEqual(r.i[0], 7);
|
---|
219 | Assert.AreEqual(r.i[1], 5);
|
---|
220 | Assert.AreEqual(r.i[2], 6);
|
---|
221 | Assert.AreEqual(r.s, "new value");
|
---|
222 | Assert.AreEqual(r.intArray[0], 3);
|
---|
223 | Assert.AreEqual(r.intArray[1], 2);
|
---|
224 | Assert.AreEqual(r.intArray[2], 1);
|
---|
225 | Assert.AreEqual(r.intList[0], 9);
|
---|
226 | Assert.AreEqual(r.intList[1], 8);
|
---|
227 | Assert.AreEqual(r.intList[2], 7);
|
---|
228 | Assert.AreEqual(r.multiDimArray[0, 0], 5);
|
---|
229 | Assert.AreEqual(r.multiDimArray[0, 1], 4);
|
---|
230 | Assert.AreEqual(r.multiDimArray[0, 2], 3);
|
---|
231 | Assert.AreEqual(r.multiDimArray[1, 0], 1);
|
---|
232 | Assert.AreEqual(r.multiDimArray[1, 1], 4);
|
---|
233 | Assert.AreEqual(r.multiDimArray[1, 2], 6);
|
---|
234 | Assert.IsFalse(r.boolean);
|
---|
235 | Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);
|
---|
236 | Assert.AreEqual(r.kvp.Key, "string key");
|
---|
237 | Assert.AreEqual(r.kvp.Value, 321);
|
---|
238 | Assert.IsNull(r.uninitialized);
|
---|
239 | Assert.AreEqual(newR.myEnum, TestEnum.va1);
|
---|
240 | Assert.AreEqual(newR.i[0], 7);
|
---|
241 | Assert.AreEqual(newR.i[1], 5);
|
---|
242 | Assert.AreEqual(newR.i[2], 6);
|
---|
243 | Assert.AreEqual(newR.s, "new value");
|
---|
244 | Assert.AreEqual(newR.intArray[0], 3);
|
---|
245 | Assert.AreEqual(newR.intArray[1], 2);
|
---|
246 | Assert.AreEqual(newR.intArray[2], 1);
|
---|
247 | Assert.AreEqual(newR.intList[0], 9);
|
---|
248 | Assert.AreEqual(newR.intList[1], 8);
|
---|
249 | Assert.AreEqual(newR.intList[2], 7);
|
---|
250 | Assert.AreEqual(newR.multiDimArray[0, 0], 5);
|
---|
251 | Assert.AreEqual(newR.multiDimArray[0, 1], 4);
|
---|
252 | Assert.AreEqual(newR.multiDimArray[0, 2], 3);
|
---|
253 | Assert.AreEqual(newR.multiDimArray[1, 0], 1);
|
---|
254 | Assert.AreEqual(newR.multiDimArray[1, 1], 4);
|
---|
255 | Assert.AreEqual(newR.multiDimArray[1, 2], 6);
|
---|
256 | Assert.AreEqual(newR.intStack.Pop(), 3);
|
---|
257 | Assert.AreEqual(newR.intStack.Pop(), 2);
|
---|
258 | Assert.AreEqual(newR.intStack.Pop(), 1);
|
---|
259 | Assert.IsFalse(newR.boolean);
|
---|
260 | Assert.IsTrue((DateTime.Now - newR.dateTime).TotalSeconds < 10);
|
---|
261 | Assert.AreEqual(newR.kvp.Key, "string key");
|
---|
262 | Assert.AreEqual(newR.kvp.Value, 321);
|
---|
263 | Assert.IsNull(newR.uninitialized);
|
---|
264 | }
|
---|
265 |
|
---|
266 | [TestMethod]
|
---|
267 | public void SelfReferences() {
|
---|
268 | C c = new C();
|
---|
269 | C[][] cs = new C[2][];
|
---|
270 | cs[0] = new C[] { c };
|
---|
271 | cs[1] = new C[] { c };
|
---|
272 | c.allCs = cs;
|
---|
273 | c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
|
---|
274 | XmlGenerator.Serialize(cs, tempFile);
|
---|
275 | object o = XmlParser.Deserialize(tempFile);
|
---|
276 | Assert.AreEqual(
|
---|
277 | DebugStringGenerator.Serialize(cs),
|
---|
278 | DebugStringGenerator.Serialize(o));
|
---|
279 | Assert.AreSame(c, c.allCs[0][0]);
|
---|
280 | Assert.AreSame(c, c.allCs[1][0]);
|
---|
281 | Assert.AreSame(c, c.kvpList.Key[0]);
|
---|
282 | Assert.AreSame(c, c.kvpList.Value);
|
---|
283 | C[][] newCs = (C[][])o;
|
---|
284 | C newC = newCs[0][0];
|
---|
285 | Assert.AreSame(newC, newC.allCs[0][0]);
|
---|
286 | Assert.AreSame(newC, newC.allCs[1][0]);
|
---|
287 | Assert.AreSame(newC, newC.kvpList.Key[0]);
|
---|
288 | Assert.AreSame(newC, newC.kvpList.Value);
|
---|
289 | }
|
---|
290 |
|
---|
291 | [TestMethod]
|
---|
292 | public void ArrayCreation() {
|
---|
293 | ArrayList[] arrayListArray = new ArrayList[4];
|
---|
294 | arrayListArray[0] = new ArrayList();
|
---|
295 | arrayListArray[0].Add(arrayListArray);
|
---|
296 | arrayListArray[0].Add(arrayListArray);
|
---|
297 | arrayListArray[1] = new ArrayList();
|
---|
298 | arrayListArray[1].Add(arrayListArray);
|
---|
299 | arrayListArray[2] = new ArrayList();
|
---|
300 | arrayListArray[2].Add(arrayListArray);
|
---|
301 | arrayListArray[2].Add(arrayListArray);
|
---|
302 | Array a = Array.CreateInstance(
|
---|
303 | typeof(object),
|
---|
304 | new[] { 1, 2 }, new[] { 3, 4 });
|
---|
305 | arrayListArray[2].Add(a);
|
---|
306 | XmlGenerator.Serialize(arrayListArray, tempFile);
|
---|
307 | object o = XmlParser.Deserialize(tempFile);
|
---|
308 | Assert.AreEqual(
|
---|
309 | DebugStringGenerator.Serialize(arrayListArray),
|
---|
310 | DebugStringGenerator.Serialize(o));
|
---|
311 | ArrayList[] newArray = (ArrayList[])o;
|
---|
312 | Assert.AreSame(arrayListArray, arrayListArray[0][0]);
|
---|
313 | Assert.AreSame(arrayListArray, arrayListArray[2][1]);
|
---|
314 | Assert.AreSame(newArray, newArray[0][0]);
|
---|
315 | Assert.AreSame(newArray, newArray[2][1]);
|
---|
316 | }
|
---|
317 |
|
---|
318 | [TestMethod]
|
---|
319 | public void CustomSerializationProperty() {
|
---|
320 | Manager m = new Manager();
|
---|
321 | XmlGenerator.Serialize(m, tempFile);
|
---|
322 | Manager newM = (Manager)XmlParser.Deserialize(tempFile);
|
---|
323 | Assert.AreNotEqual(
|
---|
324 | DebugStringGenerator.Serialize(m),
|
---|
325 | DebugStringGenerator.Serialize(newM));
|
---|
326 | Assert.AreEqual(m.dbl, newM.dbl);
|
---|
327 | Assert.AreEqual(m.lastLoadTime, new DateTime());
|
---|
328 | Assert.AreNotEqual(newM.lastLoadTime, new DateTime());
|
---|
329 | Assert.IsTrue((DateTime.Now - newM.lastLoadTime).TotalSeconds < 10);
|
---|
330 | }
|
---|
331 |
|
---|
332 | [TestMethod]
|
---|
333 | public void Primitives() {
|
---|
334 | PrimitivesTest sdt = new PrimitivesTest();
|
---|
335 | XmlGenerator.Serialize(sdt, tempFile);
|
---|
336 | object o = XmlParser.Deserialize(tempFile);
|
---|
337 | Assert.AreEqual(
|
---|
338 | DebugStringGenerator.Serialize(sdt),
|
---|
339 | DebugStringGenerator.Serialize(o));
|
---|
340 | }
|
---|
341 |
|
---|
342 | [TestMethod]
|
---|
343 | public void MultiDimensionalArray() {
|
---|
344 | string[,] mDimString = new string[,] {
|
---|
345 | {"ora", "et", "labora"},
|
---|
346 | {"Beten", "und", "Arbeiten"}
|
---|
347 | };
|
---|
348 | XmlGenerator.Serialize(mDimString, tempFile);
|
---|
349 | object o = XmlParser.Deserialize(tempFile);
|
---|
350 | Assert.AreEqual(
|
---|
351 | DebugStringGenerator.Serialize(mDimString),
|
---|
352 | DebugStringGenerator.Serialize(o));
|
---|
353 | }
|
---|
354 |
|
---|
355 | public class NestedType {
|
---|
356 | [Storable]
|
---|
357 | private string value = "value";
|
---|
358 | }
|
---|
359 |
|
---|
360 | [TestMethod]
|
---|
361 | public void NestedTypeTest() {
|
---|
362 | NestedType t = new NestedType();
|
---|
363 | XmlGenerator.Serialize(t, tempFile);
|
---|
364 | object o = XmlParser.Deserialize(tempFile);
|
---|
365 | Assert.AreEqual(
|
---|
366 | DebugStringGenerator.Serialize(t),
|
---|
367 | DebugStringGenerator.Serialize(o));
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | [TestMethod]
|
---|
372 | public void SimpleArray() {
|
---|
373 | string[] strings = { "ora", "et", "labora" };
|
---|
374 | XmlGenerator.Serialize(strings, tempFile);
|
---|
375 | object o = XmlParser.Deserialize(tempFile);
|
---|
376 | Assert.AreEqual(
|
---|
377 | DebugStringGenerator.Serialize(strings),
|
---|
378 | DebugStringGenerator.Serialize(o));
|
---|
379 | }
|
---|
380 |
|
---|
381 | [TestMethod]
|
---|
382 | public void PrimitiveRoot() {
|
---|
383 | XmlGenerator.Serialize(12.3f, tempFile);
|
---|
384 | object o = XmlParser.Deserialize(tempFile);
|
---|
385 | Assert.AreEqual(
|
---|
386 | DebugStringGenerator.Serialize(12.3f),
|
---|
387 | DebugStringGenerator.Serialize(o));
|
---|
388 | }
|
---|
389 |
|
---|
390 | private string formatFullMemberName(MemberInfo mi) {
|
---|
391 | return new StringBuilder()
|
---|
392 | .Append(mi.DeclaringType.Assembly.GetName().Name)
|
---|
393 | .Append(": ")
|
---|
394 | .Append(mi.DeclaringType.Namespace)
|
---|
395 | .Append('.')
|
---|
396 | .Append(mi.DeclaringType.Name)
|
---|
397 | .Append('.')
|
---|
398 | .Append(mi.Name).ToString();
|
---|
399 | }
|
---|
400 |
|
---|
401 | [TestMethod]
|
---|
402 | public void CodingConventions() {
|
---|
403 | List<string> lowerCaseMethodNames = new List<string>();
|
---|
404 | List<string> lowerCaseProperties = new List<string>();
|
---|
405 | List<string> lowerCaseFields = new List<string>();
|
---|
406 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
407 | if (!a.GetName().Name.StartsWith("HeuristicLab"))
|
---|
408 | continue;
|
---|
409 | foreach (Type t in a.GetTypes()) {
|
---|
410 | foreach (MemberInfo mi in t.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) {
|
---|
411 | if (mi.DeclaringType.Name.StartsWith("<>"))
|
---|
412 | continue;
|
---|
413 | if (char.IsLower(mi.Name[0])) {
|
---|
414 | if (mi.MemberType == MemberTypes.Field)
|
---|
415 | lowerCaseFields.Add(formatFullMemberName(mi));
|
---|
416 | if (mi.MemberType == MemberTypes.Property)
|
---|
417 | lowerCaseProperties.Add(formatFullMemberName(mi));
|
---|
418 | if (mi.MemberType == MemberTypes.Method &&
|
---|
419 | !mi.Name.StartsWith("get_") &&
|
---|
420 | !mi.Name.StartsWith("set_") &&
|
---|
421 | !mi.Name.StartsWith("add_") &&
|
---|
422 | !mi.Name.StartsWith("remove_"))
|
---|
423 | lowerCaseMethodNames.Add(formatFullMemberName(mi));
|
---|
424 | }
|
---|
425 | }
|
---|
426 | }
|
---|
427 | }
|
---|
428 | //Assert.AreEqual("", lowerCaseFields.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
429 | Assert.AreEqual("", lowerCaseMethodNames.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
430 | Assert.AreEqual("", lowerCaseProperties.Aggregate("", (a, b) => a + "\r\n" + b));
|
---|
431 | }
|
---|
432 |
|
---|
433 | [TestMethod]
|
---|
434 | public void Number2StringDecomposer() {
|
---|
435 | NumberTest sdt = new NumberTest();
|
---|
436 | XmlGenerator.Serialize(sdt, tempFile,
|
---|
437 | new Configuration(new XmlFormat(),
|
---|
438 | new List<IPrimitiveSerializer> { new String2XmlSerializer() },
|
---|
439 | new List<ICompositeSerializer> {
|
---|
440 | new StorableSerializer(),
|
---|
441 | new Number2StringSerializer() }));
|
---|
442 | object o = XmlParser.Deserialize(tempFile);
|
---|
443 | Assert.AreEqual(
|
---|
444 | DebugStringGenerator.Serialize(sdt),
|
---|
445 | DebugStringGenerator.Serialize(o));
|
---|
446 | }
|
---|
447 |
|
---|
448 | [TestMethod]
|
---|
449 | public void Enums() {
|
---|
450 | EnumTest et = new EnumTest();
|
---|
451 | et.simpleEnum = SimpleEnum.two;
|
---|
452 | et.complexEnum = ComplexEnum.three;
|
---|
453 | et.trickyEnum = TrickyEnum.two | TrickyEnum.one;
|
---|
454 | XmlGenerator.Serialize(et, tempFile);
|
---|
455 | EnumTest newEt = (EnumTest)XmlParser.Deserialize(tempFile);
|
---|
456 | Assert.AreEqual(et.simpleEnum, SimpleEnum.two);
|
---|
457 | Assert.AreEqual(et.complexEnum, ComplexEnum.three);
|
---|
458 | Assert.AreEqual(et.trickyEnum, (TrickyEnum)3);
|
---|
459 | }
|
---|
460 |
|
---|
461 | [TestMethod]
|
---|
462 | public void TestAliasingWithOverriddenEquals() {
|
---|
463 | List<IntWrapper> ints = new List<IntWrapper>();
|
---|
464 | ints.Add(new IntWrapper(1));
|
---|
465 | ints.Add(new IntWrapper(1));
|
---|
466 | Assert.AreEqual(ints[0], ints[1]);
|
---|
467 | Assert.AreNotSame(ints[0], ints[1]);
|
---|
468 | XmlGenerator.Serialize(ints, tempFile);
|
---|
469 | List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.Deserialize(tempFile);
|
---|
470 | Assert.AreEqual(newInts[0].Value, 1);
|
---|
471 | Assert.AreEqual(newInts[1].Value, 1);
|
---|
472 | Assert.AreEqual(newInts[0], newInts[1]);
|
---|
473 | Assert.AreNotSame(newInts[0], newInts[1]);
|
---|
474 | }
|
---|
475 |
|
---|
476 | [TestMethod]
|
---|
477 | public void NonDefaultConstructorTest() {
|
---|
478 | NonDefaultConstructorClass c = new NonDefaultConstructorClass(1);
|
---|
479 | try {
|
---|
480 | XmlGenerator.Serialize(c, tempFile);
|
---|
481 | Assert.Fail("Exception not thrown");
|
---|
482 | } catch (PersistenceException) {
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | [TestMethod]
|
---|
487 | public void TestSavingException() {
|
---|
488 | List<int> list = new List<int> { 1, 2, 3 };
|
---|
489 | XmlGenerator.Serialize(list, tempFile);
|
---|
490 | NonSerializable s = new NonSerializable();
|
---|
491 | try {
|
---|
492 | XmlGenerator.Serialize(s, tempFile);
|
---|
493 | Assert.Fail("Exception expected");
|
---|
494 | } catch (PersistenceException) { }
|
---|
495 | List<int> newList = (List<int>)XmlParser.Deserialize(tempFile);
|
---|
496 | Assert.AreEqual(list[0], newList[0]);
|
---|
497 | Assert.AreEqual(list[1], newList[1]);
|
---|
498 | }
|
---|
499 |
|
---|
500 | [TestMethod]
|
---|
501 | public void TestTypeStringConversion() {
|
---|
502 | string name = typeof(List<int>[]).AssemblyQualifiedName;
|
---|
503 | string shortName =
|
---|
504 | "System.Collections.Generic.List`1[[System.Int32, mscorlib]][], mscorlib";
|
---|
505 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
506 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
507 | Assert.AreEqual(shortName, typeof(List<int>[]).VersionInvariantName());
|
---|
508 | }
|
---|
509 |
|
---|
510 | [TestMethod]
|
---|
511 | public void TestHexadecimalPublicKeyToken() {
|
---|
512 | string name = "TestClass, TestAssembly, Version=1.2.3.4, PublicKey=1234abc";
|
---|
513 | string shortName = "TestClass, TestAssembly";
|
---|
514 | Assert.AreEqual(name, TypeNameParser.Parse(name).ToString());
|
---|
515 | Assert.AreEqual(shortName, TypeNameParser.Parse(name).ToString(false));
|
---|
516 | }
|
---|
517 |
|
---|
518 | [TestMethod]
|
---|
519 | public void TestMultipleFailure() {
|
---|
520 | List<NonSerializable> l = new List<NonSerializable>();
|
---|
521 | l.Add(new NonSerializable());
|
---|
522 | l.Add(new NonSerializable());
|
---|
523 | l.Add(new NonSerializable());
|
---|
524 | try {
|
---|
525 | Serializer s = new Serializer(l,
|
---|
526 | ConfigurationService.Instance.GetConfiguration(new XmlFormat()),
|
---|
527 | "ROOT", true);
|
---|
528 | StringBuilder tokens = new StringBuilder();
|
---|
529 | foreach (var token in s) {
|
---|
530 | tokens.Append(token.ToString());
|
---|
531 | }
|
---|
532 | Assert.Fail("Exception expected");
|
---|
533 | } catch (PersistenceException px) {
|
---|
534 | Assert.AreEqual(3, px.Data.Count);
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | [TestMethod]
|
---|
539 | public void TestAssemblyVersionCheck() {
|
---|
540 | IntWrapper i = new IntWrapper(1);
|
---|
541 | Serializer s = new Serializer(i, ConfigurationService.Instance.GetDefaultConfig(new XmlFormat()));
|
---|
542 | XmlGenerator g = new XmlGenerator();
|
---|
543 | StringBuilder dataString = new StringBuilder();
|
---|
544 | foreach (var token in s) {
|
---|
545 | dataString.Append(g.Format(token));
|
---|
546 | }
|
---|
547 | StringBuilder typeString = new StringBuilder();
|
---|
548 | foreach (var line in g.Format(s.TypeCache))
|
---|
549 | typeString.Append(line);
|
---|
550 | Deserializer d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(typeString.ToString())));
|
---|
551 | XmlParser p = new XmlParser(new StringReader(dataString.ToString()));
|
---|
552 | IntWrapper newI = (IntWrapper)d.Deserialize(p);
|
---|
553 | Assert.AreEqual(i.Value, newI.Value);
|
---|
554 |
|
---|
555 | string newTypeString = Regex.Replace(typeString.ToString(),
|
---|
556 | "Version=\\d+\\.\\d+\\.\\d+\\.\\d+",
|
---|
557 | "Version=0.0.9999.9999");
|
---|
558 | try {
|
---|
559 | d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(newTypeString)));
|
---|
560 | Assert.Fail("Exception expected");
|
---|
561 | } catch (PersistenceException x) {
|
---|
562 | Assert.IsTrue(x.Message.Contains("incompatible"));
|
---|
563 | }
|
---|
564 | newTypeString = Regex.Replace(typeString.ToString(),
|
---|
565 | "Version=(\\d+\\.\\d+)\\.\\d+\\.\\d+",
|
---|
566 | "Version=$1.9999.9999");
|
---|
567 | try {
|
---|
568 | d = new Deserializer(XmlParser.ParseTypeCache(new StringReader(newTypeString)));
|
---|
569 | Assert.Fail("Exception expected");
|
---|
570 | } catch (PersistenceException x) {
|
---|
571 | Assert.IsTrue(x.Message.Contains("newer"));
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | [TestMethod]
|
---|
576 | public void InheritanceTest() {
|
---|
577 | New n = new New();
|
---|
578 | XmlGenerator.Serialize(n, tempFile);
|
---|
579 | New nn = (New)XmlParser.Deserialize(tempFile);
|
---|
580 | Assert.AreEqual(n.Name, nn.Name);
|
---|
581 | Assert.AreEqual(((Override)n).Name, ((Override)nn).Name);
|
---|
582 | }
|
---|
583 |
|
---|
584 | struct TestStruct {
|
---|
585 | int value;
|
---|
586 | int PropertyValue { get; set; }
|
---|
587 | public TestStruct(int value) : this() {
|
---|
588 | this.value = value;
|
---|
589 | PropertyValue = value;
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | [TestMethod]
|
---|
594 | public void StructTest() {
|
---|
595 | TestStruct s = new TestStruct(10);
|
---|
596 | XmlGenerator.Serialize(s, tempFile);
|
---|
597 | TestStruct newS = (TestStruct)XmlParser.Deserialize(tempFile);
|
---|
598 | Assert.AreEqual(s, newS);
|
---|
599 | }
|
---|
600 |
|
---|
601 | [TestMethod]
|
---|
602 | public void PointTest() {
|
---|
603 | Point p = new Point(12, 34);
|
---|
604 | XmlGenerator.Serialize(p, tempFile);
|
---|
605 | Point newP = (Point)XmlParser.Deserialize(tempFile);
|
---|
606 | Assert.AreEqual(p, newP);
|
---|
607 | }
|
---|
608 |
|
---|
609 | [TestMethod]
|
---|
610 | public void NullableValueTypes() {
|
---|
611 | double?[] d = new double?[] { null, 1, 2, 3 };
|
---|
612 | XmlGenerator.Serialize(d, tempFile);
|
---|
613 | double?[] newD = (double?[])XmlParser.Deserialize(tempFile);
|
---|
614 | Assert.AreEqual(d[0], newD[0]);
|
---|
615 | Assert.AreEqual(d[1], newD[1]);
|
---|
616 | Assert.AreEqual(d[2], newD[2]);
|
---|
617 | Assert.AreEqual(d[3], newD[3]);
|
---|
618 | }
|
---|
619 |
|
---|
620 | [ClassInitialize]
|
---|
621 | public static void Initialize(TestContext testContext) {
|
---|
622 | ConfigurationService.Instance.Reset();
|
---|
623 | }
|
---|
624 | }
|
---|
625 | }
|
---|