PageRenderTime 116ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/net35/Test.Radical/ChangeTracking/Service/CollectionChangeTrackingServiceTests.cs

#
C# | 536 lines | 404 code | 131 blank | 1 comment | 0 complexity | 8b72c0233bb4512f5c034f308449c6f4 MD5 | raw file
  1. //extern alias tpx;
  2. namespace Test.Radical.ChangeTracking
  3. {
  4. using System;
  5. using System.Linq;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using SharpTestsEx;
  10. using Topics.Radical.ComponentModel.ChangeTracking;
  11. using Topics.Radical.ChangeTracking;
  12. using Topics.Radical.Linq;
  13. [TestClass]
  14. public class CollectionChangeTrackingServiceTests
  15. {
  16. [TestMethod]
  17. [TestCategory( "ChangeTracking" )]
  18. public void after_Add_canUndo_is_true()
  19. {
  20. IChangeTrackingService svc = new ChangeTrackingService();
  21. PersonCollection p = new PersonCollection( svc );
  22. p.Add( new Person( svc, false ) );
  23. svc.CanUndo.Should().Be.True();
  24. }
  25. [TestMethod]
  26. [TestCategory( "ChangeTracking" )]
  27. public void after_Undo_collection_is_rolledback()
  28. {
  29. Int32 expected = 0;
  30. IChangeTrackingService svc = new ChangeTrackingService();
  31. PersonCollection p = new PersonCollection( svc );
  32. p.Add( new Person( svc, false ) );
  33. svc.Undo();
  34. p.Count.Should().Be.EqualTo( expected );
  35. }
  36. [TestMethod]
  37. [TestCategory( "ChangeTracking" )]
  38. public void after_Add_getChangeSet_contains_change()
  39. {
  40. IChangeTrackingService svc = new ChangeTrackingService();
  41. PersonCollection p = new PersonCollection( svc );
  42. p.Add( new Person( svc, false ) );
  43. p.Add( new Person( svc, false ) );
  44. IChangeSet cSet = svc.GetChangeSet();
  45. cSet.Count.Should().Be.EqualTo( 2 );
  46. }
  47. [TestMethod]
  48. [TestCategory( "ChangeTracking" )]
  49. public void after_add_GetEntityState_is_Changed()
  50. {
  51. EntityTrackingStates expected = EntityTrackingStates.HasBackwardChanges;
  52. ChangeTrackingService svc = new ChangeTrackingService();
  53. PersonCollection p = new PersonCollection( svc );
  54. p.Add( new Person( svc, false ) );
  55. p.Add( new Person( svc, false ) );
  56. EntityTrackingStates actual = svc.GetEntityState( p );
  57. actual.Should().Be.EqualTo( expected );
  58. }
  59. [TestMethod]
  60. [TestCategory( "ChangeTracking" )]
  61. public void after_mutual_exclusive_actions_service_CanUndo()
  62. {
  63. IChangeTrackingService svc = new ChangeTrackingService();
  64. PersonCollection p = new PersonCollection( svc );
  65. p.Add( new Person( svc, false ) );
  66. p.RemoveAt( 0 );
  67. Assert.IsTrue( svc.CanUndo );
  68. }
  69. [TestMethod]
  70. [TestCategory( "ChangeTracking" )]
  71. public void after_mutual_exclusive_actions_entity_state_is_HasBackwardChanges()
  72. {
  73. EntityTrackingStates expected = EntityTrackingStates.HasBackwardChanges;
  74. IChangeTrackingService svc = new ChangeTrackingService();
  75. PersonCollection p = new PersonCollection( svc );
  76. p.Add( new Person( svc, false ) );
  77. p.RemoveAt( 0 );
  78. EntityTrackingStates actual = svc.GetEntityState( p );
  79. Assert.AreEqual<EntityTrackingStates>( expected, actual );
  80. }
  81. [TestMethod]
  82. [TestCategory( "ChangeTracking" )]
  83. public void unchangedEntity_service_canRedo_is_false()
  84. {
  85. IChangeTrackingService svc = new ChangeTrackingService();
  86. PersonCollection p = new PersonCollection( svc );
  87. Assert.IsFalse( svc.CanRedo );
  88. }
  89. [TestMethod]
  90. [TestCategory( "ChangeTracking" )]
  91. public void unchangedEntity_service_canUndo_is_false()
  92. {
  93. IChangeTrackingService svc = new ChangeTrackingService();
  94. PersonCollection p = new PersonCollection( svc );
  95. Assert.IsFalse( svc.CanUndo );
  96. }
  97. [TestMethod]
  98. [TestCategory( "ChangeTracking" )]
  99. public void unchangedEntity_service_isChanged_is_false()
  100. {
  101. IChangeTrackingService svc = new ChangeTrackingService();
  102. PersonCollection p = new PersonCollection( svc );
  103. Assert.IsFalse( svc.IsChanged );
  104. }
  105. [TestMethod]
  106. [TestCategory( "ChangeTracking" )]
  107. public void unchangedEntity_service_getEntityState_is_None()
  108. {
  109. EntityTrackingStates expected = EntityTrackingStates.None;
  110. ChangeTrackingService svc = new ChangeTrackingService();
  111. PersonCollection p = new PersonCollection( svc );
  112. EntityTrackingStates actual = svc.GetEntityState( p );
  113. Assert.AreEqual<EntityTrackingStates>( expected, actual );
  114. }
  115. [TestMethod]
  116. [TestCategory( "ChangeTracking" )]
  117. public void afterUndo_service_canRedo()
  118. {
  119. IChangeTrackingService svc = new ChangeTrackingService();
  120. PersonCollection p = new PersonCollection( svc );
  121. p.Add( new Person( null, false ) );
  122. svc.Undo();
  123. Assert.IsTrue( svc.CanRedo );
  124. }
  125. [TestMethod]
  126. [TestCategory( "ChangeTracking" )]
  127. public void after_change_canRedo_is_false()
  128. {
  129. IChangeTrackingService svc = new ChangeTrackingService();
  130. PersonCollection p = new PersonCollection( svc );
  131. p.Add( new Person( null, false ) );
  132. Assert.IsFalse( svc.CanRedo );
  133. }
  134. [TestMethod]
  135. [TestCategory( "ChangeTracking" )]
  136. public void after_multiple_change_canRedo_is_false()
  137. {
  138. IChangeTrackingService svc = new ChangeTrackingService();
  139. PersonCollection p = new PersonCollection( svc );
  140. p.Add( new Person( null, false ) );
  141. svc.Undo();
  142. p.Add( new Person( null, false ) );
  143. Assert.IsFalse( svc.CanRedo );
  144. }
  145. [TestMethod]
  146. [TestCategory( "ChangeTracking" )]
  147. public void afterUndo_service_Redo_restore_previous_value()
  148. {
  149. ChangeTrackingService svc = new ChangeTrackingService();
  150. Int32 expected = 1;
  151. PersonCollection p = new PersonCollection( svc );
  152. p.Add( new Person( null, false ) );
  153. svc.Undo();
  154. svc.Redo();
  155. Assert.AreEqual<Int32>( expected, p.Count );
  156. }
  157. [TestMethod]
  158. [TestCategory( "ChangeTracking" )]
  159. public void after_acceptChanges_entity_is_no_more_changed()
  160. {
  161. EntityTrackingStates expected = EntityTrackingStates.None;
  162. ChangeTrackingService svc = new ChangeTrackingService();
  163. PersonCollection p = new PersonCollection( svc );
  164. p.Add( new Person( null, false ) );
  165. svc.AcceptChanges();
  166. EntityTrackingStates actual = svc.GetEntityState( p );
  167. Assert.AreEqual<EntityTrackingStates>( expected, actual );
  168. }
  169. [TestMethod]
  170. [TestCategory( "ChangeTracking" )]
  171. public void after_acceptChanges_service_isChanged_is_false()
  172. {
  173. ChangeTrackingService svc = new ChangeTrackingService();
  174. PersonCollection p = new PersonCollection( svc );
  175. p.Add( new Person( null, false ) );
  176. svc.AcceptChanges();
  177. Assert.IsFalse( svc.IsChanged );
  178. }
  179. [TestMethod]
  180. [TestCategory( "ChangeTracking" )]
  181. public void after_acceptChanges_service_canUndo_is_false()
  182. {
  183. ChangeTrackingService svc = new ChangeTrackingService();
  184. PersonCollection p = new PersonCollection( svc );
  185. p.Add( new Person( null, false ) );
  186. svc.AcceptChanges();
  187. Assert.IsFalse( svc.CanUndo );
  188. }
  189. [TestMethod]
  190. [TestCategory( "ChangeTracking" )]
  191. public void after_acceptChanges_service_canRedo_is_false()
  192. {
  193. ChangeTrackingService svc = new ChangeTrackingService();
  194. PersonCollection p = new PersonCollection( svc );
  195. p.Add( new Person( null, false ) );
  196. svc.AcceptChanges();
  197. Assert.IsFalse( svc.CanRedo );
  198. }
  199. [TestMethod]
  200. [TestCategory( "ChangeTracking" )]
  201. public void after_rejectChanges_entity_is_no_more_changed()
  202. {
  203. EntityTrackingStates expected = EntityTrackingStates.None;
  204. ChangeTrackingService svc = new ChangeTrackingService();
  205. PersonCollection p = new PersonCollection( svc );
  206. p.Add( new Person( null, false ) );
  207. svc.RejectChanges();
  208. EntityTrackingStates actual = svc.GetEntityState( p );
  209. Assert.AreEqual<EntityTrackingStates>( expected, actual );
  210. }
  211. [TestMethod]
  212. [TestCategory( "ChangeTracking" )]
  213. public void after_rejectChanges_service_isChanged_is_false()
  214. {
  215. IChangeTrackingService svc = new ChangeTrackingService();
  216. PersonCollection p = new PersonCollection( svc );
  217. p.Add( new Person( null, false ) );
  218. svc.RejectChanges();
  219. Assert.IsFalse( svc.IsChanged );
  220. }
  221. [TestMethod]
  222. [TestCategory( "ChangeTracking" )]
  223. public void after_rejectChanges_service_canUndo_is_false()
  224. {
  225. IChangeTrackingService svc = new ChangeTrackingService();
  226. PersonCollection p = new PersonCollection( svc );
  227. p.Add( new Person( null, false ) );
  228. svc.RejectChanges();
  229. Assert.IsFalse( svc.CanUndo );
  230. }
  231. [TestMethod]
  232. [TestCategory( "ChangeTracking" )]
  233. public void after_rejectChanges_service_canRedo_is_false()
  234. {
  235. IChangeTrackingService svc = new ChangeTrackingService();
  236. PersonCollection p = new PersonCollection( svc );
  237. p.Add( new Person( null, false ) );
  238. svc.RejectChanges();
  239. Assert.IsFalse( svc.CanRedo );
  240. }
  241. [TestMethod]
  242. [TestCategory( "ChangeTracking" )]
  243. public void service_on_single_add_generate_valid_advisory()
  244. {
  245. ChangeTrackingService svc = new ChangeTrackingService();
  246. PersonCollection p = new PersonCollection( svc );
  247. p.Add( new Person( null, false ) );
  248. IEnumerable<IAdvisedAction> advisory = svc.GetAdvisory();
  249. Assert.IsNotNull( advisory );
  250. Assert.AreEqual<Int32>( 1, advisory.Count() );
  251. Assert.AreEqual<ProposedActions>( ProposedActions.Update, advisory.First().Action );
  252. }
  253. [TestMethod]
  254. [TestCategory( "ChangeTracking" )]
  255. public void service_on_single_add_generate_valid_advisory_even_with_transient_persistable_entity()
  256. {
  257. ChangeTrackingService svc = new ChangeTrackingService();
  258. PersonCollection p = new PersonCollection( svc );
  259. p.Add( new Person( null, ChangeTrackingRegistration.AsTransient, TransientRegistration.AsPersistable ) );
  260. IEnumerable<IAdvisedAction> advisory = svc.GetAdvisory();
  261. Assert.IsNotNull( advisory );
  262. Assert.AreEqual<Int32>( 1, advisory.Count() );
  263. Assert.AreEqual<ProposedActions>( ProposedActions.Create, advisory.First().Action );
  264. }
  265. [TestMethod]
  266. [TestCategory( "ChangeTracking" )]
  267. public void service_on_clear_generate_valid_advisory()
  268. {
  269. ChangeTrackingService svc = new ChangeTrackingService();
  270. PersonCollection p = new PersonCollection( svc );
  271. p.Add( new Person( null, false ) );
  272. p.Add( new Person( null, false ) );
  273. p.Add( new Person( null, false ) );
  274. p.Add( new Person( null, false ) );
  275. p.Add( new Person( null, false ) );
  276. svc.AcceptChanges();
  277. p.Clear();
  278. IEnumerable<IAdvisedAction> advisory = svc.GetAdvisory();
  279. Assert.IsNotNull( advisory );
  280. Assert.AreEqual<Int32>( 5, advisory.Count() );
  281. foreach( var aa in advisory )
  282. {
  283. Assert.AreEqual<ProposedActions>( ProposedActions.Delete, aa.Action );
  284. }
  285. }
  286. [TestMethod]
  287. [TestCategory( "ChangeTracking" )]
  288. public void service_getChangeSet_returns_valid_cSet()
  289. {
  290. Int32 expected = 3;
  291. Int32 actual = 0;
  292. ChangeTrackingService svc = new ChangeTrackingService();
  293. PersonCollection p = new PersonCollection( svc );
  294. p.Add( new Person( null, false ) );
  295. p.Add( new Person( null, false ) );
  296. p.Move( 0, 1 );
  297. IChangeSet cSet = svc.GetChangeSet();
  298. actual = cSet.Count;
  299. Assert.AreEqual<Int32>( expected, actual );
  300. }
  301. [TestMethod]
  302. [TestCategory( "ChangeTracking" )]
  303. public void service_on_clear_undo_entities_are_restored()
  304. {
  305. var source = new Person[]
  306. {
  307. new Person( null, false ),
  308. new Person( null, false ),
  309. new Person( null, false ),
  310. new Person( null, false ),
  311. new Person( null, false )
  312. };
  313. ChangeTrackingService svc = new ChangeTrackingService();
  314. PersonCollection list = new PersonCollection( svc );
  315. list.BeginInit();
  316. list.AddRange( source );
  317. list.EndInit();
  318. list.Clear();
  319. svc.Undo();
  320. Assert.AreEqual<Int32>( source.Length, list.Count() );
  321. source.ForEach( p =>
  322. {
  323. Int32 expected = Array.IndexOf( source, p );
  324. Int32 actual = list.IndexOf( p );
  325. Assert.AreEqual<Int32>( expected, actual );
  326. } );
  327. }
  328. [TestMethod]
  329. [TestCategory( "ChangeTracking" )]
  330. public void service_undoes_are_in_the_correct_order()
  331. {
  332. var p1 = new Person( null, false );
  333. var p2 = new Person( null, false );
  334. var p3 = new Person( null, false );
  335. ChangeTrackingService svc = new ChangeTrackingService();
  336. PersonCollection list = new PersonCollection( svc );
  337. list.Add( p1 );
  338. list.Add( p2 );
  339. list.Add( p3 );
  340. list.Move( p2, 0 );
  341. list.Remove( p1 );
  342. svc.Undo();
  343. Assert.AreEqual<Int32>( 3, list.Count );
  344. Assert.IsTrue( list.Contains( p1 ) );
  345. svc.Undo();
  346. Assert.AreEqual<Int32>( 1, list.IndexOf( p2 ) );
  347. svc.Undo();
  348. Assert.AreEqual<Int32>( 2, list.Count );
  349. Assert.IsFalse( list.Contains( p3 ) );
  350. svc.Undo();
  351. Assert.AreEqual<Int32>( 1, list.Count );
  352. Assert.IsFalse( list.Contains( p2 ) );
  353. svc.Undo();
  354. Assert.AreEqual<Int32>( 0, list.Count );
  355. Assert.IsFalse( list.Contains( p1 ) );
  356. }
  357. [TestMethod]
  358. [TestCategory( "ChangeTracking" )]
  359. public void service_redoes_are_in_the_correct_order()
  360. {
  361. var p1 = new Person( null, false );
  362. var p2 = new Person( null, false );
  363. var p3 = new Person( null, false );
  364. ChangeTrackingService svc = new ChangeTrackingService();
  365. PersonCollection list = new PersonCollection( svc );
  366. list.Add( p1 );
  367. list.Add( p2 );
  368. list.Add( p3 );
  369. list.Move( p2, 0 );
  370. list.Remove( p1 );
  371. while( svc.CanUndo )
  372. {
  373. svc.Undo();
  374. }
  375. svc.Redo();
  376. Assert.AreEqual<Int32>( 1, list.Count );
  377. Assert.IsTrue( list.Contains( p1 ) );
  378. svc.Redo();
  379. Assert.AreEqual<Int32>( 2, list.Count );
  380. Assert.IsTrue( list.Contains( p2 ) );
  381. svc.Redo();
  382. Assert.AreEqual<Int32>( 3, list.Count );
  383. Assert.IsTrue( list.Contains( p3 ) );
  384. svc.Redo();
  385. Assert.AreEqual<Int32>( 0, list.IndexOf( p2 ) );
  386. svc.Redo();
  387. Assert.AreEqual<Int32>( 2, list.Count );
  388. Assert.IsFalse( list.Contains( p1 ) );
  389. }
  390. [TestMethod]
  391. [TestCategory( "ChangeTracking" )]
  392. public void service_getAdvisory_generate_valid_advisory_with_more_changes_applied_to_the_same_entity()
  393. {
  394. ProposedActions expected = ProposedActions.Delete;
  395. ChangeTrackingService svc = new ChangeTrackingService();
  396. PersonCollection list = new PersonCollection( svc );
  397. list.Add( new Person( null, false ) ); //First IChange
  398. list.RemoveAt( 0 ); //Second IChange
  399. IAdvisory advisory = svc.GetAdvisory();
  400. IAdvisedAction action = advisory.First();
  401. ProposedActions actual = action.Action;
  402. Assert.AreEqual<ProposedActions>( expected, actual );
  403. }
  404. }
  405. }