public void TestEventHandlers() { string storeCode = "S10"; TestCustomMethods tester = new TestCustomMethods(); StoreType st1 = new StoreType() { Name = "INT", Caption = "Internal store" }; st1.Save(); Store s1 = new Store() { Code = storeCode, StoreType = st1, Caption = "Store 1", Tester = tester }; Assert.AreEqual(0, tester.Validate); Assert.AreEqual(0, tester.OnSave); Assert.AreEqual(0, tester.OnFlush); Assert.AreEqual(0, tester.OnDelete); s1.Save(); Assert.AreEqual(3, tester.Validate); Assert.AreEqual(1, tester.OnSave); Assert.AreEqual(2, tester.OnFlush); // Because of registry added Assert.AreEqual(0, tester.OnDelete); s1.Caption = "Changed"; s1.Save(); Assert.AreEqual(4, tester.Validate); Assert.AreEqual(2, tester.OnSave); Assert.AreEqual(3, tester.OnFlush); Assert.AreEqual(0, tester.OnDelete); s1.Delete(); Assert.AreEqual(4, tester.Validate); Assert.AreEqual(2, tester.OnSave); Assert.AreEqual(3, tester.OnFlush); Assert.AreEqual(1, tester.OnDelete); SessionManager.CloseSession(); Assert.IsNull(Store.GetByCode(storeCode), "Store was not deleted"); TestCustomMethods tester2 = new TestCustomMethods(); Store st2 = new Store() { Code = storeCode, StoreType = st1, Caption = "Store 2", Tester = tester2 }; bool exOccurred = false; tester2.ThrowOnValidate = true; try { st2.Save(); } catch (Exception e) { Assert.AreEqual("Store.Validate()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); Assert.IsNull(Store.GetByCode(storeCode), "Store was saved but Validate exception"); tester2.Clear(); tester2.ThrowOnSave = true; exOccurred = false; try { st2.Save(); } catch (Exception e) { Assert.AreEqual("Store.OnSave()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); Assert.IsNull(Store.GetByCode(storeCode), "Store was saved but OnSave exception"); tester2.Clear(); tester2.ThrowOnFlush = true; exOccurred = false; try { st2.Save(); } catch (Exception e) { Assert.AreEqual("Store.OnFlush()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); Assert.IsNull(Store.GetByCode(storeCode), "Store was saved but OnFlush exception"); tester2.Clear(); exOccurred = false; try { st2.Save(); } catch (Exception) { exOccurred = true; } Assert.IsTrue(exOccurred, "Saved after exception OnFlush"); SessionManager.CloseSession(); Store st3 = new Store() { Code = storeCode, StoreType = st1, Caption = "Store 2", Tester = tester2 }; st3.Save(); SessionManager.CloseSession(); Store st4 = Store.GetByCode(storeCode); Assert.IsNotNull(st4, "Store was not saved"); st4.Tester = tester2; string caption = st4.Caption; st4.Caption = "555"; tester2.Clear(); tester2.ThrowOnValidate = true; exOccurred = false; try { st4.Save(); } catch (Exception e) { Assert.AreEqual("Store.Validate()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); SessionManager.CloseSession(); Store st5 = Store.GetByCode(storeCode); Assert.AreEqual(caption, st5.Caption, "Store was updated but Validate exception"); st5.Tester = tester2; st5.Caption = "555"; tester2.Clear(); tester2.ThrowOnSave = true; exOccurred = false; try { st5.Save(); } catch (Exception e) { Assert.AreEqual("Store.OnSave()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); SessionManager.CloseSession(); Store st6 = Store.GetByCode(storeCode); Assert.AreEqual(caption, st6.Caption, "Store was updated but OnSave exception"); st6.Tester = tester2; st6.Caption = "555"; tester2.Clear(); tester2.ThrowOnFlush = true; exOccurred = false; try { st6.Save(); } catch (Exception e) { Assert.AreEqual("Store.OnFlush()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); SessionManager.CloseSession(); Store st7 = Store.GetByCode(storeCode); Assert.AreEqual(caption, st7.Caption, "Store was updated but OnFlush exception"); st7.Tester = tester2; tester2.Clear(); tester2.ThrowOnDelete = true; exOccurred = false; try { st7.Delete(); } catch (Exception e) { Assert.AreEqual("Store.OnDelete()", e.Message); exOccurred = true; } Assert.IsTrue(exOccurred); SessionManager.CloseSession(); Store st8 = Store.GetByCode(storeCode); Assert.IsNotNull(st8, "Store was deleted but OnDelete exception"); st8.Tester = tester2; tester2.Clear(); tester2.ThrowOnFlush = true; tester2.ThrowOnValidate = true; exOccurred = false; try { st8.Delete(); } catch (Exception) { exOccurred = true; } Assert.IsFalse(exOccurred, "Unexpected exception on delete. Tester: {0}", tester2.ToString()); SessionManager.CloseSession(); Assert.IsNull(Store.GetByCode(storeCode), "Store was not deleted"); }