public void Test_DereferenceRelatedObjects_ForSingle_IsOwner_WhenHasNoRelatedBO()
        {
            //The Car has a single relationship to engine. The car->engine relationship is marked
            // as a dereference related relationship.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            //---------------Set up test pack-------------------
            new Car();
            Engine engine = new Engine();

            engine.SetPropertyValue("EngineNo", "NO111");
            engine.Save();
            engine.MarkForDelete();
            SingleRelationshipDef relationshipDef = (SingleRelationshipDef)engine.Relationships["Car"].RelationshipDef;

            relationshipDef.DeleteParentAction = DeleteParentAction.DereferenceRelated;
            //---------------Assert Precondition----------------
            Assert.IsTrue(relationshipDef.OwningBOHasForeignKey);
            Assert.AreEqual(DeleteParentAction.DereferenceRelated, relationshipDef.DeleteParentAction);
            Assert.IsNull(engine.GetCar());
            //---------------Execute Test ----------------------
            engine.Save();
            //---------------Test Result -----------------------
            Assert.IsNull(engine.GetCar());
            Assert.IsTrue(engine.Status.IsNew && engine.Status.IsDeleted);
        }
Example #2
0
        public void TestCreateRelationshipHoldRelRef()
        {
            RelationshipDef mRelationshipDef;
            RelKeyDef       mRelKeyDef;
            MockBO          _mMockBO         = GetMockBO(out mRelationshipDef, out mRelKeyDef);
            RelationshipDef lRelationshipDef = new SingleRelationshipDef("Relation1", typeof(MockBO), mRelKeyDef, true,
                                                                         DeleteParentAction.Prevent);
            ISingleRelationship rel =
                (ISingleRelationship)lRelationshipDef.CreateRelationship(_mMockBO, _mMockBO.PropCol);

            Assert.AreEqual(lRelationshipDef.RelationshipName, rel.RelationshipName);
            Assert.IsTrue(_mMockBO.GetPropertyValue("MockBOProp1") == null);
            Assert.IsFalse(rel.HasRelatedObject(), "Should be false since props are not defaulted in Mock bo");
            _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
            _mMockBO.Save();
            Assert.IsTrue(rel.HasRelatedObject(), "Should be true since prop MockBOProp1 has been set");

            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOProp1"), _mMockBO.GetPropertyValue("MockBOID"));
            MockBO ltempBO = (MockBO)rel.GetRelatedObject();

            Assert.IsFalse(ltempBO == null);
            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOID"), ltempBO.GetPropertyValue("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");
            Assert.AreEqual(_mMockBO.GetPropertyValueString("MockBOProp1"), ltempBO.GetPropertyValueString("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");
            Assert.AreEqual(_mMockBO.GetPropertyValue("MockBOProp1"), ltempBO.GetPropertyValue("MockBOID"),
                            "The object returned should be the one with the ID = MockBOID");

            Assert.IsTrue(ReferenceEquals(ltempBO, rel.GetRelatedObject()));
            FixtureEnvironment.ClearBusinessObjectManager();
            Assert.IsTrue(ReferenceEquals(ltempBO, rel.GetRelatedObject()));
            _mMockBO.MarkForDelete();
            _mMockBO.Save();
        }
Example #3
0
        /// <summary>
        /// Maps the <see cref="ReflectionWrappers.PropertyWrapper"/> to a <see cref="IRelationshipDef"/>.
        /// </summary>
        /// <returns></returns>
        public IRelationshipDef MapOneToOne()
        {
            if (!MustBeMapped())
            {
                return(null);
            }
            CheckReverseRelationshipValid();

            var relatedClassType            = PropertyWrapper.RelatedClassType.UnderlyingType;
            DeleteParentAction deleteAction = GetDeleteAction();

            var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedClassType
                                                   , new RelKeyDef(), true, deleteAction)
            {
                OwningBOHasForeignKey   = this.OwningBoHasForeignKey,
                ReverseRelationshipName = this.ReverseRelationshipName
            };

            SetRelationshipType(relDef);
            relDef.SetAsOneToOne();
            IRelPropDef relPropDef = this.CreateRelPropDef();

            relDef.RelKeyDef.Add(relPropDef);
            return(relDef);
        }
        public void Test_DereferenceRelatedObjects_ForSingle_NonOwner_WhenHasNoRelatedBO()
        {
            //The Car has a single relationship to engine. The car->engine relationship is marked
            // as a dereference related relationship.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            //---------------Set up test pack-------------------
            Car car = new Car();

            car.SetPropertyValue("CarRegNo", "NP32459");
            car.Save();
            car.MarkForDelete();
            new Engine();

            //---------------Assert Precondition----------------
            SingleRelationshipDef relationshipDef = (SingleRelationshipDef)car.Relationships["Engine"].RelationshipDef;

            Assert.IsFalse(relationshipDef.OwningBOHasForeignKey);
            Assert.AreEqual(DeleteParentAction.DereferenceRelated, relationshipDef.DeleteParentAction);
            Assert.IsNull(car.GetEngine());
            //---------------Execute Test ----------------------
            car.Save();
            //---------------Test Result -----------------------
            Assert.IsNull(car.GetEngine());
            Assert.IsTrue(car.Status.IsNew && car.Status.IsDeleted);
        }
Example #5
0
        public static IClassDef LoadDefaultClassDef_SingleRel_NoReverseRelationship()
        {
            XmlClassLoader itsLoader   = CreateXmlClassLoader();
            IClassDef      itsClassDef =
                itsLoader.LoadClass(
                    @"
				<class name=""OrganisationTestBO"" assembly=""Habanero.Test.BO"" table=""organisation"">
					<property  name=""OrganisationID"" type=""Guid"" />
                    <property name=""Name"" />
					<primaryKey>
						<prop name=""OrganisationID"" />
					</primaryKey>
			    </class>
			"            );
            RelPropDef relPropDef = new RelPropDef(itsClassDef.PropDefcol["OrganisationID"], "OrganisationID");
            RelKeyDef  relKeyDef  = new RelKeyDef();

            relKeyDef.Add(relPropDef);
            IRelationshipDef relationshipDef = new SingleRelationshipDef("ContactPerson", "Habanero.Test.BO",
                                                                         "ContactPersonTestBO", relKeyDef, true, DeleteParentAction.DeleteRelated, InsertParentAction.InsertRelationship, RelationshipType.Aggregation);

            relationshipDef.OwningBOHasForeignKey = false;
            itsClassDef.RelationshipDefCol.Add(relationshipDef);
            ClassDef.ClassDefs.Add(itsClassDef);
            return(itsClassDef);
        }
Example #6
0
        private void SetRelationshipType(SingleRelationshipDef relDef)
        {
            var onToManyAtt = this.PropertyWrapper.GetAttribute <AutoMapOneToOneAttribute>();

            if (onToManyAtt != null)
            {
                relDef.RelationshipType = onToManyAtt.RelationshipType;
            }
        }
Example #7
0
        public void TestOwningBOHasForeignKey_Single_Default()
        {
            //---------------Execute Test ----------------------
            SingleRelationshipDef relDef = new SingleRelationshipDef
                                               ("rel", typeof(MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);

            //---------------Test Result -----------------------
            Assert.IsTrue(relDef.OwningBOHasForeignKey);
            //---------------Tear Down -------------------------
        }
        public void TestRemove()
        {
            SingleRelationshipDef       relDef = new SingleRelationshipDef("rel", typeof(MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);
            RelationshipDefColInheritor col    = new RelationshipDefColInheritor();

            col.CallRemove(relDef);
            col.Add(relDef);
            Assert.AreEqual(1, col.Count);
            col.CallRemove(relDef);
            Assert.AreEqual(0, col.Count);
        }
Example #9
0
        /// <summary>
        /// Maps the <see cref="PropertyInfo"/> to a Many to One relationship
        /// </summary>
        /// <returns></returns>
        public IRelationshipDef MapManyToOne()
        {
            if (this.PropertyWrapper.DeclaringType == (Type)null)
            {
                return(null);
            }
            if (!MustBeMapped())
            {
                return(null);
            }

            var relatedObjectType = this.PropertyWrapper.PropertyType;

            if (this.PropertyWrapper.HasAttribute <AutoMapManyToOneAttribute>())
            {
                var manyToOneAttribute = this.PropertyWrapper.GetAttribute <AutoMapManyToOneAttribute>();
                var specifiedType      = manyToOneAttribute.RelatedObjectClassType;
                if (specifiedType != null)
                {
                    relatedObjectType = specifiedType.ToTypeWrapper();
                }
            }
            if (!relatedObjectType.IsBusinessObject)
            {
                throw new InvalidDefinitionException(string.Format(
                                                         "The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be a Business Object",
                                                         relatedObjectType, this.PropertyWrapper.DeclaringClassName, this.PropertyWrapper.Name));
            }
            if (!relatedObjectType.IsOfType(this.PropertyWrapper.UnderlyingPropertyType))
            {
                throw new InvalidDefinitionException(string.Format(
                                                         "The specified RelatedObjectClassType '{0}' on '{1}.{2}' must be assignment compatible with the actual property type '{3}'",
                                                         relatedObjectType, this.PropertyWrapper.DeclaringClassName,
                                                         this.PropertyWrapper.Name, this.PropertyWrapper.UnderlyingPropertyType));
            }

            var relDef = new SingleRelationshipDef(this.PropertyWrapper.Name, relatedObjectType.UnderlyingType,
                                                   new RelKeyDef(), true, DeleteParentAction.DoNothing);

            SetRelationshipType(relDef);
            if (this.PropertyWrapper.HasCompulsoryAttribute)
            {
                relDef.SetAsCompulsory();
            }
            relDef.OwningBOHasForeignKey = true;
            SetReverseRelationshipName(relDef);
            var         ownerPropName   = GetOwningPropName();
            var         relatedPropName = GetRelatedPropName(relatedObjectType);
            IRelPropDef relPropDef      = new RelPropDef(ownerPropName, relatedPropName);

            relDef.RelKeyDef.Add(relPropDef);
            return(relDef);
        }
Example #10
0
        private static MockBO GetMockBO(out RelationshipDef mRelationshipDef, out RelKeyDef mRelKeyDef)
        {
            MockBO      _mMockBO    = new MockBO();
            IPropDefCol mPropDefCol = _mMockBO.PropDefCol;

            mRelKeyDef = new RelKeyDef();
            IPropDef   propDef     = mPropDefCol["MockBOProp1"];
            RelPropDef lRelPropDef = new RelPropDef(propDef, "MockBOID");

            mRelKeyDef.Add(lRelPropDef);
            mRelationshipDef = new SingleRelationshipDef("Relation1", typeof(MockBO), mRelKeyDef, false,
                                                         DeleteParentAction.Prevent);
            return(_mMockBO);
        }
Example #11
0
        private static RelationshipDefCol CreateRelationshipDefCol(IPropDefCol lPropDefCol)
        {
            RelationshipDefCol relDefCol = new RelationshipDefCol();

            //Define Owner Relationships
            RelKeyDef relKeyDef = new RelKeyDef();
            IPropDef  propDef   = lPropDefCol["OwnerId"];

            RelPropDef lRelPropDef = new RelPropDef(propDef, "ContactPersonID");

            relKeyDef.Add(lRelPropDef);

            RelationshipDef relDef = new SingleRelationshipDef("Owner", typeof(ContactPerson), relKeyDef, false, DeleteParentAction.Prevent);

            relDefCol.Add(relDef);

            //Define Driver Relationships
            relKeyDef = new RelKeyDef();
            propDef   = lPropDefCol["DriverFK1"];

            lRelPropDef = new RelPropDef(propDef, "PK1Prop1");
            relKeyDef.Add(lRelPropDef);

            propDef = lPropDefCol["DriverFK2"];

            lRelPropDef = new RelPropDef(propDef, "PK1Prop2");
            relKeyDef.Add(lRelPropDef);

            relDef = new SingleRelationshipDef("Driver", typeof(ContactPersonCompositeKey), relKeyDef, true, DeleteParentAction.Prevent);

            relDefCol.Add(relDef);

            //Define Engine Relationships
            relKeyDef = new RelKeyDef();
            propDef   = lPropDefCol["CarID"];

            lRelPropDef = new RelPropDef(propDef, "CarID");
            relKeyDef.Add(lRelPropDef);

            relDef = new SingleRelationshipDef("Engine", typeof(Engine), relKeyDef, false, DeleteParentAction.DereferenceRelated)
            {
                OwningBOHasForeignKey = false
            };
            relDefCol.Add(relDef);
            return(relDefCol);
        }
Example #12
0
        private static RelationshipDefCol CreateRelationshipDefCol(IPropDefCol lPropDefCol)
        {
            RelationshipDefCol relDefCol = new RelationshipDefCol();

            //Define Relationships
            RelKeyDef relKeyDef = new RelKeyDef();
            IPropDef  propDef   = lPropDefCol["ContactPersonID"];

            RelPropDef relPropDef = new RelPropDef(propDef, "ContactPersonID");

            relKeyDef.Add(relPropDef);

            RelationshipDef relDef = new SingleRelationshipDef("ContactPerson", typeof(ContactPerson), relKeyDef, false, DeleteParentAction.Prevent);

            relDefCol.Add(relDef);

            return(relDefCol);
        }
Example #13
0
        private static IRelationshipDef CreateReverseRelDef(IRelationshipDef rel, IClassDef classDef)
        {
            IRelationshipDef newReverseRelDef;

            if (rel.IsManyToOne)
            {
                newReverseRelDef = new MultipleRelationshipDef(rel.ReverseRelationshipName
                                                               , classDef.ClassType, new RelKeyDef(), true, ""
                                                               , DeleteParentAction.Prevent);
            }
            else
            {
                newReverseRelDef = new SingleRelationshipDef(rel.ReverseRelationshipName
                                                             , classDef.ClassType, new RelKeyDef(), true
                                                             , DeleteParentAction.DoNothing);
            }
            newReverseRelDef.ReverseRelationshipName = rel.RelationshipName;
            return(newReverseRelDef);
        }
        public void TestAddDuplicationException()
        {
            //---------------Set up test pack-------------------
            SingleRelationshipDef relDef = new SingleRelationshipDef("rel", typeof(MyRelatedBo), new RelKeyDef(), true, DeleteParentAction.Prevent);
            RelationshipDefCol    col    = new RelationshipDefCol();

            col.Add(relDef);
            //---------------Execute Test ----------------------
            try
            {
                col.Add(relDef);
                Assert.Fail("Expected to throw an ArgumentException");
            }
            //---------------Test Result -----------------------
            catch (ArgumentException ex)
            {
                StringAssert.Contains("A relationship definition with the name 'rel' already exists", ex.Message);
            }
        }
Example #15
0
        public void TestFieldDefaultLabelFromRelatedClassDef()
        {
            ClassDef.ClassDefs.Clear();
            ClassDef classDef  = CreateTestClassDef("");
            ClassDef classDef2 = CreateTestClassDef("2");

            ClassDef.ClassDefs.Add(classDef2);
            RelKeyDef  relKeyDef  = new RelKeyDef();
            RelPropDef relPropDef = new RelPropDef(classDef.PropDefcol["TestProperty"], "TestProperty2");

            relKeyDef.Add(relPropDef);
            SingleRelationshipDef def = new SingleRelationshipDef("TestRel", classDef2.AssemblyName, classDef2.ClassName, relKeyDef, false, DeleteParentAction.Prevent);

            classDef.RelationshipDefCol.Add(def);

            UIFormField uiFormField = new UIFormField(null, "TestRel.TestProperty2", typeof(TextBox), null, null, true, null, null, LayoutStyle.Label);

            Assert.AreEqual("Tested Property2:", uiFormField.GetLabel(classDef));
        }
Example #16
0
        public void TestFieldDefaultLabelFromRelatedClassDef()
        {
            ClassDef.ClassDefs.Clear();
            ClassDef classDef  = CreateTestClassDef("");
            ClassDef classDef2 = CreateTestClassDef("2");

            ClassDef.ClassDefs.Add(classDef2);
            RelKeyDef  relKeyDef  = new RelKeyDef();
            RelPropDef relPropDef = new RelPropDef(classDef.PropDefcol["TestProperty"], "TestProperty2");

            relKeyDef.Add(relPropDef);
            SingleRelationshipDef def = new SingleRelationshipDef("TestRel", classDef2.AssemblyName, classDef2.ClassName, relKeyDef, false, DeleteParentAction.Prevent);

            classDef.RelationshipDefCol.Add(def);

            UIGridColumn uiGridColumn;

            uiGridColumn = new UIGridColumn(null, "TestRel.TestProperty2", typeof(DataGridViewTextBoxColumn), false, 100, PropAlignment.left, null);
#pragma warning disable 612,618
            Assert.AreEqual("Tested Property2", uiGridColumn.GetHeading(classDef));
#pragma warning restore 612,618
        }
Example #17
0
        public void init()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
            _fakeBO     = new MockBO();
            _propDefCol = _fakeBO.PropDefCol;

            _RelKeyDef = new RelKeyDef();
            IPropDef propDef = _propDefCol["MockBOID"];

            RelPropDef relPropDef = new RelPropDef(propDef, "MockBOProp1");

            _RelKeyDef.Add(relPropDef);

            _multipleRelationshipDef = new MultipleRelationshipDef("Relation1", typeof(MockBO),
                                                                   _RelKeyDef, false, "",
                                                                   DeleteParentAction.DeleteRelated);


            _singleRelationshipDef = new SingleRelationshipDef("Single", typeof(MockBO),
                                                               _RelKeyDef, false,
                                                               DeleteParentAction.DeleteRelated);
        }
        public void init()
        {
            _mockBo     = new MockBO();
            _propDefCol = _mockBo.PropDefCol;

            _RelKeyDef = new RelKeyDef();
            IPropDef propDef = _propDefCol["MockBOID"];

            RelPropDef relPropDef = new RelPropDef(propDef, "MockBOProp1");

            _RelKeyDef.Add(relPropDef);

            _multipleRelationshipDef = new MultipleRelationshipDef("Relation1", typeof(MockBO),
                                                                   _RelKeyDef, false, "",
                                                                   DeleteParentAction.DeleteRelated);


            _singleRelationshipDef = new SingleRelationshipDef("Single", typeof(MockBO),
                                                               _RelKeyDef, false,
                                                               DeleteParentAction.DeleteRelated);
            DatabaseConnection.CurrentConnection.ConnectionString = MyDBConnection.GetConnectionString();
        }
Example #19
0
        public void TestGetRelatedObject()
        {
            RelationshipDef mRelationshipDef;
            RelKeyDef       mRelKeyDef;
            MockBO          _mMockBO         = GetMockBO(out mRelationshipDef, out mRelKeyDef);
            RelationshipDef lRelationshipDef = new SingleRelationshipDef("Relation1", typeof(MockBO), mRelKeyDef, true,
                                                                         DeleteParentAction.Prevent);
            ISingleRelationship rel =
                (ISingleRelationship)lRelationshipDef.CreateRelationship(_mMockBO, _mMockBO.PropCol);

            Assert.AreEqual(lRelationshipDef.RelationshipName, rel.RelationshipName);
            Assert.IsTrue(_mMockBO.GetPropertyValue("MockBOProp1") == null);
            Assert.IsFalse(rel.HasRelatedObject(), "Should be false since props are not defaulted in Mock bo");
            //Set a related object
            _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
            //Save the object, so that the relationship can retrieve the object from the database
            _mMockBO.Save();
            Assert.IsTrue(rel.HasRelatedObject(), "Should have a related object since the relating props have values");
            MockBO ltempBO = (MockBO)rel.GetRelatedObject();

            Assert.IsNotNull(ltempBO, "The related object should exist");
            //Clear the related object
            _mMockBO.SetPropertyValue("MockBOProp1", null);
            Assert.IsFalse(rel.HasRelatedObject(),
                           "Should not have a related object since the relating props have been set to null");
            ltempBO = (MockBO)rel.GetRelatedObject();
            Assert.IsNull(ltempBO, "The related object should now be null");
            //Set a related object again
            _mMockBO.SetPropertyValue("MockBOProp1", _mMockBO.GetPropertyValue("MockBOID"));
            Assert.IsTrue(rel.HasRelatedObject(),
                          "Should have a related object since the relating props have values again");
            ltempBO = (MockBO)rel.GetRelatedObject();
            Assert.IsNotNull(ltempBO, "The related object should exist again");
            _mMockBO.MarkForDelete();
            _mMockBO.Save();
        }
Example #20
0
        private static ClassDef CreateClassDef(long number, bool hasSingleRelationship,
                                               bool hasMultipleRelationship, bool hasMultipleRelationshipWithPreventDelete)
        {
            const string  assemblyName   = "Habanero.Test.BO";
            const string  className      = "TestBO";
            const string  idPropName     = "MyBoID";
            const string  fkPropertyName = "MyParentBoID";
            string        suffix         = number.ToString();
            PropDefCol    propDefCol     = new PropDefCol();
            PrimaryKeyDef primaryKeyDef  = new PrimaryKeyDef();

            primaryKeyDef.IsGuidObjectID = false;
            RelationshipDefCol relationshipDefCol = new RelationshipDefCol();
            PropDef            idPropDef          = new PropDef(idPropName, typeof(string), PropReadWriteRule.ReadWrite, "");

            propDefCol.Add(idPropDef);
            primaryKeyDef.Add(idPropDef);
            //propDef = new PropDef("MyProp", typeof(string), PropReadWriteRule.ReadWrite, "" );
            //propDefCol.Add(propDef);
            PropDef propDef = new PropDef(fkPropertyName, typeof(string), PropReadWriteRule.ReadWrite, "");

            propDefCol.Add(propDef);
            if (hasSingleRelationship)
            {
                string     relatedClassSuffix = (number - 1).ToString();
                RelKeyDef  relKeyDef          = new RelKeyDef();
                RelPropDef relPropDef         = new RelPropDef(propDef, idPropName);
                relKeyDef.Add(relPropDef);
                SingleRelationshipDef singleRelationshipDef = new SingleRelationshipDef(
                    "MyParent", assemblyName,
                    className + relatedClassSuffix, relKeyDef, false, DeleteParentAction.Prevent);
                relationshipDefCol.Add(singleRelationshipDef);
            }
            if (hasMultipleRelationship)
            {
                string     relatedClassSuffix = (number + 1).ToString();
                RelKeyDef  relKeyDef          = new RelKeyDef();
                RelPropDef relPropDef         = new RelPropDef(idPropDef, fkPropertyName);
                relKeyDef.Add(relPropDef);
                MultipleRelationshipDef multipleRelationshipDef =
                    new MultipleRelationshipDef("MyBO" + relatedClassSuffix,
                                                assemblyName, className + relatedClassSuffix, relKeyDef, false,
                                                "", DeleteParentAction.DeleteRelated);
                relationshipDefCol.Add(multipleRelationshipDef);
            }
            if (hasMultipleRelationshipWithPreventDelete)
            {
                string     relatedClassSuffix = (number + 1).ToString();
                RelKeyDef  relKeyDef          = new RelKeyDef();
                RelPropDef relPropDef         = new RelPropDef(idPropDef, fkPropertyName);
                relKeyDef.Add(relPropDef);
                MultipleRelationshipDef multipleRelationshipDef =
                    new MultipleRelationshipDef("MyPreventBO" + relatedClassSuffix,
                                                assemblyName, className + relatedClassSuffix, relKeyDef, false,
                                                "", DeleteParentAction.Prevent);
                relationshipDefCol.Add(multipleRelationshipDef);
            }
            ClassDef classDef = new ClassDef(assemblyName, className + suffix,
                                             primaryKeyDef, propDefCol, new KeyDefCol(), relationshipDefCol, new UIDefCol());

            return(classDef);
        }