ResXDataNodeFileRefGetValueTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // ResXDataNodeFileRefGetValueTests.cs
  3. //
  4. // Author:
  5. // Gary Barnett ([email protected])
  6. //
  7. // Copyright (C) Gary Barnett (2012)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. using System;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Drawing;
  31. using System.Resources;
  32. using System.Collections;
  33. using NUnit.Framework;
  34. using System.ComponentModel.Design;
  35. using System.Runtime.Serialization.Formatters.Binary;
  36. namespace MonoTests.System.Resources {
  37. [TestFixture]
  38. public class ResXDataNodeFileRefGetValueTests : ResourcesTestHelper {
  39. [Test]
  40. public void ITRSNotUsedWhenNodeFromReader ()
  41. {
  42. ResXDataNode originalNode, returnedNode;
  43. originalNode = GetNodeFileRefToSerializable ("ser.bbb",true);
  44. returnedNode = GetNodeFromResXReader (originalNode);
  45. Assert.IsNotNull (returnedNode, "#A1");
  46. object val = returnedNode.GetValue (new ReturnSerializableSubClassITRS ());
  47. Assert.IsNotInstanceOfType (typeof (serializableSubClass), val, "#A2");
  48. Assert.IsInstanceOfType (typeof (serializable), val, "#A3");
  49. }
  50. [Test, ExpectedException(typeof (TypeLoadException))]
  51. public void CantGetValueWithOnlyFullNameAsType ()
  52. {
  53. ResXDataNode originalNode, returnedNode;
  54. originalNode = GetNodeFileRefToSerializable ("ser.bbb", false);
  55. returnedNode = GetNodeFromResXReader (originalNode);
  56. Assert.IsNotNull (returnedNode, "#A1");
  57. object obj = returnedNode.GetValue ((AssemblyName []) null);
  58. }
  59. [Test, ExpectedException (typeof (TypeLoadException))]
  60. public void CantGetValueWithOnlyFullNameAsTypeByProvidingAssemblyName ()
  61. {
  62. ResXDataNode originalNode, returnedNode;
  63. string aName = "System.Windows.Forms_test_net_2_0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
  64. AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) };
  65. originalNode = GetNodeFileRefToSerializable ("ser.bbb", false);
  66. returnedNode = GetNodeFromResXReader (originalNode);
  67. Assert.IsNotNull (returnedNode, "#A1");
  68. object obj = returnedNode.GetValue (assemblyNames);
  69. }
  70. [Test]
  71. public void ITRSNotUsedWhenNodeCreatedNew ()
  72. {
  73. ResXDataNode node;
  74. node = GetNodeFileRefToSerializable ("ser.bbb",true);
  75. object val = node.GetValue (new ReturnSerializableSubClassITRS ());
  76. Assert.IsNotInstanceOfType (typeof (serializableSubClass), val, "#A1");
  77. Assert.IsInstanceOfType (typeof (serializable), val, "#A2");
  78. }
  79. [Test, ExpectedException (typeof (TargetInvocationException))]
  80. public void LoadingFileFails ()
  81. {
  82. string corruptFile = Path.GetTempFileName ();
  83. ResXFileRef fileRef = new ResXFileRef (corruptFile, typeof (serializable).AssemblyQualifiedName);
  84. File.AppendAllText (corruptFile, "corrupt");
  85. ResXDataNode node = new ResXDataNode ("aname", fileRef);
  86. node.GetValue ((AssemblyName []) null);
  87. }
  88. #region initial
  89. [Test]
  90. public void NullAssemblyNamesOK ()
  91. {
  92. ResXDataNode node = GetNodeFileRefToIcon ();
  93. Object ico = node.GetValue ((AssemblyName []) null);
  94. Assert.IsNotNull (ico, "#A1");
  95. Assert.IsInstanceOfType (typeof (Icon), ico, "#A2");
  96. }
  97. [Test]
  98. public void NullITRSOK ()
  99. {
  100. ResXDataNode node = GetNodeFileRefToIcon ();
  101. Object ico = node.GetValue ((ITypeResolutionService) null);
  102. Assert.IsNotNull (ico, "#A1");
  103. Assert.IsInstanceOfType (typeof (Icon), ico, "#A2");
  104. }
  105. [Test]
  106. public void WrongITRSOK ()
  107. {
  108. ResXDataNode node = GetNodeFileRefToIcon ();
  109. Object ico = node.GetValue (new DummyITRS ());
  110. Assert.IsNotNull (ico, "#A1");
  111. Assert.IsInstanceOfType (typeof (Icon), ico, "#A2");
  112. }
  113. [Test]
  114. public void WrongAssemblyNamesOK ()
  115. {
  116. ResXDataNode node = GetNodeFileRefToIcon ();
  117. AssemblyName [] ass = new AssemblyName [1];
  118. ass [0] = new AssemblyName ("System.Design");
  119. Object ico = node.GetValue (ass);
  120. Assert.IsNotNull (ico, "#A1");
  121. Assert.IsInstanceOfType (typeof (Icon), ico, "#A2");
  122. }
  123. #endregion
  124. }
  125. }