PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NHibernate.Test/Legacy/FooBarTest.cs

http://github.com/nhibernate/nhibernate-core
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

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using System.Text;
  12. using Iesi.Collections.Generic;
  13. using NHibernate.Connection;
  14. using NHibernate.Dialect;
  15. using NHibernate.DomainModel;
  16. using NHibernate.Criterion;
  17. using NHibernate.Proxy;
  18. using NHibernate.Type;
  19. using NHibernate.Util;
  20. using NUnit.Framework;
  21. namespace NHibernate.Test.Legacy
  22. {
  23. [TestFixture]
  24. public class FooBarTest : TestCase
  25. {
  26. // Equivalent of Java String.getBytes()
  27. private static byte[] GetBytes(string str)
  28. {
  29. return Encoding.Unicode.GetBytes(str);
  30. }
  31. protected override string[] Mappings
  32. {
  33. get
  34. {
  35. return new string[]
  36. {
  37. "FooBar.hbm.xml",
  38. "Baz.hbm.xml",
  39. "Qux.hbm.xml",
  40. "Glarch.hbm.xml",
  41. "Fum.hbm.xml",
  42. "Fumm.hbm.xml",
  43. "Fo.hbm.xml",
  44. "One.hbm.xml",
  45. "Many.hbm.xml",
  46. "Immutable.hbm.xml",
  47. "Fee.hbm.xml",
  48. "Vetoer.hbm.xml",
  49. "Holder.hbm.xml",
  50. "Location.hbm.xml",
  51. "Stuff.hbm.xml",
  52. "Container.hbm.xml",
  53. "Simple.hbm.xml",
  54. "XY.hbm.xml"
  55. };
  56. }
  57. }
  58. [Test]
  59. public void CollectionVersioning()
  60. {
  61. using (ISession s = OpenSession())
  62. {
  63. One one = new One();
  64. one.Manies = new HashSet<Many>();
  65. s.Save(one);
  66. s.Flush();
  67. Many many = new Many();
  68. many.One = one;
  69. one.Manies.Add(many);
  70. s.Save(many);
  71. s.Flush();
  72. // Versions are incremented compared to Hibernate because they start from 1
  73. // in NH.
  74. Assert.AreEqual(1, many.V);
  75. Assert.AreEqual(2, one.V);
  76. s.Delete(many);
  77. s.Delete(one);
  78. s.Flush();
  79. }
  80. }
  81. [Test]
  82. public void ForCertain()
  83. {
  84. Glarch g = new Glarch();
  85. Glarch g2 = new Glarch();
  86. IList<string> strings = new List<string>();
  87. strings.Add("foo");
  88. g2.Strings = strings;
  89. object gid, g2id;
  90. using (ISession s = OpenSession())
  91. {
  92. using (ITransaction t = s.BeginTransaction())
  93. {
  94. gid = s.Save(g);
  95. g2id = s.Save(g2);
  96. t.Commit();
  97. // Versions are initialized to 1 in NH.
  98. Assert.AreEqual(1, g.Version);
  99. Assert.AreEqual(1, g2.Version);
  100. }
  101. }
  102. using (ISession s = OpenSession())
  103. {
  104. using (ITransaction t = s.BeginTransaction())
  105. {
  106. g = (Glarch) s.Get(typeof(Glarch), gid);
  107. g2 = (Glarch) s.Get(typeof(Glarch), g2id);
  108. Assert.AreEqual(1, g2.Strings.Count);
  109. s.Delete(g);
  110. s.Delete(g2);
  111. t.Commit();
  112. }
  113. }
  114. }
  115. [Test]
  116. public void BagMultipleElements()
  117. {
  118. string bazCode;
  119. using (ISession s = OpenSession())
  120. {
  121. using (ITransaction t = s.BeginTransaction())
  122. {
  123. Baz baz = new Baz();
  124. baz.Bag = new List<string>();
  125. baz.ByteBag = new List<byte[]>();
  126. s.Save(baz);
  127. baz.Bag.Add("foo");
  128. baz.Bag.Add("bar");
  129. baz.ByteBag.Add(GetBytes("foo"));
  130. baz.ByteBag.Add(GetBytes("bar"));
  131. t.Commit();
  132. bazCode = baz.Code;
  133. }
  134. }
  135. using (ISession s = OpenSession())
  136. {
  137. using (ITransaction t = s.BeginTransaction())
  138. {
  139. //put in cache
  140. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  141. Assert.AreEqual(2, baz.Bag.Count);
  142. Assert.AreEqual(2, baz.ByteBag.Count);
  143. t.Commit();
  144. }
  145. }
  146. using (ISession s = OpenSession())
  147. {
  148. using (ITransaction t = s.BeginTransaction())
  149. {
  150. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  151. Assert.AreEqual(2, baz.Bag.Count);
  152. Assert.AreEqual(2, baz.ByteBag.Count);
  153. baz.Bag.Remove("bar");
  154. baz.Bag.Add("foo");
  155. baz.ByteBag.Add(GetBytes("bar"));
  156. t.Commit();
  157. }
  158. }
  159. using (ISession s = OpenSession())
  160. {
  161. using (ITransaction t = s.BeginTransaction())
  162. {
  163. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  164. Assert.AreEqual(2, baz.Bag.Count);
  165. Assert.AreEqual(3, baz.ByteBag.Count);
  166. s.Delete(baz);
  167. t.Commit();
  168. }
  169. }
  170. }
  171. [Test]
  172. public void WierdSession()
  173. {
  174. object id;
  175. using (ISession s = OpenSession())
  176. {
  177. using (ITransaction t = s.BeginTransaction())
  178. {
  179. id = s.Save(new Foo());
  180. t.Commit();
  181. }
  182. }
  183. using (ISession s = OpenSession())
  184. {
  185. s.FlushMode = FlushMode.Manual;
  186. using (ITransaction t = s.BeginTransaction())
  187. {
  188. Foo foo = (Foo) s.Get(typeof(Foo), id);
  189. t.Commit();
  190. }
  191. s.Disconnect();
  192. s.Reconnect();
  193. using (ITransaction t = s.BeginTransaction())
  194. {
  195. s.Flush();
  196. t.Commit();
  197. }
  198. }
  199. using (ISession s = OpenSession())
  200. {
  201. using (ITransaction t = s.BeginTransaction())
  202. {
  203. Foo foo = (Foo) s.Get(typeof(Foo), id);
  204. s.Delete(foo);
  205. t.Commit();
  206. }
  207. }
  208. }
  209. [Test]
  210. public void DereferenceLazyCollection()
  211. {
  212. string fooKey;
  213. string bazCode;
  214. using (ISession s = OpenSession())
  215. {
  216. Baz baz = new Baz();
  217. baz.FooSet = new HashSet<FooProxy>();
  218. Foo foo = new Foo();
  219. baz.FooSet.Add(foo);
  220. s.Save(foo);
  221. s.Save(baz);
  222. foo.Bytes = GetBytes("foobar");
  223. s.Flush();
  224. fooKey = foo.Key;
  225. bazCode = baz.Code;
  226. }
  227. using (ISession s = OpenSession())
  228. {
  229. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  230. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  231. // H2.1 has 6 here, but we are using Unicode
  232. Assert.AreEqual(12, foo.Bytes.Length);
  233. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  234. Assert.AreEqual(1, baz.FooSet.Count);
  235. s.Flush();
  236. }
  237. Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  238. using (ISession s = OpenSession())
  239. {
  240. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  241. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  242. baz.FooSet = null;
  243. s.Flush();
  244. }
  245. using (ISession s = OpenSession())
  246. {
  247. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  248. Assert.AreEqual(12, foo.Bytes.Length);
  249. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  250. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  251. Assert.AreEqual(0, baz.FooSet.Count);
  252. s.Delete(baz);
  253. s.Delete(foo);
  254. s.Flush();
  255. }
  256. }
  257. [Test]
  258. public void MoveLazyCollection()
  259. {
  260. string fooKey, bazCode, baz2Code;
  261. using (ISession s = OpenSession())
  262. {
  263. Baz baz = new Baz();
  264. Baz baz2 = new Baz();
  265. baz.FooSet = new HashSet<FooProxy>();
  266. Foo foo = new Foo();
  267. baz.FooSet.Add(foo);
  268. s.Save(foo);
  269. s.Save(baz);
  270. s.Save(baz2);
  271. foo.Bytes = GetBytes("foobar");
  272. s.Flush();
  273. fooKey = foo.Key;
  274. bazCode = baz.Code;
  275. baz2Code = baz2.Code;
  276. }
  277. using (ISession s = OpenSession())
  278. {
  279. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  280. Assert.IsTrue(NHibernateUtil.IsInitialized(foo.Bytes));
  281. Assert.AreEqual(12, foo.Bytes.Length);
  282. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  283. Assert.AreEqual(1, baz.FooSet.Count);
  284. s.Flush();
  285. }
  286. Sfi.EvictCollection("NHibernate.DomainModel.Baz.FooSet");
  287. using (ISession s = OpenSession())
  288. {
  289. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  290. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  291. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  292. baz2.FooSet = baz.FooSet;
  293. baz.FooSet = null;
  294. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  295. s.Flush();
  296. }
  297. using (ISession s = OpenSession())
  298. {
  299. Foo foo = (Foo) s.Get(typeof(Foo), fooKey);
  300. Assert.AreEqual(12, foo.Bytes.Length);
  301. Baz baz = (Baz) s.Get(typeof(Baz), bazCode);
  302. Baz baz2 = (Baz) s.Get(typeof(Baz), baz2Code);
  303. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  304. Assert.AreEqual(0, baz.FooSet.Count);
  305. Assert.IsTrue( NHibernateUtil.IsInitialized( baz2.FooSet ) ); //FooSet has batching enabled
  306. Assert.AreEqual(1, baz2.FooSet.Count);
  307. s.Delete(baz);
  308. s.Delete(baz2);
  309. s.Delete(foo);
  310. s.Flush();
  311. }
  312. }
  313. [Test]
  314. public void CriteriaCollection()
  315. {
  316. ISession s = OpenSession();
  317. Baz bb = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  318. Baz baz = new Baz();
  319. s.Save(baz);
  320. s.Flush();
  321. s.Close();
  322. s = OpenSession();
  323. Baz b = (Baz) s.CreateCriteria(typeof(Baz)).UniqueResult();
  324. Assert.IsTrue(NHibernateUtil.IsInitialized(b.TopGlarchez));
  325. Assert.AreEqual(0, b.TopGlarchez.Count);
  326. s.Delete(b);
  327. s.Flush();
  328. s.CreateCriteria(typeof(Baz))
  329. .CreateCriteria("TopFoos")
  330. .Add(Expression.IsNotNull("id"))
  331. .List();
  332. s.CreateCriteria(typeof(Baz))
  333. .CreateCriteria("Foo")
  334. .CreateCriteria("Component.Glarch")
  335. .CreateCriteria("ProxySet")
  336. .Add(Expression.IsNotNull("id"))
  337. .List();
  338. s.Close();
  339. }
  340. private static bool IsEmpty(IEnumerable enumerable)
  341. {
  342. return !enumerable.GetEnumerator().MoveNext();
  343. }
  344. private static bool ContainsSingleObject(IEnumerable enumerable, object obj)
  345. {
  346. IEnumerator enumerator = enumerable.GetEnumerator();
  347. // Fail if no items
  348. if (!enumerator.MoveNext())
  349. {
  350. return false;
  351. }
  352. // Fail if item not equal
  353. if (!Equals(obj, enumerator.Current))
  354. {
  355. return false;
  356. }
  357. // Fail if more items
  358. if (enumerator.MoveNext())
  359. {
  360. return false;
  361. }
  362. // Succeed
  363. return true;
  364. }
  365. [Test]
  366. public void Query()
  367. {
  368. ISession s = OpenSession();
  369. ITransaction txn = s.BeginTransaction();
  370. Foo foo = new Foo();
  371. s.Save(foo);
  372. Foo foo2 = new Foo();
  373. s.Save(foo2);
  374. foo.TheFoo = foo2;
  375. IList list = s.CreateQuery("from Foo foo inner join fetch foo.TheFoo").List();
  376. Foo foof = (Foo) list[0];
  377. Assert.IsTrue(NHibernateUtil.IsInitialized(foof.TheFoo));
  378. list = s.CreateQuery("from Baz baz left outer join fetch baz.FooToGlarch").List();
  379. list = s.CreateQuery("select foo, bar from Foo foo left outer join foo.TheFoo bar where foo = ?")
  380. .SetEntity(0, foo).List();
  381. object[] row1 = (object[]) list[0];
  382. Assert.IsTrue(row1[0] == foo && row1[1] == foo2);
  383. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo = 'bar'").List();
  384. s.CreateQuery("select foo.TheFoo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar'").List();
  385. s.CreateQuery("select foo.TheFoo.TheFoo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'bar'").
  386. List();
  387. // if( !( dialect is Dialect.HSQLDialect ) )
  388. // {
  389. s.CreateQuery("select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo = foo.TheFoo.TheFoo").List();
  390. // }
  391. s.CreateQuery(
  392. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo = 'bar' and foo.TheFoo.TheFoo.TheFoo = 'baz'").List
  393. ();
  394. s.CreateQuery(
  395. "select foo.String from foo in class Foo where foo.TheFoo.TheFoo.TheFoo.String = 'a' and foo.TheFoo.String = 'b'").
  396. List();
  397. s.CreateQuery("from bar in class Bar, foo in elements(bar.Baz.FooArray)").List();
  398. if (Dialect is DB2Dialect)
  399. {
  400. s.CreateQuery("from foo in class Foo where lower( foo.TheFoo.String ) = 'foo'").List();
  401. s.CreateQuery("from foo in class Foo where lower( (foo.TheFoo.String || 'foo') || 'bar' ) = 'foo'").List();
  402. s.CreateQuery("from foo in class Foo where repeat( (foo.TheFoo.STring || 'foo') || 'bar', 2 ) = 'foo'").List();
  403. s.CreateQuery(
  404. "From foo in class Bar where foo.TheFoo.Integer is not null and repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  405. .List();
  406. s.CreateQuery(
  407. "From foo in class Bar where foo.TheFoo.Integer is not null or repeat( (foo.TheFoo.String || 'foo') || 'bar', (5+5)/2 ) = 'foo'")
  408. .List();
  409. }
  410. if (Dialect is MsSql2000Dialect)
  411. {
  412. s.CreateQuery("select baz from Baz as baz join baz.FooArray foo group by baz order by sum(foo.Float)").Enumerable();
  413. }
  414. s.CreateQuery("from Foo as foo where foo.Component.Glarch.Name is not null").List();
  415. s.CreateQuery("from Foo as foo left outer join foo.Component.Glarch as glarch where glarch.Name = 'foo'").List();
  416. list = s.CreateQuery("from Foo").List();
  417. Assert.AreEqual(2, list.Count);
  418. Assert.IsTrue(list[0] is FooProxy);
  419. list = s.CreateQuery("from Foo foo left outer join foo.TheFoo").List();
  420. Assert.AreEqual(2, list.Count);
  421. Assert.IsTrue(((object[]) list[0])[0] is FooProxy);
  422. s.CreateQuery("From Foo, Bar").List();
  423. s.CreateQuery("from Baz baz left join baz.FooToGlarch, Bar bar join bar.TheFoo").List();
  424. s.CreateQuery("from Baz baz left join baz.FooToGlarch join baz.FooSet").List();
  425. s.CreateQuery("from Baz baz left join baz.FooToGlarch join fetch baz.FooSet foo left join fetch foo.TheFoo").List();
  426. list =
  427. s.CreateQuery(
  428. "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")
  429. .List();
  430. Assert.AreEqual(0, list.Count, "empty query");
  431. IEnumerable enumerable =
  432. s.CreateQuery(
  433. "from foo in class NHibernate.DomainModel.Foo where foo.String='osama bin laden' order by foo.String asc, foo.Component.Count desc")
  434. .Enumerable();
  435. Assert.IsTrue(IsEmpty(enumerable), "empty enumerator");
  436. list = s.CreateQuery("select foo.TheFoo from foo in class NHibernate.DomainModel.Foo").List();
  437. Assert.AreEqual(1, list.Count, "query");
  438. Assert.AreEqual(foo.TheFoo, list[0], "returned object");
  439. foo.TheFoo.TheFoo = foo;
  440. foo.String = "fizard";
  441. if (Dialect.SupportsSubSelects && TestDialect.SupportsOperatorSome)
  442. {
  443. if (!(Dialect is FirebirdDialect))
  444. {
  445. list = s.CreateQuery(
  446. "from foo in class NHibernate.DomainModel.Foo where ? = some elements(foo.Component.ImportantDates)").
  447. SetDateTime(0, DateTime.Today).List();
  448. Assert.AreEqual(2, list.Count, "component query");
  449. }
  450. if (Dialect.SupportsScalarSubSelects)
  451. {
  452. list =
  453. s.CreateQuery("from foo in class NHibernate.DomainModel.Foo where size(foo.Component.ImportantDates) = 3").List();
  454. Assert.AreEqual(2, list.Count, "component query");
  455. list = s.CreateQuery("from foo in class Foo where 0 = size(foo.Component.ImportantDates)").List();
  456. Assert.AreEqual(0, list.Count, "component query");
  457. }
  458. list = s.CreateQuery("from foo in class Foo where exists elements(foo.Component.ImportantDates)").List();
  459. Assert.AreEqual(2, list.Count, "component query");
  460. s.CreateQuery("from foo in class Foo where not exists (from bar in class Bar where bar.id = foo.id)").List();
  461. s.CreateQuery(
  462. "select foo.TheFoo from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  463. .List();
  464. s.CreateQuery(
  465. "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'")
  466. .List();
  467. s.CreateQuery(
  468. "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)")
  469. .List();
  470. s.CreateQuery("from foo in class Foo where foo = some(select x from x in class Foo where x.Long > foo.TheFoo.Long)")
  471. .List();
  472. s.CreateQuery(
  473. "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'")
  474. .Enumerable();
  475. }
  476. list = s.CreateQuery("from foo in class Foo where foo.Component.Count is null order by foo.Component.Count").List();
  477. Assert.AreEqual(0, list.Count, "component query");
  478. list = s.CreateQuery("from foo in class Foo where foo.Component.Name='foo'").List();
  479. Assert.AreEqual(2, list.Count, "component query");
  480. list =
  481. s.CreateQuery(
  482. "select distinct foo.Component.Name, foo.Component.Name from foo in class Foo where foo.Component.Name='foo'").List
  483. ();
  484. Assert.AreEqual(1, list.Count, "component query");
  485. list =
  486. s.CreateQuery("select distinct foo.Component.Name, foo.id from foo in class Foo where foo.Component.Name='foo'").
  487. List();
  488. Assert.AreEqual(2, list.Count, "component query");
  489. list = s.CreateQuery("select foo.TheFoo from foo in class Foo").List();
  490. Assert.AreEqual(2, list.Count, "query");
  491. list = s.CreateQuery("from foo in class Foo where foo.id=?").SetString(0, foo.Key).List();
  492. Assert.AreEqual(1, list.Count, "id query");
  493. list = s.CreateQuery("from foo in class Foo where foo.Key=?").SetString(0, foo.Key).List();
  494. Assert.AreEqual(1, list.Count, "named id query");
  495. Assert.AreSame(foo, list[0], "id query");
  496. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.String='fizard'").List();
  497. Assert.AreEqual(1, list.Count, "query");
  498. Assert.AreSame(foo.TheFoo, list[0], "returned object");
  499. list = s.CreateQuery("from foo in class Foo where foo.Component.Subcomponent.Name='bar'").List();
  500. Assert.AreEqual(2, list.Count, "components of components");
  501. list = s.CreateQuery("select foo.TheFoo from foo in class Foo where foo.TheFoo.id=?")
  502. .SetString(0, foo.TheFoo.Key).List();
  503. Assert.AreEqual(1, list.Count, "by id query");
  504. Assert.AreSame(foo.TheFoo, list[0], "by id returned object");
  505. s.CreateQuery("from foo in class Foo where foo.TheFoo = ?").SetEntity(0, foo.TheFoo).List();
  506. Assert.IsTrue(
  507. IsEmpty(s.CreateQuery("from bar in class Bar where bar.String='a string' or bar.String='a string'").Enumerable()
  508. ));
  509. enumerable = s.CreateQuery(
  510. "select foo.Component.Name, elements(foo.Component.ImportantDates) from foo in class Foo where foo.TheFoo.id=?").
  511. SetString(0, foo.TheFoo.Key).Enumerable();
  512. int i = 0;
  513. foreach (object[] row in enumerable)
  514. {
  515. i++;
  516. Assert.IsTrue(row[0] is String);
  517. Assert.IsTrue(row[1] == null || row[1] is DateTime);
  518. }
  519. Assert.AreEqual(3, i); //WAS: 4
  520. enumerable = s.CreateQuery("select max(elements(foo.Component.ImportantDates)) from foo in class Foo group by foo.id").
  521. Enumerable();
  522. IEnumerator enumerator = enumerable.GetEnumerator();
  523. Assert.IsTrue(enumerator.MoveNext());
  524. Assert.IsTrue(enumerator.Current is DateTime);
  525. list = s.CreateQuery(
  526. "select foo.TheFoo.TheFoo.TheFoo from foo in class Foo, foo2 in class Foo where"
  527. + " foo = foo2.TheFoo and not not ( not foo.String='fizard' )"
  528. + " and foo2.String between 'a' and (foo.TheFoo.String)"
  529. + (Dialect is SQLiteDialect
  530. ? " and ( foo2.String in ( 'fiz', 'blah') or 1=1 )"
  531. : " and ( foo2.String in ( 'fiz', 'blah', foo.TheFoo.String, foo.String, foo2.String ) )")
  532. ).List();
  533. Assert.AreEqual(1, list.Count, "complex query");
  534. Assert.AreSame(foo, list[0], "returned object");
  535. foo.String = "from BoogieDown -tinsel town =!@#$^&*())";
  536. list = s.CreateQuery("from foo in class Foo where foo.String='from BoogieDown -tinsel town =!@#$^&*())'").List();
  537. Assert.AreEqual(1, list.Count, "single quotes");
  538. list = s.CreateQuery("from foo in class Foo where not foo.String='foo''bar'").List();
  539. Assert.AreEqual(2, list.Count, "single quotes");
  540. list = s.CreateQuery("from foo in class Foo where foo.Component.Glarch.Next is null").List();
  541. Assert.AreEqual(2, list.Count, "query association in component");
  542. Bar bar = new Bar();
  543. Baz baz = new Baz();
  544. baz.SetDefaults();
  545. bar.Baz = baz;
  546. baz.ManyToAny = new List<object>();
  547. baz.ManyToAny.Add(bar);
  548. baz.ManyToAny.Add(foo);
  549. s.Save(bar);
  550. s.Save(baz);
  551. list =
  552. 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'").
  553. List();
  554. Assert.AreEqual(1, list.Count, "query many-to-one");
  555. list = s.CreateQuery(" from i in class Bar where i.Baz.Name='Bazza'").List();
  556. Assert.AreEqual(1, list.Count, "query many-to-one");
  557. if (TestDialect.SupportsCountDistinct)
  558. {
  559. enumerable = s.CreateQuery("select count(distinct foo.TheFoo) from foo in class Foo").Enumerable();
  560. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  561. }
  562. enumerable = s.CreateQuery("select count(foo.TheFoo.Boolean) from foo in class Foo").Enumerable();
  563. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 2), "count"); // changed to Int64 (HQLFunction H3.2)
  564. enumerable = s.CreateQuery("select count(*), foo.Int from foo in class Foo group by foo.Int").Enumerable();
  565. enumerator = enumerable.GetEnumerator();
  566. Assert.IsTrue(enumerator.MoveNext());
  567. Assert.AreEqual(3L, (long) ((object[]) enumerator.Current)[0]);
  568. Assert.IsFalse(enumerator.MoveNext());
  569. enumerable = s.CreateQuery("select sum(foo.TheFoo.Int) from foo in class Foo").Enumerable();
  570. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 4), "sum"); // changed to Int64 (HQLFunction H3.2)
  571. enumerable = s.CreateQuery("select count(foo) from foo in class Foo where foo.id=?")
  572. .SetString(0, foo.Key).Enumerable();
  573. Assert.IsTrue(ContainsSingleObject(enumerable, (long) 1), "id query count");
  574. list = s.CreateQuery("from foo in class Foo where foo.Boolean = ?").SetBoolean(0, true).List();
  575. list = s.CreateQuery("select new Foo(fo.X) from Fo fo").List();
  576. list = s.CreateQuery("select new Foo(fo.Integer) from Foo fo").List();
  577. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  578. .SetCacheable(true)
  579. .List();
  580. Assert.IsTrue(list.Count == 3);
  581. list = s.CreateQuery("select new Foo(fo.X) from Foo fo")
  582. .SetCacheable(true)
  583. .List();
  584. Assert.IsTrue(list.Count == 3);
  585. enumerable = s.CreateQuery("select new Foo(fo.X) from Foo fo").Enumerable();
  586. enumerator = enumerable.GetEnumerator();
  587. Assert.IsTrue(enumerator.MoveNext(), "projection iterate (results)");
  588. Assert.IsTrue(typeof(Foo).IsAssignableFrom(enumerator.Current.GetType()),
  589. "projection iterate (return check)");
  590. // TODO: ScrollableResults not implemented
  591. //ScrollableResults sr = s.CreateQuery("select new Foo(fo.x) from Foo fo").Scroll();
  592. //Assert.IsTrue( "projection scroll (results)", sr.next() );
  593. //Assert.IsTrue( "projection scroll (return check)", typeof(Foo).isAssignableFrom( sr.get(0).getClass() ) );
  594. list = s.CreateQuery("select foo.Long, foo.Component.Name, foo, foo.TheFoo from foo in class Foo").List();
  595. Assert.IsTrue(list.Count > 0);
  596. foreach (object[] row in list)
  597. {
  598. Assert.IsTrue(row[0] is long);
  599. Assert.IsTrue(row[1] is string);
  600. Assert.IsTrue(row[2] is Foo);
  601. Assert.IsTrue(row[3] is Foo);
  602. }
  603. if (TestDialect.SupportsCountDistinct)
  604. {
  605. list =
  606. s.CreateQuery("select avg(foo.Float), max(foo.Component.Name), count(distinct foo.id) from foo in class Foo").List();
  607. Assert.IsTrue(list.Count > 0);
  608. foreach (object[] row in list)
  609. {
  610. Assert.IsTrue(row[0] is double); // changed from float to double (HQLFunction H3.2)
  611. Assert.IsTrue(row[1] is string);
  612. Assert.IsTrue(row[2] is long); // changed from int to long (HQLFunction H3.2)
  613. }
  614. }
  615. list = s.CreateQuery("select foo.Long, foo.Component, foo, foo.TheFoo from foo in class Foo").List();
  616. Assert.IsTrue(list.Count > 0);
  617. foreach (object[] row in list)
  618. {
  619. Assert.IsTrue(row[0] is long);
  620. Assert.IsTrue(row[1] is FooComponent);
  621. Assert.IsTrue(row[2] is Foo);
  622. Assert.IsTrue(row[3] is Foo);
  623. }
  624. s.Save(new Holder("ice T"));
  625. s.Save(new Holder("ice cube"));
  626. Assert.AreEqual(15, s.CreateQuery("from o in class System.Object").List().Count);
  627. Assert.AreEqual(7, s.CreateQuery("from n in class INamed").List().Count);
  628. Assert.IsTrue(s.CreateQuery("from n in class INamed where n.Name is not null").List().Count == 4);
  629. foreach (INamed named in s.CreateQuery("from n in class INamed").Enumerable())
  630. {
  631. Assert.IsNotNull(named);
  632. }
  633. s.Save(new Holder("bar"));
  634. enumerable = s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").Enumerable();
  635. int cnt = 0;
  636. foreach (object[] row in enumerable)
  637. {
  638. if (row[0] != row[1])
  639. {
  640. cnt++;
  641. }
  642. }
  643. //if ( !(dialect is Dialect.HSQLDialect) )
  644. //{
  645. Assert.IsTrue(cnt == 2);
  646. Assert.IsTrue(s.CreateQuery("from n0 in class INamed, n1 in class INamed where n0.Name = n1.Name").List().Count == 7);
  647. //}
  648. IQuery qu = s.CreateQuery("from n in class INamed where n.Name = :name");
  649. object temp = qu.ReturnTypes;
  650. temp = qu.NamedParameters;
  651. int c = 0;
  652. foreach (object obj in s.CreateQuery("from o in class System.Object").Enumerable())
  653. {
  654. c++;
  655. }
  656. Assert.IsTrue(c == 16);
  657. s.CreateQuery("select baz.Code, min(baz.Count) from baz in class Baz group by baz.Code").Enumerable();
  658. Assert.IsTrue(
  659. IsEmpty(
  660. s.CreateQuery(
  661. "selecT baz from baz in class Baz where baz.StringDateMap['foo'] is not null or baz.StringDateMap['bar'] = ?")
  662. .SetDateTime(0, DateTime.Today).Enumerable()));
  663. list = s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap['now'] is not null").List();
  664. Assert.AreEqual(1, list.Count);
  665. list =
  666. s.CreateQuery("select baz from baz in class Baz where baz.StringDateMap[:now] is not null").SetString("now", "now").
  667. List();
  668. Assert.AreEqual(1, list.Count);
  669. list =
  670. s.CreateQuery(
  671. "select baz from baz in class Baz where baz.StringDateMap['now'] is not null and baz.StringDateMap['big bang'] < baz.StringDateMap['now']")
  672. .List();
  673. Assert.AreEqual(1, list.Count);
  674. list = s.CreateQuery("select index(date) from Baz baz join baz.StringDateMap date").List();
  675. Console.WriteLine(list);
  676. Assert.AreEqual(3, list.Count);
  677. s.CreateQuery(
  678. "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")
  679. .List();
  680. s.CreateQuery("from Baz baz inner join baz.CollectionComponent.Nested.Foos foo where foo.String is null").List();
  681. if (Dialect.SupportsSubSelects)
  682. {
  683. s.CreateQuery("from Baz baz inner join baz.FooSet where '1' in (from baz.FooSet foo where foo.String is not null)").
  684. List();
  685. s.CreateQuery(
  686. "from Baz baz where 'a' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  687. .List();
  688. s.CreateQuery(
  689. "from Baz baz where 'b' in elements(baz.CollectionComponent.Nested.Foos) and 1.0 in elements(baz.CollectionComponent.Nested.Floats)")
  690. .List();
  691. }
  692. s.CreateQuery("from Foo foo join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  693. //if ( !(dialect is Dialect.HSQLDialect) )
  694. s.CreateQuery("from Foo foo left join foo.TheFoo where foo.TheFoo in ('1','2','3')").List();
  695. s.CreateQuery("select foo.TheFoo from Foo foo where foo.TheFoo in ('1','2','3')").List();
  696. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo in ('1','2','3')").List();
  697. s.CreateQuery("select foo.TheFoo.String from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  698. s.CreateQuery("select foo.TheFoo.Long from Foo foo where foo.TheFoo.String in ('1','2','3')").List();
  699. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') or foo.TheFoo.Long in (1,2,3)").
  700. List();
  701. s.CreateQuery("select count(*) from Foo foo where foo.TheFoo.String in ('1','2','3') group by foo.TheFoo.Long").List();
  702. s.CreateQuery("from Foo foo1 left join foo1.TheFoo foo2 left join foo2.TheFoo where foo1.String is not null").List();
  703. s.CreateQuery("from Foo foo1 left join foo1.TheFoo.TheFoo where foo1.String is not null").List();
  704. s.CreateQuery(
  705. "from Foo foo1 left join foo1.TheFoo foo2 left join foo1.TheFoo.TheFoo foo3 where foo1.String is not null").List();
  706. s.CreateQuery("select foo.Formula from Foo foo where foo.Formula > 0").List();
  707. int len = s.CreateQuery("from Foo as foo join foo.TheFoo as foo2 where foo2.id >'a' or foo2.id <'a'").List().Count;
  708. Assert.IsTrue(len == 2);
  709. s.Delete("from Holder");
  710. txn.Commit();
  711. s.Close();
  712. s = OpenSession();
  713. txn = s.BeginTransaction();
  714. baz = (Baz) s.CreateQuery("from Baz baz left outer join fetch baz.ManyToAny").UniqueResult();
  715. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.ManyToAny));
  716. Assert.IsTrue(baz.ManyToAny.Count == 2);
  717. BarProxy barp = (BarProxy) baz.ManyToAny[0];
  718. s.CreateQuery("from Baz baz join baz.ManyToAny").List();
  719. Assert.IsTrue(s.CreateQuery("select baz from Baz baz join baz.ManyToAny a where index(a) = 0").List().Count == 1);
  720. FooProxy foop = (FooProxy) s.Get(typeof(Foo), foo.Key);
  721. Assert.IsTrue(foop == baz.ManyToAny[1]);
  722. barp.Baz = baz;
  723. Assert.IsTrue(s.CreateQuery("select bar from Bar bar where bar.Baz.StringDateMap['now'] is not null").List().Count ==
  724. 1);
  725. Assert.IsTrue(
  726. s.CreateQuery(
  727. "select bar from Bar bar join bar.Baz b where b.StringDateMap['big bang'] < b.StringDateMap['now'] and b.StringDateMap['now'] is not null")
  728. .List().Count == 1);
  729. Assert.IsTrue(
  730. s.CreateQuery(
  731. "select bar from Bar bar where bar.Baz.StringDateMap['big bang'] < bar.Baz.StringDateMap['now'] and bar.Baz.StringDateMap['now'] is not null")
  732. .List().Count == 1);
  733. list = s.CreateQuery("select foo.String, foo.Component, foo.id from Bar foo").List();
  734. Assert.IsTrue(((FooComponent) ((object[]) list[0])[1]).Name == "foo");
  735. list = s.CreateQuery("select elements(baz.Components) from Baz baz").List();
  736. Assert.IsTrue(list.Count == 2);
  737. list = s.CreateQuery("select bc.Name from Baz baz join baz.Components bc").List();
  738. Assert.IsTrue(list.Count == 2);
  739. //list = s.CreateQuery("select bc from Baz baz join baz.components bc").List();
  740. s.CreateQuery("from Foo foo where foo.Integer < 10 order by foo.String").SetMaxResults(12).List();
  741. s.Delete(barp);
  742. s.Delete(baz);
  743. s.Delete(foop.TheFoo);
  744. s.Delete(foop);
  745. txn.Commit();
  746. s.Close();
  747. }
  748. [Test]
  749. public void CascadeDeleteDetached()
  750. {
  751. Baz baz;
  752. using (ISession s = OpenSession())
  753. {
  754. baz = new Baz();
  755. IList<Fee> list = new List<Fee>();
  756. list.Add(new Fee());
  757. baz.Fees = list;
  758. s.Save(baz);
  759. s.Flush();
  760. }
  761. using (ISession s = OpenSession())
  762. {
  763. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  764. }
  765. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.Fees));
  766. using (ISession s = OpenSession())
  767. {
  768. s.Delete(baz);
  769. s.Flush();
  770. Assert.IsFalse(s.CreateQuery("from Fee").Enumerable().GetEnumerator().MoveNext());
  771. }
  772. using (ISession s = OpenSession())
  773. {
  774. baz = new Baz();
  775. IList<Fee> list = new List<Fee>();
  776. list.Add(new Fee());
  777. list.Add(new Fee());
  778. baz.Fees = list;
  779. s.Save(baz);
  780. s.Flush();
  781. }
  782. using (ISession s = OpenSession())
  783. {
  784. baz = (Baz) s.Get(typeof(Baz), baz.Code);
  785. NHibernateUtil.Initialize(baz.Fees);
  786. }
  787. Assert.AreEqual(2, baz.Fees.Count);
  788. using (ISession s = OpenSession())
  789. {
  790. s.Delete(baz);
  791. s.Flush();
  792. Assert.IsTrue(IsEmpty(s.CreateQuery("from Fee").Enumerable()));
  793. }
  794. }
  795. [Test]
  796. public void ForeignKeys()
  797. {
  798. Baz baz;
  799. using (ISession s = OpenSession())
  800. {
  801. baz = new Baz();
  802. Foo foo = new Foo();
  803. IList<Foo> bag = new List<Foo>();
  804. bag.Add(foo);
  805. baz.IdFooBag = bag;
  806. baz.Foo = foo;
  807. s.Save(baz);
  808. s.Flush();
  809. }
  810. using (ISession s = OpenSession())
  811. {
  812. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  813. s.Delete(baz);
  814. s.Flush();
  815. }
  816. }
  817. [Test]
  818. public void NonlazyCollections()
  819. {
  820. object glarchId;
  821. using (ISession s = OpenSession())
  822. {
  823. Glarch glarch1 = new Glarch();
  824. glarch1.ProxySet = new LinkedHashSet<GlarchProxy>();
  825. Glarch glarch2 = new Glarch();
  826. glarch1.ProxySet.Add(glarch1);
  827. s.Save(glarch2);
  828. glarchId = s.Save(glarch1);
  829. s.Flush();
  830. }
  831. Glarch loadedGlarch;
  832. using (ISession s = OpenSession())
  833. {
  834. loadedGlarch = (Glarch) s.Get(typeof(Glarch), glarchId);
  835. Assert.IsTrue(NHibernateUtil.IsInitialized(loadedGlarch.ProxySet));
  836. }
  837. // ProxySet is a non-lazy collection, so this should work outside
  838. // a session.
  839. Assert.AreEqual(1, loadedGlarch.ProxySet.Count);
  840. using (ISession s = OpenSession())
  841. {
  842. s.Delete("from Glarch");
  843. s.Flush();
  844. }
  845. }
  846. [Test]
  847. public void ReuseDeletedCollection()
  848. {
  849. Baz baz, baz2;
  850. using (ISession s = OpenSession())
  851. {
  852. baz = new Baz();
  853. baz.SetDefaults();
  854. s.Save(baz);
  855. s.Flush();
  856. s.Delete(baz);
  857. baz2 = new Baz();
  858. baz2.StringArray = new string[] {"x-y-z"};
  859. s.Save(baz2);
  860. s.Flush();
  861. }
  862. baz2.StringSet = baz.StringSet;
  863. baz2.StringArray = baz.StringArray;
  864. baz2.FooArray = baz.FooArray;
  865. using (ISession s = OpenSession())
  866. {
  867. s.Update(baz2);
  868. s.Flush();
  869. }
  870. using (ISession s = OpenSession())
  871. {
  872. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  873. Assert.AreEqual(3, baz2.StringArray.Length);
  874. Assert.AreEqual(3, baz2.StringSet.Count);
  875. s.Delete(baz2);
  876. s.Flush();
  877. }
  878. }
  879. [Test]
  880. public void PropertyRef()
  881. {
  882. object qid;
  883. object hid;
  884. using (ISession s = OpenSession())
  885. {
  886. Holder h = new Holder();
  887. h.Name = "foo";
  888. Holder h2 = new Holder();
  889. h2.Name = "bar";
  890. h.OtherHolder = h2;
  891. hid = s.Save(h);
  892. Qux q = new Qux();
  893. q.Holder = h2;
  894. qid = s.Save(q);
  895. s.Flush();
  896. }
  897. using (ISession s = OpenSession())
  898. {
  899. Holder h = (Holder) s.Load(typeof(Holder), hid);
  900. Assert.AreEqual(h.Name, "foo");
  901. Assert.AreEqual(h.OtherHolder.Name, "bar");
  902. object[] res =
  903. (object[]) s.CreateQuery("from Holder h join h.OtherHolder oh where h.OtherHolder.Name = 'bar'").List()[0];
  904. Assert.AreSame(h, res[0]);
  905. Qux q = (Qux) s.Get(typeof(Qux), qid);
  906. Assert.AreSame(q.Holder, h.OtherHolder);
  907. s.Delete(h);
  908. s.Delete(q);
  909. s.Flush();
  910. }
  911. }
  912. [Test]
  913. public void QueryCollectionOfValues()
  914. {
  915. object gid;
  916. using (ISession s = OpenSession())
  917. {
  918. Baz baz = new Baz();
  919. baz.SetDefaults();
  920. s.Save(baz);
  921. Glarch g = new Glarch();
  922. gid = s.Save(g);
  923. if (Dialect.SupportsSubSelects)
  924. {
  925. if (Dialect.SupportsScalarSubSelects)
  926. s.CreateFilter(baz.FooArray, "where size(this.Bytes) > 0").List();
  927. s.CreateFilter(baz.FooArray, "where 0 in elements(this.Bytes)").List();
  928. }
  929. s.Flush();
  930. }
  931. using (ISession s = OpenSession())
  932. {
  933. //s.CreateQuery("from Baz baz where baz.FooSet.String = 'foo'").List();
  934. //s.CreateQuery("from Baz baz where baz.FooArray.String = 'foo'").List();
  935. //s.CreateQuery("from Baz baz where baz.FooSet.foo.String = 'foo'").List();
  936. //s.CreateQuery("from Baz baz join baz.FooSet.Foo foo where foo.String = 'foo'").List();
  937. s.CreateQuery("from Baz baz join baz.FooSet foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  938. s.CreateQuery("from Baz baz join baz.FooArray foo join foo.TheFoo.TheFoo foo2 where foo2.String = 'foo'").List();
  939. s.CreateQuery("from Baz baz join baz.StringDateMap date where index(date) = 'foo'").List();
  940. s.CreateQuery("from Baz baz join baz.TopGlarchez g where index(g) = 'A'").List();
  941. s.CreateQuery("select index(g) from Baz baz join baz.TopGlarchez g").List();
  942. Assert.AreEqual(3, s.CreateQuery("from Baz baz left join baz.StringSet").List().Count);
  943. Baz baz = (Baz) s.CreateQuery("from Baz baz join baz.StringSet str where str='foo'").List()[0];
  944. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  945. baz = (Baz) s.CreateQuery("from Baz baz left join fetch baz.StringSet").List()[0];
  946. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  947. Assert.AreEqual(1, s.CreateQuery("from Baz baz join baz.StringSet string where string='foo'").List().Count);
  948. Assert.AreEqual(1, s.CreateQuery("from Baz baz inner join baz.Components comp where comp.Name='foo'").List().Count);
  949. //IList bss = s.CreateQuery("select baz, ss from Baz baz inner join baz.StringSet ss").List();
  950. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee is not null").List();
  951. s.CreateQuery("from Glarch g inner join g.FooComponents comp join comp.Fee fee where fee.Count > 0").List();
  952. s.CreateQuery("from Glarch g inner join g.FooComponents comp where comp.Fee.Count is not null").List();
  953. s.Delete(baz);
  954. //s.delete("from Glarch g");
  955. s.Delete(s.Get(typeof(Glarch), gid));
  956. s.Flush();
  957. }
  958. }
  959. [Test]
  960. public void BatchLoad()
  961. {
  962. Baz baz, baz2, baz3;
  963. using (ISession s = OpenSession())
  964. {
  965. baz = new Baz();
  966. var stringSet = new SortedSet<string> { "foo", "bar" };
  967. var fooSet = new HashSet<FooProxy>();
  968. for (int i = 0; i < 3; i++)
  969. {
  970. Foo foo = new Foo();
  971. s.Save(foo);
  972. fooSet.Add(foo);
  973. }
  974. baz.FooSet = fooSet;
  975. baz.StringSet = stringSet;
  976. s.Save(baz);
  977. baz2 = new Baz();
  978. fooSet = new HashSet<FooProxy>();
  979. for (int i = 0; i < 2; i++)
  980. {
  981. Foo foo = new Foo();
  982. s.Save(foo);
  983. fooSet.Add(foo);
  984. }
  985. baz2.FooSet = fooSet;
  986. s.Save(baz2);
  987. baz3 = new Baz();
  988. stringSet = new SortedSet<string>();
  989. stringSet.Add("foo");
  990. stringSet.Add("baz");
  991. baz3.StringSet = stringSet;
  992. s.Save(baz3);
  993. s.Flush();
  994. }
  995. using (ISession s = OpenSession())
  996. {
  997. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  998. baz2 = (Baz) s.Load(typeof(Baz), baz2.Code);
  999. baz3 = (Baz) s.Load(typeof(Baz), baz3.Code);
  1000. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooSet));
  1001. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.FooSet));
  1002. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.FooSet));
  1003. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.StringSet));
  1004. Assert.IsFalse(NHibernateUtil.IsInitialized(baz2.StringSet));
  1005. Assert.IsFalse(NHibernateUtil.IsInitialized(baz3.StringSet));
  1006. Assert.AreEqual(3, baz.FooSet.Count);
  1007. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooSet));
  1008. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.FooSet));
  1009. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.FooSet));
  1010. Assert.AreEqual(2, baz2.FooSet.Count);
  1011. Assert.IsTrue(baz3.StringSet.Contains("baz"));
  1012. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.StringSet));
  1013. Assert.IsTrue(NHibernateUtil.IsInitialized(baz2.StringSet));
  1014. Assert.IsTrue(NHibernateUtil.IsInitialized(baz3.StringSet));
  1015. Assert.AreEqual(2, baz.StringSet.Count);
  1016. Assert.AreEqual(0, baz2.StringSet.Count);
  1017. s.Delete(baz);
  1018. s.Delete(baz2);
  1019. s.Delete(baz3);
  1020. IEnumerable en = new JoinedEnumerable(
  1021. new IEnumerable[] {baz.FooSet, baz2.FooSet});
  1022. foreach (object obj in en)
  1023. {
  1024. s.Delete(obj);
  1025. }
  1026. s.Flush();
  1027. }
  1028. }
  1029. [Test]
  1030. public void FetchInitializedCollection()
  1031. {
  1032. ISession s = OpenSession();
  1033. Baz baz = new Baz();
  1034. IList<Foo> fooBag = new List<Foo>();
  1035. fooBag.Add(new Foo());
  1036. fooBag.Add(new Foo());
  1037. baz.FooBag = fooBag;
  1038. s.Save(baz);
  1039. s.Flush();
  1040. fooBag = baz.FooBag;
  1041. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1042. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1043. s.Close();
  1044. s = OpenSession();
  1045. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1046. Object bag = baz.FooBag;
  1047. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1048. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1049. Assert.IsTrue(bag == baz.FooBag);
  1050. Assert.IsTrue(baz.FooBag.Count == 2);
  1051. s.Delete(baz);
  1052. s.Flush();
  1053. s.Close();
  1054. }
  1055. [Test]
  1056. public void LateCollectionAdd()
  1057. {
  1058. object id;
  1059. using (ISession s = OpenSession())
  1060. {
  1061. Baz baz = new Baz();
  1062. IList<string> l = new List<string>();
  1063. baz.StringList = l;
  1064. l.Add("foo");
  1065. id = s.Save(baz);
  1066. l.Add("bar");
  1067. s.Flush();
  1068. l.Add("baz");
  1069. s.Flush();
  1070. }
  1071. using (ISession s = OpenSession())
  1072. {
  1073. Baz baz = (Baz) s.Load(typeof(Baz), id);
  1074. Assert.AreEqual(3, baz.StringList.Count);
  1075. Assert.IsTrue(baz.StringList.Contains("foo"));
  1076. Assert.IsTrue(baz.StringList.Contains("bar"));
  1077. Assert.IsTrue(baz.StringList.Contains("baz"));
  1078. s.Delete(baz);
  1079. s.Flush();
  1080. }
  1081. }
  1082. [Test]
  1083. public void Update()
  1084. {
  1085. ISession s = OpenSession();
  1086. Foo foo = new Foo();
  1087. s.Save(foo);
  1088. s.Flush();
  1089. s.Close();
  1090. s = OpenSession();
  1091. FooProxy foo2 = (FooProxy) s.Load(typeof(Foo), foo.Key);
  1092. foo2.String = "dirty";
  1093. foo2.Boolean = false;
  1094. foo2.Bytes = new byte[] {1, 2, 3};
  1095. foo2.Date = DateTime.Today;
  1096. foo2.Short = 69;
  1097. s.Flush();
  1098. s.Close();
  1099. s = OpenSession();
  1100. Foo foo3 = new Foo();
  1101. s.Load(foo3, foo.Key);
  1102. Assert.IsTrue(foo2.EqualsFoo(foo3), "update");
  1103. s.Delete(foo3);
  1104. s.Flush();
  1105. s.Close();
  1106. }
  1107. [Test]
  1108. public void ListRemove()
  1109. {
  1110. using (ISession s = OpenSession())
  1111. {
  1112. Baz b = new Baz();
  1113. IList<string> stringList = new List<string>();
  1114. IList<Fee> feeList = new List<Fee>();
  1115. b.Fees = feeList;
  1116. b.StringList = stringList;
  1117. feeList.Add(new Fee());
  1118. feeList.Add(new Fee());
  1119. feeList.Add(new Fee());
  1120. feeList.Add(new Fee());
  1121. stringList.Add("foo");
  1122. stringList.Add("bar");
  1123. stringList.Add("baz");
  1124. stringList.Add("glarch");
  1125. s.Save(b);
  1126. s.Flush();
  1127. stringList.RemoveAt(1);
  1128. feeList.RemoveAt(1);
  1129. s.Flush();
  1130. s.Evict(b);
  1131. s.Refresh(b);
  1132. Assert.AreEqual(3, b.Fees.Count);
  1133. stringList = b.StringList;
  1134. Assert.AreEqual(3, stringList.Count);
  1135. Assert.AreEqual("baz", stringList[1]);
  1136. Assert.AreEqual("foo", stringList[0]);
  1137. s.Delete(b);
  1138. s.Delete("from Fee");
  1139. s.Flush();
  1140. }
  1141. }
  1142. [Test]
  1143. public void FetchInitializedCollectionDupe()
  1144. {
  1145. string bazCode;
  1146. using (ISession s = OpenSession())
  1147. {
  1148. Baz baz = new Baz();
  1149. IList<Foo> fooBag = new List<Foo>();
  1150. fooBag.Add(new Foo());
  1151. fooBag.Add(new Foo());
  1152. baz.FooBag = fooBag;
  1153. s.Save(baz);
  1154. s.Flush();
  1155. fooBag = baz.FooBag;
  1156. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1157. Assert.IsTrue(NHibernateUtil.IsInitialized(fooBag));
  1158. Assert.AreSame(fooBag, baz.FooBag);
  1159. Assert.AreEqual(2, baz.FooBag.Count);
  1160. bazCode = baz.Code;
  1161. }
  1162. using (ISession s = OpenSession())
  1163. {
  1164. Baz baz = (Baz) s.Load(typeof(Baz), bazCode);
  1165. object bag = baz.FooBag;
  1166. Assert.IsFalse(NHibernateUtil.IsInitialized(bag));
  1167. s.CreateQuery("from Baz baz left join fetch baz.FooBag").List();
  1168. Assert.IsTrue(NHibernateUtil.IsInitialized(bag));
  1169. Assert.AreSame(bag, baz.FooBag);
  1170. Assert.AreEqual(2, baz.FooBag.Count);
  1171. s.Delete(baz);
  1172. s.Flush();
  1173. }
  1174. }
  1175. [Test]
  1176. public void Sortables()
  1177. {
  1178. ISession s = OpenSession();
  1179. ITransaction t = s.BeginTransaction();
  1180. Baz b = new Baz();
  1181. var ss = new HashSet<Sortable>
  1182. {
  1183. new Sortable("foo"), new Sortable("bar"), new Sortable("baz")
  1184. };
  1185. b.Sortablez = ss;
  1186. s.Save(b);
  1187. s.Flush();
  1188. t.Commit();
  1189. s.Close();
  1190. s = OpenSession();
  1191. t = s.BeginTransaction();
  1192. IList result = s.CreateCriteria(typeof(Baz))
  1193. .AddOrder(Order.Asc("Name"))
  1194. .List();
  1195. b = (Baz) result[0];
  1196. Assert.IsTrue(b.Sortablez.Count == 3);
  1197. // compare the first item in the "Set" sortablez - can't reference
  1198. // the first item using b.sortablez[0] because it thinks 0 is the
  1199. // DictionaryEntry key - not the index.
  1200. foreach (Sortable sortable in b.Sortablez)
  1201. {
  1202. Assert.AreEqual(sortable.name, "bar");
  1203. break;
  1204. }
  1205. s.Flush();
  1206. t.Commit();
  1207. s.Close();
  1208. s = OpenSession();
  1209. t = s.BeginTransaction();
  1210. result = s.CreateQuery("from Baz baz left join fetch baz.Sortablez order by baz.Name asc")
  1211. .List();
  1212. b = (Baz) result[0];
  1213. Assert.IsTrue(b.Sortablez.Count == 3);
  1214. foreach (Sortable sortable in b.Sortablez)
  1215. {
  1216. Assert.AreEqual(sortable.name, "bar");
  1217. break;
  1218. }
  1219. s.Flush();
  1220. t.Commit();
  1221. s.Close();
  1222. s = OpenSession();
  1223. t = s.BeginTransaction();
  1224. result = s.CreateQuery("from Baz baz order by baz.Name asc")
  1225. .List();
  1226. b = (Baz) result[0];
  1227. Assert.IsTrue(b.Sortablez.Count == 3);
  1228. foreach (Sortable sortable in b.Sortablez)
  1229. {
  1230. Assert.AreEqual(sortable.name, "bar");
  1231. break;
  1232. }
  1233. s.Delete(b);
  1234. s.Flush();
  1235. t.Commit();
  1236. s.Close();
  1237. }
  1238. [Test]
  1239. public void FetchList()
  1240. {
  1241. ISession s = OpenSession();
  1242. Baz baz = new Baz();
  1243. s.Save(baz);
  1244. Foo foo = new Foo();
  1245. s.Save(foo);
  1246. Foo foo2 = new Foo();
  1247. s.Save(foo2);
  1248. s.Flush();
  1249. IList<Fee> list = new List<Fee>();
  1250. for (int i = 0; i < 5; i++)
  1251. {
  1252. Fee fee = new Fee();
  1253. list.Add(fee);
  1254. }
  1255. baz.Fees = list;
  1256. var result = s.CreateQuery("from Foo foo, Baz baz left join fetch baz.Fees").List();
  1257. Assert.IsTrue(NHibernateUtil.IsInitialized(((Baz)((object[])result[0])[1]).Fees));
  1258. s.Delete(foo);
  1259. s.Delete(foo2);
  1260. s.Delete(baz);
  1261. s.Flush();
  1262. s.Close();
  1263. }
  1264. [Test]
  1265. public void BagOneToMany()
  1266. {
  1267. ISession s = OpenSession();
  1268. Baz baz = new Baz();
  1269. IList<Baz> list = new List<Baz>();
  1270. baz.Bazez = list;
  1271. list.Add(new Baz());
  1272. s.Save(baz);
  1273. s.Flush();
  1274. list.Add(new Baz());
  1275. s.Flush();
  1276. list.Insert(0, new Baz());
  1277. s.Flush();
  1278. object toDelete = list[1];
  1279. list.RemoveAt(1);
  1280. s.Delete(toDelete);
  1281. s.Flush();
  1282. s.Delete(baz);
  1283. s.Flush();
  1284. s.Close();
  1285. }
  1286. [Test]
  1287. public void QueryLockMode()
  1288. {
  1289. ISession s = OpenSession();
  1290. ITransaction tx = s.BeginTransaction();
  1291. Bar bar = new Bar();
  1292. Assert.IsNull(bar.Bytes);
  1293. s.Save(bar);
  1294. Assert.IsNotNull(bar.Bytes);
  1295. s.Flush();
  1296. Assert.IsNotNull(bar.Bytes);
  1297. bar.String = "changed";
  1298. Baz baz = new Baz();
  1299. baz.Foo = bar;
  1300. s.Save(baz);
  1301. Assert.IsNotNull(bar.Bytes);
  1302. IQuery q = s.CreateQuery("from Foo foo, Bar bar");
  1303. q.SetLockMode("bar", LockMode.Upgrade);
  1304. object[] result = (object[]) q.List()[0];
  1305. Assert.IsNotNull(bar.Bytes);
  1306. object b = result[0];
  1307. Assert.IsTrue(s.GetCurrentLockMode(b) == LockMode.Write && s.GetCurrentLockMode(result[1]) == LockMode.Write);
  1308. tx.Commit();
  1309. Assert.IsNotNull(bar.Bytes);
  1310. s.Disconnect();
  1311. s.Reconnect();
  1312. tx = s.BeginTransaction();
  1313. Assert.IsNotNull(bar.Bytes);
  1314. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1315. Assert.IsNotNull(bar.Bytes);
  1316. s.CreateQuery("from Foo foo").List();
  1317. Assert.IsNotNull(bar.Bytes);
  1318. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1319. q = s.CreateQuery("from Foo foo");
  1320. q.SetLockMode("foo", LockMode.Read);
  1321. q.List();
  1322. Assert.AreEqual(LockMode.Read, s.GetCurrentLockMode(b));
  1323. s.Evict(baz);
  1324. tx.Commit();
  1325. s.Disconnect();
  1326. s.Reconnect();
  1327. tx = s.BeginTransaction();
  1328. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1329. s.Delete(s.Load(typeof(Baz), baz.Code));
  1330. Assert.AreEqual(LockMode.None, s.GetCurrentLockMode(b));
  1331. tx.Commit();
  1332. s.Close();
  1333. s = OpenSession();
  1334. tx = s.BeginTransaction();
  1335. q = s.CreateQuery("from Foo foo, Bar bar, Bar bar2");
  1336. q.SetLockMode("bar", LockMode.Upgrade);
  1337. q.SetLockMode("bar2", LockMode.Read);
  1338. result = (object[]) q.List()[0];
  1339. Assert.IsTrue(s.GetCurrentLockMode(result[0]) == LockMode.Upgrade &&
  1340. s.GetCurrentLockMode(result[1]) == LockMode.Upgrade);
  1341. s.Delete(result[0]);
  1342. tx.Commit();
  1343. s.Close();
  1344. }
  1345. [Test]
  1346. public void ManyToManyBag()
  1347. {
  1348. ISession s = OpenSession();
  1349. Baz baz = new Baz();
  1350. object id = s.Save(baz);
  1351. s.Flush();
  1352. s.Close();
  1353. s = OpenSession();
  1354. baz = (Baz) s.Load(typeof(Baz), id);
  1355. baz.FooBag.Add(new Foo());
  1356. s.Flush();
  1357. s.Close();
  1358. s = OpenSession();
  1359. baz = (Baz) s.Load(typeof(Baz), id);
  1360. Assert.IsFalse(NHibernateUtil.IsInitialized(baz.FooBag));
  1361. Assert.AreEqual(1, baz.FooBag.Count);
  1362. Assert.IsTrue(NHibernateUtil.IsInitialized(baz.FooBag[0]));
  1363. s.Delete(baz);
  1364. s.Flush();
  1365. s.Close();
  1366. }
  1367. [Test]
  1368. public void IdBag()
  1369. {
  1370. ISession s = OpenSession();
  1371. Baz baz = new Baz();
  1372. s.Save(baz);
  1373. IList<Foo> l = new List<Foo>();
  1374. IList<byte[]> l2 = new List<byte[]>();
  1375. baz.IdFooBag = l;
  1376. baz.ByteBag = l2;
  1377. l.Add(new Foo());
  1378. l.Add(new Bar());
  1379. byte[] bytes = GetBytes("ffo");
  1380. l2.Add(bytes);
  1381. l2.Add(GetBytes("foo"));
  1382. s.Flush();
  1383. l.Add(new Foo());
  1384. l.Add(new Bar());
  1385. l2.Add(GetBytes("bar"));
  1386. s.Flush();
  1387. object removedObject = l[3];
  1388. l.RemoveAt(3);
  1389. s.Delete(removedObject);
  1390. bytes[1] = Convert.ToByte('o');
  1391. s.Flush();
  1392. s.Close();
  1393. s = OpenSession();
  1394. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1395. Assert.AreEqual(3, baz.IdFooBag.Count);
  1396. Assert.AreEqual(3, baz.ByteBag.Count);
  1397. bytes = GetBytes("foobar");
  1398. foreach (object obj in baz.IdFooBag)
  1399. {
  1400. s.Delete(obj);
  1401. }
  1402. baz.IdFooBag = null;
  1403. baz.ByteBag.Add(bytes);
  1404. baz.ByteBag.Add(bytes);
  1405. s.Flush();
  1406. s.Close();
  1407. s = OpenSession();
  1408. baz = (Baz) s.Load(typeof(Baz), baz.Code);
  1409. Assert.AreEqual(0, baz.IdFooBag.Count);
  1410. Assert.AreEqual(5, baz.ByteBag.Count);
  1411. s.Delete(baz);
  1412. s.Flush();
  1413. s.Close();
  1414. }
  1415. [Test]
  1416. public void ForceOuterJoin()
  1417. {
  1418. if (Sfi.Settings.IsOuterJoinFetchEnabled == false)
  1419. {
  1420. // don't bother to run the test if we can't test it
  1421. return;
  1422. }
  1423. ISession s = OpenSession();
  1424. Glarch g = new Glarch();
  1425. FooComponent fc = new FooComponent();
  1426. fc.Glarch = g;
  1427. FooProxy f = new Foo();
  1428. FooProxy f2 = new Foo();
  1429. f.Component = fc;
  1430. f.TheFoo = f2;
  1431. s.Save(f2);
  1432. object id = s.Save(f);
  1433. object gid = s.GetIdentifier(f.Component.Glarch);
  1434. s.Flush();
  1435. s.Close();
  1436. s = OpenSession();
  1437. f = (FooProxy) s.Load(typeof(Foo), id);
  1438. Assert.IsFalse(NHibernateUtil.IsInitialized(f));
  1439. Assert.IsTrue(NHibernateUtil.IsInitialized(f.Component.Glarch)); //outer-join="true"
  1440. Assert.IsFalse(NHibernateUtil.IsInitialized(f.TheFoo)); //outer-join="auto"
  1441. Assert.AreEqual(gid, s.GetIdentifier(f.Component.Glarch));
  1442. s.Delete(f);
  1443. s.Delete(f.TheFoo);
  1444. s.Delete(f.Component.Glarch);
  1445. s.Flush();
  1446. s.Close();
  1447. }
  1448. [Test]
  1449. public void EmptyCollection()
  1450. {
  1451. ISession s = OpenSession();
  1452. object id = s.Save(new Baz());
  1453. s.Flush();
  1454. s.Close();
  1455. s = OpenSession();
  1456. Baz baz = (Baz) s.Load(typeof(Baz), id);
  1457. Assert.IsTrue(baz.FooSet.Count == 0);
  1458. Foo foo = new Foo();
  1459. baz.FooSet.Add(foo);
  1460. s.Save(foo);
  1461. s.Flush();
  1462. s.Delete(foo);
  1463. s.Delete(baz);
  1464. s.Flush();
  1465. s.Close();
  1466. }
  1467. [Test]
  1468. public void OneToOneGenerator()
  1469. {
  1470. ISession s = OpenSession();
  1471. X x = new X();
  1472. Y y = new Y();
  1473. x.Y = y;
  1474. y.TheX = x;
  1475. object yId = s.Save(y);
  1476. object xId = s.Save(x);
  1477. Assert.AreEqual(yId, xId);
  1478. s.Flush();
  1479. A

Large files files are truncated, but you can click here to view the full file