Ejemplo n.º 1
0
        public void FileContentIsntDeletedIfTransactionWasRolledBack()
        {
            var folder = new Folder
            {
                Name = "Test folder"
            };
            Repository.Data.Save(folder);

            var file = new File
            {
                //Folder = folder,
                DisplayName = "Test file",
                Name = "TestFile.txt",
                ContentType = FileType.txt
            };
            Stream stream = new MemoryStream();
            byte[] buffer = Guid.NewGuid().ToByteArray();
            stream.Write(buffer, 0, buffer.Length);
            file.FileStream = new Lazy<Stream>(() => stream);
            Repository.Data.Save(file);

            using (var unitOfWork = new UnitOfWork())
            {
                Repository.Data.Delete(file);

                unitOfWork.Rollback();
            }

            string path = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, file.StoragePath);
            Assert.True(System.IO.File.Exists(path));

            Repository.Data.Delete(file);
        }
Ejemplo n.º 2
0
        public void NestedUnitsCombinatedTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};
            var entity3 = new SimpleEntity
                          	{
                          		Name = "UnitTest3"
                          	};
            var entity4 = new SimpleEntity
                          	{
                          		Name = "UnitTest4"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);

                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    using (var unit3 = new UnitOfWork())
                    {
                        Repository.Data.Save(entity3);
                        unit3.Commit();
                    }
                    using (var unit4 = new UnitOfWork(UnitOfWorkScopeType.New))
                    {
                        Repository.Data.Save(entity4);
                        unit4.Commit();
                    }
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }

                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            entity3 = Repository.Data.Get<SimpleEntity>(entity3.Id);
            entity4 = Repository.Data.Get<SimpleEntity>(entity4.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
            Assert.Null(entity3);
            Assert.NotNull(entity4);
        }
Ejemplo n.º 3
0
        public void UnitOfWorkRollbackTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(entity);
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
        }
Ejemplo n.º 4
0
        public void NestedUnitsWithInnerScopeRollbackAndCommitActionTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                UnitOfWork.Current.PostCommitActions.Add(() => { });
                UnitOfWork.Current.PostRollbackActions.Add(() => { });
                Repository.Data.Save(entity);
                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);

                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    UnitOfWork.Current.PostCommitActions.Add(() => { });
                    UnitOfWork.Current.PostRollbackActions.Add(() => { });
                    Repository.Data.Save(entity2);
                    unit2.Rollback();
                }

                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);
                unit.Commit();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.NotNull(entity);
            Assert.Null(entity2);
        }
Ejemplo n.º 5
0
        public void UnitOfWorkRollBackAfterRollbackTest()
        {
            var entity = new SimpleEntity
            {
                Name = "UnitTest"
            };
            var entity2 = new SimpleEntity
            {
                Name = "UnitTest2"
            };

            using (var unit = new UnitOfWork())
            {
                //Some external method
                Repository.Data.Save(entity);
                UnitOfWork.Current.Rollback();

                Assert.Throws<UnitOfWorkException>(() => Repository.Data.Save(entity2));
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(entity);
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity2);
        }
Ejemplo n.º 6
0
        public void ScopeWithoutException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                unit.Rollback(); // will not throw exception
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();
            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
        }
Ejemplo n.º 7
0
        public void NestedUnitsWithSameScopeRollbackBothTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Ejemplo n.º 8
0
        public void NestedUnitsWithSameScopeRollbackAndCommitActionTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                int onCommitActionsCount;
                int onRollBackActionsCount;

                UnitOfWork.Current.PostCommitActions.Add(() => { });
                UnitOfWork.Current.PostRollbackActions.Add(() => { });
                Repository.Data.Save(entity);
                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);

                using (var unit2 = new UnitOfWork())
                {
                    UnitOfWork.Current.PostCommitActions.Add(() => { });
                    UnitOfWork.Current.PostRollbackActions.Add(() => { });
                    onCommitActionsCount = UnitOfWork.Current.PostCommitActions.Count;
                    onRollBackActionsCount = UnitOfWork.Current.PostRollbackActions.Count;
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }

                Assert.Equal(2, onCommitActionsCount);
                Assert.Equal(2, onRollBackActionsCount);
                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Ejemplo n.º 9
0
        public void NestedUnitsWithSameScopeInnerException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                        // will throw exception because this is not the owner of the UOW
                }

                // user will not come to this point and try to commit transaction
                // else it should throw exception
                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();
            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Ejemplo n.º 10
0
        public void NestedUnitsWithNewInnerScopeRollbackAndCommitTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    Repository.Data.Save(entity2);
                    unit2.Rollback();
                }
                unit.Commit();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.NotNull(entity);
            Assert.Null(entity2);
        }
Ejemplo n.º 11
0
        public void NestedUnitsWithInnerScopeWithoutException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    Repository.Data.Save(entity2);
                    unit2.Rollback(); // will not throw exception because this is own transaction
                }
                // user can commit the outer UOW
                unit.Commit();
            }
        }