public void MessengerProviderNode() { TestingConfiguration config = new TestingConfiguration(); config.Load("desktop"); MessengerProviderNode node = config.MessengerProviderNodes["SmsMessenger"] as MessengerProviderNode; Assert.IsNotNull(node); Assert.AreEqual("SmsMessenger", node.Name); Assert.AreEqual("Nohros.Test.Toolkit.Messaging.SmsMessenger, nohros.test.desktop", node.Type); Assert.AreEqual(null, node.AssemblyLocation); node = config.MessengerProviderNodes["SimpleMessenger"] as MessengerProviderNode; Assert.IsNotNull(node); Assert.AreEqual("SimpleMessenger", node.Name); Assert.AreEqual("Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop", node.Type); Assert.AreEqual(null, node.AssemblyLocation); string host, port; Assert.IsTrue(node.Options.TryGetValue("smtp-host", out host)); Assert.IsTrue(node.Options.TryGetValue("smtp-port", out port)); Assert.AreEqual(host, "smtp.acaoassessoria.com.br"); Assert.AreEqual(port, "587"); }
/// <summary> /// Creates an instance of the <see cref="IMessenger"/> class by using the /// factory class that was defined in the specified configuration node. /// </summary> /// <param name="provider">A <see cref="MessengerProviderNode"/> object /// containing informations that will be used to create the designated /// type.</param> /// <returns>A reference to the newly created object.</returns> /// <exception cref="ArgumentNullException"><paramref name="provider"/> /// is null.</exception> /// <exception cref="ProviderException">A valid instance of the factory /// could not be created -or - a exception was throwed by the factory. /// </exception> /// <remarks> /// <para> /// If the factory could not be instantiated or if it throws an exception /// it won't be propagated to the caller. This method packs the exception /// into a <see cref="ProviderException"/> and throw it. /// </para> /// </remarks> public static IMessenger CreateInstance(MessengerProviderNode provider) { if (provider == null) throw new ArgumentNullException("provider"); IMessengerFactory factory; Type type = ProviderHelper.GetTypeFromProviderNode(provider); if (type == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } try { factory = Activator.CreateInstance(type) as IMessengerFactory; if (factory == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } IMessenger messenger = factory.CreateMessenger(provider.Name, provider.Options); // Sanitu check for nulls. By default messenger factories should not // return nulls, but we can trust on others code. if (messenger == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } return messenger; } catch (Exception e) { // pack the throwed exception and rethrow it. throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessenger"), e); } }
public void GetRequiredOptionNullOption() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); messenger.GetRequiredOptionNull(); }
public void GetRequiredOption() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); node.Options = new Dictionary<string, string>(); node.Options["required-option-name"] = "required-option-value"; SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); Assert.AreEqual("required-option-value", messenger.GetRequiredOption("required-option-name")); }
public void CreateInstanceNullOptions() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); node.Options = null; SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); }
public void GetRequiredOption() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); node.Options = new Dictionary <string, string>(); node.Options["required-option-name"] = "required-option-value"; SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); Assert.AreEqual("required-option-value", messenger.GetRequiredOption("required-option-name")); }
/// <summary> /// Creates an instance of the <see cref="IMessenger"/> class by using the /// factory class that was defined in the specified configuration node. /// </summary> /// <param name="provider">A <see cref="MessengerProviderNode"/> object /// containing informations that will be used to create the designated /// type.</param> /// <returns>A reference to the newly created object.</returns> /// <exception cref="ArgumentNullException"><paramref name="provider"/> /// is null.</exception> /// <exception cref="ProviderException">A valid instance of the factory /// could not be created -or - a exception was throwed by the factory. /// </exception> /// <remarks> /// <para> /// If the factory could not be instantiated or if it throws an exception /// it won't be propagated to the caller. This method packs the exception /// into a <see cref="ProviderException"/> and throw it. /// </para> /// </remarks> public static IMessenger CreateInstance(MessengerProviderNode provider) { if (provider == null) { throw new ArgumentNullException("provider"); } IMessengerFactory factory; Type type = ProviderHelper.GetTypeFromProviderNode(provider); if (type == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } try { factory = Activator.CreateInstance(type) as IMessengerFactory; if (factory == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } IMessenger messenger = factory.CreateMessenger(provider.Name, provider.Options); // Sanitu check for nulls. By default messenger factories should not // return nulls, but we can trust on others code. if (messenger == null) { throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessengerFactory")); } return(messenger); } catch (Exception e) { // pack the throwed exception and rethrow it. throw new ProviderException(string.Format( StringResources.Type_CreateInstanceOf, "IMessenger"), e); } }
/// <summary> /// Intiailizes a new instance of the <see cref="MessengerChain"/> class by /// using the specified chain name and configuration object. /// </summary> /// <param name="name"></param> /// <returns>An instance of the <see cref="MessengerChain"/> class with /// name <paramref name="name"/> configured accordingly to the specified /// configuration object.</returns> /// <remarks> /// This method will try to get a chain with name <paramref name="name"/> /// from the configuration object and loop through the chain trying to get /// messengers with name equals to the name of each chain node. /// <para> /// If a chain with name <paramref name="name"/> was not found, a empty /// chain will be created. /// </para> /// </remarks> public MessengerChain CreateMessengerChain(string name, Configuration.Configuration config) { MessengerChain messenger_chain = new MessengerChain(name); ChainNode chain = config.ChainNodes[name]; if (chain != null) { string[] nodes = chain.Nodes; for (int i = 0, j = nodes.Length; i < j; i++) { MessengerProviderNode node = config.MessengerProviderNodes[nodes[i]]; if (node != null) { IMessenger messenger = Messenger.CreateInstance(node); messenger_chain.Add(messenger); } } } return(messenger_chain); }
public void GetRequiredOptionNullOption() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); messenger.GetRequiredOptionNull(); }
public void CreateInstanceNullOptions() { MessengerProviderNode node = new MessengerProviderNode("SimpleMessenger", "Nohros.Test.Toolkit.Messaging.SimpleMessenger, nohros.test.desktop"); node.Options = null; SimpleMessenger messenger = Messenger.CreateInstance(node) as SimpleMessenger; Assert.IsNotNull(messenger); }