From: Michael D. <mik...@us...> - 2004-12-10 16:40:13
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate.Test/JoinedSubclass In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14749/NHibernate.Test/JoinedSubclass Added Files: Address.cs Customer.cs Employee.cs JoinedSubclass.Customer.hbm.xml JoinedSubclass.Employee.hbm.xml JoinedSubclass.hbm.xml JoinedSubclass.Person.hbm.xml JoinedSubclassExtendsFixture.cs JoinedSubclassFixture.cs Person.cs Log Message: NH-131: Describe joined-subclasses and subclasses in seperate files. Also refactored the test a little bit. Going to being moving away from the NHibernate.DomainModel and just include everything in the TestFixture. Making the domain classes smaller and easier to demonstrate the conepts. Borrowed some of the ideas from hibernate 3 --- NEW FILE: Employee.cs --- using System; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// Summary description for Employee. /// </summary> public class Employee: Person { private string _title; private Decimal _salary; private Employee _manager; public Employee() {} public string Title { get { return _title; } set { _title = value; } } public Decimal Salary { get { return _salary; } set { _salary = value; } } public Employee Manager { get { return _manager; } set { _manager = value; } } } } --- NEW FILE: JoinedSubclass.hbm.xml --- <?xml version="1.0"?> <!-- This mapping demonstrates (1) a table-per-subclass mapping strategy (2) a simple component mapping (3) recursive associations withing an inheritance tree --> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> <class name="NHibernate.Test.JoinedSubclass.Person, NHibernate.Test" table="person" > <id name="Id" type="Int32" unsaved-value="0" column="person_id" access="nosetter.camelcase-underscore" > <generator class="native" /> </id> <property name="Name" column="name" /> <property name="Sex" column="sex" /> <component name="Address"> <property name="Street"/> <property name="Zip"/> <property name="Country"/> </component> <joined-subclass name="NHibernate.Test.JoinedSubclass.Employee, NHibernate.Test" table="empl" > <key column="person_id"/> <property name="Title" not-null="true" length="20"/> <property name="Salary" length="0"/> <many-to-one name="Manager"/> </joined-subclass> <joined-subclass name="NHibernate.Test.JoinedSubclass.Customer, NHibernate.Test" table="cust" > <key column="person_id"/> <property name="Comments"/> <many-to-one name="Salesperson"/> </joined-subclass> </class> </hibernate-mapping> --- NEW FILE: JoinedSubclassFixture.cs --- using System; using System.Collections; using NUnit.Framework; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// Test the use of <c><class></c> and <c><joined-subclass></c> mappings. /// </summary> [TestFixture] public class JoinedSubclassFixture : TestCase { private DateTime testDateTime = new DateTime(2003, 8, 16); private DateTime updateDateTime = new DateTime(2003, 8, 17); [SetUp] public virtual void SetUp() { ExportSchema( new string[] { "JoinedSubclass.JoinedSubclass.hbm.xml" }, true, "NHibernate.Test" ); } [Test] public void TestJoinedSubclass() { ISession s = sessions.OpenSession(); Employee mark = new Employee(); mark.Name = "Mark"; mark.Title = "internal sales"; mark.Sex = 'M'; mark.Address.Street = "buckhead"; mark.Address.Zip = "30305"; mark.Address.Country = "USA"; Customer joe = new Customer(); joe.Name = "Joe"; joe.Address.Street = "San Francisco"; joe.Address.Zip = "54353"; joe.Address.Country = "USA"; joe.Comments = "very demanding"; joe.Sex = 'M'; joe.Salesperson = mark; Person mom = new Person(); mom.Name = "mom"; mom.Sex = 'F'; s.Save( mom ); s.Save( mark ); s.Save( joe ); s.Flush(); Assert.AreEqual( 3, s.CreateQuery( "from Person" ).List().Count ); IQuery query = s.CreateQuery( "from Customer" ); IList results = query.List(); Assert.AreEqual( 1, results.Count ); Assert.IsTrue( results[0] is Customer, "should be a customer" ); // in later versions of hibernate a Clear method was added to session s.Close(); s = sessions.OpenSession(); IList customers = s.CreateQuery( "from Customer c left join fetch c.Salesperson" ).List(); foreach( Customer c in customers ) { // when proxies is working this is important Assert.IsTrue( NHibernate.IsInitialized( c.Salesperson ) ); Assert.AreEqual( "Mark", c.Salesperson.Name ); } Assert.AreEqual( 1, customers.Count ); s.Close(); s = sessions.OpenSession(); customers = s.CreateQuery( "from Customer" ).List(); foreach( Customer c in customers ) { //TODO: proxies make this work // Assert.IsFalse( NHibernate.IsInitialized( c.Salesperson ) ); Assert.AreEqual( "Mark", c.Salesperson.Name ); } Assert.AreEqual( 1, customers.Count ); s.Close(); s = sessions.OpenSession(); mark = (Employee)s.Load( typeof(Employee), mark.Id ); joe = (Customer)s.Load( typeof(Customer), joe.Id ); mark.Address.Zip = "30306" ; Assert.AreEqual( 1, s.CreateQuery( "from Person p where p.Address.Zip = '30306'" ).List().Count ); s.Delete( mom ); s.Delete( joe ); s.Delete( mark ); Assert.AreEqual( 0, s.CreateQuery( "from Person" ).List().Count ); s.Close(); } /// <summary> /// Test the ability to insert a new row with a User Assigned Key /// Right now - the only way to verify this is to watch SQL Profiler /// </summary> [Test] public void TestCRUD() { // test the Save ISession s = sessions.OpenSession(); ITransaction t = s.BeginTransaction(); Employee emp = new Employee(); emp.Name = "test one"; emp.Title = "office clown"; s.Save(emp); Person person = new Person(); person.Name = "the test string"; s.Save(person); t.Commit(); s.Close(); int empId = emp.Id; int personId = person.Id; // lets verify the correct classes were saved s = sessions.OpenSession(); t = s.BeginTransaction(); // perform a load based on the base class Person empAsPerson = (Person)s.Load( typeof(Person), empId ); person = (Person)s.Load( typeof(Person), personId ); // the object with id=2 was loaded using the base class - lets make sure it actually loaded // the sublcass emp = empAsPerson as Employee; Assert.IsNotNull(emp); // lets update the objects person.Name = "Did it get updated"; person.Sex = 'M'; // update the properties from the subclass and base class emp.Name = "Updated Employee String"; emp.Title = "office dunce"; // save it through the base class reference and make sure that the // subclass properties get updated. s.Update( empAsPerson ); s.Update( person ); t.Commit(); s.Close(); // lets test the Criteria interface for subclassing s = sessions.OpenSession(); t = s.BeginTransaction(); IList results = s.CreateCriteria( typeof(Person) ) .Add(Expression.Expression.In( "Name", new string[] {"Did it get updated", "Updated Employee String" })) .List(); Assert.AreEqual( 2, results.Count); person = null; emp = null; foreach(Person obj in results) { if(obj is Employee) emp = (Employee)obj; else person = obj; } // verify the properties got updated Assert.AreEqual( 'M', person.Sex ); Assert.AreEqual( "office dunce", emp.Title ); s.Delete( emp ); s.Delete( person ); t.Commit(); s.Close(); } } } --- NEW FILE: JoinedSubclassExtendsFixture.cs --- using System; using System.Collections; using NUnit.Framework; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// Test the use of <c><class></c> and <c><joined-subclass></c> mappings /// that are in different files through the use of the <c>extends</c> attribute. /// </summary> /// <remarks> /// Inheriting from <see cref="JoinedSubclassFixture"/> because the only thing different /// is how the classes are mapped. /// </remarks> [TestFixture] public class JoinedSubclassExtendsFixture : JoinedSubclassFixture { [SetUp] public override void SetUp() { // order is important! The base classes must be configured before // the subclasses. ArrayList files = new ArrayList(); files.Add( "JoinedSubclass.JoinedSubclass.Person.hbm.xml" ); files.Add( "JoinedSubclass.JoinedSubclass.Employee.hbm.xml" ); files.Add( "JoinedSubclass.JoinedSubclass.Customer.hbm.xml" ); ExportSchema( files, true, "NHibernate.Test" ); } } } --- NEW FILE: JoinedSubclass.Employee.hbm.xml --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Customer.cs --- using System; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// Summary description for Customer. /// </summary> public class Customer : Person { private Employee _salesperson; private string _comments; public Customer() { } public Employee Salesperson { get { return _salesperson; } set { _salesperson = value; } } public string Comments { get { return _comments; } set { _comments = value; } } } } --- NEW FILE: JoinedSubclass.Customer.hbm.xml --- (This appears to be a binary file; contents omitted.) --- NEW FILE: JoinedSubclass.Person.hbm.xml --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Person.cs --- using System; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// Summary description for Person. /// </summary> public class Person { private int _id = 0; private string _name; private char _sex; private Address _address = new Address(); public Person() { } public int Id { get { return _id; } } public string Name { get { return _name; } set { _name = value; } } public char Sex { get { return _sex; } set { _sex = value; } } public Address Address { get { return _address; } set { _address = value; } } } } --- NEW FILE: Address.cs --- using System; namespace NHibernate.Test.JoinedSubclass { /// <summary> /// An Address that is used as a <c>component</c>. /// </summary> public class Address { private string _street; private string _zip; private string _country; public Address() { } public string Street { get { return _street; } set { _street = value; } } public string Zip { get { return _zip; } set { _zip = value; } } public string Country { get { return _country; } set { _country = value; } } } } |