ResXDataNodeTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // ResXFileRefTest.cs: Unit Tests for ResXFileRef.
  23. //
  24. // Authors:
  25. // Andreia Gaita ([email protected])
  26. // Gary Barnett ([email protected])
  27. using System;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Drawing;
  31. using System.Resources;
  32. using System.Runtime.Serialization;
  33. using System.Collections;
  34. using NUnit.Framework;
  35. using System.ComponentModel.Design;
  36. using System.Runtime.Serialization.Formatters.Binary;
  37. namespace MonoTests.System.Resources {
  38. [TestFixture]
  39. public class ResXDataNodeTest : ResourcesTestHelper
  40. {
  41. string _tempDirectory;
  42. string _otherTempDirectory;
  43. [Test]
  44. [ExpectedException (typeof (ArgumentNullException))]
  45. public void ConstructorEx1 ()
  46. {
  47. ResXDataNode d = new ResXDataNode (null, (object)null);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentNullException))]
  51. public void ConstructorEx2A ()
  52. {
  53. ResXDataNode d = new ResXDataNode (null, new ResXFileRef ("filename", "typename"));
  54. }
  55. [Test]
  56. [ExpectedException (typeof (ArgumentNullException))]
  57. public void ConstructorEx2B ()
  58. {
  59. ResXDataNode d = new ResXDataNode ("aname", (ResXFileRef) null);
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentException))]
  63. public void ConstructorEx3 ()
  64. {
  65. ResXDataNode d = new ResXDataNode ("", (object) null);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentNullException))]
  69. public void ConstructorEx4 ()
  70. {
  71. ResXDataNode d = new ResXDataNode ("", (ResXFileRef) null);
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentException))]
  75. public void ConstructorEx5 ()
  76. {
  77. ResXDataNode d = new ResXDataNode ("", new ResXFileRef ("filename", "typename"));
  78. }
  79. [Test]
  80. [ExpectedException (typeof (InvalidOperationException))]
  81. public void ConstructorEx6 ()
  82. {
  83. ResXDataNode d = new ResXDataNode ("name", new notserializable ());
  84. }
  85. [Test]
  86. public void Name ()
  87. {
  88. ResXDataNode node = new ResXDataNode ("startname", (object) null);
  89. Assert.AreEqual ("startname", node.Name, "#A1");
  90. node.Name = "newname";
  91. Assert.AreEqual ("newname", node.Name, "#A2");
  92. }
  93. [Test, ExpectedException (typeof (ArgumentNullException))]
  94. public void NameCantBeNull ()
  95. {
  96. ResXDataNode node = new ResXDataNode ("startname", (object) null);
  97. node.Name = null;
  98. }
  99. [Test, ExpectedException (typeof (ArgumentException))]
  100. public void NameCantBeEmpty ()
  101. {
  102. ResXDataNode node = new ResXDataNode ("name", (object) null);
  103. node.Name = "";
  104. }
  105. [Test]
  106. public void FileRef ()
  107. {
  108. ResXFileRef fileRef = new ResXFileRef ("fileName", "Type.Name");
  109. ResXDataNode node = new ResXDataNode ("name", fileRef);
  110. Assert.AreEqual (fileRef, node.FileRef, "#A1");
  111. }
  112. [Test]
  113. public void Comment ()
  114. {
  115. ResXDataNode node = new ResXDataNode ("name", (object) null);
  116. node.Comment = "acomment";
  117. Assert.AreEqual ("acomment", node.Comment, "#A1");
  118. }
  119. [Test]
  120. public void CommentNullToStringEmpty ()
  121. {
  122. ResXDataNode node = new ResXDataNode ("name", (object) null);
  123. node.Comment = null;
  124. Assert.AreEqual (String.Empty, node.Comment, "#A1");
  125. }
  126. [Test]
  127. public void WriteRead1 ()
  128. {
  129. serializable ser = new serializable ("aaaaa", "bbbbb");
  130. ResXDataNode dn = new ResXDataNode ("test", ser);
  131. dn.Comment = "comment";
  132. string resXFile = GetResXFileWithNode (dn, "resx.resx");
  133. bool found = false;
  134. ResXResourceReader rr = new ResXResourceReader (resXFile);
  135. rr.UseResXDataNodes = true;
  136. IDictionaryEnumerator en = rr.GetEnumerator ();
  137. while (en.MoveNext ()) {
  138. ResXDataNode node = ((DictionaryEntry)en.Current).Value as ResXDataNode;
  139. if (node == null)
  140. break;
  141. serializable o = node.GetValue ((AssemblyName []) null) as serializable;
  142. if (o != null) {
  143. found = true;
  144. Assert.AreEqual (ser, o, "#A1");
  145. Assert.AreEqual ("comment", node.Comment, "#A3");
  146. }
  147. }
  148. rr.Close ();
  149. Assert.IsTrue (found, "#A2 - Serialized object not found on resx");
  150. }
  151. [Test]
  152. public void ConstructorResXFileRef()
  153. {
  154. ResXDataNode node = GetNodeFileRefToIcon ();
  155. Assert.IsNotNull (node.FileRef, "#A1");
  156. Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, node.FileRef.TypeName, "#A2");
  157. Assert.AreEqual ("test", node.Name, "#A3");
  158. }
  159. [Test]
  160. public void NullObjectGetValueTypeNameIsNull ()
  161. {
  162. ResXDataNode node = new ResXDataNode ("aname", (object) null);
  163. Assert.IsNull (node.GetValueTypeName ((AssemblyName []) null), "#A1");
  164. }
  165. [Test]
  166. public void NullObjectWrittenToResXOK ()
  167. {
  168. ResXDataNode node = new ResXDataNode ("aname", (object) null);
  169. ResXDataNode returnedNode = GetNodeFromResXReader (node);
  170. Assert.IsNotNull (returnedNode, "#A1");
  171. Assert.IsNull (returnedNode.GetValue ((AssemblyName []) null), "#A2");
  172. }
  173. [Test]
  174. public void NullObjectReturnedFromResXGetValueTypeNameReturnsObject ()
  175. {
  176. ResXDataNode node = new ResXDataNode ("aname", (object) null);
  177. ResXDataNode returnedNode = GetNodeFromResXReader (node);
  178. Assert.IsNotNull (returnedNode, "#A1");
  179. Assert.IsNull (returnedNode.GetValue ((AssemblyName []) null), "#A2");
  180. string type = returnedNode.GetValueTypeName ((AssemblyName []) null);
  181. Assert.AreEqual (typeof (object).AssemblyQualifiedName, type, "#A3");
  182. }
  183. [Test]
  184. public void DoesNotRequireResXFileToBeOpen_Serializable ()
  185. {
  186. serializable ser = new serializable ("aaaaa", "bbbbb");
  187. ResXDataNode dn = new ResXDataNode ("test", ser);
  188. string resXFile = GetResXFileWithNode (dn,"resx.resx");
  189. ResXResourceReader rr = new ResXResourceReader (resXFile);
  190. rr.UseResXDataNodes = true;
  191. IDictionaryEnumerator en = rr.GetEnumerator ();
  192. en.MoveNext ();
  193. ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  194. rr.Close ();
  195. Assert.IsNotNull (node,"#A1");
  196. serializable o = node.GetValue ((AssemblyName []) null) as serializable;
  197. Assert.IsNotNull (o, "#A2");
  198. }
  199. [Test]
  200. public void DoesNotRequireResXFileToBeOpen_TypeConverter ()
  201. {
  202. ResXDataNode dn = new ResXDataNode ("test", 34L);
  203. string resXFile = GetResXFileWithNode (dn,"resx.resx");
  204. ResXResourceReader rr = new ResXResourceReader (resXFile);
  205. rr.UseResXDataNodes = true;
  206. IDictionaryEnumerator en = rr.GetEnumerator ();
  207. en.MoveNext ();
  208. ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  209. rr.Close ();
  210. Assert.IsNotNull (node, "#A1");
  211. object o = node.GetValue ((AssemblyName []) null);
  212. Assert.IsInstanceOfType (typeof (long), o, "#A2");
  213. Assert.AreEqual (34L, o, "#A3");
  214. }
  215. [Test,ExpectedException (typeof(TypeLoadException))]
  216. public void AssemblyNamesPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter ()
  217. {
  218. string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
  219. AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) };
  220. string resXFile = GetFileFromString ("test.resx", convertableResXWithoutAssemblyName);
  221. using (ResXResourceReader rr = new ResXResourceReader (resXFile, assemblyNames)) {
  222. rr.UseResXDataNodes = true;
  223. IDictionaryEnumerator en = rr.GetEnumerator ();
  224. en.MoveNext ();
  225. ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  226. Assert.IsNotNull (node, "#A1");
  227. //should raise exception
  228. object o = node.GetValue ((AssemblyName []) null);
  229. }
  230. }
  231. [Test]
  232. public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter ()
  233. {
  234. ResXDataNode dn = new ResXDataNode ("test", 34L);
  235. string resXFile = GetResXFileWithNode (dn,"resx.resx");
  236. ResXResourceReader rr = new ResXResourceReader (resXFile, new ReturnIntITRS ());
  237. rr.UseResXDataNodes = true;
  238. IDictionaryEnumerator en = rr.GetEnumerator ();
  239. en.MoveNext ();
  240. ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  241. Assert.IsNotNull (node, "#A1");
  242. object o = node.GetValue ((AssemblyName []) null);
  243. Assert.IsInstanceOfType (typeof (long), o, "#A2");
  244. Assert.AreEqual (34L, o, "#A3");
  245. rr.Close ();
  246. }
  247. [Test]
  248. public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_Serializable ()
  249. {
  250. serializable ser = new serializable ("aaaaa", "bbbbb");
  251. ResXDataNode dn = new ResXDataNode ("test", ser);
  252. string resXFile = GetResXFileWithNode (dn,"resx.resx");
  253. ResXResourceReader rr = new ResXResourceReader (resXFile, new ReturnSerializableSubClassITRS ());
  254. rr.UseResXDataNodes = true;
  255. IDictionaryEnumerator en = rr.GetEnumerator ();
  256. en.MoveNext ();
  257. ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  258. Assert.IsNotNull (node, "#A1");
  259. object o = node.GetValue ((AssemblyName []) null);
  260. Assert.IsNotInstanceOfType (typeof (serializableSubClass), o, "#A2");
  261. Assert.IsInstanceOfType (typeof (serializable), o, "#A3");
  262. rr.Close ();
  263. }
  264. [Test]
  265. public void BasePathSetOnResXResourceReaderDoesAffectResXDataNode ()
  266. {
  267. ResXFileRef fileRef = new ResXFileRef ("file.name", "type.name");
  268. ResXDataNode node = new ResXDataNode("anode", fileRef);
  269. string resXFile = GetResXFileWithNode (node, "afilename.xxx");
  270. using (ResXResourceReader rr = new ResXResourceReader (resXFile)) {
  271. rr.BasePath = "basePath";
  272. rr.UseResXDataNodes = true;
  273. IDictionaryEnumerator en = rr.GetEnumerator ();
  274. en.MoveNext ();
  275. ResXDataNode returnedNode = ((DictionaryEntry) en.Current).Value as ResXDataNode;
  276. Assert.IsNotNull (node, "#A1");
  277. Assert.AreEqual (Path.Combine ("basePath", "file.name"), returnedNode.FileRef.FileName, "#A2");
  278. }
  279. }
  280. [TearDown]
  281. protected override void TearDown ()
  282. {
  283. //teardown
  284. if (Directory.Exists (_tempDirectory))
  285. Directory.Delete (_tempDirectory, true);
  286. base.TearDown ();
  287. }
  288. string GetResXFileWithNode (ResXDataNode node, string filename)
  289. {
  290. string fullfileName;
  291. _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest");
  292. _otherTempDirectory = Path.Combine (_tempDirectory, "in");
  293. if (!Directory.Exists (_otherTempDirectory)) {
  294. Directory.CreateDirectory (_otherTempDirectory);
  295. }
  296. fullfileName = Path.Combine (_tempDirectory, filename);
  297. using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) {
  298. writer.AddResource (node);
  299. }
  300. return fullfileName;
  301. }
  302. private string GetFileFromString (string filename, string filecontents)
  303. {
  304. _tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest");
  305. _otherTempDirectory = Path.Combine (_tempDirectory, "in");
  306. if (!Directory.Exists (_otherTempDirectory)) {
  307. Directory.CreateDirectory (_otherTempDirectory);
  308. }
  309. string filepath = Path.Combine (_tempDirectory, filename);
  310. StreamWriter writer = new StreamWriter(filepath,false);
  311. writer.Write (filecontents);
  312. writer.Close ();
  313. return filepath;
  314. }
  315. static string convertableResXWithoutAssemblyName =
  316. @"<?xml version=""1.0"" encoding=""utf-8""?>
  317. <root>
  318. <resheader name=""resmimetype"">
  319. <value>text/microsoft-resx</value>
  320. </resheader>
  321. <resheader name=""version"">
  322. <value>2.0</value>
  323. </resheader>
  324. <resheader name=""reader"">
  325. <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  326. </resheader>
  327. <resheader name=""writer"">
  328. <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  329. </resheader>
  330. <data name=""test"" type=""DummyAssembly.Convertable"">
  331. <value>im a name im a value</value>
  332. </data>
  333. </root>";
  334. }
  335. }