MetadataResolverTest.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // MetadataResolverTest.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[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.Generic;
  30. using System.Text;
  31. using System.Runtime.Serialization;
  32. using System.ServiceModel.Description;
  33. using System.ServiceModel;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.ServiceModel.Description
  36. {
  37. //[TestFixture]
  38. public class MetadataResolverTest
  39. {
  40. //string url = "http://localhost:8080/echo/mex";
  41. string url = "http://192.168.0.1:8080/echo/mex";
  42. [Test]
  43. public void ResolveNoEndpoint ()
  44. {
  45. ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
  46. typeof (NonExistantContract),
  47. new EndpointAddress (url));
  48. Assert.IsNotNull (endpoints);
  49. Assert.AreEqual (0, endpoints.Count);
  50. }
  51. [Test]
  52. public void Resolve1 ()
  53. {
  54. ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
  55. typeof (IEchoService), new EndpointAddress (url));
  56. CheckIEchoServiceEndpoint (endpoints);
  57. }
  58. [Test]
  59. public void Resolve2 ()
  60. {
  61. ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
  62. typeof (IEchoService),
  63. new Uri (url),
  64. MetadataExchangeClientMode.MetadataExchange);
  65. CheckIEchoServiceEndpoint (endpoints);
  66. }
  67. [Test]
  68. public void Resolve3 ()
  69. {
  70. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  71. List<ContractDescription> contracts = new List<ContractDescription> ();
  72. contracts.Add (contract);
  73. ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
  74. contracts,
  75. new Uri (url),
  76. MetadataExchangeClientMode.MetadataExchange);
  77. CheckIEchoServiceEndpoint (endpoints);
  78. }
  79. [Test]
  80. public void Resolve4 ()
  81. {
  82. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  83. List<ContractDescription> contracts = new List<ContractDescription> ();
  84. contracts.Add (contract);
  85. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  86. ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
  87. contracts,
  88. new Uri (url),
  89. MetadataExchangeClientMode.MetadataExchange);
  90. CheckIEchoServiceEndpoint (endpoints);
  91. }
  92. [Test]
  93. public void Resolve5 ()
  94. {
  95. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  96. List<ContractDescription> contracts = new List<ContractDescription> ();
  97. contracts.Add (contract);
  98. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  99. //FIXME: What is the 'client' param used for?
  100. //TODO: Write test cases for the related overloads of Resolve
  101. MetadataResolver.Resolve (
  102. contracts,
  103. new EndpointAddress (url),
  104. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  105. }
  106. [Test]
  107. public void Resolve6 ()
  108. {
  109. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  110. List<ContractDescription> contracts = new List<ContractDescription> ();
  111. contracts.Add (contract);
  112. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  113. //FIXME: What is the 'client' param used for?
  114. //TODO: Write test cases for the related overloads of Resolve
  115. MetadataResolver.Resolve (
  116. contracts,
  117. new Uri (url),
  118. MetadataExchangeClientMode.MetadataExchange,
  119. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  120. }
  121. //Negative tests
  122. [Test]
  123. [ExpectedException (typeof (ArgumentNullException))]
  124. public void ErrResolve1 ()
  125. {
  126. MetadataResolver.Resolve (
  127. typeof (IEchoService),
  128. null,
  129. MetadataExchangeClientMode.MetadataExchange);
  130. }
  131. [Test]
  132. [ExpectedException (typeof (InvalidOperationException))]
  133. public void ErrResolve2 ()
  134. {
  135. //Mex cannot be fetched with HttpGet from the given url
  136. MetadataResolver.Resolve (
  137. typeof (IEchoService),
  138. new Uri (url),
  139. MetadataExchangeClientMode.HttpGet);
  140. }
  141. [Test]
  142. [ExpectedException (typeof (ArgumentNullException))]
  143. public void ErrResolve3 ()
  144. {
  145. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  146. List<ContractDescription> contracts = new List<ContractDescription> ();
  147. contracts.Add (contract);
  148. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  149. MetadataResolver.Resolve (contracts, new EndpointAddress (url),
  150. (MetadataExchangeClient) null);
  151. }
  152. [Test]
  153. [ExpectedException (typeof (InvalidOperationException))]
  154. public void ErrResolve4 ()
  155. {
  156. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  157. List<ContractDescription> contracts = new List<ContractDescription> ();
  158. contracts.Add (contract);
  159. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  160. //FIXME: What is the 'client' param used for?
  161. //TODO: Write test cases for the related overloads of Resolve
  162. MetadataResolver.Resolve (
  163. contracts,
  164. new EndpointAddress ("http://localhost"),
  165. new MetadataExchangeClient (new EndpointAddress (url)));
  166. }
  167. [Test]
  168. [ExpectedException (typeof (InvalidOperationException))]
  169. public void ErrResolve5 ()
  170. {
  171. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  172. List<ContractDescription> contracts = new List<ContractDescription> ();
  173. contracts.Add (contract);
  174. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  175. //FIXME: What is the 'client' param used for?
  176. //TODO: Write test cases for the related overloads of Resolve
  177. MetadataResolver.Resolve (
  178. contracts,
  179. new Uri (url),
  180. MetadataExchangeClientMode.HttpGet,
  181. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentException))]
  185. public void ErrResolve6 ()
  186. {
  187. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  188. List<ContractDescription> contracts = new List<ContractDescription> ();
  189. //FIXME: What is the 'client' param used for?
  190. //TODO: Write test cases for the related overloads of Resolve
  191. MetadataResolver.Resolve (
  192. contracts,
  193. new Uri (url),
  194. MetadataExchangeClientMode.HttpGet,
  195. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  196. }
  197. [Test]
  198. [ExpectedException (typeof (ArgumentNullException))]
  199. public void ErrResolve7 ()
  200. {
  201. MetadataResolver.Resolve (
  202. null,
  203. new Uri (url),
  204. MetadataExchangeClientMode.HttpGet,
  205. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  206. }
  207. [Test]
  208. [ExpectedException (typeof (ArgumentNullException))]
  209. public void ErrResolve8 ()
  210. {
  211. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  212. List<ContractDescription> contracts = new List<ContractDescription> ();
  213. contracts.Add (contract);
  214. MetadataResolver.Resolve (contracts, null);
  215. }
  216. /* Test for bad endpoint address */
  217. [Test]
  218. [ExpectedException (typeof (InvalidOperationException))]
  219. public void ErrResolve9 ()
  220. {
  221. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  222. List<ContractDescription> contracts = new List<ContractDescription> ();
  223. contracts.Add (contract);
  224. MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
  225. }
  226. private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
  227. {
  228. Assert.IsNotNull (endpoints);
  229. Assert.AreEqual (1, endpoints.Count);
  230. ServiceEndpoint ep = endpoints [0];
  231. //URI Dependent
  232. //Assert.AreEqual ("http://localhost:8080/echo/svc", ep.Address.Uri.AbsoluteUri, "#R1");
  233. Assert.AreEqual ("IEchoService", ep.Contract.Name, "#R3");
  234. Assert.AreEqual ("http://myns/echo", ep.Contract.Namespace, "#R4");
  235. Assert.AreEqual ("BasicHttpBinding_IEchoService", ep.Name, "#R5");
  236. Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
  237. }
  238. [Test]
  239. [ExpectedException (typeof (InvalidOperationException))]
  240. public void ResolveNonContract ()
  241. {
  242. MetadataResolver.Resolve (
  243. typeof (Int32), new EndpointAddress (url));
  244. }
  245. [Test]
  246. [ExpectedException (typeof (InvalidOperationException))]
  247. public void ResolveBadUri ()
  248. {
  249. MetadataResolver.Resolve (
  250. typeof (IEchoService), new EndpointAddress ("http://localhost"));
  251. }
  252. [DataContract]
  253. public class dc
  254. {
  255. [DataMember]
  256. string field;
  257. }
  258. [ServiceContract (Namespace = "http://myns/echo")]
  259. public interface IEchoService
  260. {
  261. [OperationContract]
  262. string Echo (string msg, int num, dc d);
  263. [OperationContract]
  264. string DoubleIt (int it, string prefix);
  265. }
  266. [ServiceContract]
  267. public class NonExistantContract
  268. {
  269. }
  270. }
  271. }