From c8ee04436975e4e80fe7b68867971bb8f7f89f47 Mon Sep 17 00:00:00 2001 From: Oskar Berggren Date: Sun, 20 Nov 2016 23:51:24 +0100 Subject: [PATCH] Fix session handling in a bunch of unit tests to close responsibly. --- .../ClassWithCompositeIdFixture.cs | 200 ++-- .../NHSpecificTest/MapFixture.cs | 257 +++-- .../Operations/MergeFixture.cs | 497 ++++----- .../ReadOnly/ReadOnlyVersionedNodes.cs | 949 +++++++++--------- 4 files changed, 995 insertions(+), 908 deletions(-) diff --git a/src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs b/src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs index 1aa9514f69c..234a6c73f14 100644 --- a/src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs +++ b/src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs @@ -76,97 +76,99 @@ protected override void OnTearDown() [Test] public void TestSimpleCRUD() { - // insert the new objects - ISession s = OpenSession(); - ITransaction t = s.BeginTransaction(); + ClassWithCompositeId theClass; + ClassWithCompositeId theSecondClass; - ClassWithCompositeId theClass = new ClassWithCompositeId(id); - theClass.OneProperty = 5; + // insert the new objects + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + theClass = new ClassWithCompositeId(id); + theClass.OneProperty = 5; - ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId); - theSecondClass.OneProperty = 10; + theSecondClass = new ClassWithCompositeId(secondId); + theSecondClass.OneProperty = 10; - s.Save(theClass); - s.Save(theSecondClass); + s.Save(theClass); + s.Save(theSecondClass); - t.Commit(); - s.Close(); + t.Commit(); + } // verify they were inserted and test the SELECT + ClassWithCompositeId theClass2; + ClassWithCompositeId theSecondClass2; + using (ISession s2 = OpenSession()) + using (ITransaction t2 = s2.BeginTransaction()) + { + theClass2 = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id); + Assert.AreEqual(id, theClass2.Id); - ISession s2 = OpenSession(); - ITransaction t2 = s2.BeginTransaction(); - - ClassWithCompositeId theClass2 = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id); - Assert.AreEqual(id, theClass2.Id); - - IList results2 = s2.CreateCriteria(typeof(ClassWithCompositeId)) - .Add(Expression.Eq("Id", secondId)) - .List(); + IList results2 = s2.CreateCriteria(typeof(ClassWithCompositeId)) + .Add(Expression.Eq("Id", secondId)) + .List(); - Assert.AreEqual(1, results2.Count); - ClassWithCompositeId theSecondClass2 = (ClassWithCompositeId) results2[0]; + Assert.AreEqual(1, results2.Count); + theSecondClass2 = (ClassWithCompositeId) results2[0]; - ClassWithCompositeId theClass2Copy = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id); + ClassWithCompositeId theClass2Copy = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id); - // verify the same results through Criteria & Load were achieved - Assert.AreSame(theClass2, theClass2Copy); + // verify the same results through Criteria & Load were achieved + Assert.AreSame(theClass2, theClass2Copy); - // compare them to the objects created in the first session - Assert.AreEqual(theClass.Id, theClass2.Id); - Assert.AreEqual(theClass.OneProperty, theClass2.OneProperty); + // compare them to the objects created in the first session + Assert.AreEqual(theClass.Id, theClass2.Id); + Assert.AreEqual(theClass.OneProperty, theClass2.OneProperty); - Assert.AreEqual(theSecondClass.Id, theSecondClass2.Id); - Assert.AreEqual(theSecondClass.OneProperty, theSecondClass2.OneProperty); + Assert.AreEqual(theSecondClass.Id, theSecondClass2.Id); + Assert.AreEqual(theSecondClass.OneProperty, theSecondClass2.OneProperty); - // test the update functionallity - theClass2.OneProperty = 6; - theSecondClass2.OneProperty = 11; + // test the update functionallity + theClass2.OneProperty = 6; + theSecondClass2.OneProperty = 11; - s2.Update(theClass2); - s2.Update(theSecondClass2); + s2.Update(theClass2); + s2.Update(theSecondClass2); - t2.Commit(); - s2.Close(); + t2.Commit(); + } // lets verify the update went through - ISession s3 = OpenSession(); - ITransaction t3 = s3.BeginTransaction(); - - ClassWithCompositeId theClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), id); - ClassWithCompositeId theSecondClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), secondId); + using (ISession s3 = OpenSession()) + using (ITransaction t3 = s3.BeginTransaction()) + { + ClassWithCompositeId theClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), id); + ClassWithCompositeId theSecondClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), secondId); - // check the update properties - Assert.AreEqual(theClass3.OneProperty, theClass2.OneProperty); - Assert.AreEqual(theSecondClass3.OneProperty, theSecondClass2.OneProperty); + // check the update properties + Assert.AreEqual(theClass3.OneProperty, theClass2.OneProperty); + Assert.AreEqual(theSecondClass3.OneProperty, theSecondClass2.OneProperty); - // test the delete method - s3.Delete(theClass3); - s3.Delete(theSecondClass3); + // test the delete method + s3.Delete(theClass3); + s3.Delete(theSecondClass3); - t3.Commit(); - s3.Close(); + t3.Commit(); + } // lets verify the delete went through - ISession s4 = OpenSession(); - - try + using (ISession s4 = OpenSession()) { - ClassWithCompositeId theClass4 = (ClassWithCompositeId) s4.Load(typeof(ClassWithCompositeId), id); + try + { + ClassWithCompositeId theClass4 = (ClassWithCompositeId) s4.Load(typeof(ClassWithCompositeId), id); + } + catch (ObjectNotFoundException) + { + // I expect this to be thrown because the object no longer exists... + } + + IList results = s4.CreateCriteria(typeof(ClassWithCompositeId)) + .Add(Expression.Eq("Id", secondId)) + .List(); + + Assert.AreEqual(0, results.Count); } - catch (ObjectNotFoundException onfe) - { - // I expect this to be thrown because the object no longer exists... - Assert.IsNotNull(onfe); //getting ride of 'onfe' is never used compile warning - } - - IList results = s4.CreateCriteria(typeof(ClassWithCompositeId)) - .Add(Expression.Eq("Id", secondId)) - .List(); - - Assert.AreEqual(0, results.Count); - - s4.Close(); } [Test] @@ -178,53 +180,53 @@ public void Criteria() // add the new instance to the session so I have something to get results // back for - ISession s = OpenSession(); - s.Save(cId); - s.Flush(); - s.Close(); - - s = OpenSession(); - ICriteria c = s.CreateCriteria(typeof(ClassWithCompositeId)); - c.Add(Expression.Eq("Id", id)); + using (ISession s = OpenSession()) + { + s.Save(cId); + s.Flush(); + } - // right now just want to see if the Criteria is valid - IList results = c.List(); + using (ISession s = OpenSession()) + { + ICriteria c = s.CreateCriteria(typeof(ClassWithCompositeId)); + c.Add(Expression.Eq("Id", id)); - Assert.AreEqual(1, results.Count); + // right now just want to see if the Criteria is valid + IList results = c.List(); - s.Close(); + Assert.AreEqual(1, results.Count); + } } [Test] public void Hql() { // insert the new objects - ISession s = OpenSession(); - ITransaction t = s.BeginTransaction(); - - ClassWithCompositeId theClass = new ClassWithCompositeId(id); - theClass.OneProperty = 5; - - ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId); - theSecondClass.OneProperty = 10; - - s.Save(theClass); - s.Save(theSecondClass); + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + ClassWithCompositeId theClass = new ClassWithCompositeId(id); + theClass.OneProperty = 5; - t.Commit(); - s.Close(); + ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId); + theSecondClass.OneProperty = 10; - ISession s2 = OpenSession(); + s.Save(theClass); + s.Save(theSecondClass); - IQuery hql = s2.CreateQuery("from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString"); + t.Commit(); + } - hql.SetString("keyString", id.KeyString); + using (ISession s2 = OpenSession()) + { + IQuery hql = s2.CreateQuery("from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString"); - IList results = hql.List(); + hql.SetString("keyString", id.KeyString); - Assert.AreEqual(1, results.Count); + IList results = hql.List(); - s2.Close(); + Assert.AreEqual(1, results.Count); + } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/NHSpecificTest/MapFixture.cs b/src/NHibernate.Test/NHSpecificTest/MapFixture.cs index f932861aba0..c56d80d48be 100644 --- a/src/NHibernate.Test/NHSpecificTest/MapFixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/MapFixture.cs @@ -53,23 +53,23 @@ public void TestSelect() { TestInsert(); - ISession s = OpenSession(); - ITransaction t = s.BeginTransaction(); - - ICriteria chiefsCriteria = s.CreateCriteria(typeof(Team)); - chiefsCriteria.Add(Expression.Eq("Name", "Chiefs")); + using(ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + ICriteria chiefsCriteria = s.CreateCriteria(typeof(Team)); + chiefsCriteria.Add(Expression.Eq("Name", "Chiefs")); - Team chiefs = (Team) chiefsCriteria.List()[0]; - IList players = chiefs.Players; + Team chiefs = (Team) chiefsCriteria.List()[0]; + IList players = chiefs.Players; - Parent parentDad = (Parent) s.Load(typeof(Parent), 1); - Child amyJones = (Child) s.Load(typeof(Child), 2); - Child[] friends = amyJones.Friends; + Parent parentDad = (Parent) s.Load(typeof(Parent), 1); + Child amyJones = (Child) s.Load(typeof(Child), 2); + Child[] friends = amyJones.Friends; - Child childOneRef = amyJones.FirstSibling; + Child childOneRef = amyJones.FirstSibling; - t.Commit(); - s.Close(); + t.Commit(); + } } [Test] @@ -77,131 +77,130 @@ public void TestSort() { TestInsert(); - ISession s = OpenSession(); - ITransaction t = s.BeginTransaction(); + using(ISession s = OpenSession()) + using(ITransaction t = s.BeginTransaction()) + { + Parent bobJones = (Parent) s.Load(typeof(Parent), 1); + ISet friends = bobJones.AdultFriends; + int currentId = 0; + int previousId = 0; - Parent bobJones = (Parent) s.Load(typeof(Parent), 1); - ISet friends = bobJones.AdultFriends; + foreach (Parent friend in friends) + { + previousId = currentId; + currentId = friend.Id; - int currentId = 0; - int previousId = 0; + Assert.IsTrue(currentId > previousId, "Current should have a higher Id than previous"); + } - foreach (Parent friend in friends) - { - previousId = currentId; - currentId = friend.Id; - - Assert.IsTrue(currentId > previousId, "Current should have a higher Id than previous"); + t.Commit(); } - - t.Commit(); - s.Close(); } [Test] public void TestInsert() { - ISession s = OpenSession(); - ITransaction t = s.BeginTransaction(); - - SexType male = new SexType(); - SexType female = new SexType(); - - //male.Id = 1; - male.TypeName = "Male"; - - //female.Id = 2; - female.TypeName = "Female"; - - s.Save(male); - s.Save(female); - - Parent bobJones = new Parent(); - bobJones.Id = 1; - bobJones.AdultName = "Bob Jones"; - - Parent maryJones = new Parent(); - maryJones.Id = 2; - maryJones.AdultName = "Mary Jones"; - - Parent charlieSmith = new Parent(); - charlieSmith.Id = 3; - charlieSmith.AdultName = "Charlie Smith"; - - Parent cindySmith = new Parent(); - cindySmith.Id = 4; - cindySmith.AdultName = "Cindy Smith"; - - bobJones.AddFriend(cindySmith); - bobJones.AddFriend(charlieSmith); - bobJones.AddFriend(maryJones); - maryJones.AddFriend(cindySmith); - - s.Save(bobJones, bobJones.Id); - s.Save(maryJones, maryJones.Id); - s.Save(charlieSmith, charlieSmith.Id); - s.Save(cindySmith, cindySmith.Id); - - Child johnnyJones = new Child(); - Child amyJones = new Child(); - Child brianSmith = new Child(); - Child sarahSmith = new Child(); - - johnnyJones.Id = 1; - johnnyJones.FullName = "Johnny Jones"; - johnnyJones.Dad = bobJones; - johnnyJones.Mom = maryJones; - johnnyJones.Sex = male; - johnnyJones.Friends = new Child[] {brianSmith, sarahSmith}; - johnnyJones.FavoriteDate = DateTime.Parse("2003-08-16"); - - amyJones.Id = 2; - amyJones.FullName = "Amy Jones"; - amyJones.Dad = bobJones; - amyJones.Mom = maryJones; - amyJones.Sex = female; - amyJones.FirstSibling = johnnyJones; - amyJones.Friends = new Child[] {johnnyJones, sarahSmith}; - - brianSmith.Id = 11; - brianSmith.FullName = "Brian Smith"; - brianSmith.Dad = charlieSmith; - brianSmith.Mom = cindySmith; - brianSmith.Sex = male; - brianSmith.Friends = new Child[] {johnnyJones, amyJones, sarahSmith}; - - sarahSmith.Id = 12; - sarahSmith.FullName = "Sarah Smith"; - sarahSmith.Dad = charlieSmith; - sarahSmith.Mom = cindySmith; - sarahSmith.Sex = female; - sarahSmith.Friends = new Child[] {brianSmith}; - - Team royals = new Team(); - royals.Name = "Royals"; - - Team chiefs = new Team(); - chiefs.Name = "Chiefs"; - - royals.Players = new List(); - royals.Players.Add(amyJones); - royals.Players.Add(brianSmith); - - chiefs.Players = new List(); - chiefs.Players.Add(johnnyJones); - chiefs.Players.Add(sarahSmith); - - s.Save(johnnyJones, johnnyJones.Id); - s.Save(amyJones, amyJones.Id); - s.Save(brianSmith, brianSmith.Id); - s.Save(sarahSmith, sarahSmith.Id); - - s.Save(royals); - s.Save(chiefs); - - t.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + SexType male = new SexType(); + SexType female = new SexType(); + + //male.Id = 1; + male.TypeName = "Male"; + + //female.Id = 2; + female.TypeName = "Female"; + + s.Save(male); + s.Save(female); + + Parent bobJones = new Parent(); + bobJones.Id = 1; + bobJones.AdultName = "Bob Jones"; + + Parent maryJones = new Parent(); + maryJones.Id = 2; + maryJones.AdultName = "Mary Jones"; + + Parent charlieSmith = new Parent(); + charlieSmith.Id = 3; + charlieSmith.AdultName = "Charlie Smith"; + + Parent cindySmith = new Parent(); + cindySmith.Id = 4; + cindySmith.AdultName = "Cindy Smith"; + + bobJones.AddFriend(cindySmith); + bobJones.AddFriend(charlieSmith); + bobJones.AddFriend(maryJones); + maryJones.AddFriend(cindySmith); + + s.Save(bobJones, bobJones.Id); + s.Save(maryJones, maryJones.Id); + s.Save(charlieSmith, charlieSmith.Id); + s.Save(cindySmith, cindySmith.Id); + + Child johnnyJones = new Child(); + Child amyJones = new Child(); + Child brianSmith = new Child(); + Child sarahSmith = new Child(); + + johnnyJones.Id = 1; + johnnyJones.FullName = "Johnny Jones"; + johnnyJones.Dad = bobJones; + johnnyJones.Mom = maryJones; + johnnyJones.Sex = male; + johnnyJones.Friends = new Child[] {brianSmith, sarahSmith}; + johnnyJones.FavoriteDate = DateTime.Parse("2003-08-16"); + + amyJones.Id = 2; + amyJones.FullName = "Amy Jones"; + amyJones.Dad = bobJones; + amyJones.Mom = maryJones; + amyJones.Sex = female; + amyJones.FirstSibling = johnnyJones; + amyJones.Friends = new Child[] {johnnyJones, sarahSmith}; + + brianSmith.Id = 11; + brianSmith.FullName = "Brian Smith"; + brianSmith.Dad = charlieSmith; + brianSmith.Mom = cindySmith; + brianSmith.Sex = male; + brianSmith.Friends = new Child[] {johnnyJones, amyJones, sarahSmith}; + + sarahSmith.Id = 12; + sarahSmith.FullName = "Sarah Smith"; + sarahSmith.Dad = charlieSmith; + sarahSmith.Mom = cindySmith; + sarahSmith.Sex = female; + sarahSmith.Friends = new Child[] {brianSmith}; + + Team royals = new Team(); + royals.Name = "Royals"; + + Team chiefs = new Team(); + chiefs.Name = "Chiefs"; + + royals.Players = new List(); + royals.Players.Add(amyJones); + royals.Players.Add(brianSmith); + + chiefs.Players = new List(); + chiefs.Players.Add(johnnyJones); + chiefs.Players.Add(sarahSmith); + + s.Save(johnnyJones, johnnyJones.Id); + s.Save(amyJones, amyJones.Id); + s.Save(brianSmith, brianSmith.Id); + s.Save(sarahSmith, sarahSmith.Id); + + s.Save(royals); + s.Save(chiefs); + + t.Commit(); + } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/Operations/MergeFixture.cs b/src/NHibernate.Test/Operations/MergeFixture.cs index 0788b0e113b..afa5ded161e 100644 --- a/src/NHibernate.Test/Operations/MergeFixture.cs +++ b/src/NHibernate.Test/Operations/MergeFixture.cs @@ -151,16 +151,18 @@ public void MergeDeepTree() { ClearCounts(); - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); var root = new Node {Name = "root"}; var child = new Node {Name = "child"}; var grandchild = new Node {Name = "grandchild"}; - root.AddChild(child); - child.AddChild(grandchild); - s.Merge(root); - tx.Commit(); - s.Close(); + + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + root.AddChild(child); + child.AddChild(grandchild); + s.Merge(root); + tx.Commit(); + } AssertInsertCount(3); AssertUpdateCount(0); @@ -170,11 +172,12 @@ public void MergeDeepTree() var grandchild2 = new Node {Name = "grandchild2"}; child.AddChild(grandchild2); - s = OpenSession(); - tx = s.BeginTransaction(); - s.Merge(root); - tx.Commit(); - s.Close(); + using (var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + s.Merge(root); + tx.Commit(); + } AssertInsertCount(1); AssertUpdateCount(1); @@ -185,26 +188,28 @@ public void MergeDeepTree() child2.AddChild(grandchild3); root.AddChild(child2); - s = OpenSession(); - tx = s.BeginTransaction(); - s.Merge(root); - tx.Commit(); - s.Close(); + using (var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + s.Merge(root); + tx.Commit(); + } AssertInsertCount(2); AssertUpdateCount(0); ClearCounts(); - s = OpenSession(); - tx = s.BeginTransaction(); - s.Delete(grandchild); - s.Delete(grandchild2); - s.Delete(grandchild3); - s.Delete(child); - s.Delete(child2); - s.Delete(root); - tx.Commit(); - s.Close(); + using (var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + s.Delete(grandchild); + s.Delete(grandchild2); + s.Delete(grandchild3); + s.Delete(child); + s.Delete(child2); + s.Delete(root); + tx.Commit(); + } } [Test] @@ -283,110 +288,128 @@ public void MergeDeepTreeWithGeneratedId() [Test] public void MergeManaged() { - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); - var root = new NumberedNode("root"); - s.Persist(root); - tx.Commit(); - - ClearCounts(); + using (ISession s = OpenSession()) + { + NumberedNode root; + using (ITransaction tx = s.BeginTransaction()) + { + root = new NumberedNode("root"); + s.Persist(root); + tx.Commit(); + } + ClearCounts(); - tx = s.BeginTransaction(); - var child = new NumberedNode("child"); - root.AddChild(child); - Assert.That(s.Merge(root), Is.SameAs(root)); - IEnumerator rit = root.Children.GetEnumerator(); - rit.MoveNext(); - NumberedNode mergedChild = rit.Current; - Assert.That(mergedChild, Is.Not.SameAs(child)); - Assert.That(s.Contains(mergedChild)); - Assert.That(! s.Contains(child)); - Assert.That(root.Children.Count, Is.EqualTo(1)); - Assert.That(root.Children.Contains(mergedChild)); - //assertNotSame( mergedChild, s.Merge(child) ); //yucky :( - tx.Commit(); + NumberedNode mergedChild; + using (var tx = s.BeginTransaction()) + { + var child = new NumberedNode("child"); + root.AddChild(child); + Assert.That(s.Merge(root), Is.SameAs(root)); + IEnumerator rit = root.Children.GetEnumerator(); + rit.MoveNext(); + mergedChild = rit.Current; + Assert.That(mergedChild, Is.Not.SameAs(child)); + Assert.That(s.Contains(mergedChild)); + Assert.That(!s.Contains(child)); + Assert.That(root.Children.Count, Is.EqualTo(1)); + Assert.That(root.Children.Contains(mergedChild)); + //assertNotSame( mergedChild, s.Merge(child) ); //yucky :( + tx.Commit(); + } - AssertInsertCount(1); - AssertUpdateCount(0); + AssertInsertCount(1); + AssertUpdateCount(0); - Assert.That(root.Children.Count, Is.EqualTo(1)); - Assert.That(root.Children.Contains(mergedChild)); + Assert.That(root.Children.Count, Is.EqualTo(1)); + Assert.That(root.Children.Contains(mergedChild)); - tx = s.BeginTransaction(); - Assert.That(s.CreateCriteria(typeof (NumberedNode)).SetProjection(Projections.RowCount()).UniqueResult(), - Is.EqualTo(2)); - tx.Rollback(); - s.Close(); + using (var tx = s.BeginTransaction()) + { + Assert.That( + s.CreateCriteria(typeof(NumberedNode)).SetProjection(Projections.RowCount()).UniqueResult(), + Is.EqualTo(2)); + tx.Rollback(); + } + } } [Test] public void MergeManyToManyWithCollectionDeference() { // setup base data... - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); - var competition = new Competition(); - competition.Competitors.Add(new Competitor {Name = "Name"}); - competition.Competitors.Add(new Competitor()); - competition.Competitors.Add(new Competitor()); - s.Persist(competition); - tx.Commit(); - s.Close(); + Competition competition; + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + competition = new Competition(); + competition.Competitors.Add(new Competitor {Name = "Name"}); + competition.Competitors.Add(new Competitor()); + competition.Competitors.Add(new Competitor()); + s.Persist(competition); + tx.Commit(); + } // the competition graph is now detached: // 1) create a new List reference to represent the competitors - s = OpenSession(); - tx = s.BeginTransaction(); - var newComp = new List(); - Competitor originalCompetitor = competition.Competitors[0]; - originalCompetitor.Name = "Name2"; - newComp.Add(originalCompetitor); - newComp.Add(new Competitor()); - // 2) set that new List reference unto the Competition reference - competition.Competitors = newComp; - // 3) attempt the merge - var competition2 = (Competition) s.Merge(competition); - tx.Commit(); - s.Close(); + Competition competition2; + using (var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + var newComp = new List(); + Competitor originalCompetitor = competition.Competitors[0]; + originalCompetitor.Name = "Name2"; + newComp.Add(originalCompetitor); + newComp.Add(new Competitor()); + // 2) set that new List reference unto the Competition reference + competition.Competitors = newComp; + // 3) attempt the merge + competition2 = (Competition) s.Merge(competition); + tx.Commit(); + } Assert.That(!(competition == competition2)); Assert.That(!(competition.Competitors == competition2.Competitors)); Assert.That(competition2.Competitors.Count, Is.EqualTo(2)); - s = OpenSession(); - tx = s.BeginTransaction(); - competition = s.Get(competition.Id); - Assert.That(competition.Competitors.Count, Is.EqualTo(2)); - s.Delete(competition); - tx.Commit(); - s.Close(); + using (var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + competition = s.Get(competition.Id); + Assert.That(competition.Competitors.Count, Is.EqualTo(2)); + s.Delete(competition); + tx.Commit(); + } } [Test] public void MergeStaleVersionFails() { - ISession s = OpenSession(); - s.BeginTransaction(); var entity = new VersionedEntity {Id = "entity", Name = "entity"}; - s.Persist(entity); - s.Transaction.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using(s.BeginTransaction()) + { + s.Persist(entity); + s.Transaction.Commit(); + } // make the detached 'entity' reference stale... - s = OpenSession(); - s.BeginTransaction(); - var entity2 = s.Get(entity.Id); - entity2.Name = "entity-name"; - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + var entity2 = s.Get(entity.Id); + entity2.Name = "entity-name"; + s.Transaction.Commit(); + } // now try to reattch it - s = OpenSession(); - s.BeginTransaction(); + ISession s2 = null; try { - s.Merge(entity); - s.Transaction.Commit(); + s2 = OpenSession(); + s2.BeginTransaction(); + + s2.Merge(entity); + s2.Transaction.Commit(); Assert.Fail("was expecting staleness error"); } catch (StaleObjectStateException) @@ -395,8 +418,11 @@ public void MergeStaleVersionFails() } finally { - s.Transaction.Rollback(); - s.Close(); + if (s2 != null) + { + s2.Transaction.Rollback(); + s2.Close(); + } Cleanup(); } } @@ -406,14 +432,15 @@ public void MergeTree() { ClearCounts(); - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); var root = new Node {Name = "root"}; var child = new Node {Name = "child"}; - root.AddChild(child); - s.Persist(root); - tx.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + root.AddChild(child); + s.Persist(root); + tx.Commit(); + } AssertInsertCount(2); ClearCounts(); @@ -425,11 +452,12 @@ public void MergeTree() root.AddChild(secondChild); - s = OpenSession(); - tx = s.BeginTransaction(); - s.Merge(root); - tx.Commit(); - s.Close(); + using(var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + s.Merge(root); + tx.Commit(); + } AssertInsertCount(1); AssertUpdateCount(2); @@ -440,14 +468,15 @@ public void MergeTreeWithGeneratedId() { ClearCounts(); - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); var root = new NumberedNode("root"); var child = new NumberedNode("child"); - root.AddChild(child); - s.Persist(root); - tx.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + root.AddChild(child); + s.Persist(root); + tx.Commit(); + } AssertInsertCount(2); ClearCounts(); @@ -459,11 +488,12 @@ public void MergeTreeWithGeneratedId() root.AddChild(secondChild); - s = OpenSession(); - tx = s.BeginTransaction(); - s.Merge(root); - tx.Commit(); - s.Close(); + using(var s = OpenSession()) + using (var tx = s.BeginTransaction()) + { + s.Merge(root); + tx.Commit(); + } AssertInsertCount(1); AssertUpdateCount(2); @@ -472,22 +502,24 @@ public void MergeTreeWithGeneratedId() [Test] public void NoExtraUpdatesOnMerge() { - ISession s = OpenSession(); - s.BeginTransaction(); var node = new Node {Name = "test"}; - s.Persist(node); - s.Transaction.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using (s.BeginTransaction()) + { + s.Persist(node); + s.Transaction.Commit(); + } ClearCounts(); // node is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... - s = OpenSession(); - s.BeginTransaction(); - node = (Node) s.Merge(node); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + node = (Node) s.Merge(node); + s.Transaction.Commit(); + } AssertUpdateCount(0); AssertInsertCount(0); @@ -496,11 +528,12 @@ public void NoExtraUpdatesOnMerge() // as a control measure, now update the node while it is detached and // make sure we get an update as a result... node.Description = "new description"; - s = OpenSession(); - s.BeginTransaction(); - node = (Node) s.Merge(node); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + node = (Node) s.Merge(node); + s.Transaction.Commit(); + } AssertUpdateCount(1); AssertInsertCount(0); /////////////////////////////////////////////////////////////////////// @@ -509,22 +542,25 @@ public void NoExtraUpdatesOnMerge() [Test] public void NoExtraUpdatesOnMergeVersioned() { - ISession s = OpenSession(); - s.BeginTransaction(); var entity = new VersionedEntity {Id = "entity", Name = "entity"}; - s.Persist(entity); - s.Transaction.Commit(); - s.Close(); + using (ISession s = OpenSession()) + using (s.BeginTransaction()) + { + s.Persist(entity); + s.Transaction.Commit(); + } ClearCounts(); // entity is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... - s = OpenSession(); - s.BeginTransaction(); - var mergedEntity = (VersionedEntity) s.Merge(entity); - s.Transaction.Commit(); - s.Close(); + VersionedEntity mergedEntity; + using (var s = OpenSession()) + using (s.BeginTransaction()) + { + mergedEntity = (VersionedEntity) s.Merge(entity); + s.Transaction.Commit(); + } AssertUpdateCount(0); AssertInsertCount(0); @@ -534,11 +570,12 @@ public void NoExtraUpdatesOnMergeVersioned() // as a control measure, now update the node while it is detached and // make sure we get an update as a result... entity.Name = "new name"; - s = OpenSession(); - s.BeginTransaction(); - entity = (VersionedEntity) s.Merge(entity); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + entity = (VersionedEntity) s.Merge(entity); + s.Transaction.Commit(); + } AssertUpdateCount(1); AssertInsertCount(0); /////////////////////////////////////////////////////////////////////// @@ -547,25 +584,29 @@ public void NoExtraUpdatesOnMergeVersioned() [Test] public void NoExtraUpdatesOnMergeVersionedWithCollection() { - ISession s = OpenSession(); - s.BeginTransaction(); var parent = new VersionedEntity {Id = "parent", Name = "parent"}; var child = new VersionedEntity {Id = "child", Name = "child"}; - parent.Children.Add(child); - child.Parent = parent; - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); + + using(ISession s = OpenSession()) + using (s.BeginTransaction()) + { + parent.Children.Add(child); + child.Parent = parent; + s.Persist(parent); + s.Transaction.Commit(); + } ClearCounts(); // parent is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... - s = OpenSession(); - s.BeginTransaction(); - var mergedParent = (VersionedEntity) s.Merge(parent); - s.Transaction.Commit(); - s.Close(); + VersionedEntity mergedParent; + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + mergedParent = (VersionedEntity) s.Merge(parent); + s.Transaction.Commit(); + } AssertUpdateCount(0); AssertInsertCount(0); @@ -580,11 +621,12 @@ public void NoExtraUpdatesOnMergeVersionedWithCollection() // make sure we get an update as a result... mergedParent.Name = "new name"; mergedParent.Children.Add(new VersionedEntity {Id = "child2", Name = "new child"}); - s = OpenSession(); - s.BeginTransaction(); - parent = (VersionedEntity) s.Merge(mergedParent); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + parent = (VersionedEntity) s.Merge(mergedParent); + s.Transaction.Commit(); + } AssertUpdateCount(1); AssertInsertCount(1); /////////////////////////////////////////////////////////////////////// @@ -593,25 +635,27 @@ public void NoExtraUpdatesOnMergeVersionedWithCollection() [Test] public void NoExtraUpdatesOnMergeWithCollection() { - ISession s = OpenSession(); - s.BeginTransaction(); var parent = new Node {Name = "parent"}; - var child = new Node {Name = "child"}; - parent.Children.Add(child); - child.Parent = parent; - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); + using(ISession s = OpenSession()) + using (s.BeginTransaction()) + { + var child = new Node {Name = "child"}; + parent.Children.Add(child); + child.Parent = parent; + s.Persist(parent); + s.Transaction.Commit(); + } ClearCounts(); // parent is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... - s = OpenSession(); - s.BeginTransaction(); - parent = (Node) s.Merge(parent); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + parent = (Node) s.Merge(parent); + s.Transaction.Commit(); + } AssertUpdateCount(0); AssertInsertCount(0); @@ -623,11 +667,12 @@ public void NoExtraUpdatesOnMergeWithCollection() it.MoveNext(); it.Current.Description = "child's new description"; parent.Children.Add(new Node {Name = "second child"}); - s = OpenSession(); - s.BeginTransaction(); - parent = (Node) s.Merge(parent); - s.Transaction.Commit(); - s.Close(); + using(var s = OpenSession()) + using (s.BeginTransaction()) + { + parent = (Node) s.Merge(parent); + s.Transaction.Commit(); + } AssertUpdateCount(1); AssertInsertCount(1); /////////////////////////////////////////////////////////////////////// @@ -636,49 +681,51 @@ public void NoExtraUpdatesOnMergeWithCollection() [Test] public void PersistThenMergeInSameTxnWithTimestamp() { - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); - var entity = new TimestampedEntity {Id = "test", Name = "test"}; - s.Persist(entity); - s.Merge(new TimestampedEntity {Id = "test", Name = "test-2"}); - - try - { - // control operation... - s.SaveOrUpdate(new TimestampedEntity {Id = "test", Name = "test-3"}); - Assert.Fail("saveOrUpdate() should fail here"); - } - catch (NonUniqueObjectException) + using(ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) { - // expected behavior - } + var entity = new TimestampedEntity {Id = "test", Name = "test"}; + s.Persist(entity); + s.Merge(new TimestampedEntity {Id = "test", Name = "test-2"}); + + try + { + // control operation... + s.SaveOrUpdate(new TimestampedEntity {Id = "test", Name = "test-3"}); + Assert.Fail("saveOrUpdate() should fail here"); + } + catch (NonUniqueObjectException) + { + // expected behavior + } - tx.Commit(); - s.Close(); + tx.Commit(); + } } [Test] public void PersistThenMergeInSameTxnWithVersion() { - ISession s = OpenSession(); - ITransaction tx = s.BeginTransaction(); - var entity = new VersionedEntity {Id = "test", Name = "test"}; - s.Persist(entity); - s.Merge(new VersionedEntity {Id = "test", Name = "test-2"}); - - try - { - // control operation... - s.SaveOrUpdate(new VersionedEntity {Id = "test", Name = "test-3"}); - Assert.Fail("saveOrUpdate() should fail here"); - } - catch (NonUniqueObjectException) + using(ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) { - // expected behavior - } + var entity = new VersionedEntity {Id = "test", Name = "test"}; + s.Persist(entity); + s.Merge(new VersionedEntity {Id = "test", Name = "test-2"}); + + try + { + // control operation... + s.SaveOrUpdate(new VersionedEntity {Id = "test", Name = "test-3"}); + Assert.Fail("saveOrUpdate() should fail here"); + } + catch (NonUniqueObjectException) + { + // expected behavior + } - tx.Commit(); - s.Close(); + tx.Commit(); + } } [Test] @@ -706,4 +753,4 @@ public void RecursiveMergeTransient() } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/ReadOnly/ReadOnlyVersionedNodes.cs b/src/NHibernate.Test/ReadOnly/ReadOnlyVersionedNodes.cs index b4910cc6ee6..4d68db625eb 100644 --- a/src/NHibernate.Test/ReadOnly/ReadOnlyVersionedNodes.cs +++ b/src/NHibernate.Test/ReadOnly/ReadOnlyVersionedNodes.cs @@ -30,74 +30,76 @@ protected override IList Mappings [Test] public void SetReadOnlyTrueAndFalse() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode node = new VersionedNode("node", "node"); - s.Persist(node); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(node); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - - s.BeginTransaction(); - node = s.Get(node.Id); - s.SetReadOnly(node, true); - node.Name = "node-name"; - s.Transaction.Commit(); - - AssertUpdateCount(0); - AssertInsertCount(0); - - // the changed name is still in node - Assert.That(node.Name, Is.EqualTo("node-name")); - - s.BeginTransaction(); - node = s.Get(node.Id); - // the changed name is still in the session - Assert.That(node.Name, Is.EqualTo("node-name")); - s.Refresh(node); - // after refresh, the name reverts to the original value - Assert.That(node.Name, Is.EqualTo("node")); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("node")); - s.Transaction.Commit(); - - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + s.SetReadOnly(node, true); + node.Name = "node-name"; + s.Transaction.Commit(); + + AssertUpdateCount(0); + AssertInsertCount(0); + + // the changed name is still in node + Assert.That(node.Name, Is.EqualTo("node-name")); + + s.BeginTransaction(); + node = s.Get(node.Id); + // the changed name is still in the session + Assert.That(node.Name, Is.EqualTo("node-name")); + s.Refresh(node); + // after refresh, the name reverts to the original value + Assert.That(node.Name, Is.EqualTo("node")); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("node")); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(0); - - s = OpenSession(); - s.BeginTransaction(); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("node")); - s.SetReadOnly(node, true); - node.Name = "diff-node-name"; - s.Flush(); - Assert.That(node.Name, Is.EqualTo("diff-node-name")); - s.Refresh(node); - Assert.That(node.Name, Is.EqualTo("node")); - s.SetReadOnly(node, false); - node.Name = "diff-node-name"; - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("node")); + s.SetReadOnly(node, true); + node.Name = "diff-node-name"; + s.Flush(); + Assert.That(node.Name, Is.EqualTo("diff-node-name")); + s.Refresh(node); + Assert.That(node.Name, Is.EqualTo("node")); + s.SetReadOnly(node, false); + node.Name = "diff-node-name"; + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(0); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("diff-node-name")); - Assert.That(node.Version, Is.EqualTo(2)); - s.SetReadOnly(node, true); - s.Delete(node); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("diff-node-name")); + Assert.That(node.Version, Is.EqualTo(2)); + s.SetReadOnly(node, true); + s.Delete(node); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(1); } @@ -105,38 +107,40 @@ public void SetReadOnlyTrueAndFalse() [Test] public void UpdateSetReadOnlyTwice() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode node = new VersionedNode("node", "node"); - s.Persist(node); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(node); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - - s.BeginTransaction(); - node = s.Get(node.Id); - node.Name = "node-name"; - s.SetReadOnly(node, true); - s.SetReadOnly(node, true); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + node.Name = "node-name"; + s.SetReadOnly(node, true); + s.SetReadOnly(node, true); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(0); - - s = OpenSession(); - s.BeginTransaction(); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("node")); - Assert.That(node.Version, Is.EqualTo(1)); - s.SetReadOnly(node, true); - s.Delete(node); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("node")); + Assert.That(node.Version, Is.EqualTo(1)); + s.SetReadOnly(node, true); + s.Delete(node); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(1); } @@ -144,39 +148,40 @@ public void UpdateSetReadOnlyTwice() [Test] public void UpdateSetModifiable() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode node = new VersionedNode("node", "node"); - s.Persist(node); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(node); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - - s.BeginTransaction(); - node = s.Get(node.Id); - node.Name = "node-name"; - s.SetReadOnly(node, false); - s.Transaction.Commit(); - s.Close(); - s.Dispose(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + node.Name = "node-name"; + s.SetReadOnly(node, false); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(0); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("node-name")); - Assert.That(node.Version, Is.EqualTo(2)); - //s.SetReadOnly(node, true); - s.Delete(node); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("node-name")); + Assert.That(node.Version, Is.EqualTo(2)); + //s.SetReadOnly(node, true); + s.Delete(node); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(1); } @@ -185,36 +190,38 @@ public void UpdateSetModifiable() [Ignore("Failure expected")] public void UpdateSetReadOnlySetModifiableFailureExpected() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode node = new VersionedNode("node", "node"); - s.Persist(node); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(node); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - - s.BeginTransaction(); - node = s.Get(node.Id); - node.Name = "node-name"; - s.SetReadOnly(node, true); - s.SetReadOnly(node, false); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + node.Name = "node-name"; + s.SetReadOnly(node, true); + s.SetReadOnly(node, false); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(0); - - s = OpenSession(); - s.BeginTransaction(); - node = s.Get(node.Id); - Assert.That(node.Name, Is.EqualTo("node-name")); - Assert.That(node.Version, Is.EqualTo(2)); - s.Delete(node); - s.Transaction.Commit(); - s.Close(); + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + node = s.Get(node.Id); + Assert.That(node.Name, Is.EqualTo("node-name")); + Assert.That(node.Version, Is.EqualTo(2)); + s.Delete(node); + s.Transaction.Commit(); + } } [Test] @@ -256,89 +263,95 @@ public void SetReadOnlyUpdateSetModifiableFailureExpected() [Test] public void AddNewChildToReadOnlyParent() { - ISession s = OpenSession(); - s.CacheMode = CacheMode.Ignore; - s.BeginTransaction(); VersionedNode parent = new VersionedNode("parent", "parent"); - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.CacheMode = CacheMode.Ignore; + s.BeginTransaction(); + s.Persist(parent); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - s.CacheMode = CacheMode.Ignore; - s.BeginTransaction(); - VersionedNode parentManaged = s.Get(parent.Id); - s.SetReadOnly(parentManaged, true); - parentManaged.Name = "new parent name"; + VersionedNode child = new VersionedNode("child", "child"); - parentManaged.AddChild(child); - s.Save(parentManaged); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.CacheMode = CacheMode.Ignore; + s.BeginTransaction(); + VersionedNode parentManaged = s.Get(parent.Id); + s.SetReadOnly(parentManaged, true); + parentManaged.Name = "new parent name"; + parentManaged.AddChild(child); + s.Save(parentManaged); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(1); - - s = OpenSession(); - s.CacheMode = CacheMode.Ignore; - s.BeginTransaction(); - parent = s.Get(parent.Id); - Assert.That(parent.Name, Is.EqualTo("parent")); - Assert.That(parent.Children.Count, Is.EqualTo(1)); - Assert.That(parent.Version, Is.EqualTo(2)); - child = s.Get(child.Id); - Assert.That(child, Is.Not.Null); - s.Delete(parent); - s.Transaction.Commit(); - s.Close(); + + using (ISession s = OpenSession()) + { + s.CacheMode = CacheMode.Ignore; + s.BeginTransaction(); + parent = s.Get(parent.Id); + Assert.That(parent.Name, Is.EqualTo("parent")); + Assert.That(parent.Children.Count, Is.EqualTo(1)); + Assert.That(parent.Version, Is.EqualTo(2)); + child = s.Get(child.Id); + Assert.That(child, Is.Not.Null); + s.Delete(parent); + s.Transaction.Commit(); + } } [Test] public void UpdateParentWithNewChildCommitWithReadOnlyParent() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode parent = new VersionedNode("parent", "parent"); - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(parent); + s.Transaction.Commit(); + } + ClearCounts(); parent.Name = "new parent name"; VersionedNode child = new VersionedNode("child", "child"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - s.Update(parent); - s.SetReadOnly(parent, true); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Update(parent); + s.SetReadOnly(parent, true); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(parent.Name, Is.EqualTo("parent")); - Assert.That(parent.Children.Count, Is.EqualTo(1)); - Assert.That(parent.Version, Is.EqualTo(2)); - Assert.That(child.Parent, Is.SameAs(parent)); - Assert.That(parent.Children.First(), Is.SameAs(child)); - Assert.That(child.Version, Is.EqualTo(1)); - s.SetReadOnly(parent, true); - s.SetReadOnly(child, true); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(parent.Name, Is.EqualTo("parent")); + Assert.That(parent.Children.Count, Is.EqualTo(1)); + Assert.That(parent.Version, Is.EqualTo(2)); + Assert.That(child.Parent, Is.SameAs(parent)); + Assert.That(parent.Children.First(), Is.SameAs(child)); + Assert.That(child.Version, Is.EqualTo(1)); + s.SetReadOnly(parent, true); + s.SetReadOnly(child, true); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } @@ -346,95 +359,101 @@ public void UpdateParentWithNewChildCommitWithReadOnlyParent() [Test] public void MergeDetachedParentWithNewChildCommitWithReadOnlyParent() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode parent = new VersionedNode("parent", "parent"); - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(parent); + s.Transaction.Commit(); + } + ClearCounts(); parent.Name = "new parent name"; VersionedNode child = new VersionedNode("child", "child"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - parent = (VersionedNode)s.Merge(parent); - s.SetReadOnly(parent, true); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = (VersionedNode) s.Merge(parent); + s.SetReadOnly(parent, true); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(parent.Name, Is.EqualTo("parent")); - Assert.That(parent.Children.Count, Is.EqualTo(1)); - Assert.That(parent.Version, Is.EqualTo(2)); - Assert.That(child.Parent, Is.SameAs(parent)); - Assert.That(parent.Children.First(), Is.SameAs(child)); - Assert.That(child.Version, Is.EqualTo(1)); - s.SetReadOnly(parent, true); - s.SetReadOnly(child, true); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(parent.Name, Is.EqualTo("parent")); + Assert.That(parent.Children.Count, Is.EqualTo(1)); + Assert.That(parent.Version, Is.EqualTo(2)); + Assert.That(child.Parent, Is.SameAs(parent)); + Assert.That(parent.Children.First(), Is.SameAs(child)); + Assert.That(child.Version, Is.EqualTo(1)); + s.SetReadOnly(parent, true); + s.SetReadOnly(child, true); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } - + [Test] public void GetParentMakeReadOnlyThenMergeDetachedParentWithNewChildC() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode parent = new VersionedNode("parent", "parent"); - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(parent); + s.Transaction.Commit(); + } + ClearCounts(); parent.Name = "new parent name"; VersionedNode child = new VersionedNode("child", "child"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - VersionedNode parentManaged = s.Get(parent.Id); - s.SetReadOnly(parentManaged, true); - VersionedNode parentMerged = (VersionedNode) s.Merge(parent); - Assert.That(parentManaged, Is.SameAs(parentMerged)); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + VersionedNode parentManaged = s.Get(parent.Id); + s.SetReadOnly(parentManaged, true); + VersionedNode parentMerged = (VersionedNode) s.Merge(parent); + Assert.That(parentManaged, Is.SameAs(parentMerged)); + s.Transaction.Commit(); + } + AssertUpdateCount(1); AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(parent.Name, Is.EqualTo("parent")); - Assert.That(parent.Children.Count, Is.EqualTo(1)); - Assert.That(parent.Version, Is.EqualTo(2)); - Assert.That(child.Parent, Is.SameAs(parent)); - Assert.That(parent.Children.First(), Is.SameAs(child)); - Assert.That(child.Version, Is.EqualTo(1)); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(parent.Name, Is.EqualTo("parent")); + Assert.That(parent.Children.Count, Is.EqualTo(1)); + Assert.That(parent.Version, Is.EqualTo(2)); + Assert.That(child.Parent, Is.SameAs(parent)); + Assert.That(parent.Children.First(), Is.SameAs(child)); + Assert.That(child.Version, Is.EqualTo(1)); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } @@ -442,64 +461,70 @@ public void GetParentMakeReadOnlyThenMergeDetachedParentWithNewChildC() [Test] public void MergeUnchangedDetachedParentChildren() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode parent = new VersionedNode("parent", "parent"); VersionedNode child = new VersionedNode("child", "child"); - parent.AddChild(child); - s.Persist(parent); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent.AddChild(child); + s.Persist(parent); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = (VersionedNode)s.Merge(parent); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + parent = (VersionedNode) s.Merge(parent); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(0); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - VersionedNode parentGet = s.Get(parent.Id); - s.Merge(parent); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + VersionedNode parentGet = s.Get(parent.Id); + s.Merge(parent); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(0); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - VersionedNode parentLoad = s.Load(parent.Id); - s.Merge(parent); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + VersionedNode parentLoad = s.Load(parent.Id); + s.Merge(parent); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(0); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(parent.Name, Is.EqualTo("parent")); - Assert.That(parent.Children.Count, Is.EqualTo(1)); - Assert.That(parent.Version, Is.EqualTo(1)); - Assert.That(child.Parent, Is.SameAs(parent)); - Assert.That(parent.Children.First(), Is.SameAs(child)); - Assert.That(child.Version, Is.EqualTo(1)); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (var s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(parent.Name, Is.EqualTo("parent")); + Assert.That(parent.Children.Count, Is.EqualTo(1)); + Assert.That(parent.Version, Is.EqualTo(1)); + Assert.That(child.Parent, Is.SameAs(parent)); + Assert.That(parent.Children.First(), Is.SameAs(child)); + Assert.That(child.Version, Is.EqualTo(1)); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } @@ -507,41 +532,44 @@ public void MergeUnchangedDetachedParentChildren() [Test] public void AddNewParentToReadOnlyChild() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode child = new VersionedNode("child", "child"); - s.Persist(child); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(child); + s.Transaction.Commit(); + } + ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - VersionedNode childManaged = s.Get(child.Id); - s.SetReadOnly(childManaged, true); - childManaged.Name = "new child name"; + VersionedNode parent = new VersionedNode("parent", "parent"); - parent.AddChild(childManaged); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + VersionedNode childManaged = s.Get(child.Id); + s.SetReadOnly(childManaged, true); + childManaged.Name = "new child name"; + parent.AddChild(childManaged); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(1); - - s = OpenSession(); - s.BeginTransaction(); - child = s.Get(child.Id); - Assert.That(child.Name, Is.EqualTo("child")); - Assert.That(child.Parent, Is.Null); - Assert.That(child.Version, Is.EqualTo(1)); - parent = s.Get(parent.Id); - Assert.That(parent, Is.Not.Null); - s.SetReadOnly(child, true); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + child = s.Get(child.Id); + Assert.That(child.Name, Is.EqualTo("child")); + Assert.That(child.Parent, Is.Null); + Assert.That(child.Version, Is.EqualTo(1)); + parent = s.Get(parent.Id); + Assert.That(parent, Is.Not.Null); + s.SetReadOnly(child, true); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(1); } @@ -549,47 +577,50 @@ public void AddNewParentToReadOnlyChild() [Test] public void UpdateChildWithNewParentCommitWithReadOnlyChild() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode child = new VersionedNode("child", "child"); - s.Persist(child); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(child); + s.Transaction.Commit(); + } + ClearCounts(); child.Name = "new child name"; VersionedNode parent = new VersionedNode("parent", "parent"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - s.Update(child); - s.SetReadOnly(child, true); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Update(child); + s.SetReadOnly(child, true); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(child.Name, Is.EqualTo("child")); - Assert.That(child.Parent, Is.Null); - Assert.That(child.Version, Is.EqualTo(1)); - Assert.That(parent, Is.Not.Null); - Assert.That(parent.Children.Count, Is.EqualTo(0)); - Assert.That(parent.Version, Is.EqualTo(1)); - s.SetReadOnly(parent, true); - s.SetReadOnly(child, true); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(child.Name, Is.EqualTo("child")); + Assert.That(child.Parent, Is.Null); + Assert.That(child.Version, Is.EqualTo(1)); + Assert.That(parent, Is.Not.Null); + Assert.That(parent.Children.Count, Is.EqualTo(0)); + Assert.That(parent.Version, Is.EqualTo(1)); + s.SetReadOnly(parent, true); + s.SetReadOnly(child, true); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } @@ -597,47 +628,50 @@ public void UpdateChildWithNewParentCommitWithReadOnlyChild() [Test] public void MergeDetachedChildWithNewParentCommitWithReadOnlyChild() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode child = new VersionedNode("child", "child"); - s.Persist(child); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(child); + s.Transaction.Commit(); + } + ClearCounts(); child.Name = "new child name"; VersionedNode parent = new VersionedNode("parent", "parent"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - child = (VersionedNode)s.Merge(child); - s.SetReadOnly(child, true); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + child = (VersionedNode) s.Merge(child); + s.SetReadOnly(child, true); + s.Transaction.Commit(); + } + AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(child.Name, Is.EqualTo("child")); - Assert.That(child.Parent, Is.Null); - Assert.That(child.Version, Is.EqualTo(1)); - Assert.That(parent, Is.Not.Null); - Assert.That(parent.Children.Count, Is.EqualTo(0)); - Assert.That(parent.Version, Is.EqualTo(1)); - s.SetReadOnly(parent, true); - s.SetReadOnly(child, true); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(child.Name, Is.EqualTo("child")); + Assert.That(child.Parent, Is.Null); + Assert.That(child.Version, Is.EqualTo(1)); + Assert.That(parent, Is.Not.Null); + Assert.That(parent.Children.Count, Is.EqualTo(0)); + Assert.That(parent.Version, Is.EqualTo(1)); + s.SetReadOnly(parent, true); + s.SetReadOnly(child, true); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } @@ -645,64 +679,69 @@ public void MergeDetachedChildWithNewParentCommitWithReadOnlyChild() [Test] public void GetChildMakeReadOnlyThenMergeDetachedChildWithNewParent() { - ISession s = OpenSession(); - s.BeginTransaction(); VersionedNode child = new VersionedNode("child", "child"); - s.Persist(child); - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + s.Persist(child); + s.Transaction.Commit(); + } + ClearCounts(); child.Name = "new child name"; VersionedNode parent = new VersionedNode("parent", "parent"); parent.AddChild(child); - - s = OpenSession(); - s.BeginTransaction(); - VersionedNode childManaged = s.Get(child.Id); - s.SetReadOnly(childManaged, true); - VersionedNode childMerged = (VersionedNode)s.Merge(child); - Assert.That(childManaged, Is.SameAs(childMerged)); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + VersionedNode childManaged = s.Get(child.Id); + s.SetReadOnly(childManaged, true); + VersionedNode childMerged = (VersionedNode) s.Merge(child); + Assert.That(childManaged, Is.SameAs(childMerged)); + s.Transaction.Commit(); + } + AssertUpdateCount(0); // NH-specific: Hibernate issues a separate UPDATE for the version number AssertInsertCount(1); ClearCounts(); - - s = OpenSession(); - s.BeginTransaction(); - parent = s.Get(parent.Id); - child = s.Get(child.Id); - Assert.That(child.Name, Is.EqualTo("child")); - Assert.That(child.Parent, Is.Null); - Assert.That(child.Version, Is.EqualTo(1)); - Assert.That(parent, Is.Not.Null); - Assert.That(parent.Children.Count, Is.EqualTo(0)); - Assert.That(parent.Version, Is.EqualTo(1)); // NH-specific: Hibernate incorrectly increments version number, NH does not - s.SetReadOnly(parent, true); - s.SetReadOnly(child, true); - s.Delete(parent); - s.Delete(child); - s.Transaction.Commit(); - s.Close(); - + + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + parent = s.Get(parent.Id); + child = s.Get(child.Id); + Assert.That(child.Name, Is.EqualTo("child")); + Assert.That(child.Parent, Is.Null); + Assert.That(child.Version, Is.EqualTo(1)); + Assert.That(parent, Is.Not.Null); + Assert.That(parent.Children.Count, Is.EqualTo(0)); + Assert.That(parent.Version, Is.EqualTo(1)); + // NH-specific: Hibernate incorrectly increments version number, NH does not + s.SetReadOnly(parent, true); + s.SetReadOnly(child, true); + s.Delete(parent); + s.Delete(child); + s.Transaction.Commit(); + } + AssertUpdateCount(0); AssertDeleteCount(2); } protected override void OnTearDown() { - ISession s = OpenSession(); - s.BeginTransaction(); - - s.CreateQuery("delete from VersionedNode where parent is not null").ExecuteUpdate(); - s.CreateQuery("delete from VersionedNode").ExecuteUpdate(); - - s.Transaction.Commit(); - s.Close(); - + using (ISession s = OpenSession()) + { + s.BeginTransaction(); + + s.CreateQuery("delete from VersionedNode where parent is not null").ExecuteUpdate(); + s.CreateQuery("delete from VersionedNode").ExecuteUpdate(); + + s.Transaction.Commit(); + } + base.OnTearDown(); } }