TestContext.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Reflection;
  30. using System.ServiceModel.Description;
  31. namespace MonoTests.System.ServiceModel.MetadataTests {
  32. public static class TestContext {
  33. #region Public API
  34. public static MetadataSet GetMetadata (string name)
  35. {
  36. #if USE_EMBEDDED_METADATA
  37. return LoadMetadataFromResource (name);
  38. #else
  39. return LoadMetadataFromFile (name);
  40. #endif
  41. }
  42. public static void SaveMetadata (string name, MetadataSet metadata)
  43. {
  44. SaveMetadataToFile (name, metadata);
  45. }
  46. #endregion
  47. internal static MetadataSet LoadMetadataFromFile (string name)
  48. {
  49. var asm = Assembly.GetExecutingAssembly ();
  50. if (!name.EndsWith (".xml"))
  51. name = name + ".xml";
  52. var uri = new Uri (asm.CodeBase);
  53. var path = Path.GetDirectoryName (uri.AbsolutePath);
  54. path = Path.Combine (path, "Test");
  55. path = Path.Combine (path, "MetadataTests");
  56. path = Path.Combine (path, "Resources");
  57. var filename = Path.Combine (path, name);
  58. using (var stream = new StreamReader (filename)) {
  59. var reader = new XmlTextReader (stream);
  60. return MetadataSet.ReadFrom (reader);
  61. }
  62. }
  63. internal static MetadataSet LoadMetadataFromResource (string name)
  64. {
  65. var asm = Assembly.GetExecutingAssembly ();
  66. if (!name.EndsWith (".xml"))
  67. name = name + ".xml";
  68. var resname = "MetadataTests.Resources." + name;
  69. using (var stream = asm.GetManifestResourceStream (resname)) {
  70. if (stream == null)
  71. throw new InvalidOperationException (
  72. "No such resource: " + name);
  73. var reader = new XmlTextReader (stream);
  74. return MetadataSet.ReadFrom (reader);
  75. }
  76. }
  77. internal static void SaveMetadataToFile (string name, MetadataSet metadata)
  78. {
  79. var filename = name + ".xml";
  80. if (File.Exists (filename))
  81. return;
  82. using (var file = new StreamWriter (filename, false)) {
  83. var writer = new XmlTextWriter (file);
  84. writer.Formatting = Formatting.Indented;
  85. metadata.WriteTo (writer);
  86. }
  87. Console.WriteLine ("Exported {0}.", filename);
  88. }
  89. }
  90. }