1: namespace ActiveRecordTestHelper
2: {
3: public class ActiveRecordMockConnectionProvider : NHibernate.Connection.DriverConnectionProvider
4: {
5: private static IDbConnection _connection;
6:
7: public override IDbConnection GetConnection()
8: {
9: if (_connection == null)
10: _connection = base.GetConnection();
11: return _connection;
12: }
13:
14: public override void CloseConnection(IDbConnection conn)
15: {
16: }
17:
18: /// <summary>
19: /// Destroys the connection that is kept open in order to keep the in-memory database alive. Destroying
20: /// the connection will destroy all of the data stored in the mock database. Call this method when the
21: /// test is complete.
22: /// </summary>
23: public static void ExplicitlyDestroyConnection()
24: {
25: if (_connection != null)
26: {
27: _connection.Close();
28: _connection = null;
29: }
30: }
31:
32: /// <summary>
33: /// Initializes ActiveRecord and the Database that ActiveRecord uses to store the data. Call this method
34: /// before the test executes.
35: /// </summary>
36: /// <param name="types">A list of ActiveRecord types that will be created in the database</param>
37: public static void InitializeActiveRecord(params Type[] types)
38: {
39: ActiveRecordStarter.ResetInitializationFlag();
40: ActiveRecordStarter.Initialize(GetMockConfiguration(), types);
41: ActiveRecordStarter.CreateSchema();
42: }
43:
44: private static IConfigurationSource GetMockConfiguration()
45: {
46: var properties = new Hashtable{
47: {"hibernate.connection.driver_class", "NHibernate.Driver.SQLite20Driver"},
48: {"hibernate.dialect", "NHibernate.Dialect.SQLiteDialect"},
49: {"hibernate.connection.provider", ConnectionProviderLocator},
50: {"hibernate.connection.connection_string", "Data Source=:memory:;Version=3;New=True;"}};
51:
52: var source = new InPlaceConfigurationSource();
53: source.Add(typeof(ActiveRecordBase), properties);
54: return source;
55: }
56:
57: private static string ConnectionProviderLocator
58: {
59: get { return String.Format("{0}, {1}", TypeOfEnclosingClass.FullName, EnclosingAssemblyName.Split(',')[0]); }
60: }
61:
62: private static Type TypeOfEnclosingClass
63: {
64: get { return MethodInfo.GetCurrentMethod().DeclaringType; }
65: }
66:
67: private static string EnclosingAssemblyName
68: {
69: get { return Assembly.GetAssembly(TypeOfEnclosingClass).FullName; }
70: }
71: }
72: }