Skip to content

Commit f3e8419

Browse files
committed
Fixed some dodgy code in the node sorter.. It was trying to remove all the xml nodes so it could re add them (in the correct order), but it only removed half of them.
1 parent 8edee36 commit f3e8419

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@
463463
<Compile Include="FluentInterfaceTests\NaturalIdPartTests.cs" />
464464
<Compile Include="MappingModel\Identity\CompositeIdMappingTester.cs" />
465465
<Compile Include="MappingModel\Output\XmlNaturalIdWriterTester.cs" />
466+
<Compile Include="MappingModel\SortingTests.cs" />
466467
</ItemGroup>
467468
<ItemGroup>
468469
<Content Include="..\..\tools\NHibernate\NHibernate.ByteCode.Castle.dll">
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Xml.Linq;
6+
using FluentNHibernate.MappingModel.Output.Sorting;
7+
using NUnit.Framework;
8+
using System.Xml;
9+
10+
namespace FluentNHibernate.Testing.MappingModel
11+
{
12+
[TestFixture]
13+
public class SortingTests
14+
{
15+
16+
[Test]
17+
public void ShouldSortNodes()
18+
{
19+
var sorter = new XmlClasslikeNodeSorter();
20+
21+
var xml = @"<class><property /><joined-subclass /><many-to-one /><union-subclass /><cache /><key /><one-to-one /></class>";
22+
var expected = @"<class><cache /><key /><one-to-one /><property /><many-to-one /><joined-subclass /><union-subclass /></class>";
23+
24+
var doc = new XmlDocument();
25+
doc.LoadXml(xml);
26+
27+
var node = doc.ChildNodes[0];
28+
sorter.Sort(node);
29+
30+
node.OuterXml.ShouldEqual(expected);
31+
}
32+
33+
[Test]
34+
public void ShouldPreserveOrderingOfNodesThatAreAlreadySorted()
35+
{
36+
var sorter = new XmlClasslikeNodeSorter();
37+
38+
var xml = @"<class><cache /><key /><one-to-one /><property /><many-to-one /><joined-subclass /><union-subclass /></class>";
39+
var doc = new XmlDocument();
40+
doc.LoadXml(xml);
41+
42+
var node = doc.ChildNodes[0];
43+
sorter.Sort(node);
44+
45+
node.OuterXml.ShouldEqual(xml);
46+
}
47+
48+
}
49+
}

src/FluentNHibernate/MappingModel/Output/Sorting/BaseXmlNodeSorter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ public XmlNode Sort(XmlNode node)
4141
return Array.IndexOf(originalSortOrder, x).CompareTo(Array.IndexOf(originalSortOrder, y));
4242
});
4343

44-
for (var i = 0; i < node.ChildNodes.Count; i++)
45-
{
46-
node.RemoveChild(node.ChildNodes[i]);
47-
}
44+
while (node.ChildNodes.Count > 0)
45+
node.RemoveChild(node.ChildNodes[0]);
4846

4947
foreach (var child in children)
5048
{

0 commit comments

Comments
 (0)