|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using log4net; |
| 4 | +using log4net.Core; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace NHibernate.Test.IdTest |
| 8 | +{ |
| 9 | + |
| 10 | + [TestFixture] |
| 11 | + public class AssignedFixture : IdFixtureBase |
| 12 | + { |
| 13 | + |
| 14 | + private string[] GetAssignedIdentifierWarnings(LogSpy ls) |
| 15 | + { |
| 16 | + List<string> warnings = new List<string>(); |
| 17 | + |
| 18 | + foreach (string logEntry in ls.GetWholeLog().Split('\n')) |
| 19 | + if (logEntry.Contains("Unable to determine if") && logEntry.Contains("is transient or detached")) |
| 20 | + warnings.Add(logEntry); |
| 21 | + |
| 22 | + return warnings.ToArray(); |
| 23 | + } |
| 24 | + |
| 25 | + protected override string TypeName |
| 26 | + { |
| 27 | + get { return "Assigned"; } |
| 28 | + } |
| 29 | + |
| 30 | + protected override void OnTearDown() |
| 31 | + { |
| 32 | + base.OnTearDown(); |
| 33 | + |
| 34 | + using (ISession s = OpenSession()) |
| 35 | + using (ITransaction t = s.BeginTransaction()) |
| 36 | + { |
| 37 | + s.CreateQuery("delete from Child").ExecuteUpdate(); |
| 38 | + s.CreateQuery("delete from Parent").ExecuteUpdate(); |
| 39 | + t.Commit(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void SaveOrUpdate_Save() |
| 45 | + { |
| 46 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 47 | + using (ISession s = OpenSession()) |
| 48 | + { |
| 49 | + ITransaction t = s.BeginTransaction(); |
| 50 | + |
| 51 | + Parent parent = |
| 52 | + new Parent() |
| 53 | + { |
| 54 | + Id = "parent", |
| 55 | + Children = new List<Child>(), |
| 56 | + }; |
| 57 | + |
| 58 | + s.SaveOrUpdate(parent); |
| 59 | + t.Commit(); |
| 60 | + |
| 61 | + long actual = s.CreateQuery("select count(p) from Parent p").UniqueResult<long>(); |
| 62 | + Assert.That(actual, Is.EqualTo(1)); |
| 63 | + |
| 64 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 65 | + Assert.That(warnings.Length, Is.EqualTo(1)); |
| 66 | + Assert.IsTrue(warnings[0].Contains("parent")); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void SaveNoWarning() |
| 72 | + { |
| 73 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 74 | + using (ISession s = OpenSession()) |
| 75 | + { |
| 76 | + ITransaction t = s.BeginTransaction(); |
| 77 | + |
| 78 | + Parent parent = |
| 79 | + new Parent() |
| 80 | + { |
| 81 | + Id = "parent", |
| 82 | + Children = new List<Child>(), |
| 83 | + }; |
| 84 | + |
| 85 | + s.Save(parent); |
| 86 | + t.Commit(); |
| 87 | + |
| 88 | + long actual = s.CreateQuery("select count(p) from Parent p").UniqueResult<long>(); |
| 89 | + Assert.That(actual, Is.EqualTo(1)); |
| 90 | + |
| 91 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 92 | + Assert.That(warnings.Length, Is.EqualTo(0)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void SaveOrUpdate_Update() |
| 98 | + { |
| 99 | + using (ISession s = OpenSession()) |
| 100 | + { |
| 101 | + ITransaction t = s.BeginTransaction(); |
| 102 | + |
| 103 | + s.Save(new Parent() { Id = "parent", Name = "before" }); |
| 104 | + t.Commit(); |
| 105 | + } |
| 106 | + |
| 107 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 108 | + using (ISession s = OpenSession()) |
| 109 | + { |
| 110 | + ITransaction t = s.BeginTransaction(); |
| 111 | + |
| 112 | + Parent parent = |
| 113 | + new Parent() |
| 114 | + { |
| 115 | + Id = "parent", |
| 116 | + Name = "after", |
| 117 | + }; |
| 118 | + |
| 119 | + s.SaveOrUpdate(parent); |
| 120 | + t.Commit(); |
| 121 | + |
| 122 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 123 | + Assert.That(warnings.Length, Is.EqualTo(1)); |
| 124 | + Assert.IsTrue(warnings[0].Contains("parent")); |
| 125 | + } |
| 126 | + |
| 127 | + using (ISession s = OpenSession()) |
| 128 | + { |
| 129 | + Parent parent = s.CreateQuery("from Parent").UniqueResult<Parent>(); |
| 130 | + Assert.That(parent.Name, Is.EqualTo("after")); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + public void UpdateNoWarning() |
| 136 | + { |
| 137 | + using (ISession s = OpenSession()) |
| 138 | + { |
| 139 | + ITransaction t = s.BeginTransaction(); |
| 140 | + |
| 141 | + s.Save(new Parent() { Id = "parent", Name = "before" }); |
| 142 | + t.Commit(); |
| 143 | + } |
| 144 | + |
| 145 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 146 | + using (ISession s = OpenSession()) |
| 147 | + { |
| 148 | + ITransaction t = s.BeginTransaction(); |
| 149 | + |
| 150 | + Parent parent = |
| 151 | + new Parent() |
| 152 | + { |
| 153 | + Id = "parent", |
| 154 | + Name = "after", |
| 155 | + }; |
| 156 | + |
| 157 | + s.Update(parent); |
| 158 | + t.Commit(); |
| 159 | + |
| 160 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 161 | + Assert.That(warnings.Length, Is.EqualTo(0)); |
| 162 | + } |
| 163 | + |
| 164 | + using (ISession s = OpenSession()) |
| 165 | + { |
| 166 | + Parent parent = s.CreateQuery("from Parent").UniqueResult<Parent>(); |
| 167 | + Assert.That(parent.Name, Is.EqualTo("after")); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public void InsertCascade() |
| 173 | + { |
| 174 | + using (ISession s = OpenSession()) |
| 175 | + { |
| 176 | + ITransaction t = s.BeginTransaction(); |
| 177 | + |
| 178 | + s.Save(new Child() { Id = "detachedChild" }); |
| 179 | + t.Commit(); |
| 180 | + } |
| 181 | + |
| 182 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 183 | + using (ISession s = OpenSession()) |
| 184 | + { |
| 185 | + ITransaction t = s.BeginTransaction(); |
| 186 | + |
| 187 | + Parent parent = |
| 188 | + new Parent() |
| 189 | + { |
| 190 | + Id = "parent", |
| 191 | + Children = new List<Child>(), |
| 192 | + }; |
| 193 | + |
| 194 | + parent.Children.Add(new Child() { Id = "detachedChild", Parent = parent }); |
| 195 | + parent.Children.Add(new Child() { Id = "transientChild", Parent = parent }); |
| 196 | + |
| 197 | + s.Save(parent); |
| 198 | + t.Commit(); |
| 199 | + |
| 200 | + long actual = s.CreateQuery("select count(c) from Child c").UniqueResult<long>(); |
| 201 | + Assert.That(actual, Is.EqualTo(2)); |
| 202 | + |
| 203 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 204 | + Assert.That(warnings.Length, Is.EqualTo(2)); |
| 205 | + Assert.IsTrue(warnings[0].Contains("detachedChild")); |
| 206 | + Assert.IsTrue(warnings[1].Contains("transientChild")); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + [Test] |
| 211 | + public void InsertCascadeNoWarning() |
| 212 | + { |
| 213 | + using (ISession s = OpenSession()) |
| 214 | + { |
| 215 | + ITransaction t = s.BeginTransaction(); |
| 216 | + |
| 217 | + s.Save(new Child() { Id = "persistedChild" }); |
| 218 | + t.Commit(); |
| 219 | + } |
| 220 | + |
| 221 | + using (LogSpy ls = new LogSpy(LogManager.GetLogger("NHibernate"), Level.Warn)) |
| 222 | + using (ISession s = OpenSession()) |
| 223 | + { |
| 224 | + ITransaction t = s.BeginTransaction(); |
| 225 | + |
| 226 | + Parent parent = |
| 227 | + new Parent() |
| 228 | + { |
| 229 | + Id = "parent", |
| 230 | + Children = new List<Child>(), |
| 231 | + }; |
| 232 | + |
| 233 | + s.Save(parent); |
| 234 | + |
| 235 | + Child child1 = s.Load<Child>("persistedChild"); |
| 236 | + child1.Parent = parent; |
| 237 | + parent.Children.Add(child1); |
| 238 | + |
| 239 | + Child child2 = new Child() { Id = "transientChild", Parent = parent }; |
| 240 | + s.Save(child2); |
| 241 | + parent.Children.Add(child2); |
| 242 | + |
| 243 | + t.Commit(); |
| 244 | + |
| 245 | + long actual = s.CreateQuery("select count(c) from Child c").UniqueResult<long>(); |
| 246 | + Assert.That(actual, Is.EqualTo(2)); |
| 247 | + |
| 248 | + string[] warnings = GetAssignedIdentifierWarnings(ls); |
| 249 | + Assert.That(warnings.Length, Is.EqualTo(0)); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + } |
| 254 | + |
| 255 | +} |
0 commit comments