Example #1
0
        public void SetValue_WithObjectList_NewCollectionAlreadyAssociated()
        {
            var sector1 = IndustrialSector.NewObject();
            var sector2 = IndustrialSector.NewObject();

            CreateAccessor(sector1, "Companies").SetValue(sector2.Companies);
        }
Example #2
0
        public override void SetUp()
        {
            base.SetUp();

            _transaction      = ClientTransaction.CreateRootTransaction();
            _industrialSector = _transaction.ExecuteInScope(() => IndustrialSector.NewObject());
            _order            = _transaction.ExecuteInScope(() => Order.NewObject());
        }
        public override void SetUp()
        {
            base.SetUp();

            _newIndustrialSector = IndustrialSector.NewObject();
            _newCompany1         = Company.NewObject();
            _newCompany2         = Company.NewObject();
        }
        public void VirtualEndPointQuery_OneMany_ObjectIncluded_ThatLocallyPointsToSomewhereElse()
        {
            SetDatabaseModifyable();

            var company = CreateCompanyInDatabaseAndLoad();

            Assert.That(company.IndustrialSector, Is.Null);

            var industrialSector = DomainObjectIDs.IndustrialSector1.GetObject <IndustrialSector> (); // virtual end point not yet resolved

            SetIndustrialSectorInOtherTransaction(company.ID, industrialSector.ID);

            // Resolve virtual end point - the database says that company points to industrialSector, but the transaction says it points to null!
            var companiesOfIndustrialSector = industrialSector.Companies;

            companiesOfIndustrialSector.EnsureDataComplete();

            Assert.That(company.IndustrialSector, Is.Null);
            Assert.That(companiesOfIndustrialSector, Has.Member(company));

            CheckSyncState(company, c => c.IndustrialSector, true);
            CheckSyncState(industrialSector, s => s.Companies, false);

            var otherCompany = companiesOfIndustrialSector.FirstOrDefault(c => c != company);

            CheckSyncState(otherCompany, c => c.IndustrialSector, true);

            CheckActionWorks(company.Delete);
            TestableClientTransaction.Rollback(); // required so that the remaining actions can be tried below

            // sync states not changed by Rollback
            CheckSyncState(company, c => c.IndustrialSector, true);
            CheckSyncState(industrialSector, s => s.Companies, false);

            CheckActionWorks(() => industrialSector.Companies.Remove(otherCompany));
            CheckActionWorks(() => industrialSector.Companies.Add(Company.NewObject()));

            var companyIndex = industrialSector.Companies.IndexOf(company);

            CheckActionThrows <InvalidOperationException> (industrialSector.Delete, "out of sync with the opposite object property");
            CheckActionThrows <InvalidOperationException> (() => industrialSector.Companies.Remove(company), "out of sync with the opposite object property");
            CheckActionThrows <InvalidOperationException> (() => industrialSector.Companies[companyIndex] = Company.NewObject(), "out of sync with the opposite object property");
            CheckActionThrows <InvalidOperationException> (() => industrialSector.Companies = new ObjectList <Company> (), "out of sync with the opposite object property");
            CheckActionThrows <InvalidOperationException> (industrialSector.Delete, "out of sync with the opposite object property");

            CheckActionWorks(() => company.IndustrialSector = IndustrialSector.NewObject());

            BidirectionalRelationSyncService.Synchronize(ClientTransaction.Current, RelationEndPointID.Resolve(industrialSector, s => s.Companies));

            CheckSyncState(industrialSector, s => s.Companies, true);
            Assert.That(companiesOfIndustrialSector, Has.No.Member(company));

            CheckActionWorks(() => industrialSector.Companies.Add(company));
        }
Example #5
0
        public void SetValue_WithObjectList()
        {
            IndustrialSector sector = IndustrialSector.NewObject();
            var newCompanies        = new ObjectList <Company> ();
            var oldCompanies        = sector.Companies;

            CreateAccessor(sector, "Companies").SetValue(newCompanies);

            Assert.That(sector.Companies, Is.SameAs(newCompanies));
            Assert.That(sector.Companies.AssociatedEndPointID, Is.Not.Null);
            Assert.That(oldCompanies.AssociatedEndPointID, Is.Null);
        }
Example #6
0
        public void SetValue_WithObjectList_PerformsBidirectionalChange()
        {
            var sector  = IndustrialSector.NewObject();
            var company = Company.NewObject();

            var newCompanies = new ObjectList <Company> {
                company
            };

            CreateAccessor(sector, "Companies").SetValue(newCompanies);

            Assert.That(company.IndustrialSector, Is.SameAs(sector));
        }
        public void VirtualEndPointQuery_OneMany_ObjectIncluded_ThatLocallyPointsToSomewhereElse()
        {
            Company          company;
            IndustrialSector industrialSector; // virtual end point not yet resolved

            using (ClientTransaction.Current.CreateSubTransaction().EnterDiscardingScope())
            {
                PrepareInconsistentState_OneMany_ObjectIncluded(out company, out industrialSector);

                CheckSyncState(company, c => c.IndustrialSector, true);
                CheckSyncState(industrialSector, s => s.Companies, false);

                var otherCompany = industrialSector.Companies.FirstOrDefault(c => c != company);
                CheckSyncState(otherCompany, c => c.IndustrialSector, true);

                CheckActionWorks(company.Delete);
                ClientTransaction.Current.Rollback(); // required so that the remaining actions can be tried below

                // sync states not changed by Rollback
                CheckSyncState(company, c => c.IndustrialSector, true);
                CheckSyncState(industrialSector, s => s.Companies, false);

                CheckActionWorks(() => industrialSector.Companies.Remove(otherCompany));
                CheckActionWorks(() => industrialSector.Companies.Add(Company.NewObject()));

                var companyIndex = industrialSector.Companies.IndexOf(company);
                CheckActionThrows <InvalidOperationException> (
                    () => industrialSector.Companies.Remove(company), "out of sync with the opposite object property");
                CheckActionThrows <InvalidOperationException> (
                    () => industrialSector.Companies[companyIndex] = Company.NewObject(), "out of sync with the opposite object property");
                CheckActionThrows <InvalidOperationException> (
                    () => industrialSector.Companies = new ObjectList <Company>(), "out of sync with the opposite object property");
                CheckActionThrows <InvalidOperationException> (industrialSector.Delete, "out of sync with the opposite object property");

                CheckActionWorks(() => company.IndustrialSector = IndustrialSector.NewObject());

                BidirectionalRelationSyncService.Synchronize(ClientTransaction.Current, RelationEndPointID.Resolve(industrialSector, s => s.Companies));

                CheckSyncState(industrialSector, s => s.Companies, true);
                Assert.That(industrialSector.Companies, Has.No.Member(company));

                CheckActionWorks(() => industrialSector.Companies.Add(company));
            }

            CheckSyncState(company, c => c.IndustrialSector, true);
            CheckSyncState(industrialSector, s => s.Companies, true);

            Assert.That(company.IndustrialSector, Is.Null);
            Assert.That(industrialSector.Companies, Has.No.Member(company));
        }
        public void MandatoryRelationNotSetExceptionForOneToManyRelation()
        {
            IndustrialSector newIndustrialSector = IndustrialSector.NewObject();

              Assert.That (
              () => TestableClientTransaction.Commit(),
              Throws.TypeOf<MandatoryRelationNotSetException>().With.Message.EqualTo (
              string.Format (
                  "Mandatory relation property 'Remotion.Data.DomainObjects.UnitTests.TestDomain.IndustrialSector.Companies' of domain object '{0}' contains no items.",
                  newIndustrialSector.ID))
              .And.Property<MandatoryRelationNotSetException> (ex => ex.PropertyName).EqualTo (
                  "Remotion.Data.DomainObjects.UnitTests.TestDomain.IndustrialSector.Companies")
              .And.Property<MandatoryRelationNotSetException> (ex => ex.DomainObject).SameAs (newIndustrialSector));
        }
Example #9
0
        public void SetValue_WithObjectList_Notifies()
        {
            var listenerMock = ClientTransactionTestHelperWithMocks.CreateAndAddListenerMock(TestableClientTransaction);

            IndustrialSector sector = IndustrialSector.NewObject();
            var newCompanies        = new ObjectList <Company> {
                Company.NewObject()
            };

            var propertyAccessor = CreateAccessor(sector, "Companies");

            propertyAccessor.SetValue(newCompanies);

            listenerMock.AssertWasCalled(
                mock => mock.RelationChanged(TestableClientTransaction, sector, propertyAccessor.PropertyData.RelationEndPointDefinition, null, newCompanies[0]));
        }
Example #10
0
        protected IndustrialSector CreateIndustrialSectorInDatabaseAndLoad()
        {
            ObjectID objectID;

            using (ClientTransaction.CreateRootTransaction().EnterDiscardingScope())
            {
                IndustrialSector industrialSector = IndustrialSector.NewObject();
                Company          oldCompany       = Company.NewObject();
                oldCompany.Ceo = Ceo.NewObject();
                industrialSector.Companies.Add(oldCompany);
                objectID = industrialSector.ID;

                ClientTransaction.Current.Commit();
            }
            return(objectID.GetObject <IndustrialSector> ());
        }
Example #11
0
        private Order GetDeepTestGraph()
        {
            Order root = Order.NewObject();

            root.Official    = Official.NewObject();
            root.OrderTicket = OrderTicket.NewObject();
            root.OrderItems.Add(OrderItem.NewObject());
            root.OrderItems.Add(OrderItem.NewObject());
            root.Customer     = Customer.NewObject();
            root.Customer.Ceo = Ceo.NewObject();
            root.Customer.IndustrialSector = IndustrialSector.NewObject();
            root.Customer.IndustrialSector.Companies.Add(Company.NewObject());
            root.Customer.IndustrialSector.Companies[1].Ceo = Ceo.NewObject();
            root.Customer.IndustrialSector.Companies.Add(Company.NewObject());
            root.Customer.IndustrialSector.Companies[2].Ceo = Ceo.NewObject();
            return(root);
        }
Example #12
0
        public void Construction()
        {
            var transaction = ClientTransaction.CreateRootTransaction();
            IndustrialSector sector;

            using (transaction.EnterNonDiscardingScope())
            {
                sector = IndustrialSector.NewObject();
            }
            var data = new PropertyAccessorData(sector.ID.ClassDefinition, typeof(IndustrialSector).FullName + ".Name");

            var propertyAccessor = new PropertyAccessor(sector, data, transaction);

            Assert.That(propertyAccessor.DomainObject, Is.SameAs(sector));
            Assert.That(propertyAccessor.PropertyData, Is.SameAs(data));
            Assert.That(propertyAccessor.ClientTransaction, Is.SameAs(transaction));
        }
Example #13
0
        public void GetValue_SetValue()
        {
            IndustrialSector sector = IndustrialSector.NewObject();

            Company company = Company.NewObject();

            company.IndustrialSector = sector;
            Assert.AreSame(sector, company.IndustrialSector, "related object");

            Assert.IsTrue(sector.Companies.ContainsObject(company), "related objects");
            var newCompanies = new ObjectList <Company> ();

            sector.Companies = newCompanies;
            Assert.That(sector.Companies, Is.SameAs(newCompanies));

            sector.Name = "Foo";
            Assert.AreEqual("Foo", sector.Name, "property value");
        }
        public void MandatoryRelationNotSetExceptionForOneToManyRelation()
        {
            var subTransaction = TestableClientTransaction.CreateSubTransaction();

            IndustrialSector newIndustrialSector;

            using (subTransaction.EnterDiscardingScope())
            {
                newIndustrialSector = IndustrialSector.NewObject();
                Assert.That(() => subTransaction.Commit(), Throws.Nothing);
            }

            Assert.That(
                () => TestableClientTransaction.Commit(),
                Throws.TypeOf <MandatoryRelationNotSetException> ().With.Message.EqualTo(
                    string.Format(
                        "Mandatory relation property 'Remotion.Data.DomainObjects.UnitTests.TestDomain.IndustrialSector.Companies' of domain object '{0}' contains no items.",
                        newIndustrialSector.ID)));
        }
Example #15
0
        public void GetValue_SetValue_WithTransaction()
        {
            var transaction = ClientTransaction.CreateRootTransaction();
            IndustrialSector sector;

            using (transaction.EnterNonDiscardingScope())
            {
                sector      = IndustrialSector.NewObject();
                sector.Name = "Foo";
            }
            var data     = new PropertyAccessorData(sector.ID.ClassDefinition, typeof(IndustrialSector).FullName + ".Name");
            var accessor = new PropertyAccessor(sector, data, transaction);

            Assert.That(accessor.GetValue <string> (), Is.EqualTo("Foo"));
            accessor.SetValue("Bar");

            using (transaction.EnterNonDiscardingScope())
            {
                Assert.That(sector.Name, Is.EqualTo("Bar"));
            }
        }
        public void MandatoryRelationNotSetExceptionForOneToManyRelation_WithValidationExtension()
        {
            var subTransaction = TestableClientTransaction.CreateSubTransaction();

            subTransaction.Extensions.Add(new CommitValidationClientTransactionExtension(new MandatoryRelationValidator()));

            using (subTransaction.EnterDiscardingScope())
            {
                IndustrialSector newIndustrialSector = IndustrialSector.NewObject();

                Assert.That(
                    () => subTransaction.Commit(),
                    Throws.TypeOf <MandatoryRelationNotSetException>().With.Message.EqualTo(
                        string.Format(
                            "Mandatory relation property 'Remotion.Data.DomainObjects.UnitTests.TestDomain.IndustrialSector.Companies' of domain object '{0}' contains no items.",
                            newIndustrialSector.ID))
                    .And.Property <MandatoryRelationNotSetException> (ex => ex.PropertyName).EqualTo(
                        "Remotion.Data.DomainObjects.UnitTests.TestDomain.IndustrialSector.Companies")
                    .And.Property <MandatoryRelationNotSetException> (ex => ex.DomainObject).SameAs(newIndustrialSector));
            }
        }
Example #17
0
        public void GetValue_ThrowsIfWrongType()
        {
            IndustrialSector sector = IndustrialSector.NewObject();

            CreateAccessor(sector, "Name").GetValue <int>();
        }
Example #18
0
 public void GetOriginalValueThrowsWithWrongType()
 {
     CreateAccessor(IndustrialSector.NewObject(), "Companies").GetOriginalValue <int>();
 }