ChannelFactoryTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // ChannelFactoryTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.ObjectModel;
  30. using System.ServiceModel;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Description;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.ServiceModel
  35. {
  36. [TestFixture]
  37. public class ChannelFactoryTest
  38. {
  39. class SimpleChannelFactory
  40. : ChannelFactory
  41. {
  42. public SimpleChannelFactory ()
  43. : base ()
  44. {
  45. }
  46. protected override TimeSpan DefaultCloseTimeout {
  47. get { return TimeSpan.FromMinutes (1); }
  48. }
  49. protected override TimeSpan DefaultOpenTimeout {
  50. get { return TimeSpan.FromMinutes (1); }
  51. }
  52. protected override ServiceEndpoint CreateDescription ()
  53. {
  54. return new ServiceEndpoint (ContractDescription.GetContract (typeof(ICtorUseCase2)));
  55. }
  56. public void InitEndpoint (Binding b, EndpointAddress addr)
  57. {
  58. base.InitializeEndpoint (b, addr);
  59. }
  60. public void InitEndpoint (string configName, EndpointAddress addr)
  61. {
  62. base.InitializeEndpoint (configName, addr);
  63. }
  64. public void ApplyConfig (string configName)
  65. {
  66. base.ApplyConfiguration (configName);
  67. }
  68. }
  69. [Test]
  70. public void InitializeEndpointTest1 ()
  71. {
  72. SimpleChannelFactory factory = new SimpleChannelFactory ();
  73. Assert.AreEqual (null, factory.Endpoint, "#01");
  74. Binding b = new WSHttpBinding ();
  75. factory.InitEndpoint (b, null);
  76. Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
  77. Assert.AreEqual (null, factory.Endpoint.Address, "#03");
  78. Assert.AreEqual (typeof (ICtorUseCase2), factory.Endpoint.Contract.ContractType, "#04");
  79. }
  80. [Test]
  81. [Ignore ("fails under .NET; I never bothered to fix the test")]
  82. public void InitializeEndpointTest2 ()
  83. {
  84. SimpleChannelFactory factory = new SimpleChannelFactory ();
  85. Assert.AreEqual (null, factory.Endpoint, "#01");
  86. factory.InitEndpoint ("CtorUseCase2_1", null);
  87. Assert.AreEqual (typeof (BasicHttpBinding), factory.Endpoint.Binding.GetType (), "#02");
  88. Assert.AreEqual (new EndpointAddress ("http://test2_1"), factory.Endpoint.Address, "#03");
  89. Assert.AreEqual (typeof (ICtorUseCase2), factory.Endpoint.Contract.ContractType, "#04");
  90. }
  91. [Test]
  92. [Ignore ("fails under .NET; I never bothered to fix the test")]
  93. public void InitializeEndpointTest3 ()
  94. {
  95. SimpleChannelFactory factory = new SimpleChannelFactory ();
  96. Assert.AreEqual (null, factory.Endpoint, "#01");
  97. factory.InitEndpoint ("CtorUseCase2_1", null);
  98. Binding b = new WSHttpBinding ();
  99. factory.InitEndpoint (b, null);
  100. Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
  101. Assert.AreEqual (null, factory.Endpoint.Address, "#03");
  102. }
  103. [Test]
  104. [Ignore ("fails under .NET; I never bothered to fix the test")]
  105. public void ApplyConfigurationTest1 ()
  106. {
  107. SimpleChannelFactory factory = new SimpleChannelFactory ();
  108. Binding b = new WSHttpBinding ();
  109. factory.InitEndpoint (b, null);
  110. factory.ApplyConfig ("CtorUseCase2_1");
  111. Assert.AreEqual (new EndpointAddress ("http://test2_1"), factory.Endpoint.Address, "#03");
  112. Assert.AreEqual (b, factory.Endpoint.Binding, "#02");
  113. }
  114. [Test]
  115. [Ignore ("fails under .NET; I never bothered to fix the test")]
  116. public void ApplyConfigurationTest2 ()
  117. {
  118. SimpleChannelFactory factory = new SimpleChannelFactory ();
  119. Binding b = new WSHttpBinding ();
  120. factory.InitEndpoint (b, new EndpointAddress ("http://test"));
  121. factory.ApplyConfig ("CtorUseCase2_2");
  122. Assert.AreEqual (new EndpointAddress ("http://test"), factory.Endpoint.Address, "#03");
  123. Assert.IsNotNull (factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> (), "#04");
  124. Assert.AreEqual (true, factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> ().IncludeExceptionDetailInFaults, "#04");
  125. factory.ApplyConfig ("CtorUseCase2_3");
  126. Assert.IsNotNull (factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> (), "#04");
  127. Assert.AreEqual (false, factory.Endpoint.Behaviors.Find <CallbackDebugBehavior> ().IncludeExceptionDetailInFaults, "#04");
  128. }
  129. [Test]
  130. public void DescriptionProperties ()
  131. {
  132. Binding b = new BasicHttpBinding ();
  133. ChannelFactory<IFoo> f = new ChannelFactory<IFoo> (b);
  134. // FIXME: it's not working now (though this test is silly to me.)
  135. //Assert.IsNull (f.Description.ChannelType, "ChannelType");
  136. // FIXME: it's not working now
  137. //Assert.AreEqual (1, f.Endpoint.Behaviors.Count, "Behaviors.Count");
  138. //ClientCredentials cred = f.Endpoint.Behaviors [0] as ClientCredentials;
  139. //Assert.IsNotNull (cred, "Behaviors contains ClientCredentials");
  140. Assert.IsNotNull (f.Endpoint, "Endpoint");
  141. Assert.AreEqual (b, f.Endpoint.Binding, "Endpoint.Binding");
  142. Assert.IsNull (f.Endpoint.Address, "Endpoint.Address");
  143. // You can examine this silly test on .NET.
  144. // Funky, ContractDescription.GetContract(
  145. // typeof (IRequestChannel)) also fails to raise an
  146. // error.
  147. //Assert.AreEqual ("IRequestChannel", f.Description.Endpoint.Contract.Name, "Endpoint.Contract");
  148. }
  149. public class MyChannelFactory<TChannel>
  150. : ChannelFactory<TChannel>
  151. {
  152. public MyChannelFactory (Type type)
  153. : base (type)
  154. {
  155. }
  156. }
  157. [ServiceContract]
  158. public interface IFoo
  159. {
  160. [OperationContract]
  161. string Echo (string msg);
  162. }
  163. public class Foo : IFoo
  164. {
  165. public string Echo (string msg)
  166. {
  167. return msg;
  168. }
  169. }
  170. [Test]
  171. [ExpectedException (typeof (InvalidOperationException))]
  172. public void ArgumentTypeNotInterface ()
  173. {
  174. new MyChannelFactory<IFoo> (typeof (Foo));
  175. }
  176. }
  177. }