示例#1
0
        public void testMandatory()
        {
            OrderingTestObject         te  = new OrderingTestObject();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(te.GetType());
            Stream stream;
            string json;

            object    returnValue;
            Exception exception;

            json = "{ one:2 }";
            TestUtil.run(out returnValue, out exception,
                         () =>
            {
                stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
                te     = (OrderingTestObject)ser.ReadObject(stream);
                return(te);
            }
                         );
            Assert.AreEqual(typeof(SerialisationException), exception.GetType(), "should fail w exception");

            DataContractMandatoryEnforcementListener dcListener = new DataContractMandatoryEnforcementListener();

            TestUtil.run(out returnValue, out exception,
                         () =>
            {
                Json2Object j2O = new Json2Object();
                j2O.Add(dcListener);
                te = (OrderingTestObject)j2O.toObject(json, typeof(OrderingTestObject));
                return(te);
            }
                         );
            Assert.AreEqual(typeof(SerialisationException), exception.GetType(), "should fail w exception");

            //check the enforcement listener is not giving a false positive !
            Dictionary <string, object> propertyMap = dcListener.Object2PropertyValues.First().Value;

            Assert.AreEqual(propertyMap.Count, 1, "enforcement data exists");
            Assert.AreEqual(propertyMap["one"], "2", "enforcment data contains a correct value");
        }
        public void testMandatory()
        {
            OrderingTestObject te = new OrderingTestObject();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(te.GetType());
            Stream stream;
            string json;

            object returnValue;
            Exception exception;
            json = "{ one:2 }";
            TestUtil.run(out returnValue, out exception,
                 () =>
                 {
                     stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
                     te = (OrderingTestObject)ser.ReadObject(stream);
                     return te;
                 }
                 );
            Assert.AreEqual(typeof(SerialisationException), exception.GetType(), "should fail w exception");

            DataContractMandatoryEnforcementListener dcListener = new DataContractMandatoryEnforcementListener();
            TestUtil.run(out returnValue, out exception,
                 () =>
                 {
                     Json2Object j2O = new Json2Object();
                     j2O.Add(dcListener);
                     te = (OrderingTestObject) j2O.toObject(json, typeof(OrderingTestObject));
                     return te;
                 }
                 );
            Assert.AreEqual(typeof(SerialisationException), exception.GetType(), "should fail w exception");
            
            //check the enforcement listener is not giving a false positive !
            Dictionary<string, object> propertyMap = dcListener.Object2PropertyValues.First().Value;
            Assert.AreEqual(propertyMap.Count, 1, "enforcement data exists");
            Assert.AreEqual(propertyMap["one"], "2", "enforcment data contains a correct value");


        }