forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadMemoryOnlyAssembyTest.cs
144 lines (127 loc) · 3.58 KB
/
LoadMemoryOnlyAssembyTest.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
141
142
143
144
using System;
using NUnit.Framework;
using System.CodeDom.Compiler;
using System.Linq;
using Microsoft.CSharp;
using System.Reflection;
using NHibernate.Engine;
using NHibernate.Mapping.ByCode;
using NHibernate.Tool.hbm2ddl;
namespace NHibernate.Test.CfgTest
{
[TestFixture]
public class LoadMemoryOnlyAssembyTest
{
[Test]
public void CompileMappingForDynamicInMemoryAssembly()
{
const int assemblyGenerateCount = 10;
var code = GenerateCode(assemblyGenerateCount);
var assembly = CompileAssembly(code);
var config = TestConfigurationHelper.GetDefaultConfiguration();
var mapper = new ModelMapper();
mapper.AddMappings(assembly.GetExportedTypes());
config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
Assert.That(config.ClassMappings, Has.Count.EqualTo(assemblyGenerateCount), "Not all virtual assembly mapped");
// Ascertain the mappings are usable.
using (var sf = config.BuildSessionFactory())
{
var se = new SchemaExport(config);
se.Create(false, true);
try
{
using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
foreach (var entityClass in assembly.GetExportedTypes()
.Where(t => !typeof(IConformistHoldersProvider).IsAssignableFrom(t)))
{
var ctor = entityClass.GetConstructor(Array.Empty<System.Type>());
Assert.That(ctor, Is.Not.Null, $"Default constructor for {entityClass} not found");
var entity = ctor.Invoke(Array.Empty<object>());
s.Save(entity);
}
tx.Commit();
}
using (var s = sf.OpenSession())
using (var tx = s.BeginTransaction())
{
var entities = s.CreateQuery("from System.Object").List<object>();
Assert.That(entities, Has.Count.EqualTo(assemblyGenerateCount), "Not all expected entities were persisted");
tx.Commit();
}
}
finally
{
TestCase.DropSchema(false, se, (ISessionFactoryImplementor) sf);
}
}
}
private static string[] GenerateCode(int assemblyCount)
{
const string codeTemplate = @"
using NHibernate.Mapping.ByCode.Conformist;
public class Entity$$INDEX$$
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
public class Entity$$INDEX$$Map : ClassMapping<Entity$$INDEX$$>
{
public Entity$$INDEX$$Map ()
{
Table(""Entity$$INDEX$$"");
Id(x => x.ID, m =>
{
m.Column(""`ID`"");
});
Property(x => x.Name, m =>
{
m.Column(""`Name`"");
});
}
}";
var code = new string[assemblyCount];
for (var i = 0; i < code.Length; i++)
code[i] = codeTemplate.Replace("$$INDEX$$", i.ToString("000"));
return code;
}
private static Assembly CompileAssembly(string[] code)
{
var parameters = new CompilerParameters
{
GenerateInMemory = true,
TreatWarningsAsErrors = false,
GenerateExecutable = false,
CompilerOptions = "/optimize"
};
parameters.ReferencedAssemblies.AddRange(
new[]
{
"System.dll",
"System.Core.dll",
"System.Data.dll",
new Uri(typeof(Cfg.Environment).Assembly.EscapedCodeBase).LocalPath
});
try
{
using (var provider = new CSharpCodeProvider())
{
var compile = provider.CompileAssemblyFromSource(parameters, code);
if (compile.Errors.HasErrors)
{
var text = "Compile error: " +
string.Join(System.Environment.NewLine, compile.Errors.Cast<CompilerError>());
Assert.Fail(text);
}
return compile.CompiledAssembly;
}
}
catch (PlatformNotSupportedException e)
{
Assert.Inconclusive(e.Message);
return null;
}
}
}
}