ResourceReaderTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // MonoTests.System.Resources.ResourceReaderTest.cs
  3. //
  4. // Author:
  5. // Nick Drochak ([email protected])
  6. //
  7. // (C) 2001 Nick Drochak II
  8. //
  9. using System;
  10. using System.Resources;
  11. using System.IO;
  12. using System.Collections;
  13. using MonoTests.System.Resources;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Resources {
  16. public class ResourceReaderTest : TestCase {
  17. private static string m_ResourceFile = "resources" + Path.DirectorySeparatorChar + "MyResources.resources";
  18. private static string m_BadResourceFile = "resources" + Path.DirectorySeparatorChar + "Empty.resources";
  19. protected override void SetUp() {
  20. }
  21. public void TestConstructorStringExceptions() {
  22. ResourceReader r;
  23. try {
  24. r = new ResourceReader((String)null);
  25. Fail("Should throw exception on null");
  26. } catch{}
  27. try {
  28. r = new ResourceReader("");
  29. Fail("Should throw exception on empty path");
  30. } catch{}
  31. try {
  32. // use a file name that is *very* unlikely to exsist
  33. r = new ResourceReader("j38f8axvnn9h38hfa9nxn93f8hav4zvag87vvah32o");
  34. Fail("Should throw exception on file not found");
  35. } catch{}
  36. try {
  37. r = new ResourceReader(m_BadResourceFile);
  38. Fail("Should throw exception on bad resource file");
  39. }
  40. catch {}
  41. }
  42. public void TestConstructorString() {
  43. if (!File.Exists(m_ResourceFile)) {
  44. Fail("Resource file is not where it should be:" + Directory.GetCurrentDirectory()+ "\\" + m_ResourceFile);
  45. }
  46. ResourceReader r = null;
  47. try {
  48. r = new ResourceReader(m_ResourceFile);
  49. }
  50. catch {
  51. Fail("Should have been able to open resource file.");
  52. }
  53. finally {
  54. if (null != r)
  55. r.Close();
  56. }
  57. Assert("String constructor should not be null", null != r);
  58. }
  59. public void TestConstructorStreamExceptions() {
  60. ResourceReader r;
  61. try {
  62. r = new ResourceReader((Stream)null);
  63. Fail("Should throw exception on null");
  64. } catch{}
  65. try {
  66. Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
  67. stream.Close();
  68. r = new ResourceReader(stream);
  69. Fail("Should throw exception on cannot read");
  70. } catch{}
  71. }
  72. public void TestStream(){
  73. ResourceReader r = null;
  74. try {
  75. Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
  76. r = new ResourceReader(stream);
  77. }
  78. catch{
  79. Fail("Should not throw exception constructing from stream");
  80. }
  81. finally {
  82. if (null != r) {
  83. r.Close();
  84. }
  85. }
  86. }
  87. public void TestClose() {
  88. ResourceReader r = null;
  89. Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
  90. r = new ResourceReader(stream);
  91. r.Close();
  92. try {
  93. stream = new FileStream (m_ResourceFile, FileMode.Open);
  94. }
  95. catch{
  96. Fail("Should be able to open the stream again after close");
  97. }
  98. finally {
  99. if (null != stream) {
  100. stream.Close();
  101. }
  102. }
  103. }
  104. public void TestEnumerator(){
  105. ResourceReader reader = null;
  106. Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
  107. reader = new ResourceReader(stream);
  108. IDictionaryEnumerator en = reader.GetEnumerator();
  109. // Goes through the enumerator, printing out the key and value pairs.
  110. while (en.MoveNext()) {
  111. DictionaryEntry de = (DictionaryEntry)en.Current;
  112. Assert("Current.Key should not be empty",String.Empty != (string)de.Key);
  113. Assert("Current.Value should not be empty",String.Empty != (string)de.Value);
  114. Assert("Current.Value should not be empty",String.Empty != (string)de.Value);
  115. Assert("Entry.Key should not be empty",String.Empty != (string)en.Key);
  116. Assert("Entry.Value should not be empty",String.Empty != (string)en.Value);
  117. }
  118. reader.Close();
  119. }
  120. } // class ResourceReaderTest
  121. } // namespace MonoTests.System.Resources