Skip to content

Commit 3b20e5b

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH3530: Add locale specific tests to check for invalid type conversions.
1 parent 705d794 commit 3b20e5b

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH3530
8+
{
9+
public class LocaleEntity
10+
{
11+
public virtual Guid Id { get; set; }
12+
public virtual int IntegerValue { get; set; }
13+
public virtual DateTime DateTimeValue { get; set; }
14+
public virtual double DoubleValue { get; set; }
15+
public virtual decimal DecimalValue { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using System.Globalization;
8+
using System.Threading;
9+
using System.Web.SessionState;
10+
11+
namespace NHibernate.Test.NHSpecificTest.GH3530
12+
{
13+
[TestFixture]
14+
public class Fixture : BugTestCase
15+
{
16+
private CultureInfo initialCulture;
17+
18+
[OneTimeSetUp]
19+
public void FixtureSetup()
20+
{
21+
initialCulture = CurrentCulture;
22+
}
23+
24+
[OneTimeTearDown]
25+
public void FixtureTearDown()
26+
{
27+
CurrentCulture = initialCulture;
28+
}
29+
30+
protected override void OnTearDown()
31+
{
32+
using (var session = OpenSession())
33+
using (var transaction = session.BeginTransaction())
34+
{
35+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
36+
37+
transaction.Commit();
38+
}
39+
}
40+
41+
[TestCaseSource(nameof(GenerateAllCultureCombinations))]
42+
public void TestLocales(CultureInfo from, CultureInfo to)
43+
{
44+
DateTime leapDay = new DateTime(2024, 2, 29, new GregorianCalendar(GregorianCalendarTypes.USEnglish));
45+
double doubleValue = 12.3f;
46+
int intValue = 4;
47+
object id;
48+
49+
CurrentCulture = from;
50+
using (var session = OpenSession())
51+
using (var tx = session.BeginTransaction())
52+
{
53+
var entity = new LocaleEntity()
54+
{
55+
DateTimeValue = leapDay,
56+
DoubleValue = doubleValue,
57+
IntegerValue = intValue,
58+
};
59+
60+
id = session.Save(entity);
61+
tx.Commit();
62+
}
63+
64+
CurrentCulture = to;
65+
using (var session = OpenSession())
66+
using (var tx = session.BeginTransaction())
67+
{
68+
var entity = session.Get<LocaleEntity>(id);
69+
70+
Assert.AreEqual(leapDay, entity.DateTimeValue);
71+
Assert.AreEqual(intValue, entity.IntegerValue);
72+
Assert.True(doubleValue - entity.DoubleValue < double.Epsilon);
73+
}
74+
}
75+
76+
private CultureInfo CurrentCulture
77+
{
78+
get
79+
{
80+
return Thread.CurrentThread.CurrentCulture;
81+
}
82+
set
83+
{
84+
Thread.CurrentThread.CurrentCulture = value;
85+
}
86+
}
87+
88+
public static IEnumerable<object> GenerateAllCultureCombinations()
89+
{
90+
var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
91+
92+
for (var i = 0; i < cultures.Length; i++)
93+
{
94+
for (var j = 0; j < cultures.Length; j++)
95+
{
96+
if (i == j) continue;
97+
98+
yield return new object[]
99+
{
100+
cultures[i],
101+
cultures[j]
102+
};
103+
}
104+
}
105+
}
106+
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH3530">
4+
<class name="LocaleEntity">
5+
<id name="Id" generator="guid.comb" />
6+
<property name="IntegerValue" column="IntegerValue"/>
7+
<property name="DateTimeValue" column="DateTimeValue"/>
8+
<property name="DoubleValue" column="DoubleValue"/>
9+
</class>
10+
</hibernate-mapping>

0 commit comments

Comments
 (0)