-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathSQLExceptionConversionTest.cs
151 lines (135 loc) · 4.1 KB
/
SQLExceptionConversionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections;
using System.Data;
using NHibernate.Dialect;
using NHibernate.Exceptions;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.ExceptionsTest
{
[TestFixture]
public class SQLExceptionConversionTest : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override IList Mappings
{
get { return new[] { "ExceptionsTest.User.hbm.xml", "ExceptionsTest.Group.hbm.xml" }; }
}
protected override void Configure(Cfg.Configuration configuration)
{
if(Dialect is MsSql2000Dialect)
{
configuration.SetProperty(Cfg.Environment.SqlExceptionConverter,
typeof (MSSQLExceptionConverterExample).AssemblyQualifiedName);
}
if (Dialect is Oracle8iDialect)
{
configuration.SetProperty(Cfg.Environment.SqlExceptionConverter,
typeof(OracleClientExceptionConverterExample).AssemblyQualifiedName);
}
if (Dialect is PostgreSQLDialect)
{
configuration.SetProperty(Cfg.Environment.SqlExceptionConverter,
typeof(PostgresExceptionConverterExample).AssemblyQualifiedName);
}
}
[Test]
public void IntegrityViolation()
{
//ISQLExceptionConverter converter = Dialect.BuildSQLExceptionConverter();
ISQLExceptionConverter converter = sessions.Settings.SqlExceptionConverter;
ISession session = OpenSession();
session.BeginTransaction();
IDbConnection connection = session.Connection;
// Attempt to insert some bad values into the T_MEMBERSHIP table that should
// result in a constraint violation
IDbCommand ps = null;
try
{
ps = connection.CreateCommand();
ps.CommandType = CommandType.Text;
ps.CommandText = "INSERT INTO T_MEMBERSHIP (user_id, group_id) VALUES (@p1, @p2)";
IDbDataParameter pr = ps.CreateParameter();
pr.ParameterName = "p1";
pr.DbType = DbType.Int64;
pr.Value = 52134241L; // Non-existent user_id
ps.Parameters.Add(pr);
pr = ps.CreateParameter();
pr.ParameterName = "p2";
pr.DbType = DbType.Int64;
pr.Value = 5342L; // Non-existent group_id
ps.Parameters.Add(pr);
session.Transaction.Enlist(ps);
ps.ExecuteNonQuery();
Assert.Fail("INSERT should have failed");
}
catch (Exception sqle)
{
ADOExceptionReporter.LogExceptions(sqle, "Just output!!!!");
Exception adoException = converter.Convert(new AdoExceptionContextInfo{SqlException = sqle});
Assert.AreEqual(typeof(ConstraintViolationException), adoException.GetType(),
"Bad conversion [" + sqle.Message + "]");
ConstraintViolationException ex = (ConstraintViolationException)adoException;
Console.WriteLine("Violated constraint name: " + ex.ConstraintName);
}
finally
{
if (ps != null)
{
try
{
ps.Dispose();
}
catch (Exception)
{
// ignore...
}
}
}
session.Transaction.Rollback();
session.Close();
}
[Test]
public void BadGrammar()
{
//ISQLExceptionConverter converter = Dialect.BuildSQLExceptionConverter();
ISQLExceptionConverter converter = sessions.Settings.SqlExceptionConverter;
ISession session = OpenSession();
IDbConnection connection = session.Connection;
// prepare/execute a query against a non-existent table
IDbCommand ps = null;
try
{
ps = connection.CreateCommand();
ps.CommandType = CommandType.Text;
ps.CommandText = "SELECT user_id, user_name FROM tbl_no_there";
ps.ExecuteNonQuery();
Assert.Fail("SQL compilation should have failed");
}
catch (Exception sqle)
{
Assert.AreEqual(typeof (SQLGrammarException),
converter.Convert(new AdoExceptionContextInfo {SqlException = sqle}).GetType(),
"Bad conversion [" + sqle.Message + "]");
}
finally
{
if (ps != null)
{
try
{
ps.Dispose();
}
catch (Exception)
{
// ignore...
}
}
}
session.Close();
}
}
}