PageRenderTime 21ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/nhibernate/src/NHibernate.Test/Legacy/FumTest.cs

https://bitbucket.org/fabiomaulo/nhibernate/
C# | 738 lines | 610 code | 89 blank | 39 comment | 10 complexity | 372c5485e3c54046f050851fde109a7a MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using Iesi.Collections;
  6. using NHibernate.DomainModel;
  7. using NHibernate.Criterion;
  8. using NHibernate.Type;
  9. using NUnit.Framework;
  10. namespace NHibernate.Test.Legacy
  11. {
  12. /// <summary>
  13. /// FumTest handles testing Composite Ids.
  14. /// </summary>
  15. [TestFixture]
  16. public class FumTest : TestCase
  17. {
  18. protected static short fumKeyShort = 1;
  19. protected override IList Mappings
  20. {
  21. get
  22. {
  23. return new string[]
  24. {
  25. "FooBar.hbm.xml",
  26. "Baz.hbm.xml",
  27. "Qux.hbm.xml",
  28. "Glarch.hbm.xml",
  29. "Fum.hbm.xml",
  30. "Fumm.hbm.xml",
  31. "Fo.hbm.xml",
  32. "One.hbm.xml",
  33. "Many.hbm.xml",
  34. "Immutable.hbm.xml",
  35. "Fee.hbm.xml",
  36. "Vetoer.hbm.xml",
  37. "Holder.hbm.xml",
  38. "Location.hbm.xml",
  39. "Stuff.hbm.xml",
  40. "Container.hbm.xml",
  41. "Simple.hbm.xml",
  42. "Middle.hbm.xml"
  43. };
  44. }
  45. }
  46. [Test]
  47. public void CriteriaCollection()
  48. {
  49. //if( dialect is Dialect.HSQLDialect ) return;
  50. using (ISession s = OpenSession())
  51. {
  52. Fum fum = new Fum(FumKey("fum"));
  53. fum.FumString = "a value";
  54. fum.MapComponent.Fummap["self"] = fum;
  55. fum.MapComponent.Stringmap["string"] = "a staring";
  56. fum.MapComponent.Stringmap["string2"] = "a notha staring";
  57. fum.MapComponent.Count = 1;
  58. s.Save(fum);
  59. s.Flush();
  60. }
  61. using (ISession s = OpenSession())
  62. {
  63. Fum b = (Fum) s.CreateCriteria(typeof(Fum))
  64. .Add(Expression.In(
  65. "FumString", new string[] {"a value", "no value"}))
  66. .UniqueResult();
  67. //Assert.IsTrue( NHibernateUtil.IsInitialized( b.MapComponent.Fummap ) );
  68. Assert.IsTrue(NHibernateUtil.IsInitialized(b.MapComponent.Stringmap));
  69. Assert.IsTrue(b.MapComponent.Fummap.Count == 1);
  70. Assert.IsTrue(b.MapComponent.Stringmap.Count == 2);
  71. int none = s.CreateCriteria(typeof(Fum))
  72. .Add(Expression.In("FumString", new string[0]))
  73. .List().Count;
  74. Assert.AreEqual(0, none);
  75. s.Delete(b);
  76. s.Flush();
  77. }
  78. }
  79. [Test]
  80. public void Criteria()
  81. {
  82. using (ISession s = OpenSession())
  83. using (ITransaction txn = s.BeginTransaction())
  84. {
  85. Fum fum = new Fum(FumKey("fum"));
  86. fum.Fo = new Fum(FumKey("fo"));
  87. fum.FumString = "fo fee fi";
  88. fum.Fo.FumString = "stuff";
  89. Fum fr = new Fum(FumKey("fr"));
  90. fr.FumString = "goo";
  91. Fum fr2 = new Fum(FumKey("fr2"));
  92. fr2.FumString = "soo";
  93. fum.Friends = new HashedSet();
  94. fum.Friends.Add(fr);
  95. fum.Friends.Add(fr2);
  96. s.Save(fr);
  97. s.Save(fr2);
  98. s.Save(fum.Fo);
  99. s.Save(fum);
  100. ICriteria baseCriteria = s.CreateCriteria(typeof(Fum))
  101. .Add(Expression.Like("FumString", "f", MatchMode.Start));
  102. baseCriteria.CreateCriteria("Fo")
  103. .Add(Expression.IsNotNull("FumString"));
  104. baseCriteria.CreateCriteria("Friends")
  105. .Add(Expression.Like("FumString", "g%"));
  106. IList list = baseCriteria.List();
  107. Assert.AreEqual(1, list.Count);
  108. Assert.AreSame(fum, list[0]);
  109. baseCriteria = s.CreateCriteria(typeof(Fum))
  110. .Add(Expression.Like("FumString", "f%"))
  111. .SetResultTransformer(CriteriaSpecification.AliasToEntityMap);
  112. baseCriteria.CreateCriteria("Fo", "fo")
  113. .Add(Expression.IsNotNull("FumString"));
  114. baseCriteria.CreateCriteria("Friends", "fum")
  115. .Add(Expression.Like("FumString", "g", MatchMode.Start));
  116. IDictionary map = (IDictionary) baseCriteria.UniqueResult();
  117. Assert.AreSame(fum, map["this"]);
  118. Assert.AreSame(fum.Fo, map["fo"]);
  119. Assert.IsTrue(fum.Friends.Contains(map["fum"]));
  120. Assert.AreEqual(3, map.Count);
  121. baseCriteria = s.CreateCriteria(typeof(Fum))
  122. .Add(Expression.Like("FumString", "f%"))
  123. .SetResultTransformer(CriteriaSpecification.AliasToEntityMap)
  124. .SetFetchMode("Friends", FetchMode.Eager);
  125. baseCriteria.CreateCriteria("Fo", "fo")
  126. .Add(Expression.Eq("FumString", fum.Fo.FumString));
  127. map = (IDictionary) baseCriteria.List()[0];
  128. Assert.AreSame(fum, map["this"]);
  129. Assert.AreSame(fum.Fo, map["fo"]);
  130. Assert.AreEqual(2, map.Count);
  131. list = s.CreateCriteria(typeof(Fum))
  132. .CreateAlias("Friends", "fr")
  133. .CreateAlias("Fo", "fo")
  134. .Add(Expression.Like("FumString", "f%"))
  135. .Add(Expression.IsNotNull("Fo"))
  136. .Add(Expression.IsNotNull("fo.FumString"))
  137. .Add(Expression.Like("fr.FumString", "g%"))
  138. .Add(Expression.EqProperty("fr.id.Short", "id.Short"))
  139. .List();
  140. Assert.AreEqual(1, list.Count);
  141. Assert.AreSame(fum, list[0]);
  142. txn.Commit();
  143. }
  144. using (ISession s = OpenSession())
  145. using (ITransaction txn = s.BeginTransaction())
  146. {
  147. ICriteria baseCriteria = s.CreateCriteria(typeof(Fum))
  148. .Add(Expression.Like("FumString", "f%"));
  149. baseCriteria.CreateCriteria("Fo")
  150. .Add(Expression.IsNotNull("FumString"));
  151. baseCriteria.CreateCriteria("Friends")
  152. .Add(Expression.Like("FumString", "g%"));
  153. Fum fum = (Fum) baseCriteria.List()[0];
  154. Assert.AreEqual(2, fum.Friends.Count);
  155. s.Delete(fum);
  156. s.Delete(fum.Fo);
  157. foreach (object friend in fum.Friends)
  158. {
  159. s.Delete(friend);
  160. }
  161. txn.Commit();
  162. }
  163. }
  164. [Test]
  165. public void ListIdentifiers()
  166. {
  167. ISession s = OpenSession();
  168. ITransaction txn = s.BeginTransaction();
  169. Fum fum = new Fum(FumKey("fum"));
  170. fum.FumString = "fo fee fi";
  171. s.Save(fum);
  172. fum = new Fum(FumKey("fi"));
  173. fum.FumString = "fee fi fo";
  174. s.Save(fum);
  175. // not doing a flush because the Find will do an auto flush unless we tell the session a
  176. // different FlushMode
  177. IList list =
  178. s.CreateQuery("select fum.Id from fum in class NHibernate.DomainModel.Fum where not fum.FumString = 'FRIEND'").List();
  179. Assert.AreEqual(2, list.Count, "List Identifiers");
  180. IEnumerator enumerator =
  181. s.CreateQuery("select fum.Id from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").
  182. Enumerable().GetEnumerator();
  183. int i = 0;
  184. while (enumerator.MoveNext())
  185. {
  186. Assert.IsTrue(enumerator.Current is FumCompositeID, "Iterating Identifiers");
  187. i++;
  188. }
  189. Assert.AreEqual(2, i, "Number of Ids found.");
  190. // clean up by deleting the 2 Fum objects that were added.
  191. s.Delete(s.Load(typeof(Fum), list[0]));
  192. s.Delete(s.Load(typeof(Fum), list[1]));
  193. txn.Commit();
  194. s.Close();
  195. }
  196. public static FumCompositeID FumKey(String str)
  197. {
  198. return FumKey(str, false);
  199. }
  200. public static FumCompositeID FumKey(String str, bool aCompositeQueryTest)
  201. {
  202. FumCompositeID id = new FumCompositeID();
  203. // if( dialect is Dialect.MckoiDialect )
  204. // {
  205. // GregorianCalendar now = new GregorianCalendar();
  206. // GregorianCalendar cal = new GregorianCalendar(
  207. // now.get(java.util.Calendar.YEAR),
  208. // now.get(java.util.Calendar.MONTH),
  209. // now.get(java.util.Calendar.DATE)
  210. // );
  211. // id.setDate( cal.getTime() );
  212. // }
  213. // else
  214. // {
  215. id.Date = new DateTime(2004, 4, 29, 9, 0, 0, 0);
  216. // }
  217. id.String = str;
  218. if (aCompositeQueryTest)
  219. {
  220. id.Short = fumKeyShort++;
  221. }
  222. else
  223. {
  224. id.Short = (short) 12;
  225. }
  226. return id;
  227. }
  228. [Test]
  229. public void CompositeID()
  230. {
  231. ISession s = OpenSession();
  232. ITransaction t = s.BeginTransaction();
  233. Fum fum = new Fum(FumKey("fum"));
  234. fum.FumString = "fee fi fo";
  235. s.Save(fum);
  236. Assert.AreSame(fum, s.Load(typeof(Fum), FumKey("fum"), LockMode.Upgrade));
  237. //s.Flush();
  238. t.Commit();
  239. s.Close();
  240. s = OpenSession();
  241. t = s.BeginTransaction();
  242. fum = (Fum) s.Load(typeof(Fum), FumKey("fum"), LockMode.Upgrade);
  243. Assert.IsNotNull(fum, "Load by composite key");
  244. Fum fum2 = new Fum(FumKey("fi"));
  245. fum2.FumString = "fee fo fi";
  246. fum.Fo = fum2;
  247. s.Save(fum2);
  248. IList list = s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").List();
  249. Assert.AreEqual(2, list.Count, "Find a List of Composite Keyed objects");
  250. IList list2 =
  251. s.CreateQuery("select fum from fum in class NHibernate.DomainModel.Fum where fum.FumString='fee fi fo'").List();
  252. Assert.AreEqual(fum, (Fum) list2[0], "Find one Composite Keyed object");
  253. fum.Fo = null;
  254. //s.Flush();
  255. t.Commit();
  256. s.Close();
  257. s = OpenSession();
  258. t = s.BeginTransaction();
  259. IEnumerator enumerator =
  260. s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").Enumerable().
  261. GetEnumerator();
  262. int i = 0;
  263. while (enumerator.MoveNext())
  264. {
  265. fum = (Fum) enumerator.Current;
  266. s.Delete(fum);
  267. i++;
  268. }
  269. Assert.AreEqual(2, i, "Iterate on Composite Key");
  270. //s.Flush();
  271. t.Commit();
  272. s.Close();
  273. }
  274. [Test]
  275. public void CompositeIDOneToOne()
  276. {
  277. ISession s = OpenSession();
  278. Fum fum = new Fum(FumKey("fum"));
  279. fum.FumString = "fee fi fo";
  280. //s.Save(fum); commented out in h2.0.3
  281. Fumm fumm = new Fumm();
  282. fumm.Fum = fum;
  283. s.Save(fumm);
  284. s.Flush();
  285. s.Close();
  286. s = OpenSession();
  287. fumm = (Fumm) s.Load(typeof(Fumm), FumKey("fum"));
  288. //s.delete(fumm.Fum); commented out in h2.0.3
  289. s.Delete(fumm);
  290. s.Flush();
  291. s.Close();
  292. }
  293. [Test]
  294. public void CompositeIDQuery()
  295. {
  296. ISession s = OpenSession();
  297. Fum fee = new Fum(FumKey("fee", true));
  298. fee.FumString = "fee";
  299. s.Save(fee);
  300. Fum fi = new Fum(FumKey("fi", true));
  301. fi.FumString = "fi";
  302. short fiShort = fi.Id.Short;
  303. s.Save(fi);
  304. Fum fo = new Fum(FumKey("fo", true));
  305. fo.FumString = "fo";
  306. s.Save(fo);
  307. Fum fum = new Fum(FumKey("fum", true));
  308. fum.FumString = "fum";
  309. s.Save(fum);
  310. s.Flush();
  311. s.Close();
  312. s = OpenSession();
  313. // Try to find the Fum object "fo" that we inserted searching by the string in the id
  314. IList vList = s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where fum.Id.String='fo'").List();
  315. Assert.AreEqual(1, vList.Count, "find by composite key query (find fo object)");
  316. fum = (Fum) vList[0];
  317. Assert.AreEqual("fo", fum.Id.String, "find by composite key query (check fo object)");
  318. // Try to fnd the Fum object "fi" that we inserted by searching the date in the id
  319. vList =
  320. s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where fum.Id.Short = ?").SetInt16(0, fiShort).List();
  321. Assert.AreEqual(1, vList.Count, "find by composite key query (find fi object)");
  322. fi = (Fum) vList[0];
  323. Assert.AreEqual("fi", fi.Id.String, "find by composite key query (check fi object)");
  324. // make sure we can return all of the objects by searching by the date id
  325. vList =
  326. s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where fum.Id.Date <= ? and not fum.FumString='FRIEND'").
  327. SetDateTime(0, DateTime.Now).List();
  328. Assert.AreEqual(4, vList.Count, "find by composite key query with arguments");
  329. s.Flush();
  330. s.Close();
  331. s = OpenSession();
  332. Assert.IsTrue(
  333. s.CreateQuery("select fum.Id.Short, fum.Id.Date, fum.Id.String from fum in class NHibernate.DomainModel.Fum").
  334. Enumerable().GetEnumerator().MoveNext());
  335. Assert.IsTrue(
  336. s.CreateQuery("select fum.Id from fum in class NHibernate.DomainModel.Fum").Enumerable().GetEnumerator().MoveNext());
  337. IQuery qu =
  338. s.CreateQuery("select fum.FumString, fum, fum.FumString, fum.Id.Date from fum in class NHibernate.DomainModel.Fum");
  339. IType[] types = qu.ReturnTypes;
  340. Assert.AreEqual(4, types.Length);
  341. for (int k = 0; k < types.Length; k++)
  342. {
  343. Assert.IsNotNull(types[k]);
  344. }
  345. Assert.IsTrue(types[0] is StringType);
  346. Assert.IsTrue(types[1] is EntityType);
  347. Assert.IsTrue(types[2] is StringType);
  348. Assert.IsTrue(types[3] is DateTimeType);
  349. IEnumerator enumer = qu.Enumerable().GetEnumerator();
  350. int j = 0;
  351. while (enumer.MoveNext())
  352. {
  353. j++;
  354. Assert.IsTrue(((object[]) enumer.Current)[1] is Fum);
  355. }
  356. Assert.AreEqual(8, j, "iterate on composite key");
  357. fum = (Fum) s.Load(typeof(Fum), fum.Id);
  358. s.CreateFilter(fum.QuxArray, "where this.Foo is null").List();
  359. s.CreateFilter(fum.QuxArray, "where this.Foo.id = ?").SetString(0, "fooid");
  360. IQuery f = s.CreateFilter(fum.QuxArray, "where this.Foo.id = :fooId");
  361. f.SetString("fooId", "abc");
  362. Assert.IsFalse(f.Enumerable().GetEnumerator().MoveNext());
  363. enumer =
  364. s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").Enumerable().
  365. GetEnumerator();
  366. int i = 0;
  367. while (enumer.MoveNext())
  368. {
  369. fum = (Fum) enumer.Current;
  370. s.Delete(fum);
  371. i++;
  372. }
  373. Assert.AreEqual(4, i, "iterate on composite key");
  374. s.Flush();
  375. s.CreateQuery(
  376. "from fu in class Fum, fo in class Fum where fu.Fo.Id.String = fo.Id.String and fo.FumString is not null").
  377. Enumerable();
  378. s.CreateQuery("from Fumm f1 inner join f1.Fum f2").List();
  379. s.Close();
  380. }
  381. [Test]
  382. public void CompositeIDCollections()
  383. {
  384. ISession s = OpenSession();
  385. Fum fum1 = new Fum(FumKey("fum1"));
  386. Fum fum2 = new Fum(FumKey("fum2"));
  387. fum1.FumString = "fee fo fi";
  388. fum2.FumString = "fee fo fi";
  389. s.Save(fum1);
  390. s.Save(fum2);
  391. Qux q = new Qux();
  392. s.Save(q);
  393. ISet dict = new HashedSet();
  394. IList list = new ArrayList();
  395. dict.Add(fum1);
  396. dict.Add(fum2);
  397. list.Add(fum1);
  398. q.Fums = dict;
  399. q.MoreFums = list;
  400. fum1.QuxArray = new Qux[] {q};
  401. s.Flush();
  402. s.Close();
  403. s = OpenSession();
  404. q = (Qux) s.Load(typeof(Qux), q.Key);
  405. Assert.AreEqual(2, q.Fums.Count, "collection of fums");
  406. Assert.AreEqual(1, q.MoreFums.Count, "collection of fums");
  407. Assert.AreSame(q, ((Fum) q.MoreFums[0]).QuxArray[0], "unkeyed composite id collection");
  408. IEnumerator enumer = q.Fums.GetEnumerator();
  409. enumer.MoveNext();
  410. s.Delete((Fum) enumer.Current);
  411. enumer.MoveNext();
  412. s.Delete((Fum) enumer.Current);
  413. s.Delete(q);
  414. s.Flush();
  415. s.Close();
  416. }
  417. [Test]
  418. public void DeleteOwner()
  419. {
  420. ISession s = OpenSession();
  421. Qux q = new Qux();
  422. s.Save(q);
  423. Fum f1 = new Fum(FumKey("f1"));
  424. Fum f2 = new Fum(FumKey("f2"));
  425. ISet dict = new HashedSet();
  426. dict.Add(f1);
  427. dict.Add(f2);
  428. IList list = new ArrayList();
  429. list.Add(f1);
  430. list.Add(f2);
  431. f1.FumString = "f1";
  432. f2.FumString = "f2";
  433. q.Fums = dict;
  434. q.MoreFums = list;
  435. s.Save(f1);
  436. s.Save(f2);
  437. s.Flush();
  438. s.Close();
  439. s = OpenSession();
  440. ITransaction t = s.BeginTransaction();
  441. q = (Qux) s.Load(typeof(Qux), q.Key, LockMode.Upgrade);
  442. s.Lock(q, LockMode.Upgrade);
  443. s.Delete(q);
  444. t.Commit();
  445. s.Close();
  446. s = OpenSession();
  447. t = s.BeginTransaction();
  448. list = s.CreateQuery("from fum in class NHibernate.DomainModel.Fum where not fum.FumString='FRIEND'").List();
  449. Assert.AreEqual(2, list.Count, "deleted owner");
  450. s.Lock(list[0], LockMode.Upgrade);
  451. s.Lock(list[1], LockMode.Upgrade);
  452. foreach (object obj in list)
  453. {
  454. s.Delete(obj);
  455. }
  456. t.Commit();
  457. s.Close();
  458. }
  459. [Test]
  460. public void CompositeIDs()
  461. {
  462. ISession s = OpenSession();
  463. Fo fo = Fo.NewFo();
  464. s.Save(fo, FumKey("an instance of fo"));
  465. s.Flush();
  466. s.Close();
  467. s = OpenSession();
  468. fo = (Fo) s.Load(typeof(Fo), FumKey("an instance of fo"));
  469. fo.X = 5;
  470. s.Flush();
  471. s.Close();
  472. s = OpenSession();
  473. fo = (Fo) s.Load(typeof(Fo), FumKey("an instance of fo"));
  474. Assert.AreEqual(5, fo.X);
  475. IEnumerator enumer =
  476. s.CreateQuery("from fo in class NHibernate.DomainModel.Fo where fo.id.String like 'an instance of fo'").Enumerable()
  477. .GetEnumerator();
  478. Assert.IsTrue(enumer.MoveNext());
  479. Assert.AreSame(fo, enumer.Current);
  480. s.Delete(fo);
  481. s.Flush();
  482. try
  483. {
  484. s.Save(Fo.NewFo());
  485. Assert.Fail("should not get here");
  486. }
  487. catch (Exception e)
  488. {
  489. Assert.IsNotNull(e);
  490. }
  491. s.Close();
  492. }
  493. [Test]
  494. public void KeyManyToOne()
  495. {
  496. ISession s = OpenSession();
  497. Inner sup = new Inner();
  498. InnerKey sid = new InnerKey();
  499. sup.Dudu = "dudu";
  500. sid.AKey = "a";
  501. sid.BKey = "b";
  502. sup.Id = sid;
  503. Middle m = new Middle();
  504. MiddleKey mid = new MiddleKey();
  505. mid.One = "one";
  506. mid.Two = "two";
  507. mid.Sup = sup;
  508. m.Id = mid;
  509. m.Bla = "bla";
  510. Outer d = new Outer();
  511. OuterKey did = new OuterKey();
  512. did.Master = m;
  513. did.DetailId = "detail";
  514. d.Id = did;
  515. d.Bubu = "bubu";
  516. s.Save(sup);
  517. s.Save(m);
  518. s.Save(d);
  519. s.Flush();
  520. s.Close();
  521. s = OpenSession();
  522. d = (Outer) s.Load(typeof(Outer), did);
  523. Assert.AreEqual("dudu", d.Id.Master.Id.Sup.Dudu);
  524. s.Delete(d);
  525. s.Delete(d.Id.Master);
  526. s.Save(d.Id.Master);
  527. s.Save(d);
  528. s.Flush();
  529. s.Close();
  530. s = OpenSession();
  531. d = (Outer) s.CreateQuery("from Outer o where o.id.DetailId=?").SetString(0, d.Id.DetailId).List()[0];
  532. s.CreateQuery("from Outer o where o.Id.Master.Id.Sup.Dudu is not null").List();
  533. s.CreateQuery("from Outer o where o.Id.Master.Bla = ''").List();
  534. s.CreateQuery("from Outer o where o.Id.Master.Id.One = ''").List();
  535. s.Delete(d);
  536. s.Delete(d.Id.Master);
  537. s.Delete(d.Id.Master.Id.Sup);
  538. s.Flush();
  539. s.Close();
  540. }
  541. [Test]
  542. public void CompositeKeyPathExpressions()
  543. {
  544. using (ISession s = OpenSession())
  545. {
  546. s.CreateQuery("select fum1.Fo from fum1 in class Fum where fum1.Fo.FumString is not null").List();
  547. s.CreateQuery("from fum1 in class Fum where fum1.Fo.FumString is not null order by fum1.Fo.FumString").List();
  548. if (Dialect.SupportsSubSelects)
  549. {
  550. s.CreateQuery("from fum1 in class Fum where exists elements(fum1.Friends)").List();
  551. s.CreateQuery("from fum1 in class Fum where size(fum1.Friends) = 0").List();
  552. }
  553. if (IsClassicParser)
  554. {
  555. s.CreateQuery("select fum1.Friends.elements from fum1 in class Fum").List();
  556. }
  557. else
  558. {
  559. s.CreateQuery("select elements(fum1.Friends) from fum1 in class Fum").List();
  560. }
  561. s.CreateQuery("from fum1 in class Fum, fr in elements( fum1.Friends )").List();
  562. }
  563. }
  564. [Test]
  565. public void UnflushedSessionSerialization()
  566. {
  567. ///////////////////////////////////////////////////////////////////////////
  568. // Test insertions across serializations
  569. ISession s2;
  570. // NOTE: H2.1 has getSessions().openSession() here (and below),
  571. // instead of just the usual openSession()
  572. using (ISession s = sessions.OpenSession())
  573. {
  574. s.FlushMode = FlushMode.Never;
  575. Simple simple = new Simple();
  576. simple.Address = "123 Main St. Anytown USA";
  577. simple.Count = 1;
  578. simple.Date = new DateTime(2005, 1, 1);
  579. simple.Name = "My UnflushedSessionSerialization Simple";
  580. simple.Pay = 5000.0f;
  581. s.Save(simple, 10L);
  582. // Now, try to serialize session without flushing...
  583. s.Disconnect();
  584. s2 = SpoofSerialization(s);
  585. }
  586. Simple check, other;
  587. using (ISession s = s2)
  588. {
  589. s.Reconnect();
  590. Simple simple = (Simple) s.Load(typeof(Simple), 10L);
  591. other = new Simple();
  592. other.Init();
  593. s.Save(other, 11L);
  594. simple.Other = other;
  595. s.Flush();
  596. check = simple;
  597. }
  598. ///////////////////////////////////////////////////////////////////////////
  599. // Test updates across serializations
  600. using (ISession s = sessions.OpenSession())
  601. {
  602. s.FlushMode = FlushMode.Never;
  603. Simple simple = (Simple) s.Get(typeof(Simple), 10L);
  604. Assert.AreEqual(check.Name, simple.Name, "Not same parent instances");
  605. Assert.AreEqual(check.Other.Name, other.Name, "Not same child instances");
  606. simple.Name = "My updated name";
  607. s.Disconnect();
  608. s2 = SpoofSerialization(s);
  609. check = simple;
  610. }
  611. using (ISession s = s2)
  612. {
  613. s.Reconnect();
  614. s.Flush();
  615. }
  616. ///////////////////////////////////////////////////////////////////////////
  617. // Test deletions across serializations
  618. using (ISession s = sessions.OpenSession())
  619. {
  620. s.FlushMode = FlushMode.Never;
  621. Simple simple = (Simple) s.Get(typeof(Simple), 10L);
  622. Assert.AreEqual(check.Name, simple.Name, "Not same parent instances");
  623. Assert.AreEqual(check.Other.Name, other.Name, "Not same child instances");
  624. // Now, lets delete across serialization...
  625. s.Delete(simple);
  626. s.Disconnect();
  627. s2 = SpoofSerialization(s);
  628. }
  629. using (ISession s = s2)
  630. {
  631. s.Reconnect();
  632. s.Flush();
  633. }
  634. using (ISession s = OpenSession())
  635. {
  636. s.Delete("from Simple");
  637. s.Flush();
  638. }
  639. }
  640. private ISession SpoofSerialization(ISession session)
  641. {
  642. BinaryFormatter formatter = new BinaryFormatter();
  643. MemoryStream stream = new MemoryStream();
  644. formatter.Serialize(stream, session);
  645. stream.Position = 0;
  646. return (ISession) formatter.Deserialize(stream);
  647. }
  648. }
  649. }