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