ConvDocLitWraTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Web service test for WSDL document:
  2. // http://localhost:8080/ConvDocLitWra.asmx?wsdl
  3. using System;
  4. using System.Threading;
  5. using NUnit.Framework;
  6. using ConvDocLitWraTests.Soap;
  7. using System.Web.Services.Protocols;
  8. namespace ConvDocLitWraTests
  9. {
  10. [TestFixture]
  11. public class ConvDocLitWraTest: WebServiceTest
  12. {
  13. [Test]
  14. public void TestService ()
  15. {
  16. ConverterService cs = new ConverterService ();
  17. // Test the Discover method.
  18. cs.Url = "http://localhost:8080/ConvDocLitWra.asmx?disco";
  19. cs.Discover ();
  20. cs.Login ("lluis");
  21. cs.SetCurrencyRate ("EUR", 0.5);
  22. AssertEquals ("#1", 0.5, cs.GetCurrencyRate ("EUR"));
  23. double res = cs.Convert ("EUR","USD",6);
  24. AssertEquals ("#2", (int)res, (int)12);
  25. CurrencyInfo[] infos = cs.GetCurrencyInfo ();
  26. foreach (CurrencyInfo info in infos)
  27. {
  28. double val = 0;
  29. switch (info.Name)
  30. {
  31. case "USD": val = 1; break;
  32. case "EUR": val = 0.5; break;
  33. case "GBP": val = 0.611817; break;
  34. case "JPY": val = 118.271; break;
  35. case "CAD": val = 1.36338; break;
  36. case "AUD": val = 1.51485; break;
  37. case "CHF": val = 1.36915; break;
  38. case "RUR": val = 30.4300; break;
  39. case "CNY": val = 8.27740; break;
  40. case "ZAR": val = 7.62645; break;
  41. case "MXN": val = 10.5025; break;
  42. }
  43. AssertEquals ("#3 " + info.Name, val, info.Rate);
  44. }
  45. cs.SetCurrencyRate ("EUR", 0.9);
  46. }
  47. // Async tests
  48. ConverterService acs;
  49. bool a1;
  50. bool a2;
  51. bool a3;
  52. AutoResetEvent eve = new AutoResetEvent (false);
  53. [Test]
  54. public void AsyncTestService ()
  55. {
  56. IAsyncResult ar;
  57. acs = new ConverterService ();
  58. ar = acs.BeginLogin ("lluis", null, null);
  59. acs.EndLogin (ar);
  60. acs.BeginSetCurrencyRate ("EUR", 0.5, new AsyncCallback(Callback1), null);
  61. Assert ("#0", eve.WaitOne (5000, false));
  62. Assert ("#1",a1);
  63. Assert ("#2", eve.WaitOne (5000, false));
  64. Assert ("#3",a2);
  65. Assert ("#4", eve.WaitOne (5000, false));
  66. Assert ("#5",a3);
  67. }
  68. void Callback1 (IAsyncResult ar)
  69. {
  70. acs.EndSetCurrencyRate (ar);
  71. acs.BeginGetCurrencyRate ("EUR", new AsyncCallback(Callback2), null);
  72. }
  73. void Callback2 (IAsyncResult ar)
  74. {
  75. double res = acs.EndGetCurrencyRate (ar);
  76. a1 = (res == 0.5);
  77. eve.Set ();
  78. acs.BeginConvert ("EUR","USD",6, new AsyncCallback(Callback3), null);
  79. }
  80. void Callback3 (IAsyncResult ar)
  81. {
  82. double res = acs.EndConvert (ar);
  83. a2 = (res == 12);
  84. eve.Set ();
  85. acs.BeginGetCurrencyInfo (new AsyncCallback(Callback4),null);
  86. }
  87. void Callback4 (IAsyncResult ar)
  88. {
  89. CurrencyInfo[] infos = acs.EndGetCurrencyInfo (ar);
  90. foreach (CurrencyInfo info in infos)
  91. {
  92. double val = 0;
  93. switch (info.Name)
  94. {
  95. case "USD": val = 1; break;
  96. case "EUR": val = 0.5; break;
  97. case "GBP": val = 0.611817; break;
  98. case "JPY": val = 118.271; break;
  99. case "CAD": val = 1.36338; break;
  100. case "AUD": val = 1.51485; break;
  101. case "CHF": val = 1.36915; break;
  102. case "RUR": val = 30.4300; break;
  103. case "CNY": val = 8.27740; break;
  104. case "ZAR": val = 7.62645; break;
  105. case "MXN": val = 10.5025; break;
  106. }
  107. a3 = (val == info.Rate);
  108. if (!a3) break;
  109. }
  110. eve.Set ();
  111. }
  112. [Test]
  113. public void TestException ()
  114. {
  115. ConverterService cs = new ConverterService ();
  116. try
  117. {
  118. cs.SetCurrencyRate ("EUR", 0.5);
  119. Assert ("#0",false);
  120. }
  121. catch (SoapException ex)
  122. {
  123. Assert ("#1", ex.Message.IndexOf ("User not logged") != -1);
  124. AssertEquals ("#2", SoapException.ServerFaultCode, ex.Code);
  125. }
  126. }
  127. [Test]
  128. public void AsyncTestException ()
  129. {
  130. ConverterService cs = new ConverterService ();
  131. IAsyncResult ar = cs.BeginSetCurrencyRate ("EUR", 0.5, null, null);
  132. try
  133. {
  134. cs.EndSetCurrencyRate (ar);
  135. Assert ("#0",false);
  136. }
  137. catch (SoapException ex)
  138. {
  139. Assert ("#1", ex.Message.IndexOf ("User not logged") != -1);
  140. AssertEquals ("#2", SoapException.ServerFaultCode, ex.Code);
  141. }
  142. }
  143. }
  144. }