public void WrongPropertyInCustomAttribute()
        {
            string tmpf = Path.GetTempFileName();

            using (StreamWriter sw = new StreamWriter(tmpf))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");
                sw.WriteLine("<nhv-mapping xmlns='urn:nhibernate-validator-1.0'");
                sw.WriteLine(" assembly='NHibernate.Validator.Tests'");
                sw.WriteLine(" namespace='NHibernate.Validator.Tests.Configuration'>");
                sw.WriteLine("<class name='WellKnownRules'>");
                sw.WriteLine("<property name='AP'>");
                sw.WriteLine("<rule attribute='ACustomAttribute'>");
                sw.WriteLine("<param name='WrongName' value='A string value'/>");
                sw.WriteLine("</rule>");
                sw.WriteLine("</property>");
                sw.WriteLine("</class>");
                sw.WriteLine("</nhv-mapping>");
                sw.Flush();
            }

            XmlMappingLoader ml = new XmlMappingLoader();

            using (StreamReader sr = new StreamReader(tmpf))
            {
                ml.AddInputStream(sr.BaseStream, tmpf);
            }
            NhvMapping      map = ml.Mappings[0];
            NhvmClass       cm  = map.@class[0];
            XmlClassMapping rm  = new XmlClassMapping(cm);
            MemberInfo      mi;

            mi = typeof(WellKnownRules).GetField("AP");
            Assert.That(() => rm.GetMemberAttributes(mi), Throws.TypeOf <InvalidPropertyNameException>());
        }
Ejemplo n.º 2
0
        public void MixingLoaders()
        {
            string xml =
                @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'>
		<mapping assembly='NHibernate.Validator.Tests' resource='NHibernate.Validator.Tests.Base.Address.nhv.xml'/>
	</nhv-configuration>"    ;
            XmlDocument cfgXml = new XmlDocument();

            cfgXml.LoadXml(xml);
            XmlTextReader    xtr = new XmlTextReader(xml, XmlNodeType.Document, null);
            XmlConfiguration cfg = new XmlConfiguration(xtr);
            XmlMappingLoader ml  = new XmlMappingLoader();

            ml.LoadMappings(cfg.Mappings);

            string tmpf = Path.GetTempFileName();

            using (StreamWriter sw = new StreamWriter(tmpf))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");
                sw.WriteLine("<nhv-mapping xmlns='urn:nhibernate-validator-1.0'>");
                sw.WriteLine("<class name='Boo'>");
                sw.WriteLine("<property name='field'><notnullorempty/></property>");
                sw.WriteLine("</class>");
                sw.WriteLine("</nhv-mapping>");
                sw.Flush();
            }
            ml.AddFile(tmpf);
            Assert.AreEqual(2, ml.Mappings.Length);
        }
Ejemplo n.º 3
0
        public void GetMappingForType()
        {
            NhvMapping mapping = XmlMappingLoader.GetXmlMappingFor(typeof(Base.Address));

            Assert.IsNotNull(mapping);
            Assert.IsTrue(mapping == mapping.@class[0].rootMapping);
            Assert.IsNull(XmlMappingLoader.GetXmlMappingFor(typeof(Base.Building)));
        }
Ejemplo n.º 4
0
        public void AddAssemblyByName()
        {
            // in this test we only try to load an entirely assembly without check what was load
            XmlMappingLoader ml = new XmlMappingLoader();

            ml.AddAssembly(Assembly.GetExecutingAssembly().FullName);
            Assert.Less(0, ml.Mappings.Length);
        }
Ejemplo n.º 5
0
        private static NhvmClass GetNhvClassFor(System.Type currentClass)
        {
            NhvMapping mapp = XmlMappingLoader.GetXmlMappingFor(currentClass);

            if (mapp != null && [email protected] > 0)
            {
                return(mapp.@class[0]);
            }
            return(null);
        }
        protected virtual IClassMapping GetExternalDefinitionFor(System.Type type)
        {
            log.Debug("XML convention applied for " + type.FullName);
            NhvMapping mapp = XmlMappingLoader.GetXmlMappingFor(type);

            if (mapp != null && [email protected] > 0)
            {
                return(new XmlClassMapping(mapp.@class[0]));
            }

            return(null);
        }
Ejemplo n.º 7
0
        public void ResourceNotFound()
        {
            string xml =
                @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'>
		<mapping assembly='NHibernate.Validator.Tests' resource='Base.Address.nhv.xml'/>
	</nhv-configuration>"    ;
            XmlDocument cfgXml = new XmlDocument();

            cfgXml.LoadXml(xml);
            XmlTextReader    xtr = new XmlTextReader(xml, XmlNodeType.Document, null);
            XmlConfiguration cfg = new XmlConfiguration(xtr);
            XmlMappingLoader ml  = new XmlMappingLoader();

            ActionAssert.Throws <ValidatorConfigurationException>(() => ml.LoadMappings(cfg.Mappings));
        }
Ejemplo n.º 8
0
        public void AddWrongFile()
        {
            string tmpf = Path.GetTempFileName();

            using (StreamWriter sw = new StreamWriter(tmpf))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");
                sw.WriteLine("<nhv-mapping xmlns='urn:nhibernate-validator-1.0'>");
                sw.WriteLine("<no valid node>");
                sw.WriteLine("</nhv-mapping>");
                sw.Flush();
            }
            XmlMappingLoader ml = new XmlMappingLoader();

            ActionAssert.Throws <ValidatorConfigurationException>(() => ml.AddFile(tmpf));
        }
Ejemplo n.º 9
0
        public void CompiledMappings()
        {
            const string xml    = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'>
		<mapping assembly='NHibernate.Validator.Tests' resource='NHibernate.Validator.Tests.Base.Address.nhv.xml'/>
	</nhv-configuration>"    ;
            var          cfgXml = new XmlDocument();

            cfgXml.LoadXml(xml);
            var xtr = new XmlTextReader(xml, XmlNodeType.Document, null);
            var cfg = new XmlConfiguration(xtr);
            var ml  = new XmlMappingLoader();

            ml.LoadMappings(cfg.Mappings);
            var cm = ml.GetMappings();

            Assert.That(cm.Count(), Is.EqualTo(1));
            Assert.That(cm.FirstOrDefault(x => x.EntityType == typeof(Base.Address)), Is.Not.Null);
        }
Ejemplo n.º 10
0
        public void AddFile()
        {
            string tmpf = Path.GetTempFileName();

            using (StreamWriter sw = new StreamWriter(tmpf))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");
                sw.WriteLine("<nhv-mapping xmlns='urn:nhibernate-validator-1.0'>");
                sw.WriteLine("<class name='Boo'>");
                sw.WriteLine("<property name='field'><notnullorempty/></property>");
                sw.WriteLine("</class>");
                sw.WriteLine("</nhv-mapping>");
                sw.Flush();
            }
            XmlMappingLoader ml = new XmlMappingLoader();

            ml.AddFile(tmpf);
            Assert.AreEqual(1, ml.Mappings.Length);
        }
Ejemplo n.º 11
0
        public void AddWrongStream()
        {
            string tmpf = Path.GetTempFileName();

            using (StreamWriter sw = new StreamWriter(tmpf))
            {
                sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");
                sw.WriteLine("<nhv-mapping xmlns='urn:nhibernate-validator-1.0'>");
                sw.WriteLine("<no valid node>");
                sw.WriteLine("</nhv-mapping>");
                sw.Flush();
            }

            XmlMappingLoader ml = new XmlMappingLoader();

            using (StreamReader sr = new StreamReader(tmpf))
            {
                Assert.That(() => ml.AddInputStream(sr.BaseStream, tmpf), Throws.TypeOf <ValidatorConfigurationException>());
            }
        }
        public void KnownRulesConvertAssing()
        {
            NhvMapping       map = XmlMappingLoader.GetXmlMappingFor(typeof(WellKnownRules));
            NhvmClass        cm  = map.@class[0];
            XmlClassMapping  rm  = new XmlClassMapping(cm);
            MemberInfo       mi;
            List <Attribute> attributes;

            mi         = typeof(WellKnownRules).GetField("AP");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            Assert.AreEqual("A string value", ((ACustomAttribute)attributes[0]).Value1);
            Assert.AreEqual(123, ((ACustomAttribute)attributes[0]).Value2);
            Assert.AreEqual("custom message", ((ACustomAttribute)attributes[0]).Message);

            mi         = typeof(WellKnownRules).GetField("StrProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            NotEmptyAttribute nea = FindAttribute <NotEmptyAttribute>(attributes);

            Assert.AreEqual("not-empty message", nea.Message);

            NotNullAttribute nna = FindAttribute <NotNullAttribute>(attributes);

            Assert.AreEqual("not-null message", nna.Message);

            NotNullNotEmptyAttribute nnea = FindAttribute <NotNullNotEmptyAttribute>(attributes);

            Assert.AreEqual("notnullnotempty message", nnea.Message);

            LengthAttribute la = FindAttribute <LengthAttribute>(attributes);

            Assert.AreEqual("length message", la.Message);
            Assert.AreEqual(1, la.Min);
            Assert.AreEqual(10, la.Max);

            PatternAttribute pa = FindAttribute <PatternAttribute>(attributes);

            Assert.AreEqual("pattern message", pa.Message);
            Assert.AreEqual("[0-9]+", pa.Regex);
            Assert.AreEqual(RegexOptions.Compiled, pa.Flags);

            EmailAttribute ea = FindAttribute <EmailAttribute>(attributes);

            Assert.AreEqual("email message", ea.Message);

            IPAddressAttribute ipa = FindAttribute <IPAddressAttribute>(attributes);

            Assert.AreEqual("ipAddress message", ipa.Message);

            EANAttribute enaa = FindAttribute <EANAttribute>(attributes);

            Assert.AreEqual("ean message", enaa.Message);

            CreditCardNumberAttribute ccna = FindAttribute <CreditCardNumberAttribute>(attributes);

            Assert.AreEqual("creditcardnumber message", ccna.Message);

            IBANAttribute iban = FindAttribute <IBANAttribute>(attributes);

            Assert.AreEqual("iban message", iban.Message);

            mi         = typeof(WellKnownRules).GetField("DtProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            FutureAttribute fa = FindAttribute <FutureAttribute>(attributes);

            Assert.AreEqual("future message", fa.Message);
            PastAttribute psa = FindAttribute <PastAttribute>(attributes);

            Assert.AreEqual("past message", psa.Message);

            mi         = typeof(WellKnownRules).GetField("DecProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            DigitsAttribute dga = FindAttribute <DigitsAttribute>(attributes);

            Assert.AreEqual("digits message", dga.Message);
            Assert.AreEqual(5, dga.IntegerDigits);
            Assert.AreEqual(2, dga.FractionalDigits);

            MinAttribute mina = FindAttribute <MinAttribute>(attributes);

            Assert.AreEqual("min message", mina.Message);
            Assert.AreEqual(100, mina.Value);

            MaxAttribute maxa = FindAttribute <MaxAttribute>(attributes);

            Assert.AreEqual("max message", maxa.Message);
            Assert.AreEqual(200, maxa.Value);

            DecimalMaxAttribute decimalmaxa = FindAttribute <DecimalMaxAttribute>(attributes);

            Assert.AreEqual("decimal max message", decimalmaxa.Message);
            Assert.AreEqual(200.1m, decimalmaxa.Value);

            DecimalMinAttribute decimalmina = FindAttribute <DecimalMinAttribute>(attributes);

            Assert.AreEqual("decimal min message", decimalmina.Message);
            Assert.AreEqual(99.9m, decimalmina.Value);

            mi         = typeof(WellKnownRules).GetField("BProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            AssertTrueAttribute ata = FindAttribute <AssertTrueAttribute>(attributes);

            Assert.AreEqual("asserttrue message", ata.Message);
            AssertFalseAttribute afa = FindAttribute <AssertFalseAttribute>(attributes);

            Assert.AreEqual("assertfalse message", afa.Message);


            mi         = typeof(WellKnownRules).GetField("ArrProp");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            SizeAttribute sa = FindAttribute <SizeAttribute>(attributes);

            Assert.AreEqual("size message", sa.Message);
            Assert.AreEqual(2, sa.Min);
            Assert.AreEqual(9, sa.Max);

            mi         = typeof(WellKnownRules).GetField("Pattern");
            attributes = new List <Attribute>(rm.GetMemberAttributes(mi));
            PatternAttribute spa = FindAttribute <PatternAttribute>(attributes);

            Assert.AreEqual("{validator.pattern}", spa.Message);
            Assert.AreEqual(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", spa.Regex);
            Assert.AreEqual(RegexOptions.CultureInvariant | RegexOptions.IgnoreCase, spa.Flags);
        }
Ejemplo n.º 13
0
        public void AddWrongAssembly()
        {
            XmlMappingLoader ml = new XmlMappingLoader();

            Assert.Throws <ValidatorConfigurationException>(() => ml.AddAssembly("NoExistAssemblyName"));
        }
Ejemplo n.º 14
0
        public void AddWrongFileName()
        {
            XmlMappingLoader ml = new XmlMappingLoader();

            Assert.Throws <ValidatorConfigurationException>(() => ml.AddFile("NoExistFile"), "Could not load file NoExistFile");
        }
Ejemplo n.º 15
0
        public void LoadMappingsNull()
        {
            XmlMappingLoader ml = new XmlMappingLoader();

            ActionAssert.Throws <ArgumentNullException>(() => ml.LoadMappings(null));
        }
Ejemplo n.º 16
0
        public void LoadMappingsNull()
        {
            XmlMappingLoader ml = new XmlMappingLoader();

            Assert.That(() => ml.LoadMappings(null), Throws.TypeOf <ArgumentNullException>());
        }