Skip to content

Commit 2d7b80e

Browse files
alexander.kot@gmail.comfredericDelaporte
authored andcommitted
NHV-117 - Using Validator and Envers together, fix & test
1 parent 16f1dde commit 2d7b80e

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System.Collections;
2+
3+
using NHibernate.Cfg;
4+
using NHibernate.Envers.Configuration.Fluent;
5+
using NHibernate.Validator.Cfg;
6+
using NHibernate.Validator.Engine;
7+
using NHibernate.Validator.Event;
8+
using NHibernate.Validator.Exceptions;
9+
using NHibernate.Validator.Tests.Base;
10+
11+
using NUnit.Framework;
12+
using SharpTestsEx;
13+
14+
namespace NHibernate.Validator.Tests.Integration
15+
{
16+
class EnversIntegrationFixture : PersistenceTest
17+
{
18+
protected override IList Mappings
19+
{
20+
get
21+
{
22+
return new string[]
23+
{
24+
"Integration.Address.hbm.xml",
25+
};
26+
}
27+
}
28+
29+
protected ISharedEngineProvider fortest;
30+
protected override void Configure(NHibernate.Cfg.Configuration configuration)
31+
{
32+
// The ValidatorInitializer and the ValidateEventListener share the same engine
33+
34+
// Initialize the SharedEngine
35+
fortest = new NHibernateSharedEngineProvider();
36+
Cfg.Environment.SharedEngineProvider = fortest;
37+
ValidatorEngine ve = Cfg.Environment.SharedEngineProvider.GetEngine();
38+
ve.Clear();
39+
40+
XmlConfiguration nhvc = new XmlConfiguration();
41+
nhvc.Properties[Cfg.Environment.ApplyToDDL] = "true";
42+
nhvc.Properties[Cfg.Environment.AutoregisterListeners] = "true";
43+
nhvc.Properties[Cfg.Environment.ValidatorMode] = "UseAttribute";
44+
nhvc.Properties[Cfg.Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName;
45+
46+
var enversConf = new FluentConfiguration();
47+
enversConf.Audit<Address>();
48+
configuration.IntegrateWithEnvers(enversConf);
49+
50+
ve.Configure(nhvc);
51+
52+
ValidatorInitializer.Initialize(configuration);
53+
}
54+
55+
56+
[Test]
57+
public void ShouldWorkSUIDWithEnversAndValidators()
58+
{
59+
ISession s;
60+
Address.blacklistedZipCode = "3232";
61+
62+
// Don't throw exception if it is valid
63+
Address a = new Address();
64+
65+
a.Id = 13;
66+
a.Country = "Country";
67+
a.Line1 = "Line 1";
68+
a.Zip = "4343";
69+
a.State = "NY";
70+
try
71+
{
72+
using (s = OpenSession())
73+
using (ITransaction t = s.BeginTransaction())
74+
{
75+
s.Save(a);
76+
t.Commit();
77+
}
78+
}
79+
catch (InvalidStateException)
80+
{
81+
Assert.Fail("Valid entity cause InvalidStateException");
82+
}
83+
84+
// Update check
85+
try
86+
{
87+
using (s = OpenSession())
88+
using (ITransaction t = s.BeginTransaction())
89+
{
90+
Address saved = s.Get<Address>(13L);
91+
saved.State = "TOOLONG";
92+
s.Update(saved);
93+
t.Commit();
94+
Assert.Fail("entity should have been validated");
95+
}
96+
}
97+
catch (InvalidStateException e)
98+
{
99+
e.GetInvalidValues().Should().Not.Be.Empty();
100+
}
101+
102+
try
103+
{
104+
using (s = OpenSession())
105+
using (ITransaction t = s.BeginTransaction())
106+
{
107+
Address saved = s.Get<Address>(13L);
108+
a.Zip = "1234";
109+
saved.State = "BO";
110+
s.Update(saved);
111+
t.Commit();
112+
}
113+
}
114+
catch (InvalidStateException)
115+
{
116+
Assert.Fail("Valid entity cause InvalidStateException");
117+
}
118+
119+
120+
// clean up
121+
using (s = OpenSession())
122+
{
123+
using (ITransaction t = s.BeginTransaction())
124+
{
125+
Address saved = s.Get<Address>(13L);
126+
s.Delete(saved);
127+
t.Commit();
128+
}
129+
130+
var c = s.Connection.CreateCommand();
131+
c.CommandText = "DELETE FROM Address_AUD; DELETE FROM REVINFO;";
132+
c.ExecuteScalar();
133+
134+
}
135+
}
136+
}
137+
}

src/NHibernate.Validator.Tests/NHibernate.Validator.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
<HintPath>..\packages\NHibernate.5.0.0\lib\net461\NHibernate.dll</HintPath>
7373
<Private>True</Private>
7474
</Reference>
75+
<Reference Include="NHibernate.Envers, Version=5.0.1.0, Culture=neutral, PublicKeyToken=e2c5b946037fb7f8, processorArchitecture=MSIL">
76+
<HintPath>..\packages\NHibernate.Envers.5.0.1\lib\net461\NHibernate.Envers.dll</HintPath>
77+
<Private>True</Private>
78+
</Reference>
7579
<Reference Include="nunit.framework, Version=3.8.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
7680
<HintPath>..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll</HintPath>
7781
<Private>True</Private>
@@ -178,6 +182,7 @@
178182
<Compile Include="GraphNavigation\User.cs" />
179183
<Compile Include="GraphNavigation\Address.cs" />
180184
<Compile Include="Inheritance\LoquaciousInheritanceFixture.cs" />
185+
<Compile Include="Integration\EnversIntegrationFixture.cs" />
181186
<Compile Include="Integration\IntegrationOwnEngineFixture.cs" />
182187
<Compile Include="Integration\SimpleWithRelation.cs" />
183188
<Compile Include="Integration\ValidateEventsFixture.cs" />

src/NHibernate.Validator.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<package id="Iesi.Collections" version="4.0.2" targetFramework="net461" />
55
<package id="log4net" version="2.0.8" targetFramework="net40" />
66
<package id="NHibernate" version="5.0.0" targetFramework="net461" />
7+
<package id="NHibernate.Envers" version="5.0.1" targetFramework="net461" />
78
<package id="NUnit" version="3.8.1" targetFramework="net45" />
89
<package id="Remotion.Linq" version="2.1.2" targetFramework="net461" />
910
<package id="Remotion.Linq.EagerFetching" version="2.1.0" targetFramework="net461" />

src/NHibernate.Validator/Cfg/ValidatorInitializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ private static void ApplyValidatorToDDL(PersistentClass persistentClass, Validat
8888
{
8989
try
9090
{
91+
if (!persistentClass.HasPocoRepresentation)
92+
return;
9193
IClassValidator classValidator = ve.GetClassValidator(persistentClass.MappedClass);
9294
classValidator.Apply(persistentClass);
9395
}

src/NHibernate.Validator/Event/ValidatePreInsertEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Initialize(Configuration cfg)
4646
Engine = Environment.SharedEngineProvider.GetEngine();
4747
}
4848

49-
IEnumerable<PersistentClass> classes = cfg.ClassMappings.Where(clazz => clazz.ClassName != null).Where(clazz => !clazz.ClassName.StartsWith("NHibernate.Envers"));
49+
var classes = cfg.ClassMappings.Where(clazz => clazz.HasPocoRepresentation);
5050

5151
foreach (PersistentClass clazz in classes)
5252
{

0 commit comments

Comments
 (0)