Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

NH-845 - AssemblyHbmOrderer forgets mapping files that do not contain…

… classes if ordering is done

Made AssemblyHbmOrderer public to be able to test it.

SVN: trunk@2541
  • Loading branch information...
commit 705d69e949bd6172d39b1262bef5a447c4f34224 1 parent 9ce0501
Sergey Koshcheyev authored
View
3  src/NHibernate.DomainModel/NHibernate.DomainModel-2.0.csproj
@@ -225,6 +225,9 @@
<EmbeddedResource Include="NHSpecific\Optimistic.hbm.xml" />
</ItemGroup>
<ItemGroup>
+ <EmbeddedResource Include="Query.hbm.xml" />
+ </ItemGroup>
+ <ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
View
6 src/NHibernate.DomainModel/Query.hbm.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
+ <query name="AQuery">
+ from Master
+ </query>
+</hibernate-mapping>
View
25 src/NHibernate.Test/NHSpecificTest/NH845/Fixture.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections;
+using System.Reflection;
+using NHibernate.Cfg;
+using NHibernate.DomainModel;
+using NUnit.Framework;
+
+namespace NHibernate.Test.NHSpecificTest.NH845
+{
+ [TestFixture]
+ public class Fixture
+ {
+ [Test]
+ public void HbmOrdererForgetsMappingFilesWithoutClassesIfExtendsIsUsed()
+ {
+ Assembly domain = typeof (Master).Assembly;
+ AssemblyHbmOrderer orderer = new AssemblyHbmOrderer(domain);
+ orderer.AddResource("NHibernate.DomainModel.MultiExtends.hbm.xml");
+ orderer.AddResource("NHibernate.DomainModel.Query.hbm.xml");
+
+ IList files = orderer.GetHbmFiles();
+ Assert.IsTrue(files.Contains("NHibernate.DomainModel.Query.hbm.xml"));
+ }
+ }
+}
View
1  src/NHibernate.Test/NHibernate.Test-2.0.csproj
@@ -362,6 +362,7 @@
<Compile Include="NHSpecificTest\NH826\Fixture.cs" />
<Compile Include="NHSpecificTest\NH830\AutoFlushTestFixture.cs" />
<Compile Include="NHSpecificTest\NH830\Cat.cs" />
+ <Compile Include="NHSpecificTest\NH845\Fixture.cs" />
<Compile Include="NHSpecificTest\NodeFixture.cs" />
<Compile Include="NHSpecificTest\OptimisticConcurrencyFixture.cs" />
<Compile Include="NHSpecificTest\ProxyValidator\Fixture.cs" />
View
68 src/NHibernate/Cfg/AssemblyHbmOrderer.cs
@@ -18,7 +18,7 @@ namespace NHibernate.Cfg
/// attribute. This ensures that those subclasses/joined-subclasses will not be
/// processed until after the class they extend is processed.
/// </remarks>
- internal class AssemblyHbmOrderer
+ public class AssemblyHbmOrderer
{
/*
* It almost seems like a better way to handle this would be to have
@@ -34,31 +34,45 @@ internal class AssemblyHbmOrderer
/// An unordered <see cref="IList"/> of all the mapped classes contained
/// in the assembly.
/// </summary>
- ArrayList _classes;
+ ArrayList _classes = new ArrayList();
/// <summary>
/// An <see cref="IList"/> of all the <c>hbm.xml</c> resources found
/// in the assembly.
/// </summary>
- ArrayList _hbmResources;
-
+ ArrayList _hbmResources = new ArrayList();
+
/// <summary>
- /// Creates a new instance of <see cref="AssemblyHbmOrderer"/>
+ /// Creates a new instance of AssemblyHbmOrderer with all embedded resources
+ /// ending in <c>.hbm.xml</c> added.
/// </summary>
- /// <param name="assembly">The <see cref="Assembly"/> to order the <c>hbm.xml</c> files in.</param>
- public AssemblyHbmOrderer(Assembly assembly)
+ public static AssemblyHbmOrderer CreateWithAllResourcesIn(Assembly assembly)
{
- _assembly = assembly;
- _classes = new ArrayList();
- _hbmResources = new ArrayList();
+ AssemblyHbmOrderer result = new AssemblyHbmOrderer(assembly);
foreach( string fileName in assembly.GetManifestResourceNames() )
{
if( fileName.EndsWith( ".hbm.xml" ) )
{
- _hbmResources.Add( fileName );
+ result.AddResource( fileName );
}
}
+
+ return result;
+ }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="AssemblyHbmOrderer"/>
+ /// </summary>
+ /// <param name="assembly">The <see cref="Assembly"/> to get resources from.</param>
+ public AssemblyHbmOrderer(Assembly assembly)
+ {
+ _assembly = assembly;
+ }
+
+ public void AddResource(string fileName)
+ {
+ _hbmResources.Add(fileName);
}
/// <summary>
@@ -71,17 +85,18 @@ public IList GetHbmFiles()
{
// tracks if any hbm.xml files make use of the "extends" attribute
bool containsExtends = false;
+ // tracks any extra files, i.e. those that do not contain a class definition.
+ StringCollection extraFiles = new StringCollection();
foreach( string fileName in _hbmResources )
{
Stream xmlInputStream = null;
XmlReader xmlReader = null;
+ bool addedToClasses = false;
- try
+ using (xmlInputStream = _assembly.GetManifestResourceStream( fileName ))
+ using (xmlReader = new XmlTextReader( xmlInputStream ))
{
- xmlInputStream = _assembly.GetManifestResourceStream( fileName );
- xmlReader = new XmlTextReader( xmlInputStream );
-
while( xmlReader.Read() )
{
if( xmlReader.NodeType != XmlNodeType.Element )
@@ -95,6 +110,7 @@ public IList GetHbmFiles()
string className = StringHelper.GetClassname( xmlReader.Value );
ClassEntry ce = new ClassEntry( null, className, fileName );
_classes.Add(ce);
+ addedToClasses = true;
}
else if( xmlReader.Name=="joined-subclass" || xmlReader.Name=="subclass" )
{
@@ -106,20 +122,15 @@ public IList GetHbmFiles()
string baseClassName = StringHelper.GetClassname( xmlReader.Value );
ClassEntry ce = new ClassEntry( baseClassName, className, fileName );
_classes.Add(ce);
+ addedToClasses = true;
}
}
}
}
- finally
+
+ if (!addedToClasses)
{
- if( xmlReader!=null )
- {
- xmlReader.Close();
- }
- if( xmlInputStream!=null )
- {
- xmlInputStream.Close();
- }
+ extraFiles.Add(fileName);
}
}
@@ -128,13 +139,16 @@ public IList GetHbmFiles()
// need to spend the time doing that then don't bother.
if( containsExtends )
{
- return OrderedHbmFiles( _classes );
+ string[] extraFilesArray = new string[extraFiles.Count];
+ extraFiles.CopyTo(extraFilesArray, 0);
+ StringCollection result = OrderedHbmFiles( _classes );
+ result.AddRange(extraFilesArray);
+ return result;
}
else
{
return _hbmResources;
}
-
}
/// <summary>
@@ -145,7 +159,7 @@ public IList GetHbmFiles()
/// <returns>
/// An <see cref="IList"/> of <see cref="String"/> objects that contain the <c>hbm.xml</c> file names.
/// </returns>
- private IList OrderedHbmFiles(IList unorderedClasses)
+ private StringCollection OrderedHbmFiles(IList unorderedClasses)
{
// Make sure joined-subclass mappings are loaded after base class
ArrayList sortedList = new ArrayList();
View
2  src/NHibernate/Cfg/Configuration.cs
@@ -621,7 +621,7 @@ public Configuration AddAssembly( Assembly assembly, bool skipOrdering )
}
else
{
- AssemblyHbmOrderer orderer = new AssemblyHbmOrderer( assembly );
+ AssemblyHbmOrderer orderer = AssemblyHbmOrderer.CreateWithAllResourcesIn( assembly );
resources = orderer.GetHbmFiles();
}
Please sign in to comment.
Something went wrong with that request. Please try again.