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