Skip to content

Commit bae9080

Browse files
author
Mike Doerfler
committed
some simple tests for generic implementation of <list>
SVN: trunk@1890
1 parent 5978342 commit bae9080

File tree

10 files changed

+198
-27
lines changed

10 files changed

+198
-27
lines changed

src/NHibernate.Test/ListGeneric/A.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#if NET_2_0
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace NHibernate.Test.ListGeneric
8+
{
9+
public class A
10+
{
11+
private int? _id;
12+
private string _name;
13+
private IList<B> _items;
14+
15+
public A() { }
16+
17+
public int? Id
18+
{
19+
get { return _id; }
20+
set { _id = value; }
21+
}
22+
23+
public string Name
24+
{
25+
get { return _name; }
26+
set { _name = value; }
27+
}
28+
29+
public IList<B> Items
30+
{
31+
get { return _items; }
32+
set { _items = value; }
33+
}
34+
35+
}
36+
}
37+
38+
#endif

src/NHibernate.Test/ListGeneric/B.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#if NET_2_0
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace NHibernate.Test.ListGeneric
8+
{
9+
public class B
10+
{
11+
private int? _id;
12+
private string _name;
13+
14+
public B() { }
15+
16+
public int? Id
17+
{
18+
get { return _id; }
19+
set { _id = value; }
20+
}
21+
22+
public string Name
23+
{
24+
get { return _name; }
25+
set { _name = value; }
26+
}
27+
}
28+
}
29+
30+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#if NET_2_0
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.ListGeneric
10+
{
11+
[TestFixture]
12+
public class ListGenericFixture : TestCase
13+
{
14+
15+
protected override System.Collections.IList Mappings
16+
{
17+
get { return new string[] { "ListGeneric.ListGenericFixture.hbm.xml" }; }
18+
}
19+
20+
protected override string MappingsAssembly
21+
{
22+
get { return "NHibernate.Test"; }
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (ISession s = sessions.OpenSession())
28+
{
29+
s.Delete("from A");
30+
s.Flush();
31+
}
32+
}
33+
34+
[Test]
35+
public void Simple()
36+
{
37+
int? newId;
38+
A a = new A();
39+
a.Name = "first generic type";
40+
a.Items = new List<B>();
41+
B firstB = new B();
42+
firstB.Name = "first b";
43+
B secondB = new B();
44+
secondB.Name = "second b";
45+
46+
a.Items.Add(firstB);
47+
a.Items.Add(secondB);
48+
49+
ISession s = OpenSession();
50+
s.SaveOrUpdate(a);
51+
// this flush should test how NH wraps a generic collection with its
52+
// own persistent collection
53+
s.Flush();
54+
s.Close();
55+
Assert.IsNotNull(a.Id);
56+
// should have cascaded down to B
57+
Assert.IsNotNull(firstB.Id);
58+
Assert.IsNotNull(secondB.Id);
59+
60+
s = OpenSession();
61+
a = s.Load<A>(a.Id);
62+
Assert.AreEqual("first b", a.Items[0].Name, "first item should be 'first b'");
63+
Assert.AreEqual("second b", a.Items[1].Name, "second item should be 'second b'");
64+
B thirdB = new B();
65+
thirdB.Name = "third B";
66+
// ensuring the correct generic type was constructed
67+
a.Items.Add(thirdB);
68+
Assert.AreEqual(3, a.Items.Count, "3 items in the bag now");
69+
s.Flush();
70+
s.Close();
71+
}
72+
}
73+
}
74+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="NHibernate.Test" namespace="NHibernate.Test.ListGeneric">
3+
<class name="A" table="a" lazy="false">
4+
<id name="Id" column="id" unsaved-value="null">
5+
<generator class="native" />
6+
</id>
7+
<property name="Name" column="aname" />
8+
<list name="Items" cascade="all-delete-orphan" generic="true">
9+
<key column="a_id" />
10+
<index column="a_idx" />
11+
<one-to-many class="B" />
12+
</list>
13+
</class>
14+
<class name="B" table="b" lazy="false">
15+
<id name="Id" column="id" unsaved-value="null">
16+
<generator class="native" />
17+
</id>
18+
<property name="Name" column="aname" />
19+
</class>
20+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test-2.0.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
<Compile Include="Legacy\SimpleTest.cs" />
107107
<Compile Include="Legacy\SQLFunctionsTest.cs" />
108108
<Compile Include="Legacy\SQLLoaderTest.cs" />
109+
<Compile Include="ListGeneric\A.cs" />
110+
<Compile Include="ListGeneric\B.cs" />
111+
<Compile Include="ListGeneric\ListGenericFixture.cs" />
109112
<Compile Include="Log4netConfiguration.cs" />
110113
<Compile Include="MappingExceptions\A.cs" />
111114
<Compile Include="MappingExceptions\AddClassFixture.cs" />
@@ -383,6 +386,7 @@
383386
</ItemGroup>
384387
<ItemGroup>
385388
<EmbeddedResource Include="BagGeneric\BagGenericFixture.hbm.xml" />
389+
<EmbeddedResource Include="ListGeneric\ListGenericFixture.hbm.xml" />
386390
<Content Include="NHSpecificTest\NH386\Mappings.hbm.xml" />
387391
<Content Include="NHSpecificTest\NH392\Mappings.hbm.xml" />
388392
<Content Include="NHSpecificTest\NH401\Mappings.hbm.xml" />

src/NHibernate/Collection/Generic/PersistentGenericList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public override object ReadFrom(IDataReader reader, ICollectionPersister persist
195195
int index = (int)persister.ReadIndex(reader, this.Session);
196196

197197
// pad with null from the current last element up to the new index
198-
for (int i = list.Count; i < index; i++)
198+
for (int i = list.Count; i <= index; i++)
199199
{
200200
list.Insert(i, default(T));
201201
}

src/NHibernate/Mapping/List.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ public override PersistentCollectionType CollectionType
2626
get
2727
{
2828
#if NET_2_0
29-
// TODO: uncomment this once PersistentGenricList is actually
30-
// implemented
31-
//if (this.IsGeneric)
32-
//{
33-
// return TypeFactory.GenericList( Role, this.Element.Type.ReturnedClass );
34-
//}
35-
//else
36-
//{
29+
if (this.IsGeneric)
30+
{
31+
return TypeFactory.GenericList( Role, this.Element.Type.ReturnedClass );
32+
}
33+
else
34+
{
3735
return TypeFactory.List( Role );
38-
//}
36+
}
3937
#else
4038
return TypeFactory.List( Role );
4139
#endif

src/NHibernate/Type/BagType.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public BagType( string role ) : base( role )
2626
/// </summary>
2727
/// <param name="session">The current <see cref="ISessionImplementor"/> for the bag.</param>
2828
/// <param name="persister"></param>
29-
/// <returns></returns>
30-
public override IPersistentCollection Instantiate( ISessionImplementor session, ICollectionPersister persister )
29+
/// <returns>A new <see cref="NHibernate.Collections.Bag"/>.</returns>
30+
public override IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister)
3131
{
3232
return new Bag( session );
3333
}
@@ -39,7 +39,7 @@ public override System.Type ReturnedClass
3939
}
4040

4141
/// <summary>
42-
/// Wraps an <see cref="IList"/> in a <see cref="Bag"/>.
42+
/// Wraps an <see cref="IList"/> in a NHibernate <see cref="Bag"/>.
4343
/// </summary>
4444
/// <param name="session">The <see cref="ISessionImplementor"/> for the collection to be a part of.</param>
4545
/// <param name="collection">The unwrapped <see cref="IList"/>.</param>

src/NHibernate/Type/GenericListType.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace NHibernate.Type
1313
{
1414
/// <summary>
1515
/// An <see cref="IType"/> that maps an <see cref="IList&lt;T&gt;"/> collection
16-
/// to the database using bag semantics.
16+
/// to the database using list semantics.
1717
/// </summary>
1818
public class GenericListType : ListType
1919
{

src/NHibernate/Type/ListType.cs

+20-13
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
namespace NHibernate.Type
66
{
7-
/// <summary></summary>
7+
/// <summary>
8+
/// An <see cref="IType"/> that maps an <see cref="IList"/> collection
9+
/// using list semantics to the database.
10+
/// </summary>
811
public class ListType : PersistentCollectionType
912
{
1013
/// <summary>
11-
///
14+
/// Initializes a new instance of a <see cref="ListType"/> class for
15+
/// a specific role.
1216
/// </summary>
13-
/// <param name="role"></param>
14-
public ListType( string role ) : base( role )
17+
/// <param name="role">The role the persistent collection is in.</param>
18+
public ListType(string role)
19+
: base(role)
1520
{
1621
}
1722

1823
/// <summary>
19-
///
24+
/// Instantiates a new <see cref="IPersistentCollection"/> for the bag.
2025
/// </summary>
21-
/// <param name="session"></param>
26+
/// <param name="session">The current <see cref="ISessionImplementor"/> for the bag.</param>
2227
/// <param name="persister"></param>
23-
/// <returns></returns>
24-
public override IPersistentCollection Instantiate( ISessionImplementor session, ICollectionPersister persister )
28+
/// <returns>A new <see cref="NHibernate.Collections.List"/>.</returns>
29+
public override IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister)
2530
{
2631
return new List( session );
2732
}
@@ -33,12 +38,14 @@ public override System.Type ReturnedClass
3338
}
3439

3540
/// <summary>
36-
///
41+
/// Wraps an exist <see cref="IList"/> in a NHibernate <see cref="List"/>.
3742
/// </summary>
38-
/// <param name="session"></param>
39-
/// <param name="collection"></param>
40-
/// <returns></returns>
41-
public override IPersistentCollection Wrap( ISessionImplementor session, object collection )
43+
/// <param name="session">The <see cref="ISessionImplementor"/> for the collection to be a part of.</param>
44+
/// <param name="collection">The unwrapped <see cref="IList"/>.</param>
45+
/// <returns>
46+
/// An <see cref="List"/> that wraps the non NHibernate <see cref="IList"/>.
47+
/// </returns>
48+
public override IPersistentCollection Wrap(ISessionImplementor session, object collection)
4249
{
4350
return new List( session, ( IList ) collection );
4451
}

0 commit comments

Comments
 (0)