forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHAssert.cs
140 lines (125 loc) · 3.54 KB
/
NHAssert.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Reflection;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Util;
#if !NETFX
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
#endif
namespace NHibernate.Test
{
public static class NHAssert
{
#region Serializable
public static void HaveSerializableAttribute(System.Type clazz)
{
HaveSerializableAttribute(clazz, null, null);
}
public static void HaveSerializableAttribute(System.Type clazz, string message, params object[] args)
{
Assert.That(clazz, Has.Attribute<SerializableAttribute>(), message, args);
}
public static void InheritedAreMarkedSerializable(System.Type clazz)
{
InheritedAreMarkedSerializable(clazz, null, null);
}
public static void InheritedAreMarkedSerializable(System.Type clazz, string message, params object[] args)
{
var faulty = new List<System.Type>();
Assembly nhbA = Assembly.GetAssembly(clazz);
var types = ClassList(nhbA, clazz);
foreach (System.Type tp in types)
{
object[] atts = tp.GetCustomAttributes(typeof(SerializableAttribute), false);
if (atts.Length == 0)
faulty.Add(tp);
}
if (faulty.Count > 0)
{
var classes = string.Join(Environment.NewLine, faulty.Select(t => " " + t.FullName).ToArray());
Assert.Fail("The following classes was expected to be marked as Serializable:" + Environment.NewLine + classes);
}
}
public static void IsSerializable(object obj)
{
IsSerializable(obj, null, null);
}
public static void IsSerializable(object obj, string message, params object[] args)
{
#if NETFX
Assert.That(obj, Is.BinarySerializable, message, args);
#else
if (obj == null) throw new ArgumentNullException(nameof(args));
var formatter = new BinaryFormatter
{
SurrogateSelector = new SerializationHelper.SurrogateSelector()
};
var isSuccess = false;
using (var memoryStream = new MemoryStream())
{
try
{
formatter.Serialize(memoryStream, obj);
memoryStream.Seek(0L, SeekOrigin.Begin);
var deserialized = formatter.Deserialize(memoryStream);
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
isSuccess = deserialized != null;
}
catch (SerializationException)
{
}
}
Assert.That(isSuccess, message, args);
#endif
}
public static void IsXmlSerializable(object obj)
{
IsXmlSerializable(obj, null, null);
}
public static void IsXmlSerializable(object obj, string message, params object[] args)
{
#if NETFX
Assert.That(obj, Is.XmlSerializable, message, args);
#else
if (obj == null) throw new ArgumentNullException(nameof(obj));
var isSuccess = false;
using (var memoryStream = new MemoryStream())
{
try
{
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(memoryStream, obj);
memoryStream.Seek(0L, SeekOrigin.Begin);
isSuccess = serializer.Deserialize(memoryStream) != null;
}
catch (NotSupportedException)
{
}
catch (InvalidOperationException)
{
}
}
Assert.That(isSuccess, message, args);
#endif
}
#endregion
private static IEnumerable<System.Type> ClassList(Assembly assembly, System.Type type)
{
IList<System.Type> result = new List<System.Type>();
if (assembly != null)
{
System.Type[] types = assembly.GetTypes();
foreach (System.Type tp in types)
{
if (tp != type && type.IsAssignableFrom(tp) && !tp.IsInterface)
result.Add(tp);
}
}
return result;
}
}
}