/src/NHibernate.Test/Legacy/FooBarTest.cs
C# | 5521 lines | 4537 code | 753 blank | 231 comment | 150 complexity | 24e4570caf1bd86b7b6aed2d8d307541 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, CC-BY-SA-3.0
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using Iesi.Collections.Generic;
- using NHibernate.Connection;
- using NHibernate.Dialect;
- using NHibernate.DomainModel;
- using NHibernate.Criterion;
- using NHibernate.Proxy;
- using NHibernate.Type;
- using NHibernate.Util;
- using NUnit.Framework;
- namespace NHibernate.Test.Legacy
- {
- [TestFixture]
- public class FooBarTest : TestCase
- {
- // Equivalent of Java String.getBytes()
- private static byte[] GetBytes(string str)
- {
- return Encoding.Unicode.GetBytes(str);
- }
- protected override string[] Mappings
- {
- get
- {
- return new string[]
- {
- "FooBar.hbm.xml",
- "Baz.hbm.xml",
- "Qux.hbm.xml",
- "Glarch.hbm.xml",
- "Fum.hbm.xml",
- "Fumm.hbm.xml",
- "Fo.hbm.xml",
- "One.hbm.xml",
- "Many.hbm.xml",
- "Immutable.hbm.xml",
- "Fee.hbm.xml",
- "Vetoer.hbm.xml",
- "Holder.hbm.xml",
- "Location.hbm.xml",
- "Stuff.hbm.xml",
- "Container.hbm.xml",
- "Simple.hbm.xml",
- "XY.hbm.xml"
- };
- }
- }
- [Test]
- public void CollectionVersioning()
- {
- using (ISession s = OpenSession())
- {
- One one = new One();
- one.Manies = new HashSet<Many>();
- s.Save(one);
- s.Flush();
- Many many = new Many();
- many.One = one;
- one.Manies.Add(many);
- s.Save(many);
- s.Flush();
- // Versions are incremented compared to Hibernate because they start from 1
- // in NH.
- Assert.AreEqual(1, many.V);
- Assert.AreEqual(2, one.V);
- s.Delete(many);
- s.Delete(one);
- s.Flush();
- }
- }
- [Test]
- public void ForCertain()
- {
- Glarch g = new Glarch();
- Glarch g2 = new Glarch();
- IList<string> strings = new List<string>();
- strings.Add("foo");
- g2.Strings = strings;
- object gid, g2id;
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- gid = s.Save(g);
- g2id = s.Save(g2);
- t.Commit();
- // Versions are initialized to 1 in NH.
- Assert.AreEqual(1, g.Version);
- Assert.AreEqual(1, g2.Version);
- }
- }
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- g = (Glarch) s.Get(typeof(Glarch), gid);
- g2 = (Glarch) s.Get(typeof(Glarch), g2id);
- Assert.AreEqual(1, g2.Strings.Count);
- s.Delete(g);
- s.Delete(g2);
- t.Commit();
- }
- }
- }
- [Test]
- public void BagMultipleElements()
- {
- string bazCode;
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- Baz baz = new Baz();
- baz.Bag = new List<string>();
- baz.ByteBag = new List<byte[]>();
- s.Save(baz);
- baz.Bag.Add("foo");
- baz.Bag.Add("bar");
- baz.ByteBag.Add(GetBytes("foo"));
- baz.ByteBag.Add(GetBytes("bar"));
- t.Commit();
- bazCode = baz.Code;
- }
- }
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- //put in cache
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.AreEqual(2, baz.Bag.Count);
- Assert.AreEqual(2, baz.ByteBag.Count);
- t.Commit();
- }
- }
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.AreEqual(2, baz.Bag.Count);
- Assert.AreEqual(2, baz.ByteBag.Count);
- baz.Bag.Remove("bar");
- baz.Bag.Add("foo");
- baz.ByteBag.Add(GetBytes("bar"));
- t.Commit();
- }
- }
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.AreEqual(2, baz.Bag.Count);
- Assert.AreEqual(3, baz.ByteBag.Count);
- s.Delete(baz);
- t.Commit();
- }
- }
- }
- [Test]
- public void WierdSession()
- {
- object id;
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- id = s.Save(new Foo());
- t.Commit();
- }
- }
- using (ISession s = OpenSession())
- {
- s.FlushMode = FlushMode.Manual;
- using (ITransaction t = s.BeginTransaction())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), id);
- t.Commit();
- }
- s.Disconnect();
- s.Reconnect();
- using (ITransaction t = s.BeginTransaction())
- {
- s.Flush();
- t.Commit();
- }
- }
- using (ISession s = OpenSession())
- {
- using (ITransaction t = s.BeginTransaction())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), id);
- s.Delete(foo);
- t.Commit();
- }
- }
- }
- [Test]
- public void DereferenceLazyCollection()
- {
- string fooKey;
- string bazCode;
- using (ISession s = OpenSession())
- {
- Baz baz = new Baz();
- baz.FooSet = new HashSet<FooProxy>();
- Foo foo = new Foo();
- baz.FooSet.Add(foo);
- s.Save(foo);
- s.Save(baz);
- foo.Bytes = GetBytes("foobar");
- s.Flush();
- fooKey = foo.Key;
- bazCode = baz.Code;
- }
- using (ISession s = OpenSession())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
- Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
- // H2.1 has 6 here, but we are using Unicode
- Assert.AreEqual(12, foo.Bytes.Length);
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.AreEqual(1, baz.FooSet.Count);
- s.Flush();
- }
- Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
- using (ISession s = OpenSession())
- {
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
- baz.FooSet = null;
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
- Assert.AreEqual(12, foo.Bytes.Length);
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
- Assert.AreEqual(0, baz.FooSet.Count);
- s.Delete(baz);
- s.Delete(foo);
- s.Flush();
- }
- }
- [Test]
- public void MoveLazyCollection()
- {
- string fooKey, bazCode, baz2Code;
- using (ISession s = OpenSession())
- {
- Baz baz = new Baz();
- Baz baz2 = new Baz();
- baz.FooSet = new HashSet<FooProxy>();
- Foo foo = new Foo();
- baz.FooSet.Add(foo);
- s.Save(foo);
- s.Save(baz);
- s.Save(baz2);
- foo.Bytes = GetBytes("foobar");
- s.Flush();
- fooKey = foo.Key;
- bazCode = baz.Code;
- baz2Code = baz2.Code;
- }
- using (ISession s = OpenSession())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
- Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
- Assert.AreEqual(12, foo.Bytes.Length);
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.AreEqual(1, baz.FooSet.Count);
- s.Flush();
- }
- Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
- using (ISession s = OpenSession())
- {
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
- Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
- baz2.FooSet = baz.FooSet;
- baz.FooSet = null;
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
- Assert.AreEqual(12, foo.Bytes.Length);
- Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
- Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
- Assert.AreEqual(0, baz.FooSet.Count);
- Assert.IsTrue( NHibernateUtil.IsInitialized( baz2.FooSet ) ); //FooSet has batching enabled
- Assert.AreEqual(1, baz2.FooSet.Count);
- s.Delete(baz);
- s.Delete(baz2);
- s.Delete(foo);
- s.Flush();
- }
- }
- [Test]
- public void CriteriaCollection()
- {
- ISession s = OpenSession();
- Baz bb = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
- Baz baz = new Baz();
- s.Save(baz);
- s.Flush();
- s.Close();
- s = OpenSession();
- Baz b = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
- Assert.IsTrue(NHibernateUtil.IsInitialized(b.TopGlarchez));
- Assert.AreEqual(0, b.TopGlarchez.Count);
- s.Delete(b);
- s.Flush();
- s.CreateCriteria(typeof(Baz))
- .CreateCriteria("TopFoos")
- .Add(Expression.IsNotNull("id"))
- .List();
- s.CreateCriteria(typeof(Baz))
- .CreateCriteria("Foo")
- .CreateCriteria("Component.Glarch")
- .CreateCriteria("ProxySet")
- .Add(Expression.IsNotNull("id"))
- .List();
- s.Close();
- }
- private static bool IsEmpty(IEnumerable enumerable)
- {
- return !enumerable.GetEnumerator().MoveNext();
- }
- private static bool ContainsSingleObject(IEnumerable enumerable, object obj)
- {
- IEnumerator enumerator = enumerable.GetEnumerator();
- // Fail if no items
- if (!enumerator.MoveNext())
- {
- return false;
- }
- // Fail if item not equal
- if (!Equals(obj, enumerator.Current))
- {
- return false;
- }
- // Fail if more items
- if (enumerator.MoveNext())
- {
- return false;
- }
- // Succeed
- return true;
- }
- [Test]
- public void Query()
- {
- ISession s = OpenSession();
- ITransaction txn = s.BeginTransaction();
- Foo foo = new Foo();
- s.Save(foo);
- Foo foo2 = new Foo();
- s.Save(foo2);
- foo.TheFoo = foo2;
- IList list = s.CreateQuery("from Foo foo inner join fetch foo.TheFoo").List();
- Foo foof = (Foo) list[0];
- Assert.IsTrue(NHibernateUtil.IsInitialized(foof.TheFoo));
- list = s.CreateQuery("from Baz baz left outer join fetch baz.FooToGlarch").List();
- list = s.CreateQuery("select foo, bar from Foo foo left outer join foo.TheFoo bar where foo = ?")
- .SetEntity(0, foo).List();
- object[] row1 = (object[]) list[0];
- Assert.IsTrue(row1[0] == foo && row1[1] == foo2);
- s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo = 'bar'").List();
- s.CreateQuery("select foo.TheFoo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar'").List();
- s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'bar'").
- List();
- // if( !( dialect is Dialect.HSQLDialect ) )
- // {
- s.CreateQuery("select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo = foo.TheFoo.TheFoo").List();
- // }
- s.CreateQuery(
- "select foo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar' and foo.TheFoo.TheFoo.TheFoo = 'baz'").List
- ();
- s.CreateQuery(
- "select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'a' and foo.TheFoo.String = 'b'").
- List();
- s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooArray)").List();
- if (Dialect is DB2Dialect)
- {
- s.CreateQuery("from foo in class Foo where lower( foo.TheFoo.String ) = 'foo'").List();
- s.CreateQuery("from foo in class Foo where lower( (foo.TheFoo.String || 'foo') || 'bar' ) = 'foo'").List();
- s.CreateQuery("from foo in class Foo where repeat( (foo.TheFoo.STring || 'foo') || 'bar', 2 ) = 'foo'").List();
- s.CreateQuery(
- "From foo in class Bar where foo.TheFoo.Integer is not null and repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
- .List();
- s.CreateQuery(
- "From foo in class Bar where foo.TheFoo.Integer is not null or repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
- .List();
- }
- if (Dialect is MsSql2000Dialect)
- {
- s.CreateQuery("select baz from Baz as baz join baz.FooArray foo group by baz order by sum(foo.Float)").Enumerable();
- }
- s.CreateQuery("from Foo as foo where foo.Component.Glarch.Name is not null").List();
- s.CreateQuery("from Foo as foo left outer join foo.Component.Glarch as glarch where glarch.Name = 'foo'").List();
- list = s.CreateQuery("from Foo").List();
- Assert.AreEqual(2, list.Count);
- Assert.IsTrue(list[0] is FooProxy);
- list = s.CreateQuery("from Foo foo left outer join foo.TheFoo").List();
- Assert.AreEqual(2, list.Count);
- Assert.IsTrue(((object[]) list[0])[0] is FooProxy);
- s.CreateQuery("From Foo, Bar").List();
- s.CreateQuery("from Baz baz left join baz.FooToGlarch, Bar bar join bar.TheFoo").List();
- s.CreateQuery("from Baz baz left join baz.FooToGlarch join baz.FooSet").List();
- s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooSet foo left join fetch foo.TheFoo").List();
- list =
- s.CreateQuery(
- "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' and foo.Boolean = true order by foo.String asc, foo.Component.Count desc")
- .List();
- Assert.AreEqual(0, list.Count, "empty query");
- IEnumerable enumerable =
- s.CreateQuery(
- "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' order by foo.String asc, foo.Component.Count desc")
- .Enumerable();
- Assert.IsTrue(IsEmpty(enumerable), "empty enumerator");
- list = s.CreateQuery("select foo.TheFoo from foo in class NHibernate.DomainModel.Foo").List();
- Assert.AreEqual(1, list.Count, "query");
- Assert.AreEqual(foo.TheFoo, list[0], "returned object");
- foo.TheFoo.TheFoo = foo;
- foo.String = "fizard";
- if (Dialect.SupportsSubSelects && TestDialect.SupportsOperatorSome)
- {
- if (!(Dialect is FirebirdDialect))
- {
- list = s.CreateQuery(
- "from foo in class NHibernate.DomainModel.Foo where ? = some elements(foo.Component.ImportantDates)").
- SetDateTime(0, DateTime.Today).List();
-
- Assert.AreEqual(2, list.Count, "component query");
- }
- if (Dialect.SupportsScalarSubSelects)
- {
- list =
- s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where size(foo.Component.ImportantDates) = 3").List();
- Assert.AreEqual(2, list.Count, "component query");
- list = s.CreateQuery("from foo in class Foo where 0 = size(foo.Component.ImportantDates)").List();
- Assert.AreEqual(0, list.Count, "component query");
- }
- list = s.CreateQuery("from foo in class Foo where exists elements(foo.Component.ImportantDates)").List();
- Assert.AreEqual(2, list.Count, "component query");
- s.CreateQuery("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)").List();
- s.CreateQuery(
- "select foo.TheFoo from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
- .List();
- s.CreateQuery(
- "from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long) and foo.TheFoo.String='baz'")
- .List();
- s.CreateQuery(
- "from foo in class Foo where foo.TheFoo.String='baz' and foo = some(select x from x in class Foo where x.Long>foo.TheFoo.Long)")
- .List();
- s.CreateQuery("from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
- .List();
- s.CreateQuery(
- "select foo.String, foo.Date, foo.TheFoo.String, foo.id from foo in class Foo, baz in class Baz where foo in elements(baz.FooArray) and foo.String like 'foo'")
- .Enumerable();
- }
- list = s.CreateQuery("from foo in class Foo where foo.Component.Count is null order by foo.Component.Count").List();
- Assert.AreEqual(0, list.Count, "component query");
- list = s.CreateQuery("from foo in class Foo where foo.Component.Name='foo'").List();
- Assert.AreEqual(2, list.Count, "component query");
- list =
- s.CreateQuery(
- "select distinct foo.Component.Name, foo.Component.Name from foo in class Foo where foo.Component.Name='foo'").List
- ();
- Assert.AreEqual(1, list.Count, "component query");
- list =
- s.CreateQuery("select distinct foo.Component.Name, foo.id from foo in class Foo where foo.Component.Name='foo'").
- List();
- Assert.AreEqual(2, list.Count, "component query");
- list = s.CreateQuery("select foo.TheFoo from foo in class Foo").List();
- Assert.AreEqual(2, list.Count, "query");
- list = s.CreateQuery("from foo in class Foo where foo.id=?").SetString(0, foo.Key).List();
- Assert.AreEqual(1, list.Count, "id query");
- list = s.CreateQuery("from foo in class Foo where foo.Key=?").SetString(0, foo.Key).List();
- Assert.AreEqual(1, list.Count, "named id query");
- Assert.AreSame(foo, list[0], "id query");
- list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.String='fizard'").List();
- Assert.AreEqual(1, list.Count, "query");
- Assert.AreSame(foo.TheFoo, list[0], "returned object");
- list = s.CreateQuery("from foo in class Foo where foo.Component.Subcomponent.Name='bar'").List();
- Assert.AreEqual(2, list.Count, "components of components");
- list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.TheFoo.id=?")
- .SetString(0, foo.TheFoo.Key).List();
- Assert.AreEqual(1, list.Count, "by id query");
- Assert.AreSame(foo.TheFoo, list[0], "by id returned object");
- s.CreateQuery("from foo in class Foo where foo.TheFoo = ?").SetEntity(0, foo.TheFoo).List();
- Assert.IsTrue(
- IsEmpty(s.CreateQuery("from bar in class Bar where bar.String='a string' or bar.String='a string'").Enumerable()
- ));
- enumerable = s.CreateQuery(
- "select foo.Component.Name, elements(foo.Component.ImportantDates) from foo in class Foo where foo.TheFoo.id=?").
- SetString(0, foo.TheFoo.Key).Enumerable();
- int i = 0;
- foreach (object[] row in enumerable)
- {
- i++;
- Assert.IsTrue(row[0] is String);
- Assert.IsTrue(row[1] == null || row[1] is DateTime);
- }
- Assert.AreEqual(3, i); //WAS: 4
- enumerable = s.CreateQuery("select max(elements(foo.Component.ImportantDates)) from foo in class Foo group by foo.id").
- Enumerable();
-
- IEnumerator enumerator = enumerable.GetEnumerator();
- Assert.IsTrue(enumerator.MoveNext());
- Assert.IsTrue(enumerator.Current is DateTime);
- list = s.CreateQuery(
- "select foo.TheFoo.TheFoo.TheFoo from foo in class Foo, foo2 in class Foo where"
- + " foo = foo2.TheFoo and not not ( not foo.String='fizard' )"
- + " and foo2.String between 'a' and (foo.TheFoo.String)"
- + (Dialect is SQLiteDialect
- ? " and ( foo2.String in ( 'fiz', 'blah') or 1=1 )"
- : " and ( foo2.String in ( 'fiz', 'blah', foo.TheFoo.String, foo.String, foo2.String ) )")
- ).List();
- Assert.AreEqual(1, list.Count, "complex query");
- Assert.AreSame(foo, list[0], "returned object");
- foo.String = "from BoogieDown -tinsel town =!@#$^&*())";
- list = s.CreateQuery("from foo in class Foo where foo.String='from BoogieDown -tinsel town =!@#$^&*())'").List();
- Assert.AreEqual(1, list.Count, "single quotes");
- list = s.CreateQuery("from foo in class Foo where not foo.String='foo''bar'").List();
- Assert.AreEqual(2, list.Count, "single quotes");
- list = s.CreateQuery("from foo in class Foo where foo.Component.Glarch.Next is null").List();
- Assert.AreEqual(2, list.Count, "query association in component");
- Bar bar = new Bar();
- Baz baz = new Baz();
- baz.SetDefaults();
- bar.Baz = baz;
- baz.ManyToAny = new List<object>();
- baz.ManyToAny.Add(bar);
- baz.ManyToAny.Add(foo);
- s.Save(bar);
- s.Save(baz);
- list =
- s.CreateQuery(" from bar in class Bar where bar.Baz.Count=667 and bar.Baz.Count!=123 and not bar.Baz.Name='1-E-1'").
- List();
- Assert.AreEqual(1, list.Count, "query many-to-one");
- list = s.CreateQuery(" from i in class Bar where i.Baz.Name='Bazza'").List();
- Assert.AreEqual(1, list.Count, "query many-to-one");
- if (TestDialect.SupportsCountDistinct)
- {
- enumerable = s.CreateQuery("select count(distinct foo.TheFoo) from foo in class Foo").Enumerable();
- Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
- }
- enumerable = s.CreateQuery("select count(foo.TheFoo.Boolean) from foo in class Foo").Enumerable();
- Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
- enumerable = s.CreateQuery("select count(*), foo.Int from foo in class Foo group by foo.Int").Enumerable();
- enumerator = enumerable.GetEnumerator();
- Assert.IsTrue(enumerator.MoveNext());
- Assert.AreEqual(3L, (long) ((object[]) enumerator.Current)[0]);
- Assert.IsFalse(enumerator.MoveNext());
- enumerable = s.CreateQuery("select sum(foo.TheFoo.Int) from foo in class Foo").Enumerable();
- Assert.IsTrue(ContainsSingleObject(enumerable, (long) 4), "sum"); // changed to Int64 (HQLFunction H3.2)
- enumerable = s.CreateQuery("select count(foo) from foo in class Foo where foo.id=?")
- .SetString(0, foo.Key).Enumerable();
- Assert.IsTrue(ContainsSingleObject(enumerable, (long) 1), "id query count");
- list = s.CreateQuery("from foo in class Foo where foo.Boolean = ?").SetBoolean(0, true).List();
- list = s.CreateQuery("select new Foo(fo.X) from Fo fo").List();
- list = s.CreateQuery("select new Foo(fo.Integer) from Foo fo").List();
- list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
- .SetCacheable(true)
- .List();
- Assert.IsTrue(list.Count == 3);
- list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
- .SetCacheable(true)
- .List();
- Assert.IsTrue(list.Count == 3);
- enumerable = s.CreateQuery("select new Foo(fo.X) from Foo fo").Enumerable();
- enumerator = enumerable.GetEnumerator();
- Assert.IsTrue(enumerator.MoveNext(), "projection iterate (results)");
- Assert.IsTrue(typeof(Foo).IsAssignableFrom(enumerator.Current.GetType()),
- "projection iterate (return check)");
- // TODO: ScrollableResults not implemented
- //ScrollableResults sr = s.CreateQuery("select new Foo(fo.x) from Foo fo").Scroll();
- //Assert.IsTrue( "projection scroll (results)", sr.next() );
- //Assert.IsTrue( "projection scroll (return check)", typeof(Foo).isAssignableFrom( sr.get(0).getClass() ) );
- list = s.CreateQuery("select foo.Long, foo.Component.Name, foo, foo.TheFoo from foo in class Foo").List();
- Assert.IsTrue(list.Count > 0);
- foreach (object[] row in list)
- {
- Assert.IsTrue(row[0] is long);
- Assert.IsTrue(row[1] is string);
- Assert.IsTrue(row[2] is Foo);
- Assert.IsTrue(row[3] is Foo);
- }
- if (TestDialect.SupportsCountDistinct)
- {
- list =
- s.CreateQuery("select avg(foo.Float), max(foo.Component.Name), count(distinct foo.id) from foo in class Foo").List();
- Assert.IsTrue(list.Count > 0);
- foreach (object[] row in list)
- {
- Assert.IsTrue(row[0] is double); // changed from float to double (HQLFunction H3.2)
- Assert.IsTrue(row[1] is string);
- Assert.IsTrue(row[2] is long); // changed from int to long (HQLFunction H3.2)
- }
- }
- list = s.CreateQuery("select foo.Long, foo.Component, foo, foo.TheFoo from foo in class Foo").List();
- Assert.IsTrue(list.Count > 0);
- foreach (object[] row in list)
- {
- Assert.IsTrue(row[0] is long);
- Assert.IsTrue(row[1] is FooComponent);
- Assert.IsTrue(row[2] is Foo);
- Assert.IsTrue(row[3] is Foo);
- }
- s.Save(new Holder("ice T"));
- s.Save(new Holder("ice cube"));
- Assert.AreEqual(15, s.CreateQuery("from o in class System.Object").List().Count);
- Assert.AreEqual(7, s.CreateQuery("from n in class INamed").List().Count);
- Assert.IsTrue(s.CreateQuery("from n in class INamed where n.Name is not null").List().Count == 4);
- foreach (INamed named in s.CreateQuery("from n in class INamed").Enumerable())
- {
- Assert.IsNotNull(named);
- }
- s.Save(new Holder("bar"));
- enumerable = s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").Enumerable();
- int cnt = 0;
- foreach (object[] row in enumerable)
- {
- if (row[0] != row[1])
- {
- cnt++;
- }
- }
- //if ( !(dialect is Dialect.HSQLDialect) )
- //{
- Assert.IsTrue(cnt == 2);
- Assert.IsTrue(s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").List().Count == 7);
- //}
- IQuery qu = s.CreateQuery("from n in class INamed where n.Name = :name");
- object temp = qu.ReturnTypes;
- temp = qu.NamedParameters;
- int c = 0;
- foreach (object obj in s.CreateQuery("from o in class System.Object").Enumerable())
- {
- c++;
- }
- Assert.IsTrue(c == 16);
- s.CreateQuery("select baz.Code, min(baz.Count) from baz in class Baz group by baz.Code").Enumerable();
- Assert.IsTrue(
- IsEmpty(
- s.CreateQuery(
- "selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
- .SetDateTime(0, DateTime.Today).Enumerable()));
- list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
- Assert.AreEqual(1, list.Count);
- list =
- s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").
- List();
- Assert.AreEqual(1, list.Count);
- list =
- s.CreateQuery(
- "select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']")
- .List();
- Assert.AreEqual(1, list.Count);
- list = s.CreateQuery("select index(date) from Baz baz join baz.StringDateMap date").List();
- Console.WriteLine(list);
- Assert.AreEqual(3, list.Count);
- s.CreateQuery(
- "from foo in class Foo where foo.Integer not between 1 and 5 and foo.String not in ('cde', 'abc') and foo.String is not null and foo.Integer<=3")
- .List();
- s.CreateQuery("from Baz baz inner join baz.CollectionComponent.Nested.Foos foo where foo.String is null").List();
- if (Dialect.SupportsSubSelects)
- {
- s.CreateQuery("from Baz baz inner join baz.FooSet where '1' in (from baz.FooSet foo where foo.String is not null)").
- List();
- s.CreateQuery(
- "from Baz baz where 'a' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
- .List();
- s.CreateQuery(
- "from Baz baz where 'b' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
- .List();
- }
- s.CreateQuery("from Foo foo join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
- //if ( !(dialect is Dialect.HSQLDialect) )
- s.CreateQuery("from Foo foo left join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
- s.CreateQuery("select foo.TheFoo from Foo foo where foo.TheFoo in ('1','2','3')").List();
- s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo in ('1','2','3')").List();
- s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
- s.CreateQuery("select foo.TheFoo.Long from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
- s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') or foo.TheFoo.Long in (1,2,3)").
- List();
- s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') group by foo.TheFoo.Long").List();
- s.CreateQuery("from Foo foo1 left join foo1.TheFoo foo2 left join foo2.TheFoo where foo1.String is not null").List();
- s.CreateQuery("from Foo foo1 left join foo1.TheFoo.TheFoo where foo1.String is not null").List();
- s.CreateQuery(
- "from Foo foo1 left join foo1.TheFoo foo2 left join foo1.TheFoo.TheFoo foo3 where foo1.String is not null").List();
- s.CreateQuery("select foo.Formula from Foo foo where foo.Formula > 0").List();
- int len = s.CreateQuery("from Foo as foo join foo.TheFoo as foo2 where foo2.id >'a' or foo2.id <'a'").List().Count;
- Assert.IsTrue(len == 2);
- s.Delete("from Holder");
- txn.Commit();
- s.Close();
- s = OpenSession();
- txn = s.BeginTransaction();
- baz = (Baz) s.CreateQuery("from Baz baz left outer join fetch baz.ManyToAny").UniqueResult();
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz.ManyToAny));
- Assert.IsTrue(baz.ManyToAny.Count == 2);
- BarProxy barp = (BarProxy) baz.ManyToAny[0];
- s.CreateQuery("from Baz baz join baz.ManyToAny").List();
- Assert.IsTrue(s.CreateQuery("select baz from Baz baz join baz.ManyToAny a where index(a) = 0").List().Count == 1);
- FooProxy foop = (FooProxy) s.Get(typeof(Foo), foo.Key);
- Assert.IsTrue(foop == baz.ManyToAny[1]);
- barp.Baz = baz;
- Assert.IsTrue(s.CreateQuery("select bar from Bar bar where bar.Baz.StringDateMap['now'] is not null").List().Count ==
- 1);
- Assert.IsTrue(
- s.CreateQuery(
- "select bar from Bar bar join bar.Baz b where b.StringDateMap['big bang'] < b.StringDateMap['now'] and b.StringDateMap['now'] is not null")
- .List().Count == 1);
- Assert.IsTrue(
- s.CreateQuery(
- "select bar from Bar bar where bar.Baz.StringDateMap['big bang'] < bar.Baz.StringDateMap['now'] and bar.Baz.StringDateMap['now'] is not null")
- .List().Count == 1);
- list = s.CreateQuery("select foo.String, foo.Component, foo.id from Bar foo").List();
- Assert.IsTrue(((FooComponent) ((object[]) list[0])[1]).Name == "foo");
- list = s.CreateQuery("select elements(baz.Components) from Baz baz").List();
- Assert.IsTrue(list.Count == 2);
- list = s.CreateQuery("select bc.Name from Baz baz join baz.Components bc").List();
- Assert.IsTrue(list.Count == 2);
- //list = s.CreateQuery("select bc from Baz baz join baz.components bc").List();
- s.CreateQuery("from Foo foo where foo.Integer < 10 order by foo.String").SetMaxResults(12).List();
- s.Delete(barp);
- s.Delete(baz);
- s.Delete(foop.TheFoo);
- s.Delete(foop);
- txn.Commit();
- s.Close();
- }
- [Test]
- public void CascadeDeleteDetached()
- {
- Baz baz;
- using (ISession s = OpenSession())
- {
- baz = new Baz();
- IList<Fee> list = new List<Fee>();
- list.Add(new Fee());
- baz.Fees = list;
- s.Save(baz);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- baz = (Baz) s.Get(typeof(Baz), baz.Code);
- }
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.Fees));
- using (ISession s = OpenSession())
- {
- s.Delete(baz);
- s.Flush();
- Assert.IsFalse(s.CreateQuery("from Fee").Enumerable().GetEnumerator().MoveNext());
- }
- using (ISession s = OpenSession())
- {
- baz = new Baz();
- IList<Fee> list = new List<Fee>();
- list.Add(new Fee());
- list.Add(new Fee());
- baz.Fees = list;
- s.Save(baz);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- baz = (Baz) s.Get(typeof(Baz), baz.Code);
- NHibernateUtil.Initialize(baz.Fees);
- }
- Assert.AreEqual(2, baz.Fees.Count);
- using (ISession s = OpenSession())
- {
- s.Delete(baz);
- s.Flush();
- Assert.IsTrue(IsEmpty(s.CreateQuery("from Fee").Enumerable()));
- }
- }
- [Test]
- public void ForeignKeys()
- {
- Baz baz;
- using (ISession s = OpenSession())
- {
- baz = new Baz();
- Foo foo = new Foo();
- IList<Foo> bag = new List<Foo>();
- bag.Add(foo);
- baz.IdFooBag = bag;
- baz.Foo = foo;
- s.Save(baz);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- baz = (Baz) s.Load(typeof(Baz), baz.Code);
- s.Delete(baz);
- s.Flush();
- }
- }
- [Test]
- public void NonlazyCollections()
- {
- object glarchId;
- using (ISession s = OpenSession())
- {
- Glarch glarch1 = new Glarch();
- glarch1.ProxySet = new LinkedHashSet<GlarchProxy>();
- Glarch glarch2 = new Glarch();
- glarch1.ProxySet.Add(glarch1);
- s.Save(glarch2);
- glarchId = s.Save(glarch1);
- s.Flush();
- }
- Glarch loadedGlarch;
- using (ISession s = OpenSession())
- {
- loadedGlarch = (Glarch) s.Get(typeof(Glarch), glarchId);
- Assert.IsTrue(NHibernateUtil.IsInitialized(loadedGlarch.ProxySet));
- }
- // ProxySet is a non-lazy collection, so this should work outside
- // a session.
- Assert.AreEqual(1, loadedGlarch.ProxySet.Count);
- using (ISession s = OpenSession())
- {
- s.Delete("from Glarch");
- s.Flush();
- }
- }
- [Test]
- public void ReuseDeletedCollection()
- {
- Baz baz, baz2;
- using (ISession s = OpenSession())
- {
- baz = new Baz();
- baz.SetDefaults();
- s.Save(baz);
- s.Flush();
- s.Delete(baz);
- baz2 = new Baz();
- baz2.StringArray = new string[] {"x-y-z"};
- s.Save(baz2);
- s.Flush();
- }
- baz2.StringSet = baz.StringSet;
- baz2.StringArray = baz.StringArray;
- baz2.FooArray = baz.FooArray;
- using (ISession s = OpenSession())
- {
- s.Update(baz2);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
- Assert.AreEqual(3, baz2.StringArray.Length);
- Assert.AreEqual(3, baz2.StringSet.Count);
- s.Delete(baz2);
- s.Flush();
- }
- }
- [Test]
- public void PropertyRef()
- {
- object qid;
- object hid;
- using (ISession s = OpenSession())
- {
- Holder h = new Holder();
- h.Name = "foo";
- Holder h2 = new Holder();
- h2.Name = "bar";
- h.OtherHolder = h2;
- hid = s.Save(h);
- Qux q = new Qux();
- q.Holder = h2;
- qid = s.Save(q);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- Holder h = (Holder) s.Load(typeof(Holder), hid);
- Assert.AreEqual(h.Name, "foo");
- Assert.AreEqual(h.OtherHolder.Name, "bar");
- object[] res =
- (object[]) s.CreateQuery("from Holder h join h.OtherHolder oh where h.OtherHolder.Name = 'bar'").List()[0];
- Assert.AreSame(h, res[0]);
- Qux q = (Qux) s.Get(typeof(Qux), qid);
- Assert.AreSame(q.Holder, h.OtherHolder);
- s.Delete(h);
- s.Delete(q);
- s.Flush();
- }
- }
- [Test]
- public void QueryCollectionOfValues()
- {
- object gid;
- using (ISession s = OpenSession())
- {
- Baz baz = new Baz();
- baz.SetDefaults();
- s.Save(baz);
- Glarch g = new Glarch();
- gid = s.Save(g);
- if (Dialect.SupportsSubSelects)
- {
- if (Dialect.SupportsScalarSubSelects)
- s.CreateFilter(baz.FooArray, "where size(this.Bytes) > 0").List();
- s.CreateFilter(baz.FooArray, "where 0 in elements(this.Bytes)").List();
- }
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- //s.CreateQuery("from Baz baz where baz.FooSet.String = 'foo'").List();
- //s.CreateQuery("from Baz baz where baz.FooArray.String = 'foo'").List();
- //s.CreateQuery("from Baz baz where baz.FooSet.foo.String = 'foo'").List();
- //s.CreateQuery("from Baz baz join baz.FooSet.Foo foo where foo.String = 'foo'").List();
- s.CreateQuery("from Baz baz join baz.FooSet foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
- s.CreateQuery("from Baz baz join baz.FooArray foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
- s.CreateQuery("from Baz baz join baz.StringDateMap date where index(date) = 'foo'").List();
- s.CreateQuery("from Baz baz join baz.TopGlarchez g where index(g) = 'A'").List();
- s.CreateQuery("select index(g) from Baz baz join baz.TopGlarchez g").List();
- Assert.AreEqual(3, s.CreateQuery("from Baz baz left join baz.StringSet").List().Count);
- Baz baz = (Baz) s.CreateQuery("from Baz baz join baz.StringSet str where str='foo'").List()[0];
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
- baz = (Baz) s.CreateQuery("from Baz baz left join fetch baz.StringSet").List()[0];
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
- Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.StringSet string where string='foo'").List().Count);
- Assert.AreEqual(1, s.CreateQuery("from Baz baz inner join baz.Components comp where comp.Name='foo'").List().Count);
- //IList bss = s.CreateQuery("select baz, ss from Baz baz inner join baz.StringSet ss").List();
- s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee is not null").List();
- s.CreateQuery("from Glarch g inner join g.FooComponents comp join comp.Fee fee where fee.Count > 0").List();
- s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee.Count is not null").List();
- s.Delete(baz);
- //s.delete("from Glarch g");
- s.Delete(s.Get(typeof(Glarch), gid));
- s.Flush();
- }
- }
- [Test]
- public void BatchLoad()
- {
- Baz baz, baz2, baz3;
- using (ISession s = OpenSession())
- {
- baz = new Baz();
- var stringSet = new SortedSet<string> { "foo", "bar" };
- var fooSet = new HashSet<FooProxy>();
- for (int i = 0; i < 3; i++)
- {
- Foo foo = new Foo();
- s.Save(foo);
- fooSet.Add(foo);
- }
- baz.FooSet = fooSet;
- baz.StringSet = stringSet;
- s.Save(baz);
- baz2 = new Baz();
- fooSet = new HashSet<FooProxy>();
- for (int i = 0; i < 2; i++)
- {
- Foo foo = new Foo();
- s.Save(foo);
- fooSet.Add(foo);
- }
- baz2.FooSet = fooSet;
- s.Save(baz2);
- baz3 = new Baz();
- stringSet = new SortedSet<string>();
- stringSet.Add("foo");
- stringSet.Add("baz");
- baz3.StringSet = stringSet;
- s.Save(baz3);
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- baz = (Baz) s.Load(typeof(Baz), baz.Code);
- baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
- baz3 = (Baz) s.Load(typeof(Baz), baz3.Code);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.FooSet));
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.StringSet));
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.StringSet));
- Assert.AreEqual(3, baz.FooSet.Count);
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooSet));
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.FooSet));
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.FooSet));
- Assert.AreEqual(2, baz2.FooSet.Count);
- Assert.IsTrue(baz3.StringSet.Contains("baz"));
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.StringSet));
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.StringSet));
- Assert.AreEqual(2, baz.StringSet.Count);
- Assert.AreEqual(0, baz2.StringSet.Count);
- s.Delete(baz);
- s.Delete(baz2);
- s.Delete(baz3);
- IEnumerable en = new JoinedEnumerable(
- new IEnumerable[] {baz.FooSet, baz2.FooSet});
- foreach (object obj in en)
- {
- s.Delete(obj);
- }
- s.Flush();
- }
- }
- [Test]
- public void FetchInitializedCollection()
- {
- ISession s = OpenSession();
- Baz baz = new Baz();
- IList<Foo> fooBag = new List<Foo>();
- fooBag.Add(new Foo());
- fooBag.Add(new Foo());
- baz.FooBag = fooBag;
- s.Save(baz);
- s.Flush();
- fooBag = baz.FooBag;
- s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
- Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
- s.Close();
- s = OpenSession();
- baz = (Baz) s.Load(typeof(Baz), baz.Code);
- Object bag = baz.FooBag;
- Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
- s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
- Assert.IsTrue(bag == baz.FooBag);
- Assert.IsTrue(baz.FooBag.Count == 2);
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void LateCollectionAdd()
- {
- object id;
- using (ISession s = OpenSession())
- {
- Baz baz = new Baz();
- IList<string> l = new List<string>();
- baz.StringList = l;
- l.Add("foo");
- id = s.Save(baz);
- l.Add("bar");
- s.Flush();
- l.Add("baz");
- s.Flush();
- }
- using (ISession s = OpenSession())
- {
- Baz baz = (Baz) s.Load(typeof(Baz), id);
- Assert.AreEqual(3, baz.StringList.Count);
- Assert.IsTrue(baz.StringList.Contains("foo"));
- Assert.IsTrue(baz.StringList.Contains("bar"));
- Assert.IsTrue(baz.StringList.Contains("baz"));
- s.Delete(baz);
- s.Flush();
- }
- }
- [Test]
- public void Update()
- {
- ISession s = OpenSession();
- Foo foo = new Foo();
- s.Save(foo);
- s.Flush();
- s.Close();
- s = OpenSession();
- FooProxy foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
- foo2.String = "dirty";
- foo2.Boolean = false;
- foo2.Bytes = new byte[] {1, 2, 3};
- foo2.Date = DateTime.Today;
- foo2.Short = 69;
- s.Flush();
- s.Close();
- s = OpenSession();
- Foo foo3 = new Foo();
- s.Load(foo3, foo.Key);
- Assert.IsTrue(foo2.EqualsFoo(foo3), "update");
- s.Delete(foo3);
- s.Flush();
- s.Close();
- }
- [Test]
- public void ListRemove()
- {
- using (ISession s = OpenSession())
- {
- Baz b = new Baz();
- IList<string> stringList = new List<string>();
- IList<Fee> feeList = new List<Fee>();
- b.Fees = feeList;
- b.StringList = stringList;
- feeList.Add(new Fee());
- feeList.Add(new Fee());
- feeList.Add(new Fee());
- feeList.Add(new Fee());
- stringList.Add("foo");
- stringList.Add("bar");
- stringList.Add("baz");
- stringList.Add("glarch");
- s.Save(b);
- s.Flush();
- stringList.RemoveAt(1);
- feeList.RemoveAt(1);
- s.Flush();
- s.Evict(b);
- s.Refresh(b);
- Assert.AreEqual(3, b.Fees.Count);
- stringList = b.StringList;
- Assert.AreEqual(3, stringList.Count);
- Assert.AreEqual("baz", stringList[1]);
- Assert.AreEqual("foo", stringList[0]);
- s.Delete(b);
- s.Delete("from Fee");
- s.Flush();
- }
- }
- [Test]
- public void FetchInitializedCollectionDupe()
- {
- string bazCode;
- using (ISession s = OpenSession())
- {
- Baz baz = new Baz();
- IList<Foo> fooBag = new List<Foo>();
- fooBag.Add(new Foo());
- fooBag.Add(new Foo());
- baz.FooBag = fooBag;
- s.Save(baz);
- s.Flush();
- fooBag = baz.FooBag;
- s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
- Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
- Assert.AreSame(fooBag, baz.FooBag);
- Assert.AreEqual(2, baz.FooBag.Count);
- bazCode = baz.Code;
- }
- using (ISession s = OpenSession())
- {
- Baz baz = (Baz) s.Load(typeof(Baz), bazCode);
- object bag = baz.FooBag;
- Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
- s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
- Assert.IsTrue(NHibernateUtil.IsInitialized(bag));
- Assert.AreSame(bag, baz.FooBag);
- Assert.AreEqual(2, baz.FooBag.Count);
- s.Delete(baz);
- s.Flush();
- }
- }
- [Test]
- public void Sortables()
- {
- ISession s = OpenSession();
- ITransaction t = s.BeginTransaction();
- Baz b = new Baz();
- var ss = new HashSet<Sortable>
- {
- new Sortable("foo"), new Sortable("bar"), new Sortable("baz")
- };
- b.Sortablez = ss;
- s.Save(b);
- s.Flush();
- t.Commit();
- s.Close();
- s = OpenSession();
- t = s.BeginTransaction();
- IList result = s.CreateCriteria(typeof(Baz))
- .AddOrder(Order.Asc("Name"))
- .List();
- b = (Baz) result[0];
- Assert.IsTrue(b.Sortablez.Count == 3);
- // compare the first item in the "Set" sortablez - can't reference
- // the first item using b.sortablez[0] because it thinks 0 is the
- // DictionaryEntry key - not the index.
- foreach (Sortable sortable in b.Sortablez)
- {
- Assert.AreEqual(sortable.name, "bar");
- break;
- }
- s.Flush();
- t.Commit();
- s.Close();
- s = OpenSession();
- t = s.BeginTransaction();
- result = s.CreateQuery("from Baz baz left join fetch baz.Sortablez order by baz.Name asc")
- .List();
- b = (Baz) result[0];
- Assert.IsTrue(b.Sortablez.Count == 3);
- foreach (Sortable sortable in b.Sortablez)
- {
- Assert.AreEqual(sortable.name, "bar");
- break;
- }
- s.Flush();
- t.Commit();
- s.Close();
- s = OpenSession();
- t = s.BeginTransaction();
- result = s.CreateQuery("from Baz baz order by baz.Name asc")
- .List();
- b = (Baz) result[0];
- Assert.IsTrue(b.Sortablez.Count == 3);
- foreach (Sortable sortable in b.Sortablez)
- {
- Assert.AreEqual(sortable.name, "bar");
- break;
- }
- s.Delete(b);
- s.Flush();
- t.Commit();
- s.Close();
- }
- [Test]
- public void FetchList()
- {
- ISession s = OpenSession();
- Baz baz = new Baz();
- s.Save(baz);
- Foo foo = new Foo();
- s.Save(foo);
- Foo foo2 = new Foo();
- s.Save(foo2);
- s.Flush();
- IList<Fee> list = new List<Fee>();
- for (int i = 0; i < 5; i++)
- {
- Fee fee = new Fee();
- list.Add(fee);
- }
- baz.Fees = list;
- var result = s.CreateQuery("from Foo foo, Baz baz left join fetch baz.Fees").List();
- Assert.IsTrue(NHibernateUtil.IsInitialized(((Baz)((object[])result[0])[1]).Fees));
- s.Delete(foo);
- s.Delete(foo2);
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void BagOneToMany()
- {
- ISession s = OpenSession();
- Baz baz = new Baz();
- IList<Baz> list = new List<Baz>();
- baz.Bazez = list;
- list.Add(new Baz());
- s.Save(baz);
- s.Flush();
- list.Add(new Baz());
- s.Flush();
- list.Insert(0, new Baz());
- s.Flush();
- object toDelete = list[1];
- list.RemoveAt(1);
- s.Delete(toDelete);
- s.Flush();
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void QueryLockMode()
- {
- ISession s = OpenSession();
- ITransaction tx = s.BeginTransaction();
- Bar bar = new Bar();
- Assert.IsNull(bar.Bytes);
- s.Save(bar);
- Assert.IsNotNull(bar.Bytes);
- s.Flush();
- Assert.IsNotNull(bar.Bytes);
- bar.String = "changed";
- Baz baz = new Baz();
- baz.Foo = bar;
- s.Save(baz);
- Assert.IsNotNull(bar.Bytes);
- IQuery q = s.CreateQuery("from Foo foo, Bar bar");
- q.SetLockMode("bar", LockMode.Upgrade);
- object[] result = (object[]) q.List()[0];
- Assert.IsNotNull(bar.Bytes);
- object b = result[0];
- Assert.IsTrue(s.GetCurrentLockMode(b) == LockMode.Write && s.GetCurrentLockMode(result[1]) == LockMode.Write);
- tx.Commit();
- Assert.IsNotNull(bar.Bytes);
- s.Disconnect();
- s.Reconnect();
- tx = s.BeginTransaction();
- Assert.IsNotNull(bar.Bytes);
- Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
- Assert.IsNotNull(bar.Bytes);
- s.CreateQuery("from Foo foo").List();
- Assert.IsNotNull(bar.Bytes);
- Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
- q = s.CreateQuery("from Foo foo");
- q.SetLockMode("foo", LockMode.Read);
- q.List();
- Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(b));
- s.Evict(baz);
- tx.Commit();
- s.Disconnect();
- s.Reconnect();
- tx = s.BeginTransaction();
- Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
- s.Delete(s.Load(typeof(Baz), baz.Code));
- Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
- tx.Commit();
- s.Close();
- s = OpenSession();
- tx = s.BeginTransaction();
- q = s.CreateQuery("from Foo foo, Bar bar, Bar bar2");
- q.SetLockMode("bar", LockMode.Upgrade);
- q.SetLockMode("bar2", LockMode.Read);
- result = (object[]) q.List()[0];
- Assert.IsTrue(s.GetCurrentLockMode(result[0]) == LockMode.Upgrade &&
- s.GetCurrentLockMode(result[1]) == LockMode.Upgrade);
- s.Delete(result[0]);
- tx.Commit();
- s.Close();
- }
- [Test]
- public void ManyToManyBag()
- {
- ISession s = OpenSession();
- Baz baz = new Baz();
- object id = s.Save(baz);
- s.Flush();
- s.Close();
- s = OpenSession();
- baz = (Baz) s.Load(typeof(Baz), id);
- baz.FooBag.Add(new Foo());
- s.Flush();
- s.Close();
- s = OpenSession();
- baz = (Baz) s.Load(typeof(Baz), id);
- Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooBag));
- Assert.AreEqual(1, baz.FooBag.Count);
- Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooBag[0]));
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void IdBag()
- {
- ISession s = OpenSession();
- Baz baz = new Baz();
- s.Save(baz);
- IList<Foo> l = new List<Foo>();
- IList<byte[]> l2 = new List<byte[]>();
- baz.IdFooBag = l;
- baz.ByteBag = l2;
- l.Add(new Foo());
- l.Add(new Bar());
- byte[] bytes = GetBytes("ffo");
- l2.Add(bytes);
- l2.Add(GetBytes("foo"));
- s.Flush();
- l.Add(new Foo());
- l.Add(new Bar());
- l2.Add(GetBytes("bar"));
- s.Flush();
- object removedObject = l[3];
- l.RemoveAt(3);
- s.Delete(removedObject);
- bytes[1] = Convert.ToByte('o');
- s.Flush();
- s.Close();
- s = OpenSession();
- baz = (Baz) s.Load(typeof(Baz), baz.Code);
- Assert.AreEqual(3, baz.IdFooBag.Count);
- Assert.AreEqual(3, baz.ByteBag.Count);
- bytes = GetBytes("foobar");
- foreach (object obj in baz.IdFooBag)
- {
- s.Delete(obj);
- }
- baz.IdFooBag = null;
- baz.ByteBag.Add(bytes);
- baz.ByteBag.Add(bytes);
- s.Flush();
- s.Close();
- s = OpenSession();
- baz = (Baz) s.Load(typeof(Baz), baz.Code);
- Assert.AreEqual(0, baz.IdFooBag.Count);
- Assert.AreEqual(5, baz.ByteBag.Count);
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void ForceOuterJoin()
- {
- if (Sfi.Settings.IsOuterJoinFetchEnabled == false)
- {
- // don't bother to run the test if we can't test it
- return;
- }
- ISession s = OpenSession();
- Glarch g = new Glarch();
- FooComponent fc = new FooComponent();
- fc.Glarch = g;
- FooProxy f = new Foo();
- FooProxy f2 = new Foo();
- f.Component = fc;
- f.TheFoo = f2;
- s.Save(f2);
- object id = s.Save(f);
- object gid = s.GetIdentifier(f.Component.Glarch);
- s.Flush();
- s.Close();
- s = OpenSession();
- f = (FooProxy) s.Load(typeof(Foo), id);
- Assert.IsFalse(NHibernateUtil.IsInitialized(f));
- Assert.IsTrue(NHibernateUtil.IsInitialized(f.Component.Glarch)); //outer-join="true"
- Assert.IsFalse(NHibernateUtil.IsInitialized(f.TheFoo)); //outer-join="auto"
- Assert.AreEqual(gid, s.GetIdentifier(f.Component.Glarch));
- s.Delete(f);
- s.Delete(f.TheFoo);
- s.Delete(f.Component.Glarch);
- s.Flush();
- s.Close();
- }
- [Test]
- public void EmptyCollection()
- {
- ISession s = OpenSession();
- object id = s.Save(new Baz());
- s.Flush();
- s.Close();
- s = OpenSession();
- Baz baz = (Baz) s.Load(typeof(Baz), id);
- Assert.IsTrue(baz.FooSet.Count == 0);
- Foo foo = new Foo();
- baz.FooSet.Add(foo);
- s.Save(foo);
- s.Flush();
- s.Delete(foo);
- s.Delete(baz);
- s.Flush();
- s.Close();
- }
- [Test]
- public void OneToOneGenerator()
- {
- ISession s = OpenSession();
- X x = new X();
- Y y = new Y();
- x.Y = y;
- y.TheX = x;
- object yId = s.Save(y);
- object xId = s.Save(x);
- Assert.AreEqual(yId, xId);
- s.Flush();
- A…
Large files files are truncated, but you can click here to view the full file