1
+ using System ;
2
+ using System . Linq ;
1
3
using NHibernate . Cfg . MappingSchema ;
2
4
using NHibernate . Criterion ;
3
5
using NHibernate . Dialect ;
4
6
using NHibernate . Driver ;
5
7
using NHibernate . Exceptions ;
8
+ using NHibernate . Linq ;
6
9
using NHibernate . Mapping . ByCode ;
7
10
using NUnit . Framework ;
8
11
@@ -38,16 +41,22 @@ protected override HbmMapping GetMappings()
38
41
39
42
40
43
[ Test ]
41
- public void NhThrowsOnTooLong ( )
44
+ [ Description ( "Values longer than the maximum possible string length " +
45
+ "should raise an exception if they would otherwise be truncated." ) ]
46
+ public void ShouldPreventInsertionOfVeryLongStringThatWouldBeTruncated ( )
42
47
{
43
- if ( ! ( Dialect is MsSql2008Dialect ) )
44
- Assert . Ignore (
45
- "This test only works (and is only relevant) " +
46
- "where the driver has set an explicit length " +
47
- "on the IDbDataParameter." ) ;
48
+ // This test case is for when the current driver will use a parameter size
49
+ // that is significantly larger than the mapped column size (e.g. SqlClientDriver currently).
50
+
51
+ // Note: This test could possible be written as
52
+ // "database must raise an error OR it must store and return the full value"
53
+ // to avoid this dialect specific exception.
54
+ if ( Dialect is SQLiteDialect )
55
+ Assert . Ignore ( "SQLite does not enforce specified string lengths." ) ;
48
56
49
57
int maxStringLength = GetLongStringMappedLength ( ) ;
50
- PropertyValueException ex = Assert . Throws < PropertyValueException > (
58
+
59
+ var ex = Assert . Catch < Exception > (
51
60
( ) =>
52
61
{
53
62
using ( ISession s = OpenSession ( ) )
@@ -58,21 +67,21 @@ public void NhThrowsOnTooLong()
58
67
}
59
68
} ) ;
60
69
61
- Assert . That ( ex . Message , Is . EqualTo ( "Error dehydrating property value for NHibernate.Test.TypesTest.StringClass.LongStringValue" ) ) ;
62
- Assert . That ( ex . InnerException , Is . TypeOf < HibernateException > ( ) ) ;
63
- Assert . That ( ex . InnerException . Message , Is . EqualTo ( "The length of the string value exceeds the length configured in the mapping/parameter." ) ) ;
70
+ AssertFailedInsertExceptionDetailsAndEmptyTable ( ex ) ;
64
71
}
65
72
66
73
[ Test ]
67
- public void DbThrowsOnTooLong ( )
74
+ [ Description ( "Values longer than the mapped string length " +
75
+ "should raise an exception if they would otherwise be truncated." ) ]
76
+ public void ShouldPreventInsertionOfTooLongStringThatWouldBeTruncated ( )
68
77
{
69
78
// Note: This test could possible be written as
70
79
// "database must raise an error OR it must store and return the full value"
71
80
// to avoid this dialect specific exception.
72
81
if ( Dialect is SQLiteDialect )
73
82
Assert . Ignore ( "SQLite does not enforce specified string lengths." ) ;
74
83
75
- Assert . Throws < GenericADOException > (
84
+ var ex = Assert . Catch < Exception > (
76
85
( ) =>
77
86
{
78
87
using ( ISession s = OpenSession ( ) )
@@ -82,9 +91,45 @@ public void DbThrowsOnTooLong()
82
91
s . Flush ( ) ;
83
92
}
84
93
} ,
85
- "Database did not throw an error when trying to put too large a value into a column." ) ;
94
+ "An exception was expected when trying to put too large a value into a column." ) ;
95
+
96
+ AssertFailedInsertExceptionDetailsAndEmptyTable ( ex ) ;
97
+ }
98
+
99
+ private void AssertFailedInsertExceptionDetailsAndEmptyTable ( Exception ex )
100
+ {
101
+ // We can get different sort of exceptions.
102
+ if ( ex is PropertyValueException )
103
+ {
104
+ // Some drivers/dialects set explicit parameter sizes, in which case we expect NH to
105
+ // raise a PropertyValueException (to avoid ADO.NET from silently truncating).
106
+
107
+ Assert . That (
108
+ ex . Message ,
109
+ Is . StringStarting ( "Error dehydrating property value for NHibernate.Test.TypesTest.StringClass." ) ) ;
110
+
111
+ Assert . That ( ex . InnerException , Is . TypeOf < HibernateException > ( ) ) ;
112
+
113
+ Assert . That (
114
+ ex . InnerException . Message ,
115
+ Is . EqualTo ( "The length of the string value exceeds the length configured in the mapping/parameter." ) ) ;
116
+ }
117
+ else
118
+ {
119
+ // In other cases, we expect the database itself to raise an error. This case
120
+ // will also happen if the driver does set an explicit parameter size, but that
121
+ // size is larger than the mapped column size.
122
+ Assert . That ( ex , Is . TypeOf < GenericADOException > ( ) ) ;
123
+ }
124
+
125
+ // In any case, nothing should have been inserted.
126
+ using ( ISession s = OpenSession ( ) )
127
+ {
128
+ Assert . That ( s . Query < StringClass > ( ) . ToList ( ) , Is . Empty ) ;
129
+ }
86
130
}
87
131
132
+
88
133
[ Test ]
89
134
public void CriteriaLikeParameterCanExceedColumnSize ( )
90
135
{
0 commit comments