MetadataResolverTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. [Category ("NotWorking")]
  134. public void ErrResolve2 ()
  135. {
  136. /* Not working as HttpGet is not implemented yet */
  137. //Mex cannot be fetched with HttpGet from the given url
  138. MetadataResolver.Resolve (
  139. typeof (IEchoService),
  140. new Uri (url),
  141. MetadataExchangeClientMode.HttpGet);
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentNullException))]
  145. public void ErrResolve3 ()
  146. {
  147. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  148. List<ContractDescription> contracts = new List<ContractDescription> ();
  149. contracts.Add (contract);
  150. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  151. MetadataResolver.Resolve (contracts, new EndpointAddress (url),
  152. (MetadataExchangeClient) null);
  153. }
  154. [Test]
  155. [ExpectedException (typeof (InvalidOperationException))]
  156. public void ErrResolve4 ()
  157. {
  158. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  159. List<ContractDescription> contracts = new List<ContractDescription> ();
  160. contracts.Add (contract);
  161. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  162. //FIXME: What is the 'client' param used for?
  163. //TODO: Write test cases for the related overloads of Resolve
  164. MetadataResolver.Resolve (
  165. contracts,
  166. new EndpointAddress ("http://localhost"),
  167. new MetadataExchangeClient (new EndpointAddress (url)));
  168. }
  169. [Test]
  170. [ExpectedException (typeof (InvalidOperationException))]
  171. [Category ("NotWorking")]
  172. public void ErrResolve5 ()
  173. {
  174. /* Not working as HttpGet is not implemented yet */
  175. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  176. List<ContractDescription> contracts = new List<ContractDescription> ();
  177. contracts.Add (contract);
  178. contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
  179. //FIXME: What is the 'client' param used for?
  180. //TODO: Write test cases for the related overloads of Resolve
  181. MetadataResolver.Resolve (
  182. contracts,
  183. new Uri (url),
  184. MetadataExchangeClientMode.HttpGet,
  185. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  186. }
  187. [Test]
  188. [ExpectedException (typeof (ArgumentException))]
  189. public void ErrResolve6 ()
  190. {
  191. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  192. List<ContractDescription> contracts = new List<ContractDescription> ();
  193. //FIXME: What is the 'client' param used for?
  194. //TODO: Write test cases for the related overloads of Resolve
  195. MetadataResolver.Resolve (
  196. contracts,
  197. new Uri (url),
  198. MetadataExchangeClientMode.HttpGet,
  199. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  200. }
  201. [Test]
  202. [ExpectedException (typeof (ArgumentNullException))]
  203. public void ErrResolve7 ()
  204. {
  205. MetadataResolver.Resolve (
  206. null,
  207. new Uri (url),
  208. MetadataExchangeClientMode.HttpGet,
  209. new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
  210. }
  211. [Test]
  212. [ExpectedException (typeof (ArgumentNullException))]
  213. public void ErrResolve8 ()
  214. {
  215. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  216. List<ContractDescription> contracts = new List<ContractDescription> ();
  217. contracts.Add (contract);
  218. MetadataResolver.Resolve (contracts, null);
  219. }
  220. /* Test for bad endpoint address */
  221. [Test]
  222. [ExpectedException (typeof (InvalidOperationException))]
  223. public void ErrResolve9 ()
  224. {
  225. ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
  226. List<ContractDescription> contracts = new List<ContractDescription> ();
  227. contracts.Add (contract);
  228. MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
  229. }
  230. private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
  231. {
  232. Assert.IsNotNull (endpoints);
  233. Assert.AreEqual (1, endpoints.Count);
  234. ServiceEndpoint ep = endpoints [0];
  235. //URI Dependent
  236. //Assert.AreEqual ("http://localhost:8080/echo/svc", ep.Address.Uri.AbsoluteUri, "#R1");
  237. Assert.AreEqual ("IEchoService", ep.Contract.Name, "#R3");
  238. Assert.AreEqual ("http://myns/echo", ep.Contract.Namespace, "#R4");
  239. Assert.AreEqual ("BasicHttpBinding_IEchoService", ep.Name, "#R5");
  240. Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
  241. }
  242. [Test]
  243. [ExpectedException (typeof (InvalidOperationException))]
  244. public void ResolveNonContract ()
  245. {
  246. MetadataResolver.Resolve (
  247. typeof (Int32), new EndpointAddress (url));
  248. }
  249. [Test]
  250. [ExpectedException (typeof (InvalidOperationException))]
  251. public void ResolveBadUri ()
  252. {
  253. MetadataResolver.Resolve (
  254. typeof (IEchoService), new EndpointAddress ("http://localhost"));
  255. }
  256. [DataContract]
  257. public class dc
  258. {
  259. [DataMember]
  260. string field;
  261. }
  262. [ServiceContract (Namespace = "http://myns/echo")]
  263. public interface IEchoService
  264. {
  265. [OperationContract]
  266. string Echo (string msg, int num, dc d);
  267. [OperationContract]
  268. string DoubleIt (int it, string prefix);
  269. }
  270. [ServiceContract]
  271. public class NonExistantContract
  272. {
  273. }
  274. }
  275. }