Esempio n. 1
0
        public void WhenConfigFileIsNotValid()
        {
//            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
//<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
//    <v:required test='#this'>
//        <v:message id='error.airportCode.required' providers='summary'/>
//        <v:action type='Spring.Validation.Actions.ExpressionAction, Spring.Core' when='true'/>
//    </v:required>
//</objects>
//";
            XmlDocument doc = GetValidatedXmlResource("_WhenConfigFileIsNotValid.xml");

            MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();

            XmlReaderContext             readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
            ObjectDefinitionParserHelper helper        = new ObjectDefinitionParserHelper(readerContext);

            helper.InitDefaults(doc.DocumentElement);
            ParserContext parserContext = new ParserContext(helper);

            ValidationNamespaceParser parser = new ValidationNamespaceParser();

            foreach (XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.NamespaceURI == "http://www.springframework.net/validation")
                {
                    parser.ParseElement(element, parserContext);
                }
            }
        }
Esempio n. 2
0
        public void WhenConfigFileIsValid()
        {
            XmlDocument doc = GetValidatedXmlResource("_WhenConfigFileIsValid.xml");

            MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();

            XmlReaderContext             readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
            ObjectDefinitionParserHelper helper        = new ObjectDefinitionParserHelper(readerContext);

            helper.InitDefaults(doc.DocumentElement);
            ParserContext parserContext = new ParserContext(helper);

            ValidationNamespaceParser parser = new ValidationNamespaceParser();

            foreach (XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.NamespaceURI == "http://www.springframework.net/validation")
                {
                    parser.ParseElement(element, parserContext);
                }
            }
            IList <IObjectDefinition> defs = registry.GetObjectDefinitions();

            Assert.AreEqual(9, defs.Count);

            IObjectDefinition def = registry.GetObjectDefinition("destinationAirportValidator");

            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));

            PropertyValue fastValidateProperty = def.PropertyValues.GetPropertyValue("FastValidate");

            Assert.IsNotNull(fastValidateProperty);
            Assert.AreEqual("true", fastValidateProperty.Value);

            PropertyValue validatorsProperty = def.PropertyValues.GetPropertyValue("Validators");

            Assert.IsNotNull(validatorsProperty);
            object validatorsObject = validatorsProperty.Value;

            Assert.AreEqual(typeof(ManagedList), validatorsObject.GetType());
            ManagedList validators = (ManagedList)validatorsObject;

            Assert.AreEqual(4, validators.Count);

            def = (IObjectDefinition)validators[3];
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.AreEqual(typeof(RegularExpressionValidator), def.ObjectType);
            Assert.AreEqual("[A-Z]*", def.PropertyValues.GetPropertyValue("Expression").Value);


            def = registry.GetObjectDefinition("simpleAirportValidator");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            PropertyValue actionsProperty = def.PropertyValues.GetPropertyValue("Actions");

            Assert.IsNotNull(actionsProperty);
            object actionsObject = actionsProperty.Value;

            Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
            ManagedList actions = (ManagedList)actionsObject;

            Assert.AreEqual(1, actions.Count);

            IObjectDefinition exceptionActionDefinition = (IObjectDefinition)actions[0];

            Assert.AreEqual(typeof(ExceptionAction), exceptionActionDefinition.ObjectType);
            Assert.AreEqual("'new System.InvalidOperationException(\"invalid\")' []", exceptionActionDefinition.ConstructorArgumentValues.GenericArgumentValues[0].ToString());
            //

            def = registry.GetObjectDefinition("airportCodeValidator");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            actionsProperty = def.PropertyValues.GetPropertyValue("Actions");
            Assert.IsNotNull(actionsProperty);
            actionsObject = actionsProperty.Value;
            Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
            actions = (ManagedList)actionsObject;
            Assert.AreEqual(4, actions.Count);

            IObjectDefinition messageDefinition = (IObjectDefinition)actions[1];

            Assert.AreEqual(typeof(ErrorMessageAction), messageDefinition.ObjectType);

            IObjectDefinition actionDefinition = (IObjectDefinition)actions[2];

            Assert.AreEqual(typeof(ExpressionAction), actionDefinition.ObjectType);
            Assert.AreEqual("#now = DateTime.Now", actionDefinition.PropertyValues.GetPropertyValue("Valid").Value);

            def = registry.GetObjectDefinition("regex");
            Assert.IsTrue(def.IsSingleton);
            Assert.IsTrue(def.IsLazyInit);
            Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
            PropertyValue expressionProperty = def.PropertyValues.GetPropertyValue("Expression");

            Assert.IsNotNull(expressionProperty);
            Assert.AreEqual("RegExp", expressionProperty.Value);
        }