From: <jul...@us...> - 2010-09-20 14:55:16
|
Revision: 5196 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5196&view=rev Author: julian-maughan Date: 2010-09-20 14:55:09 +0000 (Mon, 20 Sep 2010) Log Message: ----------- Test for NH-1136. Provides an example of mapping a custom dictionary with a many-to-many association. Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Address.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/FeeMatrixType.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/IMilestoneCollection.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollection.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollectionType.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/PersistentMilestoneCollection.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Person.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/ReverseSortComparer.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/TemporalAddressType.cs Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Address.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Address.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Address.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class Address : IEquatable<Address> + { + #region Fields + +#pragma warning disable 169 + private int _id; + private int _version; +#pragma warning restore 169 + + private string _number; + private string _postcode; + + #endregion + + #region Ctors + + public Address(string number, string postcode) + { + _number = number; + _postcode = postcode; + } + + private Address() + { + } + + #endregion + + #region Properties + + public string Number + { + get { return _number; } + } + + public string Postcode + { + get { return _postcode; } + } + + #endregion + + #region Object Overrides + + public override bool Equals(object obj) + { + return Equals(obj as Address); + } + + public override int GetHashCode() + { + return Postcode.GetHashCode() + 29*Number.GetHashCode(); + } + + #endregion + + #region IEquatable<Address> Members + + public bool Equals(Address other) + { + return other == null ? false : IsEqualTo(other); + } + + #endregion + + private bool IsEqualTo(Address other) + { + return ReferenceEquals(this, other) || (Postcode.Equals(other.Postcode) && Number.Equals(other.Number)); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/FeeMatrixType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/FeeMatrixType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/FeeMatrixType.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,9 @@ +using System; +using NHibernate.UserTypes; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class FeeMatrixType : MilestoneCollectionType<int, decimal> + { + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Fixture.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,60 @@ +using System; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + [TestFixture] + public class Fixture : BugTestCase + { + public override string BugNumber + { + get { return "NH1136"; } + } + + protected override void OnTearDown() + { + using (ISession s = OpenSession()) + { + s.Delete("from Address"); + s.Delete("from Person"); + s.Flush(); + } + + base.OnTearDown(); + } + + [Test] + public void Test() + { + int id = -1; + + using (ISession s = OpenSession()) + { + var address1 = new Address("60", "EH3 8BE"); + var address2 = new Address("2", "EH6 6JA"); + s.Save(address1); + s.Save(address2); + + var person1 = new Person("'lil old me"); + person1.AddPercentageToFeeMatrix(0, .20m); + person1.AddPercentageToFeeMatrix(50, .15m); + person1.AddPercentageToFeeMatrix(100, .1m); + person1.RegisterChangeOfAddress(DateTime.Parse("15/04/2005"), address1); + person1.RegisterChangeOfAddress(DateTime.Parse("29/05/2007"), address2); + + s.Save(person1); + s.Flush(); + + id = person1.Id; + } + + using (ISession s = OpenSession()) + { + var person1 = s.Load<Person>(id); + person1.RegisterChangeOfAddress(DateTime.Parse("23/03/2008"), new Address("8", "SS7 1TT")); + s.Save(person1); + s.Flush(); + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/IMilestoneCollection.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/IMilestoneCollection.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/IMilestoneCollection.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public interface IMilestoneCollection<TKey, TValue> : IDictionary<TKey, TValue> + where TKey : IComparable<TKey> + { + TValue FindValueFor(TKey key); + } +} Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Mappings.hbm.xml 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,61 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping + xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH1136"> + + <class name ="Person" table="PERSON" lazy="false"> + + <id name="_id" column="PERSON_ID" access="field"> + <generator class="identity"/> + </id> + + <version name="_version" column ="VERSION" access="field"/> + + <property name="Name" column="NAME" access="nosetter.camelcase-underscore" /> + + <map + name="_feeMatrix" + access="field" + generic="true" + cascade="all-delete-orphan" + lazy="true" + table="FEE_MATRIX" + collection-type="FeeMatrixType, NHibernate.Test"> + + <key column="PERSON_ID"/> + <index column="MILESTONE" type="System.Int32"/> + <element column="PERCENTAGE" type="System.Decimal" not-null="true"/> + </map> + + <map + name="_historyOfAddresses" + access="field" + generic="true" + cascade="all-delete-orphan" + lazy="true" + table="PERSON_ADDRESS_ASSOCIATION" + collection-type="TemporalAddressesType, NHibernate.Test"> + + <key column="PERSON_ID" /> + <index column="MOVED_IN_ON" type="System.DateTime"/> + <many-to-many class="Address" column="ADDRESS_ID" /> + </map> + + </class> + + <class name ="Address" table="ADDRESS" lazy="false"> + + <id name="_id" column="ADDRESS_ID" access="field"> + <generator class="identity"/> + </id> + + <version name="_version" column ="VERSION" access="field"/> + + <property name="Number" column="HOUSE_NUMBER" access="nosetter.camelcase-underscore" /> + + <property name="Postcode" column="POSTCODE" access="nosetter.camelcase-underscore" /> + + </class> + +</hibernate-mapping> Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollection.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollection.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollection.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class MilestoneCollection<TKey, TValue> : SortedDictionary<TKey, TValue>, IMilestoneCollection<TKey, TValue> + where TKey : IComparable<TKey> + { + public MilestoneCollection() : base(new ReverseSortComparer<TKey>()){} + + #region IMilestoneCollection<TKey,TValue> Members + + public TValue FindValueFor(TKey key) + { + foreach (TKey milestone in this.Keys) + { + if (milestone.CompareTo(key) <= 0) return this[milestone]; + } + return default(TValue); + } + + #endregion + } +} Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollectionType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollectionType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/MilestoneCollectionType.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,47 @@ +using System; +using System.Collections; +using NHibernate.Collection; +using NHibernate.Engine; +using NHibernate.Persister.Collection; +using NHibernate.UserTypes; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class MilestoneCollectionType<TKey, TValue> : IUserCollectionType where TKey : IComparable<TKey> + { + public IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister) + { + return new PersistentMilestoneCollection<TKey, TValue>(session); + } + + public IPersistentCollection Wrap(ISessionImplementor session, object collection) + { + return new PersistentMilestoneCollection<TKey, TValue>(session, (IMilestoneCollection <TKey, TValue>) collection); + } + + public IEnumerable GetElements(object collection) + { + return (IEnumerable)((IMilestoneCollection<TKey, TValue>) collection).Values; + } + + public bool Contains(object collection, object entity) + { + throw new NotImplementedException(); + } + + public object IndexOf(object collection, object entity) + { + throw new NotImplementedException(); + } + + public object ReplaceElements(object original, object target, ICollectionPersister persister, object owner, IDictionary copyCache, ISessionImplementor session) + { + throw new NotImplementedException(); + } + + public object Instantiate(int anticipatedSize) + { + return new MilestoneCollection<TKey, TValue>(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/PersistentMilestoneCollection.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/PersistentMilestoneCollection.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/PersistentMilestoneCollection.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,28 @@ +using System; +using NHibernate.Collection.Generic; +using NHibernate.Engine; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class PersistentMilestoneCollection<TKey, TValue> : PersistentGenericMap<TKey, TValue>, IMilestoneCollection<TKey, TValue> + where TKey : IComparable<TKey> + { + public PersistentMilestoneCollection(ISessionImplementor session, IMilestoneCollection<TKey, TValue> map) : base(session, map) + { + } + + public PersistentMilestoneCollection(ISessionImplementor session) : base(session) + { + } + + #region IMilestoneCollection<TKey,TValue> Members + + public TValue FindValueFor(TKey key) + { + Read(); + return ((IMilestoneCollection<TKey, TValue>) map).FindValueFor(key); + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Person.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Person.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/Person.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,104 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class Person : IEquatable<Person> + { + #region Fields + +#pragma warning disable 169 + private int _id; + private int _version; +#pragma warning restore 169 + + private string _name; + private IMilestoneCollection<int, decimal> _feeMatrix = new MilestoneCollection<int, decimal>(); + private IMilestoneCollection<DateTime, Address> _historyOfAddresses = new MilestoneCollection<DateTime, Address>(); + + #endregion + + #region Constructors + + private Person() + { + } + + public Person(string name) + { + _name = name; + } + + #endregion + + #region Properties + + public int Id + { + get { return _id; } + } + + public string Name + { + get { return _name; } + } + + public Address CurrentAddress + { + get { return _historyOfAddresses.FindValueFor(DateTime.Now); } + } + + #endregion + + #region Methods + + public void AddPercentageToFeeMatrix(int value, decimal percentage) + { + _feeMatrix[value] = percentage; + } + + public decimal FindFeePercentageForValue(int value) + { + return _feeMatrix.FindValueFor(value); + } + + public void RegisterChangeOfAddress(DateTime movingDate, Address newAddress) + { + _historyOfAddresses[movingDate] = newAddress; + } + + #endregion + + #region Object Overrides + + public override bool Equals(object obj) + { + return Equals(obj as Person); + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + + public override string ToString() + { + return "Person: " + Name; + } + + #endregion + + #region IEquatable<Person> Members + + public bool Equals(Person other) + { + return other == null ? false : IsEqualTo(other); + } + + #endregion + + private bool IsEqualTo(Person other) + { + return ReferenceEquals(this, other) || Name.Equals(other.Name); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/ReverseSortComparer.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/ReverseSortComparer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/ReverseSortComparer.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public sealed class ReverseSortComparer<T> : IComparer<T> where T : IComparable<T> + { + #region IComparer<T> Members + + public int Compare(T x, T y) + { + return y.CompareTo(x); + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/TemporalAddressType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/TemporalAddressType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1136/TemporalAddressType.cs 2010-09-20 14:55:09 UTC (rev 5196) @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NHibernate.UserTypes; + +namespace NHibernate.Test.NHSpecificTest.NH1136 +{ + public class TemporalAddressesType : MilestoneCollectionType<DateTime, Address> + { + } +} Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-09-20 09:41:14 UTC (rev 5195) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-09-20 14:55:09 UTC (rev 5196) @@ -450,6 +450,16 @@ <Compile Include="Linq\ReadonlyTestCase.cs" /> <Compile Include="Logging\Log4NetLoggerTest.cs" /> <Compile Include="Logging\LoggerProviderTest.cs" /> + <Compile Include="NHSpecificTest\NH1136\Address.cs" /> + <Compile Include="NHSpecificTest\NH1136\FeeMatrixType.cs" /> + <Compile Include="NHSpecificTest\NH1136\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1136\IMilestoneCollection.cs" /> + <Compile Include="NHSpecificTest\NH1136\MilestoneCollection.cs" /> + <Compile Include="NHSpecificTest\NH1136\MilestoneCollectionType.cs" /> + <Compile Include="NHSpecificTest\NH1136\PersistentMilestoneCollection.cs" /> + <Compile Include="NHSpecificTest\NH1136\Person.cs" /> + <Compile Include="NHSpecificTest\NH1136\ReverseSortComparer.cs" /> + <Compile Include="NHSpecificTest\NH1136\TemporalAddressType.cs" /> <Compile Include="NHSpecificTest\NH1421\AnEntity.cs" /> <Compile Include="NHSpecificTest\NH1421\Fixture.cs" /> <Compile Include="NHSpecificTest\NH1836\Entity.cs" /> @@ -1768,6 +1778,7 @@ <EmbeddedResource Include="NHSpecificTest\NH2111\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2322\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2278\Mappings.hbm.xml" /> + <EmbeddedResource Include="NHSpecificTest\NH1136\Mappings.hbm.xml" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\NHibernate.ByteCode.Castle\NHibernate.ByteCode.Castle.csproj"> @@ -2613,6 +2624,7 @@ <EmbeddedResource Include="DynamicEntity\Tuplizer\Customer.hbm.xml" /> </ItemGroup> <ItemGroup> + <Folder Include="NHSpecificTest\NH1136" /> <Folder Include="Properties\" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |