forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationSerializationTests.cs
97 lines (89 loc) · 2.57 KB
/
ConfigurationSerializationTests.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
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.CfgTest
{
[TestFixture]
public class ConfigurationSerializationTests
{
[Test]
public void Configuration_should_be_serializable()
{
NHAssert.HaveSerializableAttribute(typeof (Configuration));
}
[Test]
public void Basic_CRUD_should_work()
{
Assembly assembly = Assembly.Load("NHibernate.DomainModel");
var cfg = new Configuration();
if (TestConfigurationHelper.hibernateConfigFile != null)
{
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
}
cfg.AddResource("NHibernate.DomainModel.ParentChild.hbm.xml", assembly);
var formatter = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
var memoryStream = new MemoryStream();
formatter.Serialize(memoryStream, cfg);
memoryStream.Position = 0;
cfg = formatter.Deserialize(memoryStream) as Configuration;
Assert.That(cfg, Is.Not.Null);
var export = new SchemaExport(cfg);
export.Execute(true, true, false);
var sf = cfg.BuildSessionFactory();
object parentId;
object childId;
using (var session = sf.OpenSession())
using (var tran = session.BeginTransaction())
{
var parent = new Parent();
var child = new Child();
parent.Child = child;
parent.X = 9;
parent.Count = 5;
child.Parent = parent;
child.Count = 3;
child.X = 4;
parentId = session.Save(parent);
childId = session.Save(child);
tran.Commit();
}
using (ISession session = sf.OpenSession())
{
var parent = session.Get<Parent>(parentId);
Assert.That(parent.Count, Is.EqualTo(5));
Assert.That(parent.X, Is.EqualTo(9));
Assert.That(parent.Child, Is.Not.Null);
Assert.That(parent.Child.X, Is.EqualTo(4));
Assert.That(parent.Child.Count, Is.EqualTo(3));
Assert.That(parent.Child.Parent, Is.EqualTo(parent));
}
using (ISession session = sf.OpenSession())
using (ITransaction tran = session.BeginTransaction())
{
var p = session.Get<Parent>(parentId);
var c = session.Get<Child>(childId);
session.Delete(c);
session.Delete(p);
tran.Commit();
}
using (ISession session = sf.OpenSession())
{
var p = session.Get<Parent>(parentId);
Assert.That(p, Is.Null);
}
TestCase.DropSchema(true, export, (ISessionFactoryImplementor)sf);
}
}
}