#region Apache Notice /***************************************************************************** * $Revision$ * $LastChangedDate$ * $LastChangedBy$ * * iBATIS.NET Data Mapper * Copyright (C) 2005, 2006 The Apache Software Foundation * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion #if dotnet2 using System; using System.Collections; using IBatisNet.Common; using IBatisNet.Common.Exceptions; using IBatisNet.Common.Utilities; using IBatisNet.DataMapper.MappedStatements; using IBatisNet.DataMapper.Test.Domain; using System.Collections.Generic; using NUnit.Framework; namespace IBatisNet.DataMapper.Test.NUnit.SqlMapTests.Generics { [TestFixture] public class StatementTest : BaseTest { #region SetUp & TearDown /// /// SetUp /// [SetUp] public void Init() { InitScript(sqlMap.DataSource, ScriptDirectory + "account-init.sql"); InitScript(sqlMap.DataSource, ScriptDirectory + "order-init.sql"); InitScript(sqlMap.DataSource, ScriptDirectory + "line-item-init.sql"); InitScript(sqlMap.DataSource, ScriptDirectory + "enumeration-init.sql"); InitScript(sqlMap.DataSource, ScriptDirectory + "other-init.sql"); } /// /// TearDown /// [TearDown] public void Dispose() { /* ... */ } #endregion #region Object Query tests /// /// Test Open connection with a connection string /// [Test] public void TestOpenConnection() { sqlMap.OpenConnection(sqlMap.DataSource.ConnectionString); Account account = sqlMap.QueryForObject("SelectWithProperty", null); sqlMap.CloseConnection(); AssertAccount1(account); } /// /// Test use a statement with property subtitution /// (JIRA 22) /// [Test] public void TestSelectWithProperty() { Account account = sqlMap.QueryForObject("SelectWithProperty", null); AssertAccount1(account); } /// /// Test ExecuteQueryForObject Via ColumnName /// [Test] public void TestExecuteQueryForObjectViaColumnName() { Account account = sqlMap.QueryForObject("GetAccountViaColumnName", 1); AssertAccount1(account); } /// /// Test ExecuteQueryForObject Via ColumnIndex /// [Test] public void TestExecuteQueryForObjectViaColumnIndex() { Account account = sqlMap.QueryForObject("GetAccountViaColumnIndex", 1); AssertAccount1(account); } /// /// Test ExecuteQueryForObject Via ResultClass /// [Test] public void TestExecuteQueryForObjectViaResultClass() { Account account = sqlMap.QueryForObject("GetAccountViaResultClass", 1); AssertAccount1(account); } /// /// Test ExecuteQueryForObject With simple ResultClass : string /// [Test] public void TestExecuteQueryForObjectWithSimpleResultClass() { string email = sqlMap.QueryForObject("GetEmailAddressViaResultClass", 1); Assert.AreEqual("Joe.Dalton@somewhere.com", email); } /// /// Test ExecuteQueryForObject With simple ResultMap : string /// [Test] public void TestExecuteQueryForObjectWithSimpleResultMap() { string email = sqlMap.QueryForObject("GetEmailAddressViaResultMap", 1); Assert.AreEqual("Joe.Dalton@somewhere.com", email); } /// /// Test Primitive ReturnValue : System.DateTime /// [Test] public void TestPrimitiveReturnValue() { DateTime CardExpiry = sqlMap.QueryForObject("GetOrderCardExpiryViaResultClass", 1); Assert.AreEqual(new DateTime(2003, 02, 15, 8, 15, 00), CardExpiry); } /// /// Test ExecuteQueryForObject with result object : Account /// [Test] public void TestExecuteQueryForObjectWithResultObject() { Account account = new Account(); Account testAccount = sqlMap.QueryForObject("GetAccountViaColumnName", 1, account); AssertAccount1(account); Assert.IsTrue(account == testAccount); } /// /// Test ExecuteQueryForObject as Hashtable /// [Test] public void TestExecuteQueryForObjectAsHashtable() { Hashtable account = sqlMap.QueryForObject("GetAccountAsHashtable", 1); AssertAccount1AsHashtable(account); } /// /// Test ExecuteQueryForObject as Hashtable ResultClass /// [Test] public void TestExecuteQueryForObjectAsHashtableResultClass() { Hashtable account = sqlMap.QueryForObject("GetAccountAsHashtableResultClass", 1); AssertAccount1AsHashtableForResultClass(account); } /// /// Test ExecuteQueryForObject via Hashtable /// [Test] public void TestExecuteQueryForObjectViaHashtable() { Hashtable param = new Hashtable(); param.Add("LineItem_ID", 4); param.Add("Order_ID", 9); LineItem testItem = sqlMap.QueryForObject("GetSpecificLineItem", param); Assert.IsNotNull(testItem); Assert.AreEqual("TSM-12", testItem.Code); } /// /// Test Query Dynamic Sql Element /// [Test] public void TestQueryDynamicSqlElement() { //IList list = sqlMap.QueryForList("GetDynamicOrderedEmailAddressesViaResultMap", "Account_ID"); IList list = sqlMap.QueryForList("GetDynamicOrderedEmailAddressesViaResultMap", "Account_ID"); Assert.AreEqual("Joe.Dalton@somewhere.com", list[0]); //list = sqlMap.QueryForList("GetDynamicOrderedEmailAddressesViaResultMap", "Account_FirstName"); list = sqlMap.QueryForList("GetDynamicOrderedEmailAddressesViaResultMap", "Account_FirstName"); Assert.AreEqual("Averel.Dalton@somewhere.com", list[0]); } /// /// Test Execute QueryForList With ResultMap With Dynamic Element /// [Test] public void TestExecuteQueryForListWithResultMapWithDynamicElement() { IList list = sqlMap.QueryForList("GetAllAccountsViaResultMapWithDynamicElement", "LIKE"); AssertAccount1(list[0]); Assert.AreEqual(4, list.Count); Assert.AreEqual(1, list[0].Id); Assert.AreEqual(2, list[1].Id); Assert.AreEqual(4, list[2].Id); list = sqlMap.QueryForList("GetAllAccountsViaResultMapWithDynamicElement", "="); Assert.AreEqual(0, list.Count); } /// /// Test Simple Dynamic Substitution /// [Test] [Ignore("No longer supported.")] public void TestSimpleDynamicSubstitution() { string statement = "select" + " Account_ID as Id," + " Account_FirstName as FirstName," + " Account_LastName as LastName," + " Account_Email as EmailAddress" + " from Accounts" + " WHERE Account_ID = #id#"; Hashtable param = new Hashtable(); param.Add("id", 1); param.Add("statement", statement); IList list = sqlMap.QueryForList("SimpleDynamicSubstitution", param); AssertAccount1((Account)list[0]); Assert.AreEqual(1, list.Count); } /// /// Test Get Account Via Inline Parameters /// [Test] public void TestExecuteQueryForObjectViaInlineParameters() { Account account = new Account(); account.Id = 1; Account testAccount = sqlMap.QueryForObject("GetAccountViaInlineParameters", account); AssertAccount1(testAccount); } /// /// Test ExecuteQuery For Object With Enum property /// [Test] public void TestExecuteQueryForObjectWithEnum() { Enumeration enumClass = sqlMap.QueryForObject("GetEnumeration", 1); Assert.AreEqual(enumClass.Day, Days.Sat); Assert.AreEqual(enumClass.Color, Colors.Red); Assert.AreEqual(enumClass.Month, Months.August); enumClass = sqlMap.QueryForObject("GetEnumeration", 3) as Enumeration; Assert.AreEqual(enumClass.Day, Days.Mon); Assert.AreEqual(enumClass.Color, Colors.Blue); Assert.AreEqual(enumClass.Month, Months.September); } #endregion #region List Query tests /// /// Test QueryForList with Hashtable ResultMap /// [Test] public void TestQueryForListWithGeneric() { List accounts = new List(); sqlMap.QueryForList("GetAllAccountsViaResultMap", null, (System.Collections.IList)accounts); AssertAccount1(accounts[0]); Assert.AreEqual(5, accounts.Count); Assert.AreEqual(1, accounts[0].Id); Assert.AreEqual(2, accounts[1].Id); Assert.AreEqual(3, accounts[2].Id); Assert.AreEqual(4, accounts[3].Id); Assert.AreEqual(5, accounts[4].Id); } /// /// Test QueryForList with Hashtable ResultMap /// [Test] public void TestQueryForListWithHashtableResultMap() { IList list = sqlMap.QueryForList("GetAllAccountsAsHashMapViaResultMap", null); AssertAccount1AsHashtable(list[0]); Assert.AreEqual(5, list.Count); Assert.AreEqual(1, list[0]["Id"]); Assert.AreEqual(2, list[1]["Id"]); Assert.AreEqual(3, list[2]["Id"]); Assert.AreEqual(4, list[3]["Id"]); Assert.AreEqual(5, list[4]["Id"]); } /// /// Test QueryForList with Hashtable ResultClass /// [Test] public void TestQueryForListWithHashtableResultClass() { IList list = sqlMap.QueryForList("GetAllAccountsAsHashtableViaResultClass", null); AssertAccount1AsHashtableForResultClass(list[0]); Assert.AreEqual(5, list.Count); Assert.AreEqual(1, list[0][BaseTest.ConvertKey("Id")]); Assert.AreEqual(2, list[1][BaseTest.ConvertKey("Id")]); Assert.AreEqual(3, list[2][BaseTest.ConvertKey("Id")]); Assert.AreEqual(4, list[3][BaseTest.ConvertKey("Id")]); Assert.AreEqual(5, list[4][BaseTest.ConvertKey("Id")]); } /// /// Test QueryForList with IList ResultClass /// [Test] public void TestQueryForListWithIListResultClass() { IList list = sqlMap.QueryForList("GetAllAccountsAsArrayListViaResultClass", null); IList listAccount = list[0]; Assert.AreEqual(1, listAccount[0]); Assert.AreEqual("Joe", listAccount[1]); Assert.AreEqual("Dalton", listAccount[2]); Assert.AreEqual("Joe.Dalton@somewhere.com", listAccount[3]); Assert.AreEqual(5, list.Count); listAccount = (IList)list[0]; Assert.AreEqual(1, listAccount[0]); listAccount = (IList)list[1]; Assert.AreEqual(2, listAccount[0]); listAccount = (IList)list[2]; Assert.AreEqual(3, listAccount[0]); listAccount = (IList)list[3]; Assert.AreEqual(4, listAccount[0]); listAccount = (IList)list[4]; Assert.AreEqual(5, listAccount[0]); } /// /// Test QueryForList With ResultMap, result collection as ArrayList /// [Test] public void TestQueryForListWithResultMap() { IList list = sqlMap.QueryForList("GetAllAccountsViaResultMap", null); AssertAccount1(list[0]); Assert.AreEqual(5, list.Count); Assert.AreEqual(1, list[0].Id); Assert.AreEqual(2, list[1].Id); Assert.AreEqual(3, list[2].Id); Assert.AreEqual(4, list[3].Id); Assert.AreEqual(5, list[4].Id); } /// /// Test QueryForList with ResultObject : /// AccountCollection strongly typed collection /// [Test] public void TestQueryForListWithResultObject() { IList accounts = new List(); sqlMap.QueryForList("GetAllAccountsViaResultMap", null, accounts); AssertAccount1(accounts[0]); Assert.AreEqual(5, accounts.Count); Assert.AreEqual(1, accounts[0].Id); Assert.AreEqual(2, accounts[1].Id); Assert.AreEqual(3, accounts[2].Id); Assert.AreEqual(4, accounts[3].Id); Assert.AreEqual(5, accounts[4].Id); } /// /// Test QueryForList with no result. /// [Test] public void TestQueryForListWithNoResult() { IList list = sqlMap.QueryForList("GetNoAccountsViaResultMap", null); Assert.AreEqual(0, list.Count); } /// /// Test QueryForList with ResultClass : Account. /// [Test] public void TestQueryForListResultClass() { IList list = sqlMap.QueryForList("GetAllAccountsViaResultClass", null); AssertAccount1(list[0]); Assert.AreEqual(5, list.Count); Assert.AreEqual(1, list[0].Id); Assert.AreEqual(2, list[1].Id); Assert.AreEqual(3, list[2].Id); Assert.AreEqual(4, list[3].Id); Assert.AreEqual(5, list[4].Id); } /// /// Test QueryForList with simple resultClass : string /// [Test] public void TestQueryForListWithSimpleResultClass() { IList list = sqlMap.QueryForList("GetAllEmailAddressesViaResultClass", null); Assert.AreEqual("Joe.Dalton@somewhere.com", list[0]); Assert.AreEqual("Averel.Dalton@somewhere.com", list[1]); Assert.IsNull(list[2]); Assert.AreEqual("Jack.Dalton@somewhere.com", list[3]); Assert.AreEqual("gilles.bayon@nospam.org", list[4]); } /// /// Test QueryForList with simple ResultMap : string /// [Test] public void TestQueryForListWithSimpleResultMap() { IList list = sqlMap.QueryForList("GetAllEmailAddressesViaResultMap", null); Assert.AreEqual("Joe.Dalton@somewhere.com", list[0]); Assert.AreEqual("Averel.Dalton@somewhere.com", list[1]); Assert.IsNull(list[2]); Assert.AreEqual("Jack.Dalton@somewhere.com", list[3]); Assert.AreEqual("gilles.bayon@nospam.org", list[4]); } /// /// Test QueryForListWithSkipAndMax /// [Test] public void TestQueryForListWithSkipAndMax() { IList list = sqlMap.QueryForList("GetAllAccountsViaResultMap", null, 2, 2); Assert.AreEqual(2, list.Count); Assert.AreEqual(3, list[0].Id); Assert.AreEqual(4, list[1].Id); } [Test] public void TestQueryWithRowDelegate() { _index = 0; RowDelegate handler = new RowDelegate(this.RowHandler); IList list = sqlMap.QueryWithRowDelegate("GetAllAccountsViaResultMap", null, handler); Assert.AreEqual(5, _index); Assert.AreEqual(5, list.Count); AssertAccount1(list[0]); Assert.AreEqual(1, list[0].Id); Assert.AreEqual(2, list[1].Id); Assert.AreEqual(3, list[2].Id); Assert.AreEqual(4, list[3].Id); Assert.AreEqual(5, list[4].Id); } /// /// Test QueryForList with constructor use on result object /// [Test] public void TestJIRA172() { IList list = sqlMap.QueryForList("GetManyOrderWithConstructor", null); Assert.IsTrue(list.Count > 0); } #endregion #region Row delegate private int _index = 0; public void RowHandler(object obj, object paramterObject, IList list) { _index++; Assert.AreEqual(_index, ((Account)obj).Id); list.Add(((Account)obj)); } #endregion #region QueryForDictionary /// /// Test ExecuteQueryForDictionary /// [Test] public void TestExecuteQueryForDictionary() { IDictionary map = sqlMap.QueryForDictionary("GetAllAccountsViaResultClass", null, "FirstName"); Assert.AreEqual(5, map.Count); AssertAccount1(map["Joe"]); Assert.AreEqual(1, map["Joe"].Id); Assert.AreEqual(2, map["Averel"].Id); Assert.AreEqual(3, map["William"].Id); Assert.AreEqual(4, map["Jack"].Id); Assert.AreEqual(5, map["Gilles"].Id); } /// /// Test ExecuteQueryForDictionary With Cache. /// [Test] public void TestExecuteQueryQueryForDictionaryWithCache() { IDictionary map = sqlMap.QueryForDictionary("GetAllAccountsCache", null, "FirstName"); int firstId = HashCodeProvider.GetIdentityHashCode(map); Assert.AreEqual(5, map.Count); AssertAccount1(map["Joe"]); Assert.AreEqual(1, map["Joe"].Id); Assert.AreEqual(2, map["Averel"].Id); Assert.AreEqual(3, map["William"].Id); Assert.AreEqual(4, map["Jack"].Id); Assert.AreEqual(5, map["Gilles"].Id); map = sqlMap.QueryForDictionary("GetAllAccountsCache", null, "FirstName"); int secondId = HashCodeProvider.GetIdentityHashCode(map); Assert.AreEqual(firstId, secondId); } /// /// Test ExecuteQueryForMap : Hashtable. /// /// /// If the keyProperty is an integer, you must acces the map /// by map[integer] and not by map["integer"] /// [Test] public void TestExecuteQueryForDictionary2() { IDictionary map = sqlMap.QueryForDictionary("GetAllOrderWithLineItems", null, "PostalCode"); Assert.AreEqual(11, map.Count); Order order = map["T4H 9G4"]; Assert.AreEqual(2, order.LineItemsIList.Count); } /// /// Test ExecuteQueryForMap with value property : /// "FirstName" as key, "EmailAddress" as value /// [Test] public void TestExecuteQueryForDictionaryWithValueProperty() { IDictionary map = sqlMap.QueryForDictionary("GetAllAccountsViaResultClass", null, "FirstName", "EmailAddress"); Assert.AreEqual(5, map.Count); Assert.AreEqual("Joe.Dalton@somewhere.com", map["Joe"]); Assert.AreEqual("Averel.Dalton@somewhere.com", map["Averel"]); Assert.IsNull(map["William"]); Assert.AreEqual("Jack.Dalton@somewhere.com", map["Jack"]); Assert.AreEqual("gilles.bayon@nospam.org", map["Gilles"]); } #endregion } } #endif