public void SetValue_WithExceptionHandledByPropertyAccessStrategy_ThrowsBusinessObjectPropertyAccessException()
        {
            var bindablePropertyWriteAccessStrategyStub = MockRepository.GenerateStub <IBindablePropertyWriteAccessStrategy>();

            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "ThrowingProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    bindablePropertyWriteAccessStrategy: bindablePropertyWriteAccessStrategyStub));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var originalException = new Exception("The Exception");
            var expectedException = new BusinessObjectPropertyAccessException("The Message", null);

            bindablePropertyWriteAccessStrategyStub
            .Stub(
                mock => mock.IsPropertyAccessException(
                    Arg.Is((IBusinessObject)instance),
                    Arg.Is(propertyBase),
                    Arg.Is(originalException),
                    out Arg <BusinessObjectPropertyAccessException> .Out(expectedException).Dummy))
            .Return(true);

            instance.PrepareException(originalException);

            var actualException =
                Assert.Throws <BusinessObjectPropertyAccessException> (() => propertyBase.SetValue((IBusinessObject)instance, new SimpleReferenceType()));

            Assert.That(actualException, Is.SameAs(expectedException));
        }
        public void GetDisplayName_WithGlobalizationSerivce()
        {
            var mockMemberInformationGlobalizationService = _mockRepository.StrictMock <IMemberInformationGlobalizationService>();
            IPropertyInformation propertyInfo             = GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String");
            PropertyBase         property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false,
                    bindableObjectGlobalizationService: new BindableObjectGlobalizationService(
                        MockRepository.GenerateStub <IGlobalizationService>(),
                        mockMemberInformationGlobalizationService,
                        MockRepository.GenerateStub <IEnumerationGlobalizationService>(),
                        MockRepository.GenerateStub <IExtensibleEnumGlobalizationService>())));

            property.SetReflectedClass(_bindableObjectClass);

            Expect.Call(
                mockMemberInformationGlobalizationService.TryGetPropertyDisplayName(
                    Arg.Is(propertyInfo),
                    Arg <ITypeInformation> .Matches(c => c.ConvertToRuntimeType() == _bindableObjectClass.TargetType),
                    out Arg <string> .Out("MockString").Dummy))
            .Return(true);
            _mockRepository.ReplayAll();

            string actual = property.DisplayName;

            _mockRepository.VerifyAll();
            Assert.That(actual, Is.EqualTo("MockString"));
        }
        public void GetReflectedClass_WithoutBusinessObjectClass()
        {
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String"),
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));

            Dev.Null = property.ReflectedClass;
        }
        public void GetListInfo_WithNonListProperty()
        {
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar"),
                    underlyingType: typeof(SimpleReferenceType),
                    concreteType: typeof(SimpleReferenceType),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));

            Assert.That(property.IsList, Is.False);
            Dev.Null = property.ListInfo;
        }
        public void ConvertToNativePropertyType_Scalar()
        {
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar"),
                    underlyingType: typeof(SimpleReferenceType),
                    concreteType: typeof(SimpleReferenceType),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));
            var expected = new SimpleReferenceType();

            Assert.That(property.ConvertToNativePropertyType(expected), Is.SameAs(expected));
        }
        public void GetListInfo()
        {
            IListInfo    expected = new ListInfo(typeof(SimpleReferenceType[]), typeof(SimpleReferenceType));
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Array"),
                    underlyingType: typeof(SimpleReferenceType),
                    concreteType: typeof(SimpleReferenceType),
                    listInfo: expected,
                    isRequired: false,
                    isReadOnly: false));

            Assert.That(property.IsList, Is.True);
            Assert.That(property.ListInfo, Is.SameAs(expected));
        }
        public void SetAndGetReflectedClass()
        {
            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String"),
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));

            property.SetReflectedClass(_bindableObjectClass);

            Assert.That(property.ReflectedClass, Is.SameAs(_bindableObjectClass));
            Assert.That(((IBusinessObjectProperty)property).ReflectedClass, Is.SameAs(_bindableObjectClass));
        }
        public void GetDefaultValueStrategy()
        {
            var businessObject = MockRepository.GenerateStub <IBusinessObject>();
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar");
            PropertyBase         propertyBase =
                new StubPropertyBase(
                    CreateParameters(
                        propertyInfo: propertyInfo,
                        underlyingType: propertyInfo.PropertyType,
                        concreteType: propertyInfo.PropertyType,
                        listInfo: null,
                        isRequired: true,
                        isReadOnly: true));

            Assert.That(propertyBase.IsDefaultValue(businessObject), Is.False);
        }
        public void SetReflectedClass_Twice()
        {
            BindableObjectClass bindableObjectClass = BindableObjectProviderTestHelper.GetBindableObjectClass(typeof(SimpleBusinessObjectClass));

            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String"),
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));

            property.SetReflectedClass(bindableObjectClass);
            property.SetReflectedClass(BindableObjectProviderTestHelper.GetBindableObjectClass(typeof(ClassWithIdentity)));
        }
        public void GetDisplayName_ReflectedClassNotSet()
        {
            var propertyInfo = GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String");
            var property     = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false,
                    bindableObjectGlobalizationService: new BindableObjectGlobalizationService(
                        MockRepository.GenerateStub <IGlobalizationService>(),
                        MockRepository.GenerateStub <IMemberInformationGlobalizationService>(),
                        MockRepository.GenerateStub <IEnumerationGlobalizationService>(),
                        MockRepository.GenerateStub <IExtensibleEnumGlobalizationService>())));

            Dev.Null = property.DisplayName;
        }
        public void Initialize()
        {
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            Assert.That(propertyBase.PropertyInfo, Is.SameAs(propertyInfo));
            Assert.That(propertyBase.PropertyType, Is.SameAs(propertyInfo.PropertyType));
            Assert.That(propertyBase.IsRequired, Is.True);
            Assert.That(propertyBase.IsReadOnly(null), Is.True);
            Assert.That(propertyBase.BusinessObjectProvider, Is.SameAs(_bindableObjectProvider));
            Assert.That(((IBusinessObjectProperty)propertyBase).BusinessObjectProvider, Is.SameAs(_bindableObjectProvider));
        }
        public void IsReadOnly_WithReadOnlyProperty_ReturnsTrue_DoesNotUseStrategy()
        {
            var bindablePropertyWriteAccessStrategyMock = MockRepository.GenerateMock <IBindablePropertyWriteAccessStrategy>();

            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "ThrowingProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isReadOnly: true,
                    bindablePropertyWriteAccessStrategy: bindablePropertyWriteAccessStrategyMock));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            Assert.That(propertyBase.IsReadOnly((IBusinessObject)instance), Is.True);
            bindablePropertyWriteAccessStrategyMock.AssertWasNotCalled(mock => mock.CanWrite(null, null), options => options.IgnoreArguments());
        }
        public void SetReflectedClass_FromDifferentProviders()
        {
            var provider = new BindableObjectProvider();
            BindableObjectClass bindableObjectClass = BindableObjectProviderTestHelper.GetBindableObjectClass(typeof(SimpleBusinessObjectClass));

            PropertyBase property = new StubPropertyBase(
                CreateParameters(
                    businessObjectProvider: provider,
                    propertyInfo: GetPropertyInfo(typeof(SimpleBusinessObjectClass), "String"),
                    underlyingType: typeof(string),
                    concreteType: typeof(string),
                    listInfo: null,
                    isRequired: false,
                    isReadOnly: false));

            property.SetReflectedClass(bindableObjectClass);

            Assert.That(property.ReflectedClass, Is.SameAs(bindableObjectClass));
        }
        public void GetValue_NoSetter()
        {
            var propertyInfo = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInfo.Stub(stub => stub.PropertyType).Return(typeof(bool));
            propertyInfo.Stub(stub => stub.GetIndexParameters()).Return(new ParameterInfo[0]);
            propertyInfo.Stub(stub => stub.GetSetMethod(true)).Return(null);
            PropertyBase propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            propertyBase.SetValue(((IBusinessObject)instance), new object());
        }
        public void GetValue()
        {
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "Scalar");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var value = new SimpleReferenceType();

            instance.Scalar = value;

            Assert.That(propertyBase.GetValue(((IBusinessObject)instance)), Is.SameAs(value));
        }
        public void SetValue_PrivateAccessor()
        {
            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "PrivateProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    isRequired: true,
                    isReadOnly: true));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var value = new SimpleReferenceType();

            propertyBase.SetValue((IBusinessObject)instance, value);

            Assert.That(PrivateInvoke.GetNonPublicProperty(instance, "PrivateProperty"), Is.SameAs(value));
        }
        public void IsAccessible_ReturnsValueFromStrategy()
        {
            var bindablePropertyReadAccessStrategyMock = MockRepository.GenerateMock <IBindablePropertyReadAccessStrategy>();

            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "ThrowingProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    bindablePropertyReadAccessStrategy: bindablePropertyReadAccessStrategyMock));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var expectedValue = BooleanObjectMother.GetRandomBoolean();

            bindablePropertyReadAccessStrategyMock.Expect(mock => mock.CanRead((IBusinessObject)instance, propertyBase)).Return(expectedValue);

            Assert.That(propertyBase.IsAccessible((IBusinessObject)instance), Is.EqualTo(expectedValue));
            bindablePropertyReadAccessStrategyMock.VerifyAllExpectations();
        }
        public void SetValue_WithExceptionNotHandledByPropertyAccessStrategy_RethrowsOriginalException()
        {
            var bindablePropertyWriteAccessStrategyStub = MockRepository.GenerateStub <IBindablePropertyWriteAccessStrategy>();

            IPropertyInformation propertyInfo = GetPropertyInfo(typeof(ClassWithReferenceType <SimpleReferenceType>), "ThrowingProperty");
            PropertyBase         propertyBase = new StubPropertyBase(
                CreateParameters(
                    propertyInfo: propertyInfo,
                    underlyingType: propertyInfo.PropertyType,
                    concreteType: propertyInfo.PropertyType,
                    listInfo: null,
                    bindablePropertyWriteAccessStrategy: bindablePropertyWriteAccessStrategyStub));

            var instance = ObjectFactory.Create <ClassWithReferenceType <SimpleReferenceType> > (ParamList.Empty);

            var originalException = new Exception("The Exception");

            bindablePropertyWriteAccessStrategyStub
            .Stub(
                mock => mock.IsPropertyAccessException(
                    Arg.Is((IBusinessObject)instance),
                    Arg.Is(propertyBase),
                    Arg.Is(originalException),
                    out Arg <BusinessObjectPropertyAccessException> .Out(new BusinessObjectPropertyAccessException("Unexpected", null)).Dummy))
            .Return(false);

            instance.PrepareException(originalException);

            var actualException = Assert.Throws <Exception> (() => propertyBase.SetValue((IBusinessObject)instance, new SimpleReferenceType()));

            Assert.That(actualException, Is.SameAs(originalException));
#if DEBUG
            Assert.That(
                originalException.StackTrace,
                Is.StringStarting("   at Remotion.ObjectBinding.UnitTests.TestDomain.ClassWithReferenceType`1.set_ThrowingProperty(T value)"));
#endif
        }