/
LazyFieldInterceptorTests.cs
52 lines (45 loc) · 1.68 KB
/
LazyFieldInterceptorTests.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
using System;
using System.Collections.Generic;
using NHibernate.Proxy;
using NUnit.Framework;
using NHibernate.Intercept;
namespace NHibernate.Test.DynamicProxyTests
{
public class LazyFieldInterceptorTests
{
[Serializable]
public class MyClass
{
public virtual int Id { get; set; }
public virtual void ThrowError()
{
// Use some specific exception type to avoid using the base class.
throw new FormatException("test");
}
}
[Test]
public void LazyFieldInterceptorMarkedAsSerializable()
{
Assert.That(typeof(DefaultDynamicLazyFieldInterceptor), Has.Attribute<SerializableAttribute>());
}
[Test]
public void LazyFieldInterceptorIsBinarySerializable()
{
var pf = new DefaultProxyFactory();
var propertyInfo = typeof(MyClass).GetProperty("Id");
pf.PostInstantiate("MyClass", typeof(MyClass), new HashSet<System.Type>(), propertyInfo.GetGetMethod(), propertyInfo.GetSetMethod(), null);
var fieldInterceptionProxy = (IFieldInterceptorAccessor)pf.GetFieldInterceptionProxy(new MyClass());
fieldInterceptionProxy.FieldInterceptor = new DefaultFieldInterceptor(null, null, null, "MyClass", typeof(MyClass));
Assert.That(fieldInterceptionProxy, Is.BinarySerializable);
}
[Test]
public void DefaultDynamicLazyFieldInterceptorUnWrapsTIEExceptions()
{
var pf = new DefaultProxyFactory();
var propertyInfo = typeof(MyClass).GetProperty("Id");
pf.PostInstantiate("MyClass", typeof(MyClass), new HashSet<System.Type>(), propertyInfo.GetGetMethod(), propertyInfo.GetSetMethod(), null);
var myClassProxied = (MyClass)pf.GetFieldInterceptionProxy(new MyClass());
Assert.Throws<FormatException>(() => myClassProxied.ThrowError(), "test");
}
}
}