From: <fab...@us...> - 2011-04-02 18:31:10
|
Revision: 5574 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5574&view=rev Author: fabiomaulo Date: 2011-04-02 18:31:03 +0000 (Sat, 02 Apr 2011) Log Message: ----------- Root classes registration Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/RootClassMappingStrategyTests.cs Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 15:10:48 UTC (rev 5573) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 18:31:03 UTC (rev 5574) @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Reflection; namespace NHibernate.Mapping.ByCode @@ -147,22 +148,55 @@ public void AddAsTablePerClassEntity(System.Type type) { - tablePerClassEntities.Add(type); + var rootEntity = GetRootEntityOrNull(type); + if(rootEntity != null) + { + if(rootEntity.Equals(type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class strategy", type.FullName)); + } + tablePerClassEntities.Add(rootEntity); + } } public void AddAsTablePerClassHierarchyEntity(System.Type type) { - tablePerClassHierarchyEntities.Add(type); + var rootEntity = GetRootEntityOrNull(type); + if (rootEntity != null) + { + if (rootEntity.Equals(type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class-hierarchy strategy", type.FullName)); + } + tablePerClassHierarchyEntities.Add(rootEntity); + } } public void AddAsTablePerClassHierarchyJoinEntity(System.Type type) { + var rootEntity = GetRootEntityOrNull(type); + if (rootEntity != null) + { + if (rootEntity.Equals(type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class-hierarchy strategy", type.FullName)); + } + tablePerClassHierarchyEntities.Add(rootEntity); + } tablePerClassHierarchyJoinEntities.Add(type); } public void AddAsTablePerConcreteClassEntity(System.Type type) { - tablePerConcreteClassEntities.Add(type); + var rootEntity = GetRootEntityOrNull(type); + if (rootEntity != null) + { + if (rootEntity.Equals(type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-concrete-class strategy", type.FullName)); + } + tablePerConcreteClassEntities.Add(rootEntity); + } } public void AddAsOneToOneRelation(MemberInfo member) @@ -379,5 +413,18 @@ } #endregion + + private System.Type GetRootEntityOrNull(System.Type entityType) + { + if (entityType == null) + { + return null; + } + if (IsRootEntity(entityType)) + { + return entityType; + } + return entityType.GetBaseTypes().SingleOrDefault(IsRootEntity); + } } } \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/RootClassMappingStrategyTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/RootClassMappingStrategyTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/RootClassMappingStrategyTests.cs 2011-04-02 18:31:03 UTC (rev 5574) @@ -0,0 +1,158 @@ +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode.ExplicitlyDeclaredModelTests +{ + public class RootClassMappingStrategyTests + { + private class MyClass + { + + } + private class Inherited1: MyClass + { + + } + private class Inherited2 : Inherited1 + { + + } + + [Test] + public void WhenRegisteredAsRootThenDoesNotRegisterTheStrategy() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredJoinedSubclassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredJoinedDeepSubclassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited2)); + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredSubclassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassHierarchyEntity(typeof(Inherited1)); + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredDeepSubclassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassHierarchyEntity(typeof(Inherited2)); + + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredSubclassJoinThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassHierarchyJoinEntity(typeof(Inherited1)); + + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredDeepSubclassJoinThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassHierarchyJoinEntity(typeof(Inherited2)); + + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.True(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredConcreteClassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerConcreteClassEntity(typeof(Inherited1)); + + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.True(); + } + + [Test] + public void WhenRegisteredDeepConcreteClassThenTheStrategyIsDefinedEvenForRoot() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerConcreteClassEntity(typeof(Inherited2)); + + inspector.IsTablePerClass(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchy(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(MyClass)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(MyClass)).Should().Be.True(); + } + + [Test] + public void WhenRegisteredAsRootThenCantRegisterAsSubclass() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.Executing(x=> x.AddAsTablePerClassEntity(typeof(MyClass))).Throws<MappingException>(); + } + + [Test] + public void WhenRegisteredAsRootThenCantRegisterAsJoinedSubclass() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.Executing(x => x.AddAsTablePerClassEntity(typeof(MyClass))).Throws<MappingException>(); + } + + [Test] + public void WhenRegisteredAsRootThenCantRegisterAsUnionSubclass() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.Executing(x => x.AddAsTablePerClassEntity(typeof(MyClass))).Throws<MappingException>(); + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-02 15:10:48 UTC (rev 5573) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-02 18:31:03 UTC (rev 5574) @@ -508,6 +508,7 @@ <Compile Include="Logging\LoggerProviderTest.cs" /> <Compile Include="MappingByCode\BasicMappingOfSimpleClass.cs" /> <Compile Include="MappingByCode\ColumnsNamingConvetions.cs" /> + <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\RootClassMappingStrategyTests.cs" /> <Compile Include="MappingByCode\MappingOfPrivateMembersOnRootEntity.cs" /> <Compile Include="NHSpecificTest\AccessAndCorrectPropertyName\Fixture.cs" /> <Compile Include="NHSpecificTest\AccessAndCorrectPropertyName\Model.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |