PageRenderTime 346ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NHibernate.Test/NHSpecificTest/NH2583/AbstractMassTestingFixture.cs

https://github.com/Saleslogix/nhibernate-core
C# | 279 lines | 252 code | 22 blank | 5 comment | 25 complexity | 12663c68b8bf106c1abd65fd79b189f6 MD5 | raw file
  1. using NHibernate.Cfg;
  2. using NHibernate.Linq;
  3. using NUnit.Framework;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using Environment = NHibernate.Cfg.Environment;
  9. namespace NHibernate.Test.NHSpecificTest.NH2583
  10. {
  11. public abstract class AbstractMassTestingFixture : BugTestCase
  12. {
  13. public const int BatchSize = 200;
  14. protected override void Configure(Configuration configuration)
  15. {
  16. base.Configure(configuration);
  17. configuration.DataBaseIntegration(x => x.BatchSize = BatchSize+5);
  18. List<string> cacheSettings = new List<string>(configuration.Properties.Keys.Where(x => x.Contains("cache")));
  19. foreach (var cacheSetting in cacheSettings)
  20. {
  21. configuration.Properties.Remove(cacheSetting);
  22. }
  23. configuration.SetProperty(Environment.UseSecondLevelCache, "false");
  24. }
  25. private class ValueTuple<T1, T2, T3, T4, T5, T6, T7>
  26. {
  27. public T1 Item1;
  28. public T2 Item2;
  29. public T3 Item3;
  30. public T4 Item4;
  31. public T5 Item5;
  32. public T6 Item6;
  33. public T7 Item7;
  34. }
  35. private static IEnumerable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> GetAllTestCases<T1, T2, T3, T4, T5, T6, T7>()
  36. {
  37. foreach (T1 v1 in Enum.GetValues(typeof(T1)))
  38. {
  39. foreach (T2 v2 in Enum.GetValues(typeof(T2)))
  40. {
  41. foreach (T3 v3 in Enum.GetValues(typeof(T3)))
  42. {
  43. foreach (T4 v4 in Enum.GetValues(typeof(T4)))
  44. {
  45. foreach (T5 v5 in Enum.GetValues(typeof(T5)))
  46. {
  47. foreach (T6 v6 in Enum.GetValues(typeof(T6)))
  48. {
  49. foreach (T7 v7 in Enum.GetValues(typeof(T7)))
  50. {
  51. yield return
  52. new ValueTuple<T1, T2, T3, T4, T5, T6, T7> { Item1 = v1, Item2 = v2, Item3 = v3, Item4 = v4, Item5 = v5, Item6 = v6, Item7 = v7 };
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. public class SetterTuple<T1, T2, T3, T4, T5, T6, T7>
  62. {
  63. private readonly Action<MyBO, ISession, T1> _set1;
  64. private readonly Action<MyBO, ISession, T2> _set2;
  65. private readonly Action<MyBO, ISession, T3> _set3;
  66. private readonly Action<MyBO, ISession, T4> _set4;
  67. private readonly Action<MyBO, ISession, T5> _set5;
  68. private readonly Action<MyBO, ISession, T6> _set6;
  69. private readonly Action<MyBO, ISession, T7> _set7;
  70. public SetterTuple(Action<MyBO, ISession, T1> set1,
  71. Action<MyBO, ISession, T2> set2,
  72. Action<MyBO, ISession, T3> set3,
  73. Action<MyBO, ISession, T4> set4,
  74. Action<MyBO, ISession, T5> set5,
  75. Action<MyBO, ISession, T6> set6,
  76. Action<MyBO, ISession, T7> set7)
  77. {
  78. _set1 = set1;
  79. _set2 = set2;
  80. _set3 = set3;
  81. _set4 = set4;
  82. _set5 = set5;
  83. _set6 = set6;
  84. _set7 = set7;
  85. }
  86. public void Set(MyBO bo, ISession s, T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
  87. {
  88. if (_set1 != null) { _set1(bo, s, item1); }
  89. if (_set2 != null) { _set2(bo, s, item2); }
  90. if (_set3 != null) { _set3(bo, s, item3); }
  91. if (_set4 != null) { _set4(bo, s, item4); }
  92. if (_set5 != null) { _set5(bo, s, item5); }
  93. if (_set6 != null) { _set6(bo, s, item6); }
  94. if (_set7 != null) { _set7(bo, s, item7); }
  95. }
  96. }
  97. protected int RunTest<T1, T2, T3, T4, T5, T6, T7>(Expression<Func<MyBO, bool>> condition, SetterTuple<T1, T2, T3, T4, T5, T6, T7> setters)
  98. {
  99. if (condition == null)
  100. {
  101. throw new ArgumentNullException("condition");
  102. }
  103. if (setters == null)
  104. {
  105. throw new ArgumentNullException("setters");
  106. }
  107. IEnumerable<int> expectedIds;
  108. // Setup
  109. using (var session = OpenSession())
  110. {
  111. expectedIds = CreateObjects(session, setters, condition.Compile());
  112. }
  113. try
  114. {
  115. // Test
  116. using (var session = OpenSession())
  117. {
  118. session.CacheMode = CacheMode.Ignore;
  119. session.DefaultReadOnly = true;
  120. using (session.BeginTransaction())
  121. {
  122. return TestAndAssert(condition, session, expectedIds);
  123. }
  124. }
  125. }
  126. finally
  127. {
  128. // Teardown
  129. using (var session = OpenSession())
  130. {
  131. using (var tx = session.BeginTransaction())
  132. {
  133. DeleteAll<MyBO>(session);
  134. DeleteAll<MyRef1>(session);
  135. DeleteAll<MyRef2>(session);
  136. DeleteAll<MyRef3>(session);
  137. tx.Commit();
  138. }
  139. }
  140. }
  141. }
  142. protected abstract int TestAndAssert(Expression<Func<MyBO, bool>> condition, ISession session, IEnumerable<int> expectedIds);
  143. protected static SetterTuple<T1, T2, T3, T4, T5, T6, T7> Setters<T1, T2, T3, T4, T5, T6, T7>(Action<MyBO, ISession, T1> set1,
  144. Action<MyBO, ISession, T2> set2,
  145. Action<MyBO, ISession, T3> set3,
  146. Action<MyBO, ISession, T4> set4,
  147. Action<MyBO, ISession, T5> set5,
  148. Action<MyBO, ISession, T6> set6,
  149. Action<MyBO, ISession, T7> set7)
  150. {
  151. return new SetterTuple<T1, T2, T3, T4, T5, T6, T7>(set1, set2, set3, set4, set5, set6, set7);
  152. }
  153. protected static SetterTuple<T1, T2, T3, T4, T5, T6, Ignore> Setters<T1, T2, T3, T4, T5, T6>(Action<MyBO, ISession, T1> set1,
  154. Action<MyBO, ISession, T2> set2,
  155. Action<MyBO, ISession, T3> set3,
  156. Action<MyBO, ISession, T4> set4,
  157. Action<MyBO, ISession, T5> set5,
  158. Action<MyBO, ISession, T6> set6)
  159. {
  160. return new SetterTuple<T1, T2, T3, T4, T5, T6, Ignore>(set1, set2, set3, set4, set5, set6, null);
  161. }
  162. protected static SetterTuple<T1, T2, T3, T4, T5, Ignore, Ignore> Setters<T1, T2, T3, T4, T5>(Action<MyBO, ISession, T1> set1,
  163. Action<MyBO, ISession, T2> set2,
  164. Action<MyBO, ISession, T3> set3,
  165. Action<MyBO, ISession, T4> set4,
  166. Action<MyBO, ISession, T5> set5)
  167. {
  168. return new SetterTuple<T1, T2, T3, T4, T5, Ignore, Ignore>(set1, set2, set3, set4, set5, null, null);
  169. }
  170. protected static SetterTuple<T1, T2, T3, T4, Ignore, Ignore, Ignore> Setters<T1, T2, T3, T4>(Action<MyBO, ISession, T1> set1,
  171. Action<MyBO, ISession, T2> set2,
  172. Action<MyBO, ISession, T3> set3,
  173. Action<MyBO, ISession, T4> set4)
  174. {
  175. return new SetterTuple<T1, T2, T3, T4, Ignore, Ignore, Ignore>(set1, set2, set3, set4, null, null, null);
  176. }
  177. protected static SetterTuple<T1, T2, T3, Ignore, Ignore, Ignore, Ignore> Setters<T1, T2, T3>(Action<MyBO, ISession, T1> set1,
  178. Action<MyBO, ISession, T2> set2,
  179. Action<MyBO, ISession, T3> set3)
  180. {
  181. return new SetterTuple<T1, T2, T3, Ignore, Ignore, Ignore, Ignore>(set1, set2, set3,null, null, null, null);
  182. }
  183. protected static SetterTuple<T1, T2, Ignore, Ignore, Ignore, Ignore, Ignore> Setters<T1, T2>(Action<MyBO, ISession, T1> set1,
  184. Action<MyBO, ISession, T2> set2)
  185. {
  186. return new SetterTuple<T1, T2, Ignore, Ignore, Ignore, Ignore, Ignore>(set1, set2, null, null, null, null, null);
  187. }
  188. protected static SetterTuple<T1, Ignore, Ignore, Ignore, Ignore, Ignore, Ignore> Setters<T1>(Action<MyBO, ISession, T1> set1)
  189. {
  190. return new SetterTuple<T1, Ignore, Ignore, Ignore, Ignore, Ignore, Ignore>(set1, null, null, null, null, null, null);
  191. }
  192. private static void DeleteAll<T>(ISession session)
  193. {
  194. session.CreateQuery("delete from " + typeof(T).Name).ExecuteUpdate();
  195. }
  196. private static IEnumerable<int> CreateObjects<T1, T2, T3, T4, T5, T6, T7>(ISession session, SetterTuple<T1, T2, T3, T4, T5, T6, T7> setters, Func<MyBO, bool> condition)
  197. {
  198. var expectedIds = new List<int>();
  199. bool thereAreSomeWithTrue = false;
  200. bool thereAreSomeWithFalse = false;
  201. var allTestCases = GetAllTestCases<T1, T2, T3, T4, T5, T6, T7>().ToList();
  202. var i = 0;
  203. foreach (var q in allTestCases)
  204. {
  205. MyBO bo = new MyBO();
  206. setters.Set(bo, session, q.Item1, q.Item2, q.Item3, q.Item4, q.Item5, q.Item6, q.Item7);
  207. try
  208. {
  209. if (condition(bo))
  210. {
  211. expectedIds.Add(bo.Id);
  212. thereAreSomeWithTrue = true;
  213. }
  214. else
  215. {
  216. thereAreSomeWithFalse = true;
  217. }
  218. if ((i%BatchSize) == 0)
  219. {
  220. if (session.Transaction.IsActive)
  221. {
  222. session.Transaction.Commit();
  223. session.Clear();
  224. }
  225. session.BeginTransaction();
  226. }
  227. session.Save(bo);
  228. i++;
  229. }
  230. catch (NullReferenceException)
  231. {
  232. // ignore - we only check consistency with Linq2Objects in non-failing cases;
  233. // emulating the outer-join logic for exceptional cases in Lin2Objects is IMO very hard.
  234. }
  235. }
  236. if (session.Transaction.IsActive)
  237. {
  238. session.Transaction.Commit();
  239. session.Clear();
  240. }
  241. Console.WriteLine("Congratulation!! you have saved "+ i +" entities.");
  242. if (!thereAreSomeWithTrue)
  243. {
  244. throw new ArgumentException("Condition is false for all - not a good test", "condition");
  245. }
  246. if (!thereAreSomeWithFalse)
  247. {
  248. throw new ArgumentException("Condition is true for all - not a good test", "condition");
  249. }
  250. return expectedIds;
  251. }
  252. protected static void AreEqual(IEnumerable<int> expectedIds, IEnumerable<int> actualList)
  253. {
  254. Assert.That(() => actualList.ToList(), Is.EquivalentTo(expectedIds));
  255. }
  256. }
  257. }