1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Tests
.Config
19 using Castle
.ActiveRecord
.Framework
;
20 using Castle
.ActiveRecord
.Framework
.Config
;
21 using Castle
.ActiveRecord
.Framework
.Scopes
;
22 using NUnit
.Framework
;
25 public class ConfigurationSource
28 public void TestLoadDefaultThreadScopeConfig()
30 String xmlConfig
= @"<activerecord>" + GetDefaultHibernateConfigAndCloseActiveRecordSection();
32 //null == use default. == typeof(ThreadScopeInfo);
33 Type expectedType
= null;
35 AssertConfig(xmlConfig
, expectedType
, null);
40 public void TestLoadWebThreadScopeInfo()
42 String xmlConfig
= @"<activerecord isWeb=""true"">" + GetDefaultHibernateConfigAndCloseActiveRecordSection();
44 Type expectedType
= typeof(WebThreadScopeInfo
);
46 AssertConfig(xmlConfig
, expectedType
, null);
51 public void TestDefaultSessionFactoryHolder()
53 String xmlConfig
= @"<activerecord isWeb=""true"">" + GetDefaultHibernateConfigAndCloseActiveRecordSection();
55 Type sfh
= typeof(SessionFactoryHolder
);
57 AssertConfig(xmlConfig
, null, sfh
);
62 public void TestCustomSessionFactoryholder()
65 @"<activerecord isWeb=""true"" sessionfactoryholdertype=""Castle.ActiveRecord.Tests.Config.MySessionFactoryHolder, Castle.ActiveRecord.Tests"">"
66 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
68 Type sfh
= typeof(MySessionFactoryHolder
);
70 AssertConfig(xmlConfig
, null, sfh
);
75 public void TestDebug()
78 @"<activerecord isDebug=""true"" isWeb=""true"" sessionfactoryholdertype=""Castle.ActiveRecord.Tests.Config.MySessionFactoryHolder, Castle.ActiveRecord.Tests"">"
79 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
81 AssertConfig(xmlConfig
, null, null, true, false, false);
85 public void TestPluralizeTableNames()
88 @"<activerecord pluralizeTableNames=""true"">"
89 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
91 AssertConfig(xmlConfig1
, null, null, false, true, false);
94 @"<activerecord pluralizeTableNames=""false"">"
95 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
97 AssertConfig(xmlConfig2
, null, null, false, false, false);
101 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
103 AssertConfig(xmlConfig3
, null, null, false, false, false);
107 public void TestVerifyModelsAgainstDBSchema()
110 @"<activerecord verifyModelsAgainstDBSchema=""true"">"
111 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
113 AssertConfig(xmlConfig1
, null, null, false, false, true);
116 @"<activerecord verifyModelsAgainstDBSchema=""false"">"
117 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
119 AssertConfig(xmlConfig2
, null, null, false, false, false);
123 + GetDefaultHibernateConfigAndCloseActiveRecordSection();
125 AssertConfig(xmlConfig3
, null, null, false, false, false);
129 private static void AssertConfig(string xmlConfig
, Type webinfotype
, Type sessionFactoryHolderType
)
131 AssertConfig(xmlConfig
, webinfotype
, sessionFactoryHolderType
, false, false, false);
134 private static void AssertConfig(string xmlConfig
, Type webinfotype
, Type sessionFactoryHolderType
, bool isDebug
,
135 bool pluralize
, bool verifyModelsAgainstDBSchema
)
137 StringReader sr
= new StringReader(xmlConfig
);
139 XmlConfigurationSource c
= new XmlConfigurationSource(sr
);
141 if (null != webinfotype
)
143 Assert
.IsTrue(c
.ThreadScopeInfoImplementation
== webinfotype
,
144 "Expected {0}, Got {1}", webinfotype
, c
.ThreadScopeInfoImplementation
);
147 if (null != sessionFactoryHolderType
)
149 Assert
.IsTrue(c
.SessionFactoryHolderImplementation
== sessionFactoryHolderType
,
150 "Expected {0}, Got {1}", sessionFactoryHolderType
, c
.SessionFactoryHolderImplementation
);
153 Assert
.IsTrue(c
.Debug
== isDebug
);
154 Assert
.IsTrue(c
.PluralizeTableNames
== pluralize
);
155 Assert
.IsTrue(c
.VerifyModelsAgainstDBSchema
== verifyModelsAgainstDBSchema
);
158 private static string GetDefaultHibernateConfigAndCloseActiveRecordSection()
161 <add key=""connection.driver_class"" value=""NHibernate.Driver.SqlClientDriver"" />
162 <add key=""dialect"" value=""NHibernate.Dialect.MsSql2000Dialect"" />
163 <add key=""connection.provider"" value=""NHibernate.Connection.DriverConnectionProvider"" />
164 <add key=""connection.connection_string"" value=""Data Source=.;Initial Catalog=test;Integrated Security=True;Pooling=False"" />
171 public class MySessionFactoryHolder
: SessionFactoryHolder