TestContext.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // ITestContext.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  6. //
  7. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. using System.Xml;
  29. using System.Text;
  30. using System.Reflection;
  31. using System.ServiceModel.Description;
  32. namespace MonoTests.System.ServiceModel.MetadataTests {
  33. public abstract class TestContext {
  34. #region Abstract API
  35. public abstract MetadataSet GetMetadata (string name);
  36. /*
  37. * Whether or not to check whether the `WS.Binding.Extensions'
  38. * contains the policy XmlElement.
  39. *
  40. * We only check when importing from XML, not when generating
  41. * the MetadataSet programmatically.
  42. */
  43. public abstract bool CheckPolicyXml {
  44. get;
  45. }
  46. #endregion
  47. #region Default Context
  48. public static TestContext LoadMetadataContext = new _LoadMetadataContext ();
  49. public static TestContext CreateMetadataContext = new _CreateMetadataContext ();
  50. public static TestContext RoundTripContext = new _RoundTripContext ();
  51. #endregion
  52. #region Implementations
  53. class _LoadMetadataContext : TestContext {
  54. public override MetadataSet GetMetadata (string name)
  55. {
  56. return LoadMetadata (name);
  57. }
  58. public override bool CheckPolicyXml {
  59. get {
  60. return true;
  61. }
  62. }
  63. }
  64. class _CreateMetadataContext : TestContext {
  65. public override MetadataSet GetMetadata (string name)
  66. {
  67. return MetadataSamples.GetMetadataByName (name);
  68. }
  69. public override bool CheckPolicyXml {
  70. get {
  71. // FIXME: Not supported yet.
  72. return false;
  73. }
  74. }
  75. }
  76. class _RoundTripContext : TestContext {
  77. public override MetadataSet GetMetadata (string name)
  78. {
  79. return RoundTrip (name);
  80. }
  81. public override bool CheckPolicyXml {
  82. get {
  83. // FIXME: Not supported yet.
  84. return false;
  85. }
  86. }
  87. }
  88. #endregion
  89. #region Public Static API
  90. public static MetadataSet LoadMetadata (string name)
  91. {
  92. #if USE_EMBEDDED_METADATA
  93. return LoadMetadataFromResource (name);
  94. #else
  95. return LoadMetadataFromFile (name);
  96. #endif
  97. }
  98. public static void SaveMetadata (string name, MetadataSet metadata)
  99. {
  100. SaveMetadataToFile (name, metadata);
  101. }
  102. public static MetadataSet LoadMetadataFromFile (string name)
  103. {
  104. var asm = Assembly.GetExecutingAssembly ();
  105. if (!name.EndsWith (".xml"))
  106. name = name + ".xml";
  107. var uri = new Uri (asm.CodeBase);
  108. var path = Path.GetDirectoryName (uri.AbsolutePath);
  109. path = Path.Combine (path, "Test");
  110. path = Path.Combine (path, "MetadataTests");
  111. path = Path.Combine (path, "Resources");
  112. var filename = Path.Combine (path, name);
  113. using (var stream = new StreamReader (filename)) {
  114. var reader = new XmlTextReader (stream);
  115. return MetadataSet.ReadFrom (reader);
  116. }
  117. }
  118. public static MetadataSet LoadMetadataFromResource (string name)
  119. {
  120. var asm = Assembly.GetExecutingAssembly ();
  121. if (!name.EndsWith (".xml"))
  122. name = name + ".xml";
  123. var resname = "MetadataTests.Resources." + name;
  124. using (var stream = asm.GetManifestResourceStream (resname)) {
  125. if (stream == null)
  126. throw new InvalidOperationException (
  127. "No such resource: " + name);
  128. var reader = new XmlTextReader (stream);
  129. return MetadataSet.ReadFrom (reader);
  130. }
  131. }
  132. public static void SaveMetadataToFile (string name, MetadataSet metadata)
  133. {
  134. var filename = name + ".xml";
  135. if (File.Exists (filename))
  136. return;
  137. using (var file = new StreamWriter (filename, false)) {
  138. var writer = new XmlTextWriter (file);
  139. writer.Formatting = Formatting.Indented;
  140. metadata.WriteTo (writer);
  141. }
  142. Console.WriteLine ("Exported {0}.", filename);
  143. }
  144. internal static string SaveMetadataToString (MetadataSet metadata)
  145. {
  146. using (var ms = new MemoryStream ()) {
  147. var writer = new XmlTextWriter (new StreamWriter (ms));
  148. writer.Formatting = Formatting.Indented;
  149. metadata.WriteTo (writer);
  150. writer.Flush ();
  151. return Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int)ms.Position);
  152. }
  153. }
  154. internal static MetadataSet LoadMetadataFromString (string doc)
  155. {
  156. var buffer = Encoding.UTF8.GetBytes (doc);
  157. using (var ms = new MemoryStream (buffer)) {
  158. var reader = new XmlTextReader (ms);
  159. return MetadataSet.ReadFrom (reader);
  160. }
  161. }
  162. public static MetadataSet RoundTrip (string name)
  163. {
  164. var metadata = MetadataSamples.GetMetadataByName (name);
  165. var doc = SaveMetadataToString (metadata);
  166. return LoadMetadataFromString (doc);
  167. }
  168. #endregion
  169. }
  170. }