Skip to content

Commit 8b63946

Browse files
committed
Cleanup code
1 parent 1dcdbb1 commit 8b63946

File tree

6 files changed

+128
-99
lines changed

6 files changed

+128
-99
lines changed

src/NHibernate.Test/NHSpecificTest/NH3772/CustomGenericCollection.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,63 @@
77
using NHibernate.Persister.Collection;
88
using NHibernate.UserTypes;
99

10-
namespace NHibernate.Test.NHSpecificTest.NH3772 {
11-
public class CustomGenericCollection<T> : IUserCollectionType where T : class {
12-
// Whether or not to use the failing test behavior, since it causes issues with TearDown
13-
public static bool TestBehavior = false;
10+
namespace NHibernate.Test.NHSpecificTest.NH3772
11+
{
12+
public class CustomGenericCollection<T> : IUserCollectionType where T : class
13+
{
14+
public static bool InstantiateInitializedCollection = true;
1415

15-
public IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister) {
16-
if (!TestBehavior) return new PersistentGenericSet<T>(session);
17-
18-
return new PersistentGenericSet<T>(session, new HashSet<T>(EqualityComparer<T>.Default));
16+
public IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister)
17+
{
18+
if (InstantiateInitializedCollection)
19+
return new PersistentGenericSet<T>(session, new HashSet<T>(EqualityComparer<T>.Default));
20+
21+
return new PersistentGenericSet<T>(session);
1922
}
2023

21-
public IPersistentCollection Wrap(ISessionImplementor session, object collection) {
24+
public IPersistentCollection Wrap(ISessionImplementor session, object collection)
25+
{
2226
var realCollection = (ISet<T>) collection;
2327
return new PersistentGenericSet<T>(session, realCollection);
2428
}
2529

26-
public IEnumerable GetElements(object collection) {
30+
public IEnumerable GetElements(object collection)
31+
{
2732
var realCollection = (ISet<T>) collection;
2833
return realCollection.ToList();
2934
}
3035

31-
public bool Contains(object collection, object entity) {
36+
public bool Contains(object collection, object entity)
37+
{
3238
var realCollection = (ISet<T>) collection;
3339
return realCollection.Contains((T) entity);
3440
}
3541

36-
public object IndexOf(object collection, object entity) {
42+
public object IndexOf(object collection, object entity)
43+
{
3744
return -1; // no indexing supported
3845
}
3946

40-
public object ReplaceElements(object original, object target, ICollectionPersister persister, object owner,
41-
IDictionary copyCache, ISessionImplementor session) {
47+
public object ReplaceElements(
48+
object original,
49+
object target,
50+
ICollectionPersister persister,
51+
object owner,
52+
IDictionary copyCache,
53+
ISessionImplementor session)
54+
{
4255
var originalCollection = (ISet<T>) original;
4356
var targetCollection = (ISet<T>) target;
4457

4558
targetCollection.Clear();
46-
Utils.AddRange(targetCollection, originalCollection);
59+
targetCollection.UnionWith(originalCollection);
4760

4861
return targetCollection;
4962
}
5063

51-
public object Instantiate(int anticipatedSize) {
64+
public object Instantiate(int anticipatedSize)
65+
{
5266
return new HashSet<T>(EqualityComparer<T>.Default);
5367
}
5468
}
55-
}
69+
}

src/NHibernate.Test/NHSpecificTest/NH3772/Entity.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Collections.Generic;
52

6-
namespace NHibernate.Test.NHSpecificTest.NH3772 {
3+
namespace NHibernate.Test.NHSpecificTest.NH3772
4+
{
75
public class Entity
86
{
97
private ICollection<SubEntity> _subEntities;
108
public virtual int Id { get; set; }
119
public virtual string Name { get; set; }
1210

13-
public virtual ICollection<SubEntity> SubEntities {
14-
get { return this._subEntities ?? (this._subEntities = new HashSet<SubEntity>()); }
15-
set { this._subEntities = value; }
11+
public virtual ICollection<SubEntity> SubEntities
12+
{
13+
get => _subEntities ?? (_subEntities = new HashSet<SubEntity>());
14+
set => _subEntities = value;
1615
}
1716
}
1817

Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,107 @@
1-
using System.Diagnostics;
2-
using System.Linq;
1+
using System.Linq;
32
using NHibernate.Cfg.MappingSchema;
4-
using NHibernate.Linq;
53
using NHibernate.Mapping.ByCode;
64
using NUnit.Framework;
75

8-
namespace NHibernate.Test.NHSpecificTest.NH3772 {
9-
public class Fixture : TestCaseMappingByCode {
10-
protected override HbmMapping GetMappings() {
6+
namespace NHibernate.Test.NHSpecificTest.NH3772
7+
{
8+
public class Fixture : TestCaseMappingByCode
9+
{
10+
protected override HbmMapping GetMappings()
11+
{
1112
var mapper = new ModelMapper();
12-
mapper.Class<Entity>(rc => {
13-
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
14-
rc.Property(x => x.Name);
15-
16-
rc.Set(x => x.SubEntities,
17-
x => {
18-
x.Type<CustomGenericCollection<SubEntity>>();
19-
},
20-
x => {
21-
x.ManyToMany();
22-
});
23-
});
24-
25-
mapper.Class<SubEntity>(rc => {
26-
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
27-
rc.Property(x => x.Name);
28-
});
13+
mapper.Class<Entity>(
14+
rc =>
15+
{
16+
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
17+
rc.Property(x => x.Name);
18+
19+
rc.Set(
20+
x => x.SubEntities,
21+
x => x.Type<CustomGenericCollection<SubEntity>>(),
22+
x => x.ManyToMany());
23+
});
24+
25+
mapper.Class<SubEntity>(
26+
rc =>
27+
{
28+
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
29+
rc.Property(x => x.Name);
30+
});
2931

3032
return mapper.CompileMappingForAllExplicitlyAddedEntities();
3133
}
3234

33-
protected override void OnSetUp() {
34-
using (var session = this.OpenSession()) {
35-
using (var transaction = session.BeginTransaction()) {
36-
var e1 = new Entity {Name = "Bob"};
37-
session.Save(e1);
35+
protected override void OnSetUp()
36+
{
37+
using (var session = OpenSession())
38+
using (var transaction = session.BeginTransaction())
39+
{
40+
session.Save(new Entity {Name = "Bob"});
3841

39-
var e2 = new Entity {Name = "Sally"};
40-
session.Save(e2);
42+
session.Save(new Entity {Name = "Sally"});
4143

42-
var s1 = new SubEntity {Name = "Bob"};
43-
session.Save(s1);
44+
session.Save(new SubEntity {Name = "Bob"});
4445

45-
var s2 = new SubEntity {Name = "Sally"};
46-
session.Save(s2);
47-
48-
session.Flush();
49-
transaction.Commit();
50-
}
46+
session.Save(new SubEntity {Name = "Sally"});
5147

48+
session.Flush();
49+
transaction.Commit();
5250
}
53-
CustomGenericCollection<SubEntity>.TestBehavior = true;
5451
}
5552

56-
protected override void OnTearDown() {
57-
CustomGenericCollection<SubEntity>.TestBehavior = false;
58-
59-
using (var session = this.OpenSession()) {
60-
using (var transaction = session.BeginTransaction()) {
61-
session.Delete("from NHibernate.Test.NHSpecificTest.NH3772.Entity");
62-
session.Delete("from NHibernate.Test.NHSpecificTest.NH3772.SubEntity");
53+
protected override void OnTearDown()
54+
{
55+
using (var session = OpenSession())
56+
using (var transaction = session.BeginTransaction())
57+
{
58+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
6359

64-
session.Flush();
60+
session.Flush();
6561

66-
transaction.Commit();
67-
}
62+
transaction.Commit();
6863
}
6964
}
7065

7166
[Test]
72-
public void CustomCollectionType_ThrowsHibernateException_WhenUserCollectionTypeReturnsInitializedPersistentCollection() {
73-
using (var session = this.OpenSession()) {
74-
using (session.BeginTransaction()) {
75-
TestDelegate action = () => (from e in session.Query<Entity>() where e.Name == "Bob" select e).First();
67+
public void CustomCollectionType_ThrowsHibernateException_WhenUserCollectionTypeReturnsInitializedCollection()
68+
{
69+
CustomGenericCollection<SubEntity>.InstantiateInitializedCollection = true;
70+
71+
using (var session = OpenSession())
72+
using (session.BeginTransaction())
73+
{
74+
TestDelegate action = () => (
75+
from e in session.Query<Entity>()
76+
where e.Name == "Bob"
77+
select e
78+
).First();
79+
80+
Assert.That(
81+
action,
82+
Throws.InstanceOf<HibernateException>().And.Message.EqualTo(
83+
"UserCollectionType.Instantiate should return a non-initialized persistent " +
84+
"collection. Implement UserCollectionType.Instantiate(int anticipatedSize) to " +
85+
"actually create the collection that needs to be wrapped by the persistent collection."));
86+
}
87+
}
7688

77-
Assert.That(action, Throws.InstanceOf<HibernateException>().And.Message.EqualTo("UserCollectionType.Instantiate should return a non-initialized persistent collection. Implement UserCollectionType.Instantiate(int anticipatedSize) to actually create the collection that needs to be wrapped by the persistent collection."));
78-
}
89+
[Test]
90+
public void CustomCollectionType_DoesNotThrowHibernateException_WhenUserCollectionTypeReturnsUninitializedCollection()
91+
{
92+
CustomGenericCollection<SubEntity>.InstantiateInitializedCollection = false;
93+
94+
using (var session = OpenSession())
95+
using (session.BeginTransaction())
96+
{
97+
TestDelegate action = () => (
98+
from e in session.Query<Entity>()
99+
where e.Name == "Bob"
100+
select e
101+
).First();
102+
103+
Assert.That(action, Throws.Nothing);
79104
}
80105
}
81106
}
82-
}
107+
}

src/NHibernate.Test/NHSpecificTest/NH3772/Utils.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/NHibernate/Async/Type/CustomCollectionType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ namespace NHibernate.Type
2222
public partial class CustomCollectionType : CollectionType
2323
{
2424

25-
public override Task<object> ReplaceElementsAsync(object original, object target, object owner, IDictionary copyCache,
26-
ISessionImplementor session, CancellationToken cancellationToken)
25+
public override Task<object> ReplaceElementsAsync(object original, object target, object owner, IDictionary copyCache, ISessionImplementor session, CancellationToken cancellationToken)
2726
{
2827
if (cancellationToken.IsCancellationRequested)
2928
{

src/NHibernate/Type/CustomCollectionType.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ public IUserCollectionType UserType
4747

4848
public override IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister, object key)
4949
{
50-
IPersistentCollection createdCollection = userType.Instantiate(session, persister);
50+
var createdCollection = userType.Instantiate(session, persister);
5151
EnsureNotInitialized(createdCollection);
5252
return createdCollection;
5353
}
5454

55-
private static void EnsureNotInitialized(IPersistentCollection createdCollection)
55+
private static void EnsureNotInitialized(IPersistentCollection createdCollection)
5656
{
57-
if (createdCollection.WasInitialized) {
57+
if (createdCollection.WasInitialized)
58+
{
5859
throw new HibernateException(
59-
"UserCollectionType.Instantiate should return a non-initialized persistent collection. Implement UserCollectionType.Instantiate(int anticipatedSize) to actually create the collection that needs to be wrapped by the persistent collection.");
60+
"UserCollectionType.Instantiate should return a non-initialized persistent collection. " +
61+
"Implement UserCollectionType.Instantiate(int anticipatedSize) to actually create the collection " +
62+
"that needs to be wrapped by the persistent collection.");
6063
}
6164
}
6265

@@ -96,4 +99,4 @@ public override object ReplaceElements(object original, object target, object ow
9699
return userType.ReplaceElements(original, target, cp, owner, copyCache, session);
97100
}
98101
}
99-
}
102+
}

0 commit comments

Comments
 (0)