Skip to content

Fix session handling in a bunch of unit tests to close responsibly. #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 101 additions & 99 deletions src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
}
}
}
}
}
Loading