Add files overlooked in migration to svn
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2008 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
|
||||
using System.Data;
|
||||
using Rhino.Mocks;
|
||||
using Spring.Data.Common;
|
||||
|
||||
namespace Spring.Data.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains shared mocking calls for performing tests on AdoQuery and subclasses.
|
||||
/// </summary>
|
||||
public abstract class AbstractAdoQueryTests
|
||||
{
|
||||
|
||||
protected MockRepository mocks;
|
||||
protected IDbProvider provider;
|
||||
protected IDbCommand command;
|
||||
|
||||
|
||||
public void SetUpMocks()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
provider = (IDbProvider)mocks.DynamicMock(typeof(IDbProvider));
|
||||
|
||||
IDbConnection connection = (IDbConnection)mocks.DynamicMock(typeof(IDbConnection));
|
||||
|
||||
Expect.Call(provider.CreateConnection()).Return(connection);
|
||||
|
||||
// Creating a query (setting DbProvider property)
|
||||
// will call new DbParameters(IDbProvider), which is a real pain to mock.
|
||||
// to store the declared parameters.
|
||||
|
||||
command = (IDbCommand)mocks.DynamicMock(typeof(IDbCommand));
|
||||
//This IDbCommand is used as a container for the underlying parameter collection.
|
||||
Expect.Call(provider.CreateCommand()).Return(command);
|
||||
|
||||
//Create a real instance of IDbParameters to stored the declared parameters
|
||||
IDbProvider realDbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
|
||||
IDbParameters dbParameters = new DbParameters(realDbProvider);
|
||||
|
||||
//Pass real instance into mock instance.
|
||||
Expect.Call(command.Parameters).Return(dbParameters.DataParameterCollection);
|
||||
Expect.Call(provider.CreateCommand()).Return(command);
|
||||
|
||||
// done with init of DbParameters mock/stubbing
|
||||
}
|
||||
}
|
||||
}
|
||||
112
test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs
Normal file
112
test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2008 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.Data;
|
||||
using NUnit.Framework;
|
||||
using Spring.Dao;
|
||||
using Spring.Data.Common;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Data.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for AdoOperation
|
||||
/// </summary>
|
||||
/// <author>Mark Pollack</author>
|
||||
/// <version>$Id: AdoOperationTests.cs,v 1.1 2008/05/30 21:10:00 markpollack Exp $</version>
|
||||
[TestFixture]
|
||||
public class AdoOperationTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(InvalidDataAccessApiUsageException))]
|
||||
public void EmptySql()
|
||||
{
|
||||
TestAdoOperation operation = new TestAdoOperation();
|
||||
operation.Compile();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(InvalidDataAccessApiUsageException))]
|
||||
public void DeclareParameterAfterCompile()
|
||||
{
|
||||
TestAdoOperation operation = new TestAdoOperation();
|
||||
operation.DbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
|
||||
operation.Sql = "select * from table";
|
||||
operation.Compile();
|
||||
IDbParameters parameters = new DbParameters(operation.DbProvider);
|
||||
operation.DeclaredParameters = parameters;
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(InvalidDataAccessApiUsageException))]
|
||||
public void TooFewParameters()
|
||||
{
|
||||
TestAdoOperation operation = new TestAdoOperation();
|
||||
operation.DbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
|
||||
operation.Sql = "select * from table";
|
||||
IDbParameters parameters = new DbParameters(operation.DbProvider);
|
||||
parameters.Add("name");
|
||||
operation.DeclaredParameters = parameters;
|
||||
operation.Compile();
|
||||
operation.ValidateParams(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class TestAdoOperation : AdoOperation
|
||||
{
|
||||
protected override void CompileInternal()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void ValidateParams(params object[] inParamValues)
|
||||
{
|
||||
ValidateParameters(inParamValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,35 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2008 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
using DotNetMock.Dynamic;
|
||||
using System.Data.SqlClient;
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
using Spring.Dao;
|
||||
using Spring.Data.Common;
|
||||
|
||||
#endregion
|
||||
@@ -32,43 +52,39 @@ using Spring.Data.Common;
|
||||
namespace Spring.Data.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains tests for AdoQuery subclasses
|
||||
/// Tests for AdoQuery
|
||||
/// </summary>
|
||||
/// <author>Mark Pollack</author>
|
||||
/// <version>$Id: AdoQueryTests.cs,v 1.3 2007/07/25 08:25:34 markpollack Exp $</version>
|
||||
/// <version>$Id: AdoQueryTests.cs,v 1.4 2008/05/30 21:10:00 markpollack Exp $</version>
|
||||
[TestFixture]
|
||||
public class AdoQueryTests
|
||||
public class AdoQueryTests : AbstractAdoQueryTests
|
||||
{
|
||||
private MockRepository mocks;
|
||||
|
||||
private static string SELECT_ID_WHERE = "select id from custmr";
|
||||
private static string[] COLUMN_NAMES = new string[] {"id", "forename"};
|
||||
private static DbType[] COLUMN_TYPES = new DbType[] {DbType.Int32, DbType.String};
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
mocks = new MockRepository();
|
||||
SetUpMocks();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
[Test]
|
||||
public void QueryWithoutContextRhino()
|
||||
public void MappingAdoQueryWithContextWithoutParams()
|
||||
{
|
||||
IDbProvider provider = (IDbProvider) mocks.DynamicMock(typeof (IDbProvider));
|
||||
|
||||
IDbConnection connection = (IDbConnection) mocks.DynamicMock(typeof (IDbConnection));
|
||||
|
||||
Expect.Call(provider.CreateConnection()).Return(connection);
|
||||
|
||||
IDbCommand command = (IDbCommand) mocks.DynamicMock(typeof (IDbCommand));
|
||||
|
||||
IDataReader reader = (IDataReader) mocks.DynamicMock(typeof (IDataReader));
|
||||
IDataReader reader = (IDataReader)mocks.DynamicMock(typeof(IDataReader));
|
||||
Expect.Call(reader.Read()).Return(true);
|
||||
Expect.Call(reader.GetInt32(0)).Return(1);
|
||||
Expect.Call(reader.Read()).Return(false);
|
||||
|
||||
Expect.Call(command.ExecuteReader()).Return(reader);
|
||||
Expect.Call(provider.CreateCommand()).Return(command);
|
||||
mocks.ReplayAll();
|
||||
|
||||
IntMappingQueryWithNoContext queryWithNoContext = new IntMappingQueryWithNoContext(provider);
|
||||
mocks.ReplayAll();
|
||||
IntMappingQueryWithContext queryWithNoContext = new IntMappingQueryWithContext(provider);
|
||||
queryWithNoContext.Compile();
|
||||
IList list = queryWithNoContext.QueryByNamedParam(null, null);
|
||||
Assert.IsTrue(list.Count != 0);
|
||||
@@ -78,66 +94,39 @@ namespace Spring.Data.Objects
|
||||
}
|
||||
|
||||
mocks.VerifyAll();
|
||||
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
[Test]
|
||||
public void QueryWithoutContext()
|
||||
[ExpectedException(typeof(InvalidDataAccessApiUsageException))]
|
||||
public void QueryWithoutEnoughParams()
|
||||
{
|
||||
SqlParameter sqlParameter1 = new SqlParameter();
|
||||
Expect.Call(command.CreateParameter()).Return(sqlParameter1);
|
||||
Expect.Call(provider.CreateParameterNameForCollection(COLUMN_NAMES[0])).Return("@" + COLUMN_NAMES[0]);
|
||||
|
||||
//Prepare provider
|
||||
IDynamicMock mockProvider = new DynamicMock(typeof(IDbProvider));
|
||||
|
||||
//Prepare connection to return from provider
|
||||
IDynamicMock mockConnection = new DynamicMock(typeof(IDbConnection));
|
||||
IDbConnection connection = (IDbConnection) mockConnection.Object;
|
||||
mockProvider.ExpectAndReturn("CreateConnection", connection);
|
||||
|
||||
//Prepare command
|
||||
IDynamicMock mockCommand = new DynamicMock(typeof(IDbCommand));
|
||||
|
||||
//Prepare reader to return from command
|
||||
IDynamicMock mockReader = new DynamicMock(typeof (IDataReader));
|
||||
mockReader.ExpectAndReturn("Read", true);
|
||||
mockReader.ExpectAndReturn("GetInt32", 1, 0);
|
||||
mockReader.ExpectAndReturn("Read", false);
|
||||
IDataReader reader = (IDataReader) mockReader.Object;
|
||||
mockCommand.ExpectAndReturn("ExecuteReader", reader);
|
||||
|
||||
//Preppare command to return from provider
|
||||
IDbCommand command = (IDbCommand)mockCommand.Object;
|
||||
mockProvider.ExpectAndReturn("CreateCommand", command);
|
||||
|
||||
SqlParameter sqlParameter2 = new SqlParameter();
|
||||
Expect.Call(command.CreateParameter()).Return(sqlParameter2);
|
||||
Expect.Call(provider.CreateParameterNameForCollection(COLUMN_NAMES[1])).Return("@" + COLUMN_NAMES[1]);
|
||||
|
||||
IDbProvider dbProvider = (IDbProvider)mockProvider.Object;
|
||||
|
||||
|
||||
//Test MappingAdoQueryWithContext
|
||||
IntMappingQueryWithNoContext queryWithNoContext = new IntMappingQueryWithNoContext(dbProvider);
|
||||
queryWithNoContext.Compile();
|
||||
//IList list = queryWithNoContext.QueryByNamedParam(null, null);
|
||||
IList list = queryWithNoContext.Query();
|
||||
Assert.IsTrue(list.Count != 0);
|
||||
foreach (int count in list)
|
||||
{
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mocks.ReplayAll();
|
||||
|
||||
IntMappingAdoQuery query = new IntMappingAdoQuery();
|
||||
query.DbProvider = provider;
|
||||
query.Sql = SELECT_ID_WHERE;
|
||||
query.DeclaredParameters.Add(COLUMN_NAMES[0], COLUMN_TYPES[0]);
|
||||
query.DeclaredParameters.Add(COLUMN_NAMES[1], COLUMN_TYPES[1]);
|
||||
query.Compile();
|
||||
IList list = query.Query();
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
public class IntMappingQueryWithNoContext : MappingAdoQueryWithContext
|
||||
|
||||
public class IntMappingQueryWithContext : MappingAdoQueryWithContext
|
||||
{
|
||||
private static string sql = "select id from custmr";
|
||||
|
||||
public IntMappingQueryWithNoContext(IDbProvider dbProvider)
|
||||
public IntMappingQueryWithContext(IDbProvider dbProvider)
|
||||
: base(dbProvider, sql)
|
||||
{
|
||||
CommandType = CommandType.Text;
|
||||
@@ -152,6 +141,14 @@ namespace Spring.Data.Objects
|
||||
return reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class IntMappingAdoQuery : MappingAdoQuery
|
||||
{
|
||||
protected override object MapRow(IDataReader reader, int rowNum)
|
||||
{
|
||||
return reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2008 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using NUnit.Framework;
|
||||
using Rhino.Mocks;
|
||||
using Spring.Data.Common;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Data.Objects
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for StoredProcedure
|
||||
/// </summary>
|
||||
/// <author>Mark Pollack</author>
|
||||
/// <version>$Id: StoredProcedureTests.cs,v 1.1 2008/05/30 21:10:00 markpollack Exp $</version>
|
||||
[TestFixture]
|
||||
public class StoredProcedureTests : AbstractAdoQueryTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
SetUpMocks();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NullArg()
|
||||
{
|
||||
SqlParameter sqlParameter1 = new SqlParameter();
|
||||
Expect.Call(command.CreateParameter()).Return(sqlParameter1);
|
||||
Expect.Call(provider.CreateParameterNameForCollection("ptest")).Return("@ptest");
|
||||
|
||||
//Create a real instance of IDbParameters to store the executable parameters
|
||||
//IDbProvider realDbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
|
||||
//IDbParameters dbParameters = new DbParameters(realDbProvider);
|
||||
IDataParameterCollection dbParamCollection = new SqlCommand().Parameters;
|
||||
Expect.Call(command.Parameters).Return(dbParamCollection);
|
||||
|
||||
//provide the same instance to another call to extract output params
|
||||
Expect.Call(command.Parameters).Return(dbParamCollection);
|
||||
|
||||
|
||||
mocks.ReplayAll();
|
||||
NullArg na = new NullArg(provider);
|
||||
na.Execute(null);
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
internal class NullArg : StoredProcedure
|
||||
{
|
||||
public NullArg(IDbProvider provider)
|
||||
: base(provider, "takes_null")
|
||||
{
|
||||
DeclaredParameters.Add("ptest", DbType.String);
|
||||
Compile();
|
||||
}
|
||||
|
||||
public void Execute(string s)
|
||||
{
|
||||
ExecuteNonQuery(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user