Exemplo n.º 1
0
        public void ValidateBehaviorWhenValidDerivedTypeIsFound()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderTests),
                                                             new[] { typeof(StubClass) });

            var valueProvider     = MockRepository.GenerateMock <IValueProvider>();
            var typeStampOperator = MockRepository.GenerateMock <ITypeStampOperator>();

            valueProvider.Expect(b => b.ContainsPrefix("test"))
            .Return(true);

            typeStampOperator.Expect(a => a.DetectTypeStamp(null, null)).IgnoreArguments().Return(DerivedTypeModelBinderCache.GetTypeName(typeof(StubClass)));

            var binder = new DerivedTypeModelBinder(typeStampOperator);

            var bindingContext = new ModelBindingContext
            {
                ModelMetadata =
                    new ModelMetadata(new EmptyModelMetadataProvider(), typeof(DerivedTypeModelBinder),
                                      null, typeof(DerivedTypeModelBinderTests), "propertyName"
                                      ),
                ModelName     = "test",
                ValueProvider = valueProvider
            };


            object model = binder.BindModel(new ControllerContext(), bindingContext);

            Assert.That(model, Is.TypeOf(typeof(StubClass)));
            Assert.That(bindingContext.ModelType.FullName, Is.EqualTo(typeof(StubClass).FullName));

            DerivedTypeModelBinderCache.Reset();
        }
        public void GetTypeName_verify_error_on_unrecognized_type()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            Assert.Throws <KeyNotFoundException>(() => DerivedTypeModelBinderCache.GetTypeName(typeof(DerivedTypeModelBinderCacheTests)));
        }
Exemplo n.º 3
0
        public void TypeStamp_throws_error_when_requested_class_is_not_registered()
        {
            DerivedTypeModelBinderCache.Reset();

            var html = CreateHtmlHelper <HtmlExtensionTests>();

            Assert.Throws <KeyNotFoundException>(() => html.TypeStamp().ToHtmlString());
        }
        public void GetTypeName_verify_encryption()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var typeName = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));

            Assert.That(typeName, Is.StringStarting("VFxCac+"));
        }
Exemplo n.º 5
0
        public void validate_attribute_scan_on_getDerivedTypes_call()
        {
            DerivedTypeModelBinderCache.Reset();

            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(int).Name
                         select p).FirstOrDefault(), Is.Not.Null);

            DerivedTypeModelBinderCache.Reset();
        }
        public void validate_declarative_registration_of_derived_types()
        {
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderCacheTests),
                                                             new[] { typeof(string) });

            Assert.That(DerivedTypeModelBinderCache.GetTypeName(typeof(string)), Is.Not.Empty);

            DerivedTypeModelBinderCache.Reset();

            // next, let's validate that the cache was cleared by reset
            Assert.Throws <KeyNotFoundException>(() => DerivedTypeModelBinderCache.GetTypeName(typeof(string)));
        }
Exemplo n.º 7
0
        public void TypeStamp_returns_proper_encoded_identifier()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var html = CreateHtmlHelper <TestClass>();

            var output = html.TypeStamp();

            Assert.That(output.ToHtmlString(), Is.StringContaining("name=\"_xTypeStampx_\""));
            Assert.That(output.ToHtmlString(), Is.StringContaining("value=\""));
            Assert.That(output.ToHtmlString(), Is.StringContaining("type=\"hidden\""));
        }
Exemplo n.º 8
0
        public void validate_declarative_registration_of_derived_types()
        {
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(DerivedTypeModelBinderCacheTests),
                                                             new[] { typeof(string) });

            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(string).Name
                         select p).FirstOrDefault(), Is.Not.Null);

            DerivedTypeModelBinderCache.Reset();

            // next, let's validate that the cache was cleared by reset
            Assert.That((from p in DerivedTypeModelBinderCache.GetDerivedTypes(typeof(DerivedTypeModelBinderCacheTests))
                         where p.Name == typeof(string).Name
                         select p).FirstOrDefault(), Is.Null);
        }
Exemplo n.º 9
0
        public void TypeStamp_uses_type_stamp_name()
        {
            DerivedTypeModelBinderCache.Reset();

            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var originalField = DerivedTypeModelBinderCache.TypeStampFieldName;

            DerivedTypeModelBinderCache.TypeStampFieldName = "test";

            var html = CreateHtmlHelper <TestClass>();

            var output = html.TypeStamp();

            DerivedTypeModelBinderCache.TypeStampFieldName = originalField;

            Assert.That(output.ToHtmlString(), Is.StringContaining("name=\"test\""));
        }
        public void TypeStamp_verify_encryption_set_and_reset_behaviors()
        {
            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var standardTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.SetTypeStampSaltValue(Guid.NewGuid());
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var alteredTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            DerivedTypeModelBinderCache.Reset();
            DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ITestClass), new[] { typeof(TestClass) });

            var resetTypeStamp = DerivedTypeModelBinderCache.GetTypeName(typeof(TestClass));


            Assert.That(standardTypeStamp, Is.Not.EqualTo(alteredTypeStamp));
            Assert.That(standardTypeStamp, Is.EqualTo(resetTypeStamp));
        }