ResXFileRefTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // ResXFileRefTest.cs: Unit Tests for ResXFileRef.
  3. //
  4. // Authors:
  5. // Gert Driesen <[email protected]>
  6. //
  7. using System;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Resources;
  12. using System.Text;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Resources
  15. {
  16. [TestFixture]
  17. public class ResXFileRefTest : MonoTests.System.Windows.Forms.TestHelper
  18. {
  19. [Test]
  20. public void Constructor1 ()
  21. {
  22. ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
  23. MonoTests.System.Windows.Forms.TestHelper.RemoveWarning (r);
  24. Assert.AreEqual ("mono.bmp", r.FileName, "#1");
  25. Assert.AreEqual ("Bitmap", r.TypeName, "#2");
  26. }
  27. [Test]
  28. public void Constructor1_FileName_Null ()
  29. {
  30. try {
  31. new ResXFileRef ((string) null, "Bitmap");
  32. Assert.Fail ("#1");
  33. } catch (ArgumentNullException ex) {
  34. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  35. Assert.IsNotNull (ex.Message, "#3");
  36. Assert.IsNotNull (ex.ParamName, "#4");
  37. Assert.AreEqual ("fileName", ex.ParamName, "#5");
  38. Assert.IsNull (ex.InnerException, "#6");
  39. }
  40. }
  41. [Test]
  42. public void Constructor1_TypeName_Null ()
  43. {
  44. try {
  45. new ResXFileRef ("mono.bmp", (string) null);
  46. Assert.Fail ("#1");
  47. } catch (ArgumentNullException ex) {
  48. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  49. Assert.IsNotNull (ex.Message, "#3");
  50. Assert.IsNotNull (ex.ParamName, "#4");
  51. Assert.AreEqual ("typeName", ex.ParamName, "#5");
  52. Assert.IsNull (ex.InnerException, "#6");
  53. }
  54. }
  55. [Test]
  56. public void Constructor2 ()
  57. {
  58. Encoding utf8 = Encoding.UTF8;
  59. ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap", utf8);
  60. Assert.AreEqual ("mono.bmp", r.FileName, "#A1");
  61. Assert.AreSame (utf8, r.TextFileEncoding, "#A2");
  62. Assert.AreEqual ("Bitmap", r.TypeName, "#A3");
  63. r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
  64. Assert.AreEqual ("mono.bmp", r.FileName, "#B1");
  65. Assert.IsNull (r.TextFileEncoding, "#B2");
  66. Assert.AreEqual ("Bitmap", r.TypeName, "#B3");
  67. }
  68. [Test]
  69. public void Constructor2_FileName_Null ()
  70. {
  71. try {
  72. new ResXFileRef ((string) null, "Bitmap", Encoding.UTF8);
  73. Assert.Fail ("#1");
  74. } catch (ArgumentNullException ex) {
  75. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  76. Assert.IsNotNull (ex.Message, "#3");
  77. Assert.IsNotNull (ex.ParamName, "#4");
  78. Assert.AreEqual ("fileName", ex.ParamName, "#5");
  79. Assert.IsNull (ex.InnerException, "#6");
  80. }
  81. }
  82. [Test]
  83. public void Constructor2_TypeName_Null ()
  84. {
  85. try {
  86. new ResXFileRef ("mono.bmp", (string) null, Encoding.UTF8);
  87. Assert.Fail ("#1");
  88. } catch (ArgumentNullException ex) {
  89. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  90. Assert.IsNotNull (ex.Message, "#3");
  91. Assert.IsNotNull (ex.ParamName, "#4");
  92. Assert.AreEqual ("typeName", ex.ParamName, "#5");
  93. Assert.IsNull (ex.InnerException, "#6");
  94. }
  95. }
  96. [Test]
  97. public void ToStringTest ()
  98. {
  99. ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
  100. Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#1");
  101. r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
  102. Assert.AreEqual ("mono.bmp;Bitmap;utf-8", r.ToString (), "#2");
  103. r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
  104. Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#3");
  105. }
  106. }
  107. [TestFixture]
  108. public class ResXFileRefConverterTest : MonoTests.System.Windows.Forms.TestHelper
  109. {
  110. [SetUp]
  111. protected override void SetUp () {
  112. _converter = new ResXFileRef.Converter ();
  113. _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXResourceReaderTest");
  114. if (!Directory.Exists (_tempDirectory)) {
  115. Directory.CreateDirectory (_tempDirectory);
  116. }
  117. _tempFileUTF7 = Path.Combine (_tempDirectory, "string_utf7.txt");
  118. using (StreamWriter sw = new StreamWriter (_tempFileUTF7, false, Encoding.UTF7)) {
  119. sw.Write ("\u0021\u0026\u002A\u003B");
  120. }
  121. base.SetUp ();
  122. }
  123. [TearDown]
  124. protected override void TearDown ()
  125. {
  126. if (Directory.Exists (_tempDirectory))
  127. Directory.Delete (_tempDirectory, true);
  128. base.TearDown ();
  129. }
  130. [Test]
  131. public void CanConvertFrom ()
  132. {
  133. Assert.IsTrue (_converter.CanConvertFrom (typeof (string)), "#1");
  134. Assert.IsFalse (_converter.CanConvertFrom (typeof (byte [])), "#2");
  135. }
  136. [Test]
  137. public void CanConvertTo ()
  138. {
  139. Assert.IsTrue (_converter.CanConvertTo (typeof (string)), "#1");
  140. Assert.IsFalse (_converter.CanConvertTo (typeof (MemoryStream)), "#2");
  141. Assert.IsFalse (_converter.CanConvertTo (typeof (Bitmap)), "#3");
  142. }
  143. [Test]
  144. public void ConvertFrom_File_DoesNotExist ()
  145. {
  146. // file does not exist
  147. string fileRef = "doesnotexist.txt;" + typeof (string).AssemblyQualifiedName;
  148. try {
  149. _converter.ConvertFrom (fileRef);
  150. Assert.Fail ("#A1");
  151. } catch (FileNotFoundException ex) {
  152. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
  153. Assert.IsNull (ex.InnerException, "#A3");
  154. Assert.IsNotNull (ex.FileName, "#A4");
  155. Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "doesnotexist.txt"), ex.FileName, "#A5");
  156. Assert.IsNotNull (ex.Message, "#A6");
  157. }
  158. }
  159. [Test]
  160. public void ConvertFrom_Type_NotSet ()
  161. {
  162. string fileRef = "doesnotexist.txt";
  163. try {
  164. _converter.ConvertFrom (fileRef);
  165. Assert.Fail ("#B1");
  166. } catch (ArgumentException ex) {
  167. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  168. Assert.IsNull (ex.InnerException, "#B3");
  169. Assert.IsNotNull (ex.Message, "#B4");
  170. Assert.AreEqual ("value", ex.Message, "#B5");
  171. Assert.IsNull (ex.ParamName, "#B6");
  172. }
  173. }
  174. [Test]
  175. public void ConvertFrom_NotString ()
  176. {
  177. Assert.IsNull (_converter.ConvertFrom (null), "#G1");
  178. Assert.IsNull (_converter.ConvertFrom (1), "#G2");
  179. Assert.IsNull (_converter.ConvertFrom (true), "#G3");
  180. }
  181. [Test]
  182. public void ConvertFrom_Type_String ()
  183. {
  184. // read UTF-7 content without setting encoding
  185. string fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName;
  186. string result = _converter.ConvertFrom (fileRef) as string;
  187. Assert.IsNotNull (result, "#A1");
  188. Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#A2");
  189. // read UTF-7 content using UTF-7 encoding
  190. fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-7";
  191. result = _converter.ConvertFrom (fileRef) as string;
  192. Assert.IsNotNull (result, "#B1");
  193. Assert.AreEqual ("\u0021\u0026\u002A\u003B", result, "#B2");
  194. // invalid encoding
  195. fileRef = _tempFileUTF7 + ";" + typeof (string).AssemblyQualifiedName + ";utf-99";
  196. try {
  197. _converter.ConvertFrom (fileRef);
  198. Assert.Fail ("#D1");
  199. } catch (ArgumentException ex) {
  200. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  201. Assert.IsNull (ex.InnerException, "#D3");
  202. Assert.IsNotNull (ex.Message, "#D4");
  203. Assert.IsTrue (ex.Message.IndexOf ("'utf-99'") != -1, "#D5");
  204. Assert.IsNotNull (ex.ParamName, "#D6");
  205. Assert.AreEqual ("name", ex.ParamName, "#D7");
  206. }
  207. }
  208. [Test]
  209. public void ConvertFrom_Type_StreamReader ()
  210. {
  211. // read UTF-7 content without setting encoding
  212. string fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName;
  213. using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
  214. string result = sr.ReadToEnd ();
  215. Assert.IsTrue (result.Length > 0, "#D1");
  216. Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#D2");
  217. }
  218. // UTF-7 encoding is set, but not used
  219. fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName + ";utf-7";
  220. using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
  221. string result = sr.ReadToEnd ();
  222. Assert.IsTrue (result.Length > 0, "#F1");
  223. Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#F2");
  224. }
  225. // invalid encoding is set, no error
  226. fileRef = _tempFileUTF7 + ";" + typeof (StreamReader).AssemblyQualifiedName + ";utf-99";
  227. using (StreamReader sr = (StreamReader) _converter.ConvertFrom (fileRef)) {
  228. string result = sr.ReadToEnd ();
  229. Assert.IsTrue (result.Length > 0, "#A1");
  230. Assert.IsFalse (result == "\u0021\u0026\u002A\u003B", "#A2");
  231. }
  232. }
  233. [Test]
  234. public void ConvertFrom_Type_MemoryStream ()
  235. {
  236. string fileRef = _tempFileUTF7 + ";" + typeof (MemoryStream).AssemblyQualifiedName;
  237. using (MemoryStream ms = (MemoryStream) _converter.ConvertFrom (fileRef)) {
  238. Assert.IsTrue (ms.Length > 0);
  239. }
  240. }
  241. [Test]
  242. public void ConvertTo ()
  243. {
  244. ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
  245. Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
  246. r, typeof (string)), "#1");
  247. r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
  248. Assert.AreEqual ("mono.bmp;Bitmap;utf-8", (string) _converter.ConvertTo (
  249. r, typeof (string)), "#2");
  250. r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
  251. Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
  252. r, typeof (string)), "#3");
  253. }
  254. private TypeConverter _converter;
  255. private string _tempDirectory;
  256. private string _tempFileUTF7;
  257. }
  258. }