private void btnSend_Click(object sender, EventArgs e) { //NOTE...IMPORTANT!: //This is an example of how-to-change-the-configuration. //You should maintain in your code just 1 (one) ValidatorEngine, //see SharedEngine: http://nhforge.org/blogs/nhibernate/archive/2009/02/26/diving-in-nhibernate-validator.aspx ValidatorEngine ve = vvtor.ValidatorEngine; ve.Clear(); //Configuration of NHV. You can also configure this stuff using xml, outside of the code XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "false"; nhvc.Properties[Environment.ValidatorMode] = GetMode(); nhvc.Mappings.Add(new MappingConfiguration("NHibernate.Validator.Demo.Winforms",null)); ve.Configure(nhvc); Customer customer = GetCustomerFromUI(); if (ve.IsValid(customer)) { listBox1.Items.Clear(); Send(); } else { InvalidValue[] values = ve.Validate(customer); FillErrorsOnListBox(values); } }
public void ApplyWrongConstraint() { XmlConfigurator.Configure(); NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); cfg.AddResource("NHibernate.Validator.Tests.Integration.WrongClass.whbm.xml", Assembly.GetExecutingAssembly()); Environment.SharedEngineProvider = null; XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "true"; nhvc.Properties[Environment.AutoregisterListeners] = "true"; nhvc.Properties[Environment.ValidatorMode] = "UseAttribute"; using (LoggerSpy ls = new LoggerSpy(typeof(ValidatorInitializer), Level.Warn)) { ValidatorInitializer.Initialize(cfg); int found = ls.GetOccurenceContaining( string.Format("Unable to apply constraints on DDL for [MappedClass={0}]", typeof (WrongClass).FullName)); Assert.AreEqual(1, found); found = ls.GetOccurenceContaining( string.Format("Unable to apply constraints on DDL for [MappedClass={0}]", typeof (WrongClass1).FullName)); Assert.AreEqual(1, found); } }
public void CreateEngine() { var conf = new XmlConfiguration(); conf.Properties[Environment.ValidatorMode] = "UseExternal"; conf.Mappings.Add(new MappingConfiguration("NHibernate.Validator.Tests", "NHibernate.Validator.Tests.Engine.Tagging.EntityXml.nhv.xml")); ve = new ValidatorEngine(); ve.Configure(conf); }
public virtual ValidatorEngine GetValidatorEngine() { var ve = new ValidatorEngine(); var cfg = new XmlConfiguration(); cfg.Properties[Environment.ValidatorMode] = "UseAttribute"; cfg.Properties[Environment.ConstraintValidatorFactoryClass] = typeof (TestConstraintValidatorFactory).AssemblyQualifiedName; ve.Configure(cfg); return ve; }
public ValidatorEngine GetValidatorEngine() { var vtor = new ValidatorEngine(); var cfg = new XmlConfiguration(); cfg.Properties[Environment.ValidatorMode] = "UseExternal"; string an = Assembly.GetExecutingAssembly().FullName; cfg.Mappings.Add(new MappingConfiguration(an, "NHibernate.Validator.Tests.Specifics.NHV29.Mappings.nhv.xml")); vtor.Configure(cfg); return vtor; }
public static ValidatorEngine Get_Engine_Configured_for_Xml() { var vtor = new ValidatorEngine(); var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "false"; nhvc.Properties[Environment.ValidatorMode] = "UseExternal"; nhvc.Mappings.Add(new MappingConfiguration("NHibernate.Validator.Demo.Ev", null)); vtor.Configure(nhvc); return vtor; }
public void ClassNotFound() { const string xml = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <mapping assembly='NHibernate.Validator.Tests' resource='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 FluentMappingLoader(); Assert.Throws<ValidatorConfigurationException>(() => ml.LoadMappings(cfg.Mappings)); }
public void IgnoreEmptyConfiguration() { string xml = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> </nhv-configuration>"; XmlDocument cfgXml = new XmlDocument(); cfgXml.LoadXml(xml); XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); XmlConfiguration cfg = new XmlConfiguration(xtr); Assert.AreEqual(0, cfg.Properties.Count); Assert.AreEqual(0, cfg.Mappings.Count); }
protected override void Configure(NHibernate.Cfg.Configuration configuration) { Environment.SharedEngineProvider = new NHibernateSharedEngineProvider(); vtor = Environment.SharedEngineProvider.GetEngine(); vtor.Clear(); var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "true"; nhvc.Properties[Environment.ValidatorMode] = "UseAttribute"; vtor.Configure(nhvc); configuration.Initialize(); }
public void CanReadEntityTypeInspectors() { string xml = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <entity-type-inspector class='NHibernate.Validator.Engine.DefaultEntityTypeInspector, NHibernate.Validator'/> </nhv-configuration>"; XmlDocument cfgXml = new XmlDocument(); cfgXml.LoadXml(xml); XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); XmlConfiguration cfg = new XmlConfiguration(xtr); Assert.That(cfg.EntityTypeInspectors, Is.Not.Empty); Assert.That(cfg.EntityTypeInspectors.First(), Is.EqualTo(typeof(Validator.Engine.DefaultEntityTypeInspector))); }
public void CanReadCustomResourceManager() { XmlConfigurator.Configure(); string xml = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <property name='resource_manager'>YourFullNameSpace.TheBaseNameOfTheResourceFileWithoutExtensionNorCulture, YourAssembly</property> </nhv-configuration>"; XmlDocument cfgXml = new XmlDocument(); cfgXml.LoadXml(xml); XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); XmlConfiguration cfg = new XmlConfiguration(xtr); Assert.That(cfg.Properties["resource_manager"], Is.EqualTo("YourFullNameSpace.TheBaseNameOfTheResourceFileWithoutExtensionNorCulture, YourAssembly")); }
public void ShouldAppendToExistingListeners() { var cfg = new NHibernate.Cfg.Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); cfg.SetListener(ListenerType.PreInsert, new ListenersStub()); cfg.SetListener(ListenerType.PreUpdate, new ListenersStub()); var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "true"; nhvc.Properties[Environment.AutoregisterListeners] = "true"; nhvc.Properties[Environment.ValidatorMode] = "UseAttribute"; ValidatorInitializer.Initialize(cfg); Assert.That(cfg.EventListeners.PreInsertEventListeners.Length, Is.EqualTo(2)); Assert.That(cfg.EventListeners.PreInsertEventListeners[1], Is.TypeOf<ValidatePreInsertEventListener>()); Assert.That(cfg.EventListeners.PreUpdateEventListeners.Length, Is.EqualTo(2)); Assert.That(cfg.EventListeners.PreUpdateEventListeners[1], Is.TypeOf<ValidatePreUpdateEventListener>()); }
public void WorkAfterDeserialization() { ValidatorEngine ve = new ValidatorEngine(); XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Cfg.Environment.ValidatorMode] = "UseAttribute"; nhvc.Properties[Cfg.Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); RunSerializationTest(ve); ve = new ValidatorEngine(); nhvc = new XmlConfiguration(); nhvc.Properties[Cfg.Environment.ValidatorMode] = "UseExternal"; nhvc.Properties[Cfg.Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); RunSerializationTest(ve); }
protected override void Configure(NHibernate.Cfg.Configuration configuration) { // The ValidatorInitializer and the ValidateEventListener share the same engine // Initialize the SharedEngine fortest = new NHibernateSharedEngineProvider(); Environment.SharedEngineProvider = fortest; ValidatorEngine ve = Environment.SharedEngineProvider.GetEngine(); ve.Clear(); XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "true"; nhvc.Properties[Environment.AutoregisterListeners] = "true"; nhvc.Properties[Environment.ValidatorMode] = "useExternal"; nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); ve.IsValid(new AnyClass());// add the element to engine for test ValidatorInitializer.Initialize(configuration); }
public void IgnoreEmptyItems() { string xml = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <property name='apply_to_ddl'></property> <property name='autoregister_listeners'></property> <property name='message_interpolator_class'></property> <property name='default_validator_mode'></property> <mapping assembly=''/> <mapping file=''/> <mapping assembly='' resource=''/> </nhv-configuration>"; XmlDocument cfgXml = new XmlDocument(); cfgXml.LoadXml(xml); XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); XmlConfiguration cfg = new XmlConfiguration(xtr); Assert.AreEqual(0, cfg.Properties.Count); Assert.AreEqual(0, cfg.Mappings.Count); }
public void LoadMappingsSpecific() { var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ValidatorMode] = "useExternal"; nhvc.Properties[Environment.MappingLoaderClass] = "NHibernate.Validator.Cfg.Loquacious.FluentMappingLoader, NHibernate.Validator"; nhvc.Mappings.Add(new MappingConfiguration("NHibernate.Validator.Tests", "NHibernate.Validator.Tests.Configuration.Loquacious.AddressValidationDef")); nhvc.Mappings.Add(new MappingConfiguration("NHibernate.Validator.Tests", "NHibernate.Validator.Tests.Configuration.Loquacious.BooValidationDef")); var ve = new ValidatorEngine(); ve.Configure(nhvc); var a = new Address {Country = string.Empty}; var b = new Boo(); Assert.That(ve.IsValid(a)); Assert.That(!ve.IsValid(b)); a.Country = "bigThan5Chars"; Assert.That(!ve.IsValid(a)); b.field = "whatever"; Assert.That(ve.IsValid(b)); }
public void WorkWithOutSharedEngine() { NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); if (TestConfigurationHelper.hibernateConfigFile != null) cfg.Configure(TestConfigurationHelper.hibernateConfigFile); string[] mappings = new string[] { "Integration.Address.hbm.xml", "Integration.Tv.hbm.xml", "Integration.TvOwner.hbm.xml", "Integration.Martian.hbm.xml", "Integration.Music.hbm.xml" }; foreach (string file in mappings) { cfg.AddResource("NHibernate.Validator.Tests" + "." + file, Assembly.GetExecutingAssembly()); } Environment.SharedEngineProvider = null; XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "true"; nhvc.Properties[Environment.AutoregisterListeners] = "true"; nhvc.Properties[Environment.ValidatorMode] = "UseAttribute"; ValidatorInitializer.Initialize(cfg); }
public void WhenExceptionWithResourceManager_ValidatorConfigurationException() { var ve = new ValidatorEngine(); var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.CustomResourceManager] = "Whatever.Messages, Whatever"; var exception = Assert.Throws<ValidatorConfigurationException>(() => ve.Configure(nhvc)); Assert.That(exception.Message, Text.Contains("resource manager")); }
public void UseCustomResourceManager() { var ve = new ValidatorEngine(); var nhvc = new XmlConfiguration(); nhvc.Properties[Environment.CustomResourceManager] = "NHibernate.Validator.Tests.Resource.Messages, " + Assembly.GetExecutingAssembly().FullName; nhvc.Properties[Environment.ValidatorMode] = "UseAttribute"; ve.Configure(nhvc); var a = new Address {floor = -50}; var iv = ve.Validate(a); Assert.That(iv.Any(x => x.Message.StartsWith("Floor cannot"))); }
public void GetInterpolator() { ValidatorEngine ve = new ValidatorEngine(); XmlConfiguration nhvc = new XmlConfiguration(); try { nhvc.Properties[Environment.MessageInterpolatorClass] = typeof (NoDefConstructorInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid interpolator"); } catch(ValidatorConfigurationException e) { Assert.AreEqual( "Public constructor was not found at message interpolator: " + typeof (NoDefConstructorInterpolator).AssemblyQualifiedName, e.Message); } try { nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(WhatEver).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid interpolator"); } catch (ValidatorConfigurationException e) { Assert.AreEqual( "Type does not implement '" + typeof(IMessageInterpolator).FullName + "': " + typeof(WhatEver).AssemblyQualifiedName, e.Message); } try { nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(BadConstructorInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid interpolator"); } catch (ValidatorConfigurationException e) { Assert.AreEqual( "Unable to instanciate message interpolator: " + typeof(BadConstructorInterpolator).AssemblyQualifiedName, e.Message); } }
public void DuplicateClassDef() { XmlConfigurator.Configure(); ValidatorEngine ve = new ValidatorEngine(); XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "false"; nhvc.Properties[Environment.ValidatorMode] = "useExternal"; string an = Assembly.GetExecutingAssembly().FullName; nhvc.Mappings.Add(new MappingConfiguration(an, "NHibernate.Validator.Tests.Base.Address.nhv.xml")); nhvc.Mappings.Add(new MappingConfiguration(an, "NHibernate.Validator.Tests.Engine.DuplicatedAddress.nhv.xml")); using (LoggerSpy ls = new LoggerSpy("NHibernate.Validator.Engine.StateFullClassMappingFactory", Level.Warn)) { ve.Configure(nhvc); int found = ls.GetOccurencesOfMessage( x => x.StartsWith("Duplicated external definition for class " + typeof (Address).AssemblyQualifiedName)); Assert.AreEqual(1, found); } }
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 GetMappingLoader() { var ve = new ValidatorEngine(); var nhvc = new XmlConfiguration(); try { nhvc.Properties[Environment.MappingLoaderClass] = typeof(NoDefConstructorLoader).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid loader"); } catch (ValidatorConfigurationException e) { Assert.AreEqual( "Public constructor was not found at mapping loader: " + typeof(NoDefConstructorLoader).AssemblyQualifiedName, e.Message); } try { nhvc.Properties[Environment.MappingLoaderClass] = typeof(WhatEver).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid loader"); } catch (ValidatorConfigurationException e) { Assert.AreEqual( "Type does not implement '" + typeof(IMappingLoader).FullName + "': " + typeof(WhatEver).AssemblyQualifiedName, e.Message); } try { nhvc.Properties[Environment.MappingLoaderClass] = typeof(BadConstructorLoader).AssemblyQualifiedName; ve.Configure(nhvc); Assert.Fail("Expected exception for invalid loader"); } catch (ValidatorConfigurationException e) { Assert.AreEqual( "Unable to instanciate mapping loader: " + typeof(BadConstructorLoader).AssemblyQualifiedName, e.Message); } // should work nhvc.Properties[Environment.MappingLoaderClass] = typeof(XmlMappingLoader).AssemblyQualifiedName; ve.Configure(nhvc); }
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 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 LoadMappingsFromAssembly() { const string xmlConf = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <mapping assembly='NHibernate.Validator.Tests'/> </nhv-configuration>"; var cfgXml = new XmlDocument(); cfgXml.LoadXml(xmlConf); var xtr = new XmlTextReader(xmlConf, XmlNodeType.Document, null); var cfg = new XmlConfiguration(xtr); var ml = new FluentMappingLoader(); ml.LoadMappings(cfg.Mappings); Assert.That(ml.GetMappings().Count(), Is.GreaterThan(1)); // the mappings of tests are more than 1 ;) }
public void InitializeValidators() { ValidatorEngine ve = new ValidatorEngine(); XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "false"; nhvc.Properties[Environment.ValidatorMode] = "overrideattributewithExternal"; nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName; string an = Assembly.GetExecutingAssembly().FullName; nhvc.Mappings.Add(new MappingConfiguration(an, "NHibernate.Validator.Tests.Base.Address.nhv.xml")); ve.Configure(nhvc); Assert.IsNotNull(ve.GetValidator<Address>()); Assert.IsNull(ve.GetValidator<Boo>()); // Validate something and then its validator is initialized Boo b = new Boo(); ve.IsValid(b); Assert.IsNotNull(ve.GetValidator<Boo>()); }
public void LoadMappingsSpecific() { const string xmlConf = @"<nhv-configuration xmlns='urn:nhv-configuration-1.0'> <mapping assembly='NHibernate.Validator.Tests' resource='NHibernate.Validator.Tests.Configuration.Loquacious.AddressValidationDef'/> <mapping assembly='NHibernate.Validator.Tests' resource='NHibernate.Validator.Tests.Configuration.Loquacious.BooValidationDef'/> </nhv-configuration>"; var cfgXml = new XmlDocument(); cfgXml.LoadXml(xmlConf); var xtr = new XmlTextReader(xmlConf, XmlNodeType.Document, null); var cfg = new XmlConfiguration(xtr); var ml = new FluentMappingLoader(); ml.LoadMappings(cfg.Mappings); Assert.That(ml.GetMappings().Count(), Is.EqualTo(2)); }
private static ValidatorEngine CreateValidatorEngine() { var cfg = new XmlConfiguration(); cfg.Mappings.Add(new MappingConfiguration(typeof(User).Assembly.FullName, null)); var ve = new ValidatorEngine(); ve.Configure(cfg); return ve; }
public void ConfigureNHVConfiguration() { ValidatorEngine ve = new ValidatorEngine(); XmlConfiguration nhvc = new XmlConfiguration(); nhvc.Properties[Environment.ApplyToDDL] = "false"; nhvc.Properties[Environment.AutoregisterListeners] = "false"; nhvc.Properties[Environment.ValidatorMode] = "UseExternal"; nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(PrefixMessageInterpolator).AssemblyQualifiedName; ve.Configure(nhvc); Assert.AreEqual(false, ve.ApplyToDDL); Assert.AreEqual(false, ve.AutoRegisterListeners); Assert.AreEqual(ValidatorMode.UseExternal, ve.DefaultMode); Assert.IsNotNull(ve.Interpolator); Assert.AreEqual(typeof(PrefixMessageInterpolator), ve.Interpolator.GetType()); }