public void SaveOrUpdate_Save() { using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) using (ISession s = OpenSession()) { ITransaction t = s.BeginTransaction(); Parent parent = new Parent() { Id = "parent", Children = new List<Child>(), }; s.SaveOrUpdate(parent); t.Commit(); long actual = s.CreateQuery("select count(p) from Parent p").UniqueResult<long>(); Assert.That(actual, Is.EqualTo(1)); string[] warnings = GetAssignedIdentifierWarnings(ls); Assert.That(warnings.Length, Is.EqualTo(1)); Assert.IsTrue(warnings[0].Contains("parent")); } }
public void CanGetQueryDurationForBatch() { using (LogSpy spy = new LogSpy(typeof(AbstractBatcher))) using (ISession session = OpenSession()) using (ITransaction tx = session.BeginTransaction()) { for (int i = 0; i < 3; i++) { var customer = new Customer { Name = "foo" }; session.Save(customer); session.Delete(customer); } session.Flush(); var wholeLog = spy.GetWholeLog(); var lines = wholeLog.Split(new[]{System.Environment.NewLine},StringSplitOptions.RemoveEmptyEntries); int batches = 0; foreach (var line in lines) { if (line.Contains("ExecuteBatch for 1 statements took ")) batches += 1; } Assert.AreEqual(3, batches); tx.Rollback(); } }
public void ExplicitUpdate() { ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[] { new AssertOldStatePostListener( eArgs => Assert.That(eArgs.OldState, Is.Not.Null)) }; FillDb(); using (var ls = new LogSpy(typeof (AssertOldStatePostListener))) { using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { IList<SimpleEntity> l = s.CreateCriteria<SimpleEntity>().List<SimpleEntity>(); l[0].Description = "Modified"; s.Update(l[0]); tx.Commit(); } } Assert.That(ls.GetWholeLog(), Text.Contains(AssertOldStatePostListener.LogMessage)); } DbCleanup(); ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[0]; }
public void CanLogLinqExpressionWithoutInitializingContainedProxy() { var productId = db.Products.Select(p => p.ProductId).First(); using (var logspy = new LogSpy("NHibernate.Linq")) { var productProxy = session.Load<Product>(productId); Assert.That(NHibernateUtil.IsInitialized(productProxy), Is.False); var result = from product in db.Products where product == productProxy select product; Assert.That(result.Count(), Is.EqualTo(1)); // Verify that the expected logging did happen. var actualLog = logspy.GetWholeLog(); string expectedLog = "Expression (partially evaluated): value(NHibernate.Linq.NhQueryable`1[NHibernate.DomainModel.Northwind.Entities.Product])" + ".Where(product => (product == Product#" + productId + ")).Count()"; Assert.That(actualLog, Is.StringContaining(expectedLog)); // And verify that the proxy in the expression wasn't initialized. Assert.That(NHibernateUtil.IsInitialized(productProxy), Is.False, "ERROR: We expected the proxy to NOT be initialized."); } }
protected override void BuildSessionFactory() { using (var logSpy = new LogSpy(typeof(EntityMetamodel))) { base.BuildSessionFactory(); log = logSpy.GetWholeLog(); } }
protected override void BuildSessionFactory() { // Without configured cache, should log warn. using (var ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) { base.BuildSessionFactory(); Assert.That(ls.GetWholeLog(), Text.Contains("Fake cache used")); } }
private string[] GetAssignedIdentifierWarnings(LogSpy ls) { List<string> warnings = new List<string>(); foreach (string logEntry in ls.GetWholeLog().Split('\n')) if (logEntry.Contains("Unable to determine if") && logEntry.Contains("is transient or detached")) warnings.Add(logEntry); return warnings.ToArray(); }
public void CanGetQueryDurationForDelete() { using (LogSpy spy = new LogSpy(typeof(AbstractBatcher))) using (ISession session = OpenSession()) using (ITransaction tx = session.BeginTransaction()) { session.CreateQuery("delete Customer").ExecuteUpdate(); var wholeLog = spy.GetWholeLog(); Assert.That(wholeLog.Contains("ExecuteNonQuery took"), Is.True); tx.Rollback(); } }
public void CanSaveInSingleBatch() { if (configuration.Properties[Environment.ConnectionDriver].Contains(typeof (OracleDataClientDriver).Name) == false) { Assert.Ignore("Only applicable for Oracle Data Client driver"); } MainClass[] mc = new MainClass[] { new MainClass("d0"), new MainClass("d0"), new MainClass("d1"), new MainClass("d1"), new MainClass("d1") }; bool executedBatch = false; using (LogSpy spy = new LogSpy(typeof (AbstractBatcher))) { using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { foreach (MainClass mainClass in mc) { s.Save(mainClass); } tx.Commit(); foreach (LoggingEvent loggingEvent in spy.Appender.GetEvents()) { if ("Executing batch".Equals(loggingEvent.MessageObject)) { executedBatch = true; break; } } } } } using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.Delete("from MainClass"); tx.Commit(); } } Assert.IsTrue(executedBatch); }
public void CanGetQueryDurationForSelect() { using (LogSpy spy = new LogSpy(typeof(AbstractBatcher))) using (ISession session = OpenSession()) using (ITransaction tx = session.BeginTransaction()) { session.CreateQuery("from Customer").List(); var wholeLog = spy.GetWholeLog(); Assert.That(wholeLog.Contains("ExecuteReader took"), Is.True); Assert.That(wholeLog.Contains("DataReader was closed after"), Is.True); tx.Rollback(); } }
protected override void BuildSessionFactory() { using (LogSpy ls = new LogSpy(typeof (AbstractEntityTuplizer))) { base.BuildSessionFactory(); StringBuilder wholeMessage = new StringBuilder(); foreach (LoggingEvent loggingEvent in ls.Appender.GetEvents()) { string singleMessage = loggingEvent.RenderedMessage; if (singleMessage.IndexOf("AbstractEntityTuplizer") > 0) Assert.Greater(singleMessage.IndexOf("No custom accessors found"), -1); wholeMessage.Append(singleMessage); } string logs = wholeMessage.ToString(); Assert.AreEqual(-1, logs.IndexOf("Custom accessors found")); } }
public void PageBetweenProjections() { using (var spy = new LogSpy("NHibernate.Linq")) { var subquery = db.Products.Where(p => p.ProductId > 5); var list = db.Products.Where(p => subquery.Contains(p)) .Skip(5).Take(10) .ToList(); var logtext = spy.GetWholeLog(); const string expected = "Expression (partially evaluated): value(NHibernate.Linq.NhQueryable`1[NHibernate.DomainModel.Northwind.Entities.Product]).Where(p => value(NHibernate.Linq.NhQueryable`1[NHibernate.DomainModel.Northwind.Entities.Product]).Where(p => (p.ProductId > 5)).Contains(p)).Skip(5).Take(10)"; Assert.That(logtext, Is.StringContaining(expected)); } }
public void WhenNoCustomAccessorIsDefinedThenSholdFindOnlyNoCustom() { var cfg = new Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); cfg.AddResource("NHibernate.Test.NHSpecificTest.NH1304.Mappings.hbm.xml", GetType().Assembly); using (LogSpy ls = new LogSpy(typeof(AbstractEntityTuplizer))) { cfg.BuildSessionFactory(); StringBuilder wholeMessage = new StringBuilder(); foreach (LoggingEvent loggingEvent in ls.Appender.GetEvents()) { string singleMessage = loggingEvent.RenderedMessage; if (singleMessage.IndexOf("AbstractEntityTuplizer") > 0) Assert.Greater(singleMessage.IndexOf("No custom"), -1); wholeMessage.Append(singleMessage); } string logs = wholeMessage.ToString(); Assert.AreEqual(-1, logs.IndexOf("Custom")); } }
public void Bug() { A a = new A("NH1332"); using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { s.Save(a); tx.Commit(); } using (LogSpy ls = new LogSpy(log)) { using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { s.Delete(a); tx.Commit(); } Assert.AreEqual(1, ls.Appender.GetEvents().Length); string logs = ls.Appender.GetEvents()[0].RenderedMessage; Assert.Greater(logs.IndexOf("PostCommitDelete fired."), -1); } }
public void Bug() { XmlConfigurator.Configure(); var cfg = new Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); cfg.AddResource("NHibernate.Test.NHSpecificTest.NH1587.Mappings.hbm.xml", GetType().Assembly); cfg.Configure(); bool useOptimizer= false; using (var ls = new LogSpy("NHibernate.Tuple.Entity.PocoEntityTuplizer")) { cfg.BuildSessionFactory(); foreach (LoggingEvent loggingEvent in ls.Appender.GetEvents()) { if (((string)(loggingEvent.MessageObject)).StartsWith("Create Instantiator using optimizer")) { useOptimizer = true; break; } } } Assert.That(useOptimizer); }
private static int Count(LogSpy sqlLog, string s) { var log = sqlLog.GetWholeLog(); return log.Split(new[] {s}, StringSplitOptions.None).Length - 1; }
public void UpdateDetachedObject() { // When the update is used directly as method to reattach a entity the OldState is null // that mean that NH should not retrieve info from DB ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[] { new AssertOldStatePostListener( eArgs => Assert.That(eArgs.OldState, Is.Null)) }; FillDb(); SimpleEntity toModify; using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { IList<SimpleEntity> l = s.CreateCriteria<SimpleEntity>().List<SimpleEntity>(); toModify = l[0]; tx.Commit(); } } toModify.Description = "Modified"; using (var ls = new LogSpy(typeof (AssertOldStatePostListener))) { using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.Update(toModify); tx.Commit(); } } Assert.That(ls.GetWholeLog(), Text.Contains(AssertOldStatePostListener.LogMessage)); } DbCleanup(); ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[0]; }
public void InsertCascadeNoWarning() { using (ISession s = OpenSession()) { ITransaction t = s.BeginTransaction(); s.Save(new Child() { Id = "persistedChild" }); t.Commit(); } using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) using (ISession s = OpenSession()) { ITransaction t = s.BeginTransaction(); Parent parent = new Parent() { Id = "parent", Children = new List<Child>(), }; s.Save(parent); Child child1 = s.Load<Child>("persistedChild"); child1.Parent = parent; parent.Children.Add(child1); Child child2 = new Child() { Id = "transientChild", Parent = parent }; s.Save(child2); parent.Children.Add(child2); t.Commit(); long actual = s.CreateQuery("select count(c) from Child c").UniqueResult<long>(); Assert.That(actual, Is.EqualTo(2)); string[] warnings = GetAssignedIdentifierWarnings(ls); Assert.That(warnings.Length, Is.EqualTo(0)); } }
public void WithDetachedObject() { ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[] { new AssertOldStatePostListener( eArgs => Assert.That(eArgs.OldState, Is.Not.Null)) }; FillDb(); SimpleEntity toModify; using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { IList<SimpleEntity> l = s.CreateCriteria<SimpleEntity>().List<SimpleEntity>(); toModify = l[0]; tx.Commit(); } } toModify.Description = "Modified"; using (var ls = new LogSpy(typeof (AssertOldStatePostListener))) { using (ISession s = OpenSession()) { using (ITransaction tx = s.BeginTransaction()) { s.Merge(toModify); tx.Commit(); } } Assert.That(ls.GetWholeLog(), Is.StringContaining(AssertOldStatePostListener.LogMessage)); } DbCleanup(); ((SessionFactoryImpl) sessions).EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[0]; }
public void SaveOrUpdate_Update() { using (ISession s = OpenSession()) { ITransaction t = s.BeginTransaction(); s.Save(new Parent() { Id = "parent", Name = "before" }); t.Commit(); } using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) using (ISession s = OpenSession()) { ITransaction t = s.BeginTransaction(); Parent parent = new Parent() { Id = "parent", Name = "after", }; s.SaveOrUpdate(parent); t.Commit(); string[] warnings = GetAssignedIdentifierWarnings(ls); Assert.That(warnings.Length, Is.EqualTo(1)); Assert.IsTrue(warnings[0].Contains("parent")); } using (ISession s = OpenSession()) { Parent parent = s.CreateQuery("from Parent").UniqueResult<Parent>(); Assert.That(parent.Name, Is.EqualTo("after")); } }
private static int CountJoins(LogSpy sqlLog) { return Count(sqlLog, "join"); }