Skip to content

Commit ec27f9d

Browse files
davidrothhazzik
authored andcommitted
Added Testcase for NH3374
Session.Merge throws InvalidCastException when using a Lazy bytes[] property
1 parent cf26aa3 commit ec27f9d

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3374
4+
{
5+
public class Document
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
public virtual Blob Blob { get; set; }
10+
}
11+
12+
public class Blob
13+
{
14+
public virtual int Id { get; set; }
15+
public virtual byte[] Bytes { get; set; }
16+
}
17+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Linq;
4+
using NHibernate.Mapping.ByCode;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3374
8+
{
9+
[Igonre("Not fixed yet.")]
10+
public class ByCodeFixture : TestCaseMappingByCode
11+
{
12+
protected override HbmMapping GetMappings()
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.Class<Document>(rc =>
16+
{
17+
rc.Id(x => x.Id, idMapper => idMapper.Generator(Generators.Identity));
18+
rc.ManyToOne(x => x.Blob, m =>
19+
{
20+
m.Cascade(Mapping.ByCode.Cascade.All);
21+
});
22+
rc.Property(x => x.Name);
23+
});
24+
25+
mapper.Class<Blob>(map =>
26+
{
27+
map.Id(x => x.Id, idMapper => idMapper.Generator(Generators.Identity));
28+
map.Property(x => x.Bytes, y =>
29+
{
30+
y.Column(x =>
31+
{
32+
x.SqlType("varbinary(max)");
33+
x.Length(int.MaxValue);
34+
});
35+
y.Lazy(true);
36+
});
37+
});
38+
39+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
40+
}
41+
42+
protected override void OnSetUp()
43+
{
44+
using (ISession session = OpenSession())
45+
using (ITransaction transaction = session.BeginTransaction())
46+
{
47+
var e1 = new Document { Name = "Bob" };
48+
e1.Blob = new Blob { Bytes = new byte[] { 1, 2, 3 } };
49+
session.Save(e1);
50+
51+
session.Flush();
52+
transaction.Commit();
53+
}
54+
}
55+
56+
protected override void OnTearDown()
57+
{
58+
using (ISession session = OpenSession())
59+
using (ITransaction transaction = session.BeginTransaction())
60+
{
61+
session.Delete("from System.Object");
62+
63+
session.Flush();
64+
transaction.Commit();
65+
}
66+
}
67+
68+
[Test]
69+
public void TestNoTargetException()
70+
{
71+
Document document = LoadDetachedEntity();
72+
Blob blob = LoadDetachedBlob();
73+
74+
blob.Bytes = new byte[] { 4, 5, 6 };
75+
document.Blob = blob;
76+
77+
using (ISession session = OpenSession())
78+
using (ITransaction transaction = session.BeginTransaction())
79+
{
80+
session.Merge(document);
81+
}
82+
}
83+
84+
private Blob LoadDetachedBlob()
85+
{
86+
using (ISession session = OpenSession())
87+
using (session.BeginTransaction())
88+
{
89+
var blob = session.Get<Blob>(1);
90+
NHibernateUtil.Initialize(blob.Bytes);
91+
return blob;
92+
}
93+
}
94+
95+
private Document LoadDetachedEntity()
96+
{
97+
using (ISession session = OpenSession())
98+
using (session.BeginTransaction())
99+
{
100+
return session.Get<Document>(1);
101+
}
102+
}
103+
}
104+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@
664664
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
665665
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
666666
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
667+
<Compile Include="NHSpecificTest\NH3374\Document.cs" />
668+
<Compile Include="NHSpecificTest\NH3374\FixtureByCode.cs" />
667669
<Compile Include="NHSpecificTest\Dates\DateTimeOffsetQueryFixture.cs" />
668670
<Compile Include="NHSpecificTest\NH3050\Fixture.cs" />
669671
<Compile Include="NHSpecificTest\NH3050\Person.cs" />
@@ -2860,6 +2862,7 @@
28602862
</ItemGroup>
28612863
<ItemGroup>
28622864
<EmbeddedResource Include="NHSpecificTest\NH2860\Mappings.hbm.xml" />
2865+
<EmbeddedResource Include="NHSpecificTest\NH3374\Mappings.hbm.xml" />
28632866
<EmbeddedResource Include="NHSpecificTest\NH3332\Mappings.hbm.xml" />
28642867
<EmbeddedResource Include="NHSpecificTest\NH3050\Mappings.hbm.xml" />
28652868
<EmbeddedResource Include="NHSpecificTest\NH2651\Mappings.hbm.xml" />

0 commit comments

Comments
 (0)