forked from ccnet/CruiseControl.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHelpers.cs
More file actions
58 lines (55 loc) · 1.9 KB
/
Copy pathTestHelpers.cs
File metadata and controls
58 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using NUnit.Framework;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
namespace ThoughtWorks.CruiseControl.UnitTests
{
/// <summary>
/// Helper methods for testing the serialization / serialization of objects.
/// </summary>
public static class TestHelpers
{
/// <summary>
/// Ensures that the correct language is set for the unit tests.
/// </summary>
public static void EnsureLanguageIsValid()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
/// <summary>
/// Tests that an object can be serialized and deserialized.
/// </summary>
/// <param name="value">The value to test.</param>
/// <returns>The deserialized object.</returns>
/// <remarks>
/// This does not test that the deserialized object is the same as the source, it only tests
/// that the object can actually be serialized and deserialized.
/// </remarks>
public static object RunSerialisationTest(object value)
{
object result = null;
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(stream, value);
}
catch (Exception error)
{
Assert.Fail(string.Format(CultureInfo.CurrentCulture,"Unable to serialize: {0}", error.Message));
}
stream.Position = 0;
try
{
result = formatter.Deserialize(stream);
}
catch (Exception error)
{
Assert.Fail(string.Format(CultureInfo.CurrentCulture,"Unable to serialize: {0}", error.Message));
}
return result;
}
}
}