WriterTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //
  2. // WriterTest.cs: Unit Tests for ResXResourceWriter.
  3. //
  4. // Authors:
  5. // Robert Jordan <[email protected]>
  6. // Gary Barnett <[email protected]>
  7. using System;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.ComponentModel.Design;
  11. using System.Drawing;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Resources;
  15. using System.Text;
  16. using System.Windows.Forms;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Resources
  19. {
  20. [TestFixture]
  21. public class WriterTest : MonoTests.System.Windows.Forms.TestHelper
  22. {
  23. string fileName;
  24. [SetUp]
  25. protected override void SetUp ()
  26. {
  27. fileName = Path.GetTempFileName ();
  28. base.SetUp ();
  29. }
  30. [TearDown]
  31. protected override void TearDown ()
  32. {
  33. File.Delete (fileName);
  34. base.TearDown ();
  35. }
  36. [Test] // ctor (Stream)
  37. [NUnit.Framework.Category ("NotDotNet")]
  38. public void Constructor1_Stream_Null ()
  39. {
  40. try {
  41. new ResXResourceWriter ((Stream) null);
  42. Assert.Fail ("#1");
  43. } catch (ArgumentNullException ex) {
  44. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  45. Assert.IsNull (ex.InnerException, "#3");
  46. Assert.IsNotNull (ex.Message, "#4");
  47. Assert.AreEqual ("stream", ex.ParamName, "#5");
  48. }
  49. }
  50. [Test] // ctor (Stream)
  51. [NUnit.Framework.Category ("NotDotNet")]
  52. public void Constructor1_Stream_NotWritable ()
  53. {
  54. MemoryStream ms = new MemoryStream (new byte [0], false);
  55. try {
  56. new ResXResourceWriter (ms);
  57. Assert.Fail ("#1");
  58. } catch (ArgumentException ex) {
  59. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  60. Assert.IsNull (ex.InnerException, "#3");
  61. Assert.IsNotNull (ex.Message, "#4");
  62. Assert.AreEqual ("stream", ex.ParamName, "#5");
  63. }
  64. }
  65. [Test] // ctor (TextWriter)
  66. [NUnit.Framework.Category ("NotDotNet")]
  67. public void Constructor2_TextWriter_Null ()
  68. {
  69. try {
  70. new ResXResourceWriter ((TextWriter) null);
  71. Assert.Fail ("#1");
  72. } catch (ArgumentNullException ex) {
  73. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  74. Assert.IsNull (ex.InnerException, "#3");
  75. Assert.IsNotNull (ex.Message, "#4");
  76. Assert.AreEqual ("textWriter", ex.ParamName, "#5");
  77. }
  78. }
  79. [Test] // ctor (String)
  80. [NUnit.Framework.Category ("NotDotNet")]
  81. public void Constructor3_FileName_Null ()
  82. {
  83. try {
  84. new ResXResourceWriter ((string) null);
  85. Assert.Fail ("#1");
  86. } catch (ArgumentNullException ex) {
  87. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  88. Assert.IsNull (ex.InnerException, "#3");
  89. Assert.IsNotNull (ex.Message, "#4");
  90. Assert.AreEqual ("fileName", ex.ParamName, "#5");
  91. }
  92. }
  93. [Test]
  94. public void AddResource_WithComment ()
  95. {
  96. ResXResourceWriter w = new ResXResourceWriter (fileName);
  97. ResXDataNode node = new ResXDataNode ("key", "value");
  98. node.Comment = "comment is preserved";
  99. w.AddResource (node);
  100. w.Generate ();
  101. w.Close ();
  102. ResXResourceReader r = new ResXResourceReader (fileName);
  103. ITypeResolutionService typeres = null;
  104. r.UseResXDataNodes = true;
  105. int count = 0;
  106. foreach (DictionaryEntry o in r)
  107. {
  108. string key = o.Key.ToString();
  109. node = (ResXDataNode)o.Value;
  110. string value = node.GetValue (typeres).ToString ();
  111. string comment = node.Comment;
  112. Assert.AreEqual ("key", key, "key");
  113. Assert.AreEqual ("value", value, "value");
  114. Assert.AreEqual ("comment is preserved", comment, "comment");
  115. Assert.AreEqual (0, count, "too many nodes");
  116. count++;
  117. }
  118. r.Close ();
  119. File.Delete (fileName);
  120. }
  121. [Test]
  122. public void TestWriter ()
  123. {
  124. ResXResourceWriter w = new ResXResourceWriter (fileName);
  125. w.AddResource ("String", "hola");
  126. w.AddResource ("String2", (object) "hello");
  127. w.AddResource ("Int", 42);
  128. w.AddResource ("Enum", PlatformID.Win32NT);
  129. w.AddResource ("Convertible", new Point (43, 45));
  130. w.AddResource ("Serializable", new ArrayList (new byte [] { 1, 2, 3, 4 }));
  131. w.AddResource ("ByteArray", new byte [] { 12, 13, 14 });
  132. w.AddResource ("ByteArray2", (object) new byte [] { 15, 16, 17 });
  133. w.AddResource ("IntArray", new int [] { 1012, 1013, 1014 });
  134. w.AddResource ("StringArray", new string [] { "hello", "world" });
  135. w.AddResource ("Image", new Bitmap (1, 1));
  136. w.AddResource ("StrType", new MyStrType ("hello"));
  137. w.AddResource ("BinType", new MyBinType ("world"));
  138. try {
  139. w.AddResource ("NonSerType", new MyNonSerializableType ());
  140. Assert.Fail ("#0");
  141. } catch (InvalidOperationException) {
  142. }
  143. w.Generate ();
  144. w.Close ();
  145. ResXResourceReader r = new ResXResourceReader (fileName);
  146. Hashtable h = new Hashtable ();
  147. foreach (DictionaryEntry e in r) {
  148. h.Add (e.Key, e.Value);
  149. }
  150. r.Close ();
  151. Assert.AreEqual ("hola", (string) h ["String"], "#1");
  152. Assert.AreEqual ("hello", (string) h ["String2"], "#2");
  153. Assert.AreEqual (42, (int) h ["Int"], "#3");
  154. Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h ["Enum"], "#4");
  155. Assert.AreEqual (43, ((Point) h ["Convertible"]).X, "#5");
  156. Assert.AreEqual (2, (byte) ((ArrayList) h ["Serializable"]) [1], "#6");
  157. Assert.AreEqual (13, ((byte []) h ["ByteArray"]) [1], "#7");
  158. Assert.AreEqual (16, ((byte []) h ["ByteArray2"]) [1], "#8");
  159. Assert.AreEqual (1013, ((int []) h ["IntArray"]) [1], "#9");
  160. Assert.AreEqual ("world", ((string []) h ["StringArray"]) [1], "#10");
  161. Assert.AreEqual (typeof (Bitmap), h ["Image"].GetType (), "#11");
  162. Assert.AreEqual ("hello", ((MyStrType) h ["StrType"]).Value, "#12");
  163. Assert.AreEqual ("world", ((MyBinType) h ["BinType"]).Value, "#13");
  164. File.Delete (fileName);
  165. }
  166. ResXDataNode GetNodeFromResXWithBasePath (ResXDataNode node, string basePath)
  167. {
  168. StringWriter sw = new StringWriter ();
  169. using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
  170. writer.BasePath = basePath;
  171. writer.AddResource (node);
  172. }
  173. StringReader sr = new StringReader (sw.ToString ());
  174. using (ResXResourceReader reader = new ResXResourceReader (sr)) {
  175. reader.UseResXDataNodes = true;
  176. IDictionaryEnumerator enumerator = reader.GetEnumerator ();
  177. enumerator.MoveNext ();
  178. return ((DictionaryEntry) enumerator.Current).Value as ResXDataNode;
  179. }
  180. }
  181. [Test]
  182. public void BasePath_ChangesAbsoluteFileRef_Node ()
  183. {
  184. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/dir/filename.ext", "System.String"));
  185. var returnedNode = GetNodeFromResXWithBasePath (node, @"/dir");
  186. Assert.IsNotNull (returnedNode, "#A1");
  187. Assert.AreEqual (@"dir/filename.ext", returnedNode.FileRef.FileName, "#A2");
  188. }
  189. [Test]
  190. public void BasePath_ChangesAbsoluteFileRef_Object ()
  191. {
  192. var fileref = new ResXFileRef (@"/dir/dir/filename.ext", "System.String");
  193. string basePath = @"/dir";
  194. StringWriter sw = new StringWriter ();
  195. using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
  196. writer.BasePath = basePath;
  197. writer.AddResource ("name", fileref);
  198. }
  199. StringReader sr = new StringReader (sw.ToString ());
  200. using (ResXResourceReader reader = new ResXResourceReader (sr)) {
  201. reader.UseResXDataNodes = true;
  202. IDictionaryEnumerator enumerator = reader.GetEnumerator ();
  203. enumerator.MoveNext ();
  204. var returnedNode = ((DictionaryEntry) enumerator.Current).Value as ResXDataNode;
  205. Assert.AreEqual (@"dir/filename.ext", returnedNode.FileRef.FileName);
  206. }
  207. }
  208. [Test]
  209. public void BasePath_ChangesAbsoluteFileRef_Deeper ()
  210. {
  211. var node = new ResXDataNode ("name", new ResXFileRef (@"/adir/filename.ext", "System.String"));
  212. var returnedNode = GetNodeFromResXWithBasePath (node, @"/dir1/dir2");
  213. Assert.IsNotNull (returnedNode, "#A1");
  214. Assert.AreEqual (@"../../adir/filename.ext", returnedNode.FileRef.FileName, "#A2");
  215. }
  216. [Test]
  217. public void BasePath_ComplexPath ()
  218. {
  219. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/dir/../filename.ext", "System.String"));
  220. var returnedNode = GetNodeFromResXWithBasePath (node, @"/dir");
  221. Assert.IsNotNull (returnedNode, "#A1");
  222. Assert.AreEqual (@"dir/../filename.ext", returnedNode.FileRef.FileName, "#A2");
  223. }
  224. [Test]
  225. public void BasePath_IgnoresTrailingSeparator ()
  226. {
  227. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/filename.ext", "System.String"));
  228. var nonTrailNode = GetNodeFromResXWithBasePath (node, @"/dir");
  229. Assert.IsNotNull (nonTrailNode, "#A1");
  230. Assert.AreEqual (@"filename.ext", nonTrailNode.FileRef.FileName, "#A2");
  231. var trailingNode = GetNodeFromResXWithBasePath (node, @"/dir/");
  232. Assert.IsNotNull (trailingNode, "#A3");
  233. Assert.AreEqual (@"filename.ext", trailingNode.FileRef.FileName, "#A4");
  234. }
  235. [Test]
  236. public void BasePath_IgnoredIfNotPartOfPath () // not really a valid test on linux systems
  237. {
  238. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/filename.ext", "System.String"));
  239. var returnedNode = GetNodeFromResXWithBasePath (node, @"D/");
  240. Assert.IsNotNull (returnedNode, "#A1");
  241. Assert.AreEqual (@"/dir/filename.ext", returnedNode.FileRef.FileName, "#A2");
  242. }
  243. [Test] // FIXME: this fails on mono ("/dir/filename.ext" is returned) but passes on .net
  244. [NUnit.Framework.Category ("NotWorking")]
  245. public void BasePath_Root ()
  246. {
  247. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/filename.ext", "System.String"));
  248. var returnedNode = GetNodeFromResXWithBasePath (node, @"/");
  249. Assert.IsNotNull (returnedNode, "#A1");
  250. Assert.AreEqual (@"dir/filename.ext", returnedNode.FileRef.FileName, "#A2");
  251. }
  252. [Test]
  253. public void BasePath_RelativeFileRef ()
  254. {
  255. var node = new ResXDataNode ("name", new ResXFileRef (@"../../filename.ext", "System.String"));
  256. var returnedNode = GetNodeFromResXWithBasePath (node, @"../");
  257. Assert.IsNotNull (returnedNode, "#A1");
  258. Assert.AreEqual (@"../filename.ext", returnedNode.FileRef.FileName, "#A2");
  259. }
  260. [Test]
  261. public void BasePath_DoesntAffectOriginalNode ()
  262. {
  263. var node = new ResXDataNode ("name", new ResXFileRef (@"/dir/dir/filename.ext", "System.String"));
  264. var returnedNode = GetNodeFromResXWithBasePath (node, @"/dir");
  265. Assert.IsNotNull (returnedNode, "#A1");
  266. Assert.AreEqual (@"dir/filename.ext", returnedNode.FileRef.FileName, "#A2");
  267. Assert.AreEqual (@"/dir/dir/filename.ext", node.FileRef.FileName, "#A3");
  268. }
  269. }
  270. [Serializable]
  271. [TypeConverter (typeof (MyStrTypeConverter))]
  272. public class MyStrType
  273. {
  274. public string Value;
  275. public MyStrType (string s)
  276. {
  277. Value = s;
  278. }
  279. }
  280. public class MyStrTypeConverter : TypeConverter
  281. {
  282. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  283. {
  284. if (destinationType == typeof (string))
  285. return true;
  286. return base.CanConvertTo (context, destinationType);
  287. }
  288. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  289. {
  290. if (sourceType == typeof (string))
  291. return true;
  292. return base.CanConvertFrom (context, sourceType);
  293. }
  294. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  295. {
  296. if (destinationType == typeof (string))
  297. return ((MyStrType) value).Value;
  298. return base.ConvertTo (context, culture, value, destinationType);
  299. }
  300. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  301. {
  302. if (value.GetType () == typeof (string))
  303. return new MyStrType ((string) value);
  304. return base.ConvertFrom (context, culture, value);
  305. }
  306. }
  307. [Serializable]
  308. [TypeConverter (typeof (MyBinTypeConverter))]
  309. public class MyBinType
  310. {
  311. public string Value;
  312. public MyBinType (string s)
  313. {
  314. Value = s;
  315. }
  316. }
  317. public class MyBinTypeConverter : TypeConverter
  318. {
  319. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  320. {
  321. if (destinationType == typeof (byte []))
  322. return true;
  323. return base.CanConvertTo (context, destinationType);
  324. }
  325. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  326. {
  327. if (sourceType == typeof (byte []))
  328. return true;
  329. return base.CanConvertFrom (context, sourceType);
  330. }
  331. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  332. {
  333. if (destinationType == typeof (byte []))
  334. return Encoding.Default.GetBytes (((MyBinType) value).Value);
  335. return base.ConvertTo (context, culture, value, destinationType);
  336. }
  337. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  338. {
  339. if (value.GetType () == typeof (byte []))
  340. return new MyBinType (Encoding.Default.GetString ((byte []) value));
  341. return base.ConvertFrom (context, culture, value);
  342. }
  343. }
  344. [TypeConverter (typeof (MyNonSerializableTypeConverter))]
  345. public class MyNonSerializableType
  346. {
  347. public MyNonSerializableType ()
  348. {
  349. }
  350. }
  351. public class MyNonSerializableTypeConverter : TypeConverter
  352. {
  353. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  354. {
  355. if (destinationType == typeof (byte []))
  356. return true;
  357. return base.CanConvertTo (context, destinationType);
  358. }
  359. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  360. {
  361. if (sourceType == typeof (byte []))
  362. return true;
  363. return base.CanConvertFrom (context, sourceType);
  364. }
  365. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  366. {
  367. if (destinationType == typeof (byte []))
  368. return new byte [] { 0, 1, 2, 3 };
  369. return base.ConvertTo (context, culture, value, destinationType);
  370. }
  371. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  372. {
  373. if (value.GetType () == typeof (byte []))
  374. return new MyNonSerializableType ();
  375. return base.ConvertFrom (context, culture, value);
  376. }
  377. }
  378. }