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);
 }
 /// <summary>
 /// Load and return a mapping for a given type.
 /// </summary>
 /// <param name="type">The given type.</param>
 /// <returns>The mapping.</returns>
 /// <remarks>
 /// The method use a convention to find the resource that represent the mapping for the given class.
 /// - The mapping must be compiled like embedded resource in the same assembly of the given type
 /// - The name o the resource must be the same name of the type and end with ".nhv.xml"
 /// - The resource must stay in the same namespace of the type
 /// </remarks>
 public static NhvMapping GetXmlMappingFor(System.Type type)
 {
     string resourceName = type.FullName + MappingFileDefaultExtension;
     var ml = new XmlMappingLoader();
     try
     {
         ml.AddResource(type.Assembly, resourceName);
     }
     catch (ValidatorConfigurationException)
     {
         return null;
     }
     return ml.Mappings[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);
 }
 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();
     ml.AddFile(tmpf);
 }
 public void AddWrongAssembly()
 {
     XmlMappingLoader ml = new XmlMappingLoader();
     ml.AddAssembly("NoExistAssemblyName");
 }
 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();
     ml.LoadMappings(cfg.Mappings);
 }
        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);
        }
 public void LoadMappingsNull()
 {
     XmlMappingLoader ml = new XmlMappingLoader();
     ml.LoadMappings(null);
 }
        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);
        }
        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))
            {
                ml.AddInputStream(sr.BaseStream, tmpf);
            }
            Assert.AreEqual(1, ml.Mappings.Length);
        }
 public void AddWrongFileName()
 {
     XmlMappingLoader ml = new XmlMappingLoader();
     Assert.Throws<ValidatorConfigurationException>(() => ml.AddFile("NoExistFile"),"Could not load file NoExistFile");
 }
Ejemplo n.º 12
0
 public void AddWrongAssembly()
 {
     XmlMappingLoader ml = new XmlMappingLoader();
     Assert.Throws<ValidatorConfigurationException>(() =>ml.AddAssembly("NoExistAssemblyName"));
 }
Ejemplo n.º 13
0
 public void LoadMappingsNull()
 {
     XmlMappingLoader ml = new XmlMappingLoader();
     ActionAssert.Throws<ArgumentNullException>(() => ml.LoadMappings(null));
 }
Ejemplo n.º 14
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))
            {
                ActionAssert.Throws<ValidatorConfigurationException>(() => ml.AddInputStream(sr.BaseStream, tmpf));
            }
        }