ResourcesTestHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // ResourcesTestHelper.cs : Base class for new resource tests with methods
  3. // required across many.
  4. //
  5. // Author:
  6. // Gary Barnett ([email protected])
  7. //
  8. // Copyright (C) Gary Barnett (2012)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using NUnit.Framework;
  32. using System.Windows.Forms;
  33. using System.IO;
  34. using System.Resources;
  35. using System.Collections;
  36. using System.Drawing;
  37. using System.Runtime.Serialization.Formatters.Binary;
  38. namespace MonoTests.System.Resources {
  39. public class ResourcesTestHelper {
  40. string tempFileWithIcon = null;
  41. string tempFileWithSerializable = null;
  42. [SetUp]
  43. protected virtual void SetUp ()
  44. {
  45. }
  46. protected ResXDataNode GetNodeFromResXReader (ResXDataNode node)
  47. {
  48. StringWriter sw = new StringWriter ();
  49. using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
  50. writer.AddResource (node);
  51. }
  52. StringReader sr = new StringReader (sw.GetStringBuilder ().ToString ());
  53. using (ResXResourceReader reader = new ResXResourceReader (sr)) {
  54. reader.UseResXDataNodes = true;
  55. IDictionaryEnumerator enumerator = reader.GetEnumerator ();
  56. enumerator.MoveNext ();
  57. return ((DictionaryEntry) enumerator.Current).Value as ResXDataNode;
  58. }
  59. }
  60. protected ResXDataNode GetNodeFromResXReader (string contents)
  61. {
  62. StringReader sr = new StringReader (contents);
  63. using (ResXResourceReader reader = new ResXResourceReader (sr)) {
  64. reader.UseResXDataNodes = true;
  65. IDictionaryEnumerator enumerator = reader.GetEnumerator ();
  66. enumerator.MoveNext ();
  67. return (ResXDataNode) ((DictionaryEntry) enumerator.Current).Value;
  68. }
  69. }
  70. public ResXDataNode GetNodeEmdeddedIcon ()
  71. {
  72. Stream input = typeof (ResXDataNodeTest).Assembly.
  73. GetManifestResourceStream ("32x32.ico");
  74. Icon ico = new Icon (input);
  75. ResXDataNode node = new ResXDataNode ("test", ico);
  76. return node;
  77. }
  78. public ResXDataNode GetNodeFileRefToIcon ()
  79. {
  80. tempFileWithIcon = Path.GetTempFileName (); // remember to delete file in teardown
  81. Path.ChangeExtension (tempFileWithIcon, "ico");
  82. WriteEmbeddedResource ("32x32.ico", tempFileWithIcon);
  83. ResXFileRef fileRef = new ResXFileRef (tempFileWithIcon, typeof (Icon).AssemblyQualifiedName);
  84. ResXDataNode node = new ResXDataNode ("test", fileRef);
  85. return node;
  86. }
  87. void WriteEmbeddedResource (string name, string filename)
  88. {
  89. const int size = 512;
  90. byte [] buffer = new byte [size];
  91. int count = 0;
  92. Stream input = typeof (ResXDataNodeTest).Assembly.
  93. GetManifestResourceStream (name);
  94. Stream output = File.Open (filename, FileMode.Create);
  95. try {
  96. while ((count = input.Read (buffer, 0, size)) > 0) {
  97. output.Write (buffer, 0, count);
  98. }
  99. } finally {
  100. output.Close ();
  101. }
  102. }
  103. public ResXDataNode GetNodeEmdeddedBytes1To10 ()
  104. {
  105. byte [] someBytes = new byte [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  106. ResXDataNode node = new ResXDataNode ("test", someBytes);
  107. return node;
  108. }
  109. public ResXDataNode GetNodeEmdeddedSerializable ()
  110. {
  111. serializable ser = new serializable ("testName", "testValue");
  112. ResXDataNode node = new ResXDataNode ("test", ser);
  113. return node;
  114. }
  115. public ResXDataNode GetNodeFileRefToSerializable (string filename, bool assemblyQualifiedName)
  116. {
  117. tempFileWithSerializable = Path.GetTempFileName (); // remember to delete file in teardown
  118. serializable ser = new serializable ("name", "value");
  119. SerializeToFile (tempFileWithSerializable, ser);
  120. string typeName;
  121. if (assemblyQualifiedName)
  122. typeName = typeof (serializable).AssemblyQualifiedName;
  123. else
  124. typeName = typeof (serializable).FullName;
  125. ResXFileRef fileRef = new ResXFileRef (tempFileWithSerializable, typeName);
  126. ResXDataNode node = new ResXDataNode ("test", fileRef);
  127. return node;
  128. }
  129. static void SerializeToFile (string filepath, serializable ser)
  130. {
  131. Stream stream = File.Open (filepath, FileMode.Create);
  132. BinaryFormatter bFormatter = new BinaryFormatter ();
  133. bFormatter.Serialize (stream, ser);
  134. stream.Close ();
  135. }
  136. [TearDown]
  137. protected virtual void TearDown ()
  138. {
  139. if (tempFileWithIcon != null) {
  140. File.Delete (tempFileWithIcon);
  141. tempFileWithIcon = null;
  142. }
  143. if (tempFileWithSerializable != null) {
  144. File.Delete (tempFileWithSerializable);
  145. tempFileWithSerializable = null;
  146. }
  147. }
  148. }
  149. }