nhibernate example application: add config files that were moved to new location
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<objects xmlns="http://www.springframework.net"
|
||||
xmlns:db="http://www.springframework.net/database">
|
||||
|
||||
<!-- Referenced by main application context configuration file -->
|
||||
<description>
|
||||
The Northwind object definitions for the Data Access Objects.
|
||||
</description>
|
||||
|
||||
<!-- Property placeholder configurer for database settings -->
|
||||
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
|
||||
<property name="ConfigSections" value="databaseSettings"/>
|
||||
</object>
|
||||
|
||||
<!-- Database and NHibernate Configuration -->
|
||||
<db:provider id="DbProvider"
|
||||
provider="SqlServer-2.0"
|
||||
connectionString="Data Source=.\SQLExpress;Integrated Security=true;User Instance=true;AttachDBFilename=|DataDirectory|\Northwnd.mdf"/>
|
||||
|
||||
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
|
||||
<property name="DbProvider" ref="DbProvider"/>
|
||||
<property name="MappingAssemblies">
|
||||
<list>
|
||||
<value>Spring.Northwind.Dao.NHibernate</value>
|
||||
</list>
|
||||
</property>
|
||||
<property name="HibernateProperties">
|
||||
<dictionary>
|
||||
|
||||
<entry key="hibernate.connection.provider"
|
||||
value="NHibernate.Connection.DriverConnectionProvider"/>
|
||||
|
||||
<entry key="hibernate.dialect"
|
||||
value="NHibernate.Dialect.MsSql2000Dialect"/>
|
||||
|
||||
<entry key="hibernate.connection.driver_class"
|
||||
value="NHibernate.Driver.SqlClientDriver"/>
|
||||
|
||||
</dictionary>
|
||||
</property>
|
||||
|
||||
<property name="ExposeTransactionAwareSessionFactory" value="true" />
|
||||
|
||||
|
||||
</object>
|
||||
|
||||
|
||||
<object id="transactionManager"
|
||||
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
|
||||
|
||||
<property name="DbProvider" ref="DbProvider"/>
|
||||
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
|
||||
|
||||
</object>
|
||||
|
||||
<object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate">
|
||||
<property name="SessionFactory" ref="NHibernateSessionFactory" />
|
||||
<property name="TemplateFlushMode" value="Auto" />
|
||||
<property name="CacheQueries" value="true" />
|
||||
</object>
|
||||
|
||||
<!-- Data Access Objects -->
|
||||
<object id="CustomerDao" type="Spring.Northwind.Dao.NHibernate.HibernateCustomerDao, Spring.Northwind.Dao.NHibernate">
|
||||
<property name="HibernateTemplate" ref="HibernateTemplate"/>
|
||||
</object>
|
||||
|
||||
<object id="OrderDao" type="Spring.Northwind.Dao.NHibernate.HibernateOrderDao, Spring.Northwind.Dao.NHibernate">
|
||||
<property name="HibernateTemplate" ref="HibernateTemplate"/>
|
||||
</object>
|
||||
|
||||
|
||||
</objects>
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<objects xmlns="http://www.springframework.net"
|
||||
xmlns:tx="http://www.springframework.net/tx">
|
||||
|
||||
<!-- Referenced by main application context configuration file -->
|
||||
|
||||
<description>
|
||||
The Northwind service layer definitions
|
||||
</description>
|
||||
|
||||
<!-- Property placeholder configurer for database settings -->
|
||||
<object id="FulfillmentService" type="Spring.Northwind.Service.FulfillmentService, Spring.Northwind.Service">
|
||||
<property name="CustomerDao" ref="CustomerDao"/>
|
||||
<property name="OrderDao" ref="OrderDao"/>
|
||||
<property name="ShippingService" ref="ShippingService"/>
|
||||
</object>
|
||||
|
||||
<object id="ShippingService" type="Spring.Northwind.Service.FedExShippingService, Spring.Northwind.Service">
|
||||
|
||||
</object>
|
||||
|
||||
<tx:attribute-driven/>
|
||||
|
||||
|
||||
</objects>
|
||||
@@ -0,0 +1,53 @@
|
||||
#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 Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NUnit.Framework;
|
||||
using Spring.Testing.NUnit;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Northwind.IntegrationTests
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains tests for
|
||||
/// </summary>
|
||||
/// <author>Mark Pollack</author>
|
||||
/// <version>$Id:$</version>
|
||||
[TestFixture]
|
||||
public abstract class AbstractDaoIntegrationTests : AbstractTransactionalDbProviderSpringContextTests
|
||||
{
|
||||
|
||||
protected override string[] ConfigLocations
|
||||
{
|
||||
get
|
||||
{
|
||||
return new string[]
|
||||
{
|
||||
"assembly://Spring.Northwind.Dao.NHibernate/Spring.Northwind.Dao/Dao.xml",
|
||||
"assembly://Spring.Northwind.Service/Spring.Northwind.Service/Services.xml"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
#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 Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
using NHibernate;
|
||||
using NUnit.Framework;
|
||||
using Spring.Data.NHibernate;
|
||||
using Spring.Northwind.Dao;
|
||||
using Spring.Northwind.Domain;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Northwind.IntegrationTests
|
||||
{
|
||||
/// <summary>
|
||||
/// This class contains tests for
|
||||
/// </summary>
|
||||
/// <author>Mark Pollack</author>
|
||||
/// <version>$Id:$</version>
|
||||
[TestFixture]
|
||||
public class NorthwindIntegrationTests : AbstractDaoIntegrationTests
|
||||
{
|
||||
private ICustomerDao customerDao;
|
||||
private IOrderDao orderDao;
|
||||
|
||||
private ISessionFactory sessionFactory;
|
||||
|
||||
public ICustomerDao CustomerDao
|
||||
{
|
||||
set { customerDao = value; }
|
||||
}
|
||||
|
||||
|
||||
public IOrderDao OrderDao
|
||||
{
|
||||
set { orderDao = value; }
|
||||
}
|
||||
|
||||
|
||||
public ISessionFactory SessionFactory
|
||||
{
|
||||
set { sessionFactory = value; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SanityCheck()
|
||||
{
|
||||
|
||||
Assert.IsNotNull(customerDao, "customer dao is null");
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CustomerDaoTests()
|
||||
{
|
||||
Assert.AreEqual(91, customerDao.FindAll().Count);
|
||||
|
||||
Customer c = new Customer();
|
||||
c.Id = "MPOLL";
|
||||
c.CompanyName = "Interface21";
|
||||
customerDao.Save(c);
|
||||
c = customerDao.FindById("MPOLL");
|
||||
Assert.AreEqual(c.Id, "MPOLL");
|
||||
Assert.AreEqual(c.CompanyName, "Interface21");
|
||||
|
||||
//Without flushing, nothing changes in the database:
|
||||
int customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
|
||||
Assert.AreEqual(91, customerCount);
|
||||
|
||||
//Flush the session to execute sql in the db.
|
||||
SessionFactoryUtils.GetSession(sessionFactory, true).Flush();
|
||||
|
||||
//Now changes are visible outside the session but within the same database transaction
|
||||
customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
|
||||
Assert.AreEqual(92, customerCount);
|
||||
|
||||
Assert.AreEqual(92, customerDao.FindAll().Count);
|
||||
|
||||
c.CompanyName = "SpringSource";
|
||||
|
||||
customerDao.SaveOrUpdate(c);
|
||||
|
||||
c = customerDao.FindById("MPOLL");
|
||||
Assert.AreEqual(c.Id, "MPOLL");
|
||||
Assert.AreEqual(c.CompanyName, "SpringSource");
|
||||
|
||||
customerDao.Delete(c);
|
||||
|
||||
|
||||
SessionFactoryUtils.GetSession(sessionFactory, true).Flush();
|
||||
customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
|
||||
Assert.AreEqual(92, customerCount);
|
||||
|
||||
try
|
||||
{
|
||||
c = customerDao.FindById("MPOLL");
|
||||
Assert.Fail("Should have thrown HibernateObjectRetrievalFailureException when finding customer with Id = MPOLL");
|
||||
}
|
||||
catch (HibernateObjectRetrievalFailureException e)
|
||||
{
|
||||
Assert.AreEqual("Customer", e.PersistentClassName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductDaoTests()
|
||||
{
|
||||
Assert.AreEqual(830, orderDao.FindAll().Count);
|
||||
|
||||
Order order = new Order();
|
||||
Customer customer = customerDao.FindById("PICCO");
|
||||
order.Customer = customer;
|
||||
order.ShipCity = "New York";
|
||||
|
||||
orderDao.Save(order);
|
||||
int orderId = order.Id;
|
||||
order = orderDao.FindById(orderId);
|
||||
Assert.AreEqual("PICCO", order.Customer.Id);
|
||||
Assert.AreEqual("New York", order.ShipCity);
|
||||
|
||||
//SessionFactoryUtils.GetSession(sessionFactory, true).Flush();
|
||||
// Required trip to db to get idendity column, so data is visible
|
||||
|
||||
int ordersCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Orders");
|
||||
Assert.AreEqual(831, ordersCount);
|
||||
|
||||
Assert.AreEqual(831, orderDao.FindAll().Count);
|
||||
|
||||
order.ShipCity = "Sao Paulo";
|
||||
orderDao.SaveOrUpdate(order);
|
||||
|
||||
order = orderDao.FindById(orderId);
|
||||
Assert.AreEqual("PICCO", order.Customer.Id);
|
||||
Assert.AreEqual("Sao Paulo", order.ShipCity);
|
||||
|
||||
|
||||
orderDao.Delete(order);
|
||||
|
||||
//Without flushing, nothing changes in the database:
|
||||
SessionFactoryUtils.GetSession(sessionFactory, true).Flush();
|
||||
|
||||
ordersCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Orders");
|
||||
Assert.AreEqual(830, ordersCount);
|
||||
|
||||
try
|
||||
{
|
||||
order = orderDao.FindById(orderId);
|
||||
Assert.Fail("Should have thrown HibernateObjectRetrievalFailureException when finding order with Id = " + orderId);
|
||||
}
|
||||
catch (HibernateObjectRetrievalFailureException e)
|
||||
{
|
||||
Assert.AreEqual("Order", e.PersistentClassName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user