Skip to content

Commit 38164e7

Browse files
committed
Rewrite tests for custom collections that use PersistentList to instead be based on PersistentGenericList.
1 parent 82ad2c9 commit 38164e7

File tree

10 files changed

+72
-54
lines changed

10 files changed

+72
-54
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
2-
using System.Collections;
2+
using System.Collections.Generic;
33

44
namespace NHibernate.Test.UserCollection
55
{
6-
public class MyList : ArrayList
6+
public class MyList : List<Email>
77
{
88
}
99
}

src/NHibernate.Test/UserCollection/MyListType.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using NHibernate.Collection;
45
using NHibernate.Engine;
56
using NHibernate.Persister.Collection;
@@ -16,7 +17,7 @@ public IPersistentCollection Instantiate(ISessionImplementor session, ICollectio
1617

1718
public IPersistentCollection Wrap(ISessionImplementor session, object collection)
1819
{
19-
return new PersistentMylist(session, (IList) collection);
20+
return new PersistentMylist(session, (IList<Email>) collection);
2021
}
2122

2223
public IEnumerable GetElements(object collection)
@@ -26,23 +27,31 @@ public IEnumerable GetElements(object collection)
2627

2728
public bool Contains(object collection, object entity)
2829
{
29-
return ((IList) collection).Contains(entity);
30+
var email = entity as Email;
31+
if (entity != null && email == null)
32+
return false;
33+
34+
return ((IList<Email>) collection).Contains(email);
3035
}
3136

3237
public object IndexOf(object collection, object entity)
3338
{
34-
return ((IList) collection).IndexOf(entity);
39+
var email = entity as Email;
40+
if (entity != null && email == null)
41+
return -1;
42+
43+
return ((IList<Email>)collection).IndexOf(email);
3544
}
3645

3746
public object ReplaceElements(object original, object target, ICollectionPersister persister, object owner,
3847
IDictionary copyCache, ISessionImplementor session)
3948
{
40-
IList result = (IList) target;
49+
IList<Email> result = (IList<Email>) target;
4150
result.Clear();
51+
4252
foreach (object o in ((IEnumerable) original))
43-
{
44-
result.Add(o);
45-
}
53+
result.Add((Email) o);
54+
4655
return result;
4756
}
4857

@@ -51,4 +60,4 @@ public object Instantiate(int anticipatedSize)
5160
return new MyList();
5261
}
5362
}
54-
}
63+
}

src/NHibernate.Test/UserCollection/Parameterized/DefaultableListImpl.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
using System.Collections;
1+
using System.Collections.Generic;
2+
23
namespace NHibernate.Test.UserCollection.Parameterized
34
{
4-
public class DefaultableListImpl : ArrayList, IDefaultableList
5+
public class DefaultableListImpl : List<string>, IDefaultableList
56
{
6-
private string defaultValue;
77
public DefaultableListImpl() {}
88
public DefaultableListImpl(int capacity) : base(capacity) {}
99

1010
#region IDefaultableList Members
1111

12-
public string DefaultValue
13-
{
14-
get { return defaultValue; }
15-
set { defaultValue = value; }
16-
}
12+
public string DefaultValue { get; set; }
1713

1814
#endregion
1915
}

src/NHibernate.Test/UserCollection/Parameterized/DefaultableListType.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IPersistentCollection Wrap(ISessionImplementor session, object collection
2727
}
2828
else
2929
{
30-
return new PersistentDefaultableList(session, (IList)collection);
30+
return new PersistentDefaultableList(session, (IList<string>)collection);
3131
}
3232
}
3333

@@ -38,23 +38,31 @@ public IEnumerable GetElements(object collection)
3838

3939
public bool Contains(object collection, object entity)
4040
{
41-
return ((IDefaultableList)collection).Contains(entity);
41+
var item = entity as string;
42+
if (entity != null && item == null)
43+
return false;
44+
45+
return ((IDefaultableList)collection).Contains(item);
4246
}
4347

4448
public object IndexOf(object collection, object entity)
4549
{
46-
int index = ((IDefaultableList)collection).IndexOf(entity);
50+
var item = entity as string;
51+
if (entity != null && item == null)
52+
return null;
53+
54+
int index = ((IDefaultableList)collection).IndexOf(item);
4755
return index >= 0 ? (object) index : null;
4856
}
4957

5058
public object ReplaceElements(object original, object target, ICollectionPersister persister, object owner, IDictionary copyCache, ISessionImplementor session)
5159
{
5260
IDefaultableList result = (IDefaultableList)target;
5361
result.Clear();
54-
foreach (object o in (IDefaultableList)original)
55-
{
62+
63+
foreach (string o in (IDefaultableList) original)
5664
result.Add(o);
57-
}
65+
5866
return result;
5967
}
6068

src/NHibernate.Test/UserCollection/Parameterized/Entity.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Collections;
2+
using System.Collections.Generic;
23

34
namespace NHibernate.Test.UserCollection.Parameterized
45
{
56
public class Entity
67
{
78
private string name;
8-
private IList values = new ArrayList();
9+
private IList<string> values = new List<string>();
910

1011
public Entity() {}
1112

@@ -19,7 +20,7 @@ public virtual string Name
1920
get { return name; }
2021
}
2122

22-
public virtual IList Values
23+
public virtual IList<string> Values
2324
{
2425
get { return values; }
2526
set { values = value; }

src/NHibernate.Test/UserCollection/Parameterized/IDefaultableList.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System.Collections;
1+
using System.Collections.Generic;
2+
23
namespace NHibernate.Test.UserCollection.Parameterized
34
{
4-
public interface IDefaultableList : IList
5+
public interface IDefaultableList : IList<string>
56
{
67
string DefaultValue { get;}
78
}

src/NHibernate.Test/UserCollection/Parameterized/ParameterizedUserCollectionTypeFixture.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ protected override IList Mappings
1919
[Test]
2020
public void BasicOperation()
2121
{
22-
ISession s = OpenSession();
23-
ITransaction t = s.BeginTransaction();
24-
Entity entity = new Entity("tester");
25-
entity.Values.Add("value-1");
26-
s.Persist(entity);
27-
t.Commit();
28-
s.Close();
22+
using (ISession s = OpenSession())
23+
using (ITransaction t = s.BeginTransaction())
24+
{
25+
var entity = new Entity("tester");
26+
entity.Values.Add("value-1");
27+
s.Persist(entity);
28+
t.Commit();
29+
}
2930

30-
s = OpenSession();
31-
t = s.BeginTransaction();
32-
entity = s.Get<Entity>("tester");
33-
Assert.IsTrue(NHibernateUtil.IsInitialized(entity.Values));
34-
Assert.AreEqual(1, entity.Values.Count);
35-
Assert.AreEqual("Hello", ((IDefaultableList)entity.Values).DefaultValue);
36-
s.Delete(entity);
37-
t.Commit();
38-
s.Close();
31+
using (var s = OpenSession())
32+
using (var t = s.BeginTransaction())
33+
{
34+
var entity = s.Get<Entity>("tester");
35+
Assert.IsTrue(NHibernateUtil.IsInitialized(entity.Values));
36+
Assert.AreEqual(1, entity.Values.Count);
37+
Assert.AreEqual("Hello", ((IDefaultableList) entity.Values).DefaultValue);
38+
39+
s.Delete(entity);
40+
t.Commit();
41+
}
3942
}
4043
}
4144
}

src/NHibernate.Test/UserCollection/Parameterized/PersistentDefaultableList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
using System.Collections;
2-
using NHibernate.Collection;
1+
using System.Collections.Generic;
2+
using NHibernate.Collection.Generic;
33
using NHibernate.Engine;
44

55
namespace NHibernate.Test.UserCollection.Parameterized
66
{
7-
public class PersistentDefaultableList: PersistentList, IDefaultableList
7+
public class PersistentDefaultableList: PersistentGenericList<string>, IDefaultableList
88
{
99
public PersistentDefaultableList() {}
1010

1111
public PersistentDefaultableList(ISessionImplementor session) : base(session) {}
1212

13-
public PersistentDefaultableList(ISessionImplementor session, IList list) : base(session, list) {}
13+
public PersistentDefaultableList(ISessionImplementor session, IList<string> list) : base(session, list) {}
1414

1515
public string DefaultValue
1616
{

src/NHibernate.Test/UserCollection/PersistentMylist.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
2-
using System.Collections;
3-
using NHibernate.Collection;
2+
using System.Collections.Generic;
3+
using NHibernate.Collection.Generic;
44
using NHibernate.Engine;
55

66
namespace NHibernate.Test.UserCollection
77
{
8-
public class PersistentMylist : PersistentList
8+
public class PersistentMylist : PersistentGenericList<Email>
99
{
10-
public PersistentMylist(ISessionImplementor session, IList list) : base(session, list)
10+
public PersistentMylist(ISessionImplementor session, IList<Email> list) : base(session, list)
1111
{
1212
}
1313

src/NHibernate.Test/UserCollection/User.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NHibernate.Test.UserCollection
77
public class User
88
{
99
private string userName;
10-
private IList emailAddresses = new MyList();
10+
private IList<Email> emailAddresses = new MyList();
1111
private ISet<object> sessionData = new HashSet<object>();
1212

1313
public string UserName
@@ -16,7 +16,7 @@ public string UserName
1616
set { userName = value; }
1717
}
1818

19-
public IList EmailAddresses
19+
public IList<Email> EmailAddresses
2020
{
2121
get { return emailAddresses; }
2222
set { emailAddresses = value; }

0 commit comments

Comments
 (0)