Skip to content

Commit c6003f0

Browse files
ngbrownoskarb
authored andcommitted
NH-3900 - Replace ExpectedExceptionAttribute with Assert delegate.
(With some formatting cleanup but OskarB.)
1 parent 16c1459 commit c6003f0

File tree

6 files changed

+98
-69
lines changed

6 files changed

+98
-69
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
23

34
namespace NHibernate.Test
45
{
5-
public class KnownBugAttribute : ExpectedExceptionAttribute
6+
public class KnownBugAttribute : IgnoreAttribute
67
{
8+
public string UserMessage { get; set; }
9+
710
public KnownBugAttribute(string bug)
11+
: base("Known bug " + bug)
812
{
913
UserMessage = "Known bug " + bug;
1014
}
1115

12-
public KnownBugAttribute(string bug, System.Type exceptionType)
13-
: base(exceptionType)
16+
public KnownBugAttribute(string bug, System.Type exceptionType)
17+
: base("Known bug " + bug)
1418
{
1519
UserMessage = "Known bug " + bug;
1620
}
1721

1822
public KnownBugAttribute(string bug, string exceptionName)
19-
: base(exceptionName)
23+
: base("Known bug " + bug)
2024
{
2125
UserMessage = "Known bug " + bug;
2226
}
2327
}
24-
}
28+
}

src/NHibernate.Test/Linq/ByMethod/DistinctTests.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,51 @@ public void DistinctOnTypeProjectionWithHqlMethodIsOk()
9898
}
9999

100100
[Test]
101-
[ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Cannot use distinct on result that depends on methods for which no SQL equivalent exist.")]
102101
public void DistinctOnTypeProjectionWithCustomProjectionMethodsIsBlocked1()
103102
{
104103
// Sort of related to NH-2645.
105104

106-
OrderDto[] result = db.Orders
107-
.Select(x => new OrderDto
108-
{
109-
ShippingDate = Transform(x.ShippingDate),
110-
OrderDate = Transform(x.OrderDate)
111-
})
112-
.Distinct()
113-
.ToArray();
105+
Assert.That(
106+
() =>
107+
{
108+
OrderDto[] result = db.Orders
109+
.Select(
110+
x => new OrderDto
111+
{
112+
ShippingDate = Transform(x.ShippingDate),
113+
OrderDate = Transform(x.OrderDate)
114+
})
115+
.Distinct()
116+
.ToArray();
117+
},
118+
Throws.TypeOf<NotSupportedException>()
119+
.And.Message.EqualTo(
120+
"Cannot use distinct on result that depends on methods for which no SQL equivalent exist."));
114121
}
115122

116123

117124
[Test]
118-
[ExpectedException(typeof(NotSupportedException), ExpectedMessage = "Cannot use distinct on result that depends on methods for which no SQL equivalent exist.")]
119125
public void DistinctOnTypeProjectionWithCustomProjectionMethodsIsBlocked2()
120126
{
121127
// Sort of related to NH-2645.
122128

123-
OrderDto[] result = db.Orders
124-
.Select(x => new OrderDto
129+
Assert.That(
130+
() =>
125131
{
126-
ShippingDate = x.ShippingDate,
127-
OrderDate = x.OrderDate.Value.AddMonths(5), // As of 2012-01-25, AddMonths() is executed locally.
128-
})
129-
.Distinct()
130-
.ToArray();
132+
OrderDto[] result = db.Orders
133+
.Select(
134+
x => new OrderDto
135+
{
136+
ShippingDate = x.ShippingDate,
137+
// As of 2012-01-25, AddMonths() is executed locally.
138+
OrderDate = x.OrderDate.Value.AddMonths(5),
139+
})
140+
.Distinct()
141+
.ToArray();
142+
},
143+
Throws.TypeOf<NotSupportedException>()
144+
.And.Message.EqualTo(
145+
"Cannot use distinct on result that depends on methods for which no SQL equivalent exist."));
131146
}
132147
}
133-
}
148+
}

src/NHibernate.Test/Linq/ByMethod/SumTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ namespace NHibernate.Test.Linq.ByMethod
77
public class SumTests : LinqTestCase
88
{
99
[Test]
10-
[ExpectedException]
1110
public void EmptySumDecimal()
1211
{
13-
db.OrderLines.Where(ol => false).Sum(ol => ol.Discount);
12+
Assert.That(
13+
() =>
14+
{
15+
db.OrderLines.Where(ol => false).Sum(ol => ol.Discount);
16+
},
17+
Throws.InstanceOf<HibernateException>());
1418
}
1519

1620
[Test]
@@ -48,4 +52,4 @@ public void SumSingle()
4852
Assert.Greater(total, 0);
4953
}
5054
}
51-
}
55+
}

src/NHibernate.Test/NHSpecificTest/NH2065/Fixture.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
21
using System.Collections.Generic;
3-
using System.Data;
4-
using System.Transactions;
5-
using NHibernate.Impl;
62
using NUnit.Framework;
7-
using NHibernate.Criterion;
3+
84

95
namespace NHibernate.Test.NHSpecificTest.NH2065
106
{
@@ -40,28 +36,32 @@ protected override void OnTearDown()
4036
}
4137

4238
[Test]
43-
[ExpectedException(
44-
ExpectedException=typeof(HibernateException),
45-
ExpectedMessage="reassociated object has dirty collection: NHibernate.Test.NHSpecificTest.NH2065.Person.Children")]
4639
public void GetGoodErrorForDirtyReassociatedCollection()
4740
{
48-
Person person;
49-
using (var s = OpenSession())
50-
using (s.BeginTransaction())
51-
{
52-
person = s.Get<Person>(1);
53-
NHibernateUtil.Initialize(person.Children);
54-
s.Transaction.Commit();
55-
}
41+
Person person;
42+
using (var s = OpenSession())
43+
using (s.BeginTransaction())
44+
{
45+
person = s.Get<Person>(1);
46+
NHibernateUtil.Initialize(person.Children);
47+
s.Transaction.Commit();
48+
}
5649

57-
person.Children.Clear();
50+
person.Children.Clear();
5851

59-
using (var s = OpenSession())
60-
using (s.BeginTransaction())
61-
{
62-
s.Lock(person, LockMode.None);
63-
}
64-
}
52+
using (var s = OpenSession())
53+
using (s.BeginTransaction())
54+
{
55+
Assert.That(
56+
() =>
57+
{
58+
s.Lock(person, LockMode.None);
59+
},
60+
Throws.TypeOf<HibernateException>()
61+
.And.Message.EqualTo(
62+
"reassociated object has dirty collection: NHibernate.Test.NHSpecificTest.NH2065.Person.Children"));
63+
}
64+
}
6565

6666
}
6767
}
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections;
3-
using System.Reflection;
1+
using System.Reflection;
42
using NHibernate.Cfg;
53
using NUnit.Framework;
64

@@ -10,33 +8,36 @@ namespace NHibernate.Test.NHSpecificTest.NH2297
108
public class Fixture // Purposefully doesn't inherit from BugTestCase
119
{
1210
[TestCase(".MappingsNames.hbm.xml",
13-
ExpectedException = typeof (InvalidOperationException),
14-
ExpectedMessage =
15-
"ICompositeUserType NHibernate.Test.NHSpecificTest.NH2297.InvalidNamesCustomCompositeUserType returned a null value for 'PropertyNames'."
16-
)]
11+
"ICompositeUserType NHibernate.Test.NHSpecificTest.NH2297.InvalidNamesCustomCompositeUserType returned a null value for 'PropertyNames'."
12+
)]
1713
[TestCase(".MappingsTypes.hbm.xml",
18-
ExpectedException = typeof (InvalidOperationException),
19-
ExpectedMessage =
20-
"ICompositeUserType NHibernate.Test.NHSpecificTest.NH2297.InvalidTypesCustomCompositeUserType returned a null value for 'PropertyTypes'."
21-
)]
22-
public void InvalidCustomCompositeUserTypeThrowsMeaningfulException(string mappingFile)
14+
"ICompositeUserType NHibernate.Test.NHSpecificTest.NH2297.InvalidTypesCustomCompositeUserType returned a null value for 'PropertyTypes'."
15+
)]
16+
public void InvalidCustomCompositeUserTypeThrowsMeaningfulException(
17+
string mappingFile,
18+
string expectedExceptionMessage)
2319
{
2420
var cfg = new Configuration();
2521

2622
if (TestConfigurationHelper.hibernateConfigFile != null)
2723
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
2824

29-
const string MappingsAssembly = "NHibernate.Test";
25+
const string mappingsAssembly = "NHibernate.Test";
3026

31-
Assembly assembly = Assembly.Load(MappingsAssembly);
27+
Assembly assembly = Assembly.Load(mappingsAssembly);
3228

3329
string ns = GetType().Namespace;
3430
string bugNumber = ns.Substring(ns.LastIndexOf('.') + 1);
3531

36-
cfg.AddResource(MappingsAssembly + "." + "NHSpecificTest." + bugNumber + mappingFile, assembly);
32+
cfg.AddResource(mappingsAssembly + "." + "NHSpecificTest." + bugNumber + mappingFile, assembly);
3733

3834
// build session factory creates the invalid custom type mapper, and throws the exception
39-
cfg.BuildSessionFactory();
35+
Assert.That(
36+
() =>
37+
{
38+
cfg.BuildSessionFactory();
39+
},
40+
Throws.InvalidOperationException.And.Message.EqualTo(expectedExceptionMessage));
4041
}
4142
}
4243
}

src/NHibernate.Test/UtilityTest/TypeNameParserFixture.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ public void ParseComplexGenericTypeName()
5959
CheckInput(typeName, expectedTypeName, null);
6060
}
6161

62-
[Test, Ignore("Not a big problem because the next type request will throw the exception"), ExpectedException(typeof(ParserException))]
62+
[Test, Ignore("Not a big problem because the next type request will throw the exception")]
6363
public void ParseUnmatchedBracket()
6464
{
65-
TypeNameParser.Parse("SomeName[");
65+
Assert.That(
66+
() =>
67+
{
68+
TypeNameParser.Parse("SomeName[");
69+
},
70+
Throws.TypeOf<ParserException>());
6671
}
6772

6873
[Test]
@@ -295,4 +300,4 @@ public void ComplexNestedWithGeneric()
295300
CheckInput(typeof(Aa<int>.Bb<int, short, string>.C).FullName, typeof(Aa<int>.Bb<int, short, string>.C).FullName, null);
296301
}
297302
}
298-
}
303+
}

0 commit comments

Comments
 (0)