* ResourceManager.cs: Default to RuntimeResourceSet for all ctors.
[mono.git] / mcs / class / corlib / Test / System.Resources / ResourceSetTest.cs
1 // 
2 // ResourceSetTest.cs: NUnit Test Cases for System.Resources.ResourceSet
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc. (http://www.novell.com)
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 //
28
29 using System;
30 using System.Collections;
31 using System.IO;
32 using System.Resources;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Resources {
37
38         public class DefaultResourceSet : ResourceSet {
39
40                 public DefaultResourceSet ()
41                 {
42                 }
43
44                 public Hashtable GetTable ()
45                 {
46                         return base.Table;
47                 }
48
49                 public IResourceReader GetReader ()
50                 {
51                         return base.Reader;
52                 }
53         }
54
55         public class ClonableObject : ICloneable {
56
57                 private int n = 0;
58
59                 private ClonableObject (int value)
60                 {
61                         n = value;
62                 }
63
64                 public ClonableObject ()
65                         : this (0)
66                 {
67                 }
68
69                 public int Value {
70                         get { return n; }
71                 }
72
73                 public object Clone ()
74                 {
75                         object clone = new ClonableObject (n);
76                         n++;
77                         return (ClonableObject) clone;
78                 }
79         }
80
81         public class CloneResourceSet : ResourceSet {
82
83                 public CloneResourceSet (ClonableObject c)
84                 {
85                         Table.Add ("clone", c);
86                 }
87         }
88
89         [TestFixture]
90         public class ResourceSetTest {
91
92                 [Test] // ctor (IResourceReader)
93                 public void Constructor1_Reader_Null ()
94                 {
95                         try {
96                                 new ResourceSet ((IResourceReader) null);
97                                 Assert.Fail ("#1");
98                         } catch (ArgumentNullException ex) {
99                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
100                                 Assert.IsNull (ex.InnerException, "#3");
101                                 Assert.IsNotNull (ex.Message, "#4");
102                                 Assert.AreEqual ("reader", ex.ParamName, "#5");
103                         }
104                 }
105
106                 [Test] // ctor (Stream)
107                 public void Constructor2_Stream_Closed ()
108                 {
109                         MemoryStream ms = new MemoryStream ();
110                         ms.Close ();
111
112                         try {
113                                 new ResourceSet (ms);
114                                 Assert.Fail ("#1");
115                         } catch (ArgumentException ex) {
116                                 // Stream was not readable
117                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
118                                 Assert.IsNull (ex.InnerException, "#3");
119                                 Assert.IsNotNull (ex.Message, "#4");
120                                 Assert.IsNull (ex.ParamName, "#5");
121                         }
122                 }
123
124                 [Test] // ctor (Stream)
125                 public void Constructor2_Stream_Null ()
126                 {
127                         try {
128                                 new ResourceSet ((Stream) null);
129                                 Assert.Fail ("#1");
130                         } catch (ArgumentNullException ex) {
131                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
132                                 Assert.IsNull (ex.InnerException, "#3");
133                                 Assert.IsNotNull (ex.Message, "#4");
134                                 Assert.AreEqual ("stream", ex.ParamName, "#5");
135                         }
136                 }
137
138                 [Test] // ctor (String)
139                 public void Constructor3_FileName_Null ()
140                 {
141                         try {
142                                 new ResourceSet ((string) null);
143                                 Assert.Fail ("#1");
144                         } catch (ArgumentNullException ex) {
145                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
146                                 Assert.IsNull (ex.InnerException, "#3");
147                                 Assert.IsNotNull (ex.Message, "#4");
148                                 Assert.AreEqual ("path", ex.ParamName, "#5");
149                         }
150                 }
151
152                 [Test] // ctor (String)
153                 public void Constructor3_FileName_Empty ()
154                 {
155                         try {
156                                 new ResourceSet (string.Empty);
157                                 Assert.Fail ("#1");
158                         } catch (ArgumentException ex) {
159                                 // Empty path name is not legal
160                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
161                                 Assert.IsNull (ex.InnerException, "#3");
162                                 Assert.IsNotNull (ex.Message, "#4");
163                                 Assert.IsNull (ex.ParamName, "#5");
164                         }
165                 }
166
167                 [Test]
168                 public void Defaults ()
169                 {
170                         DefaultResourceSet rs = new DefaultResourceSet ();
171                         Assert.IsNotNull (rs.GetTable (), "Table");
172                         Assert.IsNull (rs.GetReader (), "Reader");
173                         Assert.AreEqual (typeof (ResourceReader), rs.GetDefaultReader (), "DefaultReaderType");
174                         Assert.AreEqual (typeof (ResourceWriter), rs.GetDefaultWriter (), "DefaultWriterType");
175                         rs.Dispose ();
176                         Assert.IsNull (rs.GetTable (), "Disposed/Table");
177                         Assert.IsNull (rs.GetReader (), "Disposed/Reader");
178                         Assert.AreEqual (typeof (ResourceReader), rs.GetDefaultReader (), "Disposed/DefaultReaderType");
179                         Assert.AreEqual (typeof (ResourceWriter), rs.GetDefaultWriter (), "Disposed/DefaultWriterType");
180                 }
181
182                 [Test]
183                 public void Clonable ()
184                 {
185                         ClonableObject c0 = new ClonableObject ();
186                         Assert.AreEqual (0, c0.Value, "Original");
187                         CloneResourceSet rs = new CloneResourceSet (c0);
188                         ClonableObject c1 = (ClonableObject) rs.GetObject ("clone");
189                         Assert.AreEqual (c1.Value, c0.Value, "Clone");
190                         Assert.IsTrue (Object.ReferenceEquals (c0, c1), "Same");
191                 }
192
193                 [Test]
194                 public void Dispose ()
195                 {
196                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
197                         rs.Dispose ();
198                         rs.Dispose ();
199                 }
200
201                 [Test]
202                 public void GetEnumerator ()
203                 {
204                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
205                         Assert.IsNotNull (rs.GetEnumerator ());
206                 }
207
208                 [Test]
209                 public void GetEnumerator_Disposed ()
210                 {
211                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
212                         rs.Dispose ();
213                         try {
214                                 rs.GetEnumerator ();
215                                 Assert.Fail ("#1");
216 #if NET_2_0
217                         } catch (ObjectDisposedException ex) {
218                                 // Cannot access a closed resource set
219                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
220                                 Assert.IsNull (ex.InnerException, "#3");
221                                 Assert.IsNotNull (ex.Message, "#4");
222                         }
223 #else
224                         } catch (InvalidOperationException ex) {
225                                 // Resource table is closed
226                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
227                                 Assert.IsNull (ex.InnerException, "#3");
228                                 Assert.IsNotNull (ex.Message, "#4");
229                         }
230 #endif
231                 }
232
233                 [Test]
234                 public void GetObject_DoesNotExists ()
235                 {
236                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
237                         Assert.IsNull (rs.GetObject ("doesnotexists"), "default");
238                         Assert.IsNull (rs.GetObject ("doesnotexists", true), "case");
239                         Assert.IsNull (rs.GetObject ("doesnotexists", false), "!case");
240                 }
241
242                 [Test]
243                 public void GetObject_Disposed ()
244                 {
245                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
246                         rs.Dispose ();
247                         try {
248                                 rs.GetObject ("doesnotexists");
249                                 Assert.Fail ("#1");
250 #if NET_2_0
251                         } catch (ObjectDisposedException ex) {
252                                 // Cannot access a closed resource set
253                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
254                                 Assert.IsNull (ex.InnerException, "#3");
255                                 Assert.IsNotNull (ex.Message, "#4");
256                         }
257 #else
258                         } catch (InvalidOperationException ex) {
259                                 // Resource table is closed
260                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
261                                 Assert.IsNull (ex.InnerException, "#3");
262                                 Assert.IsNotNull (ex.Message, "#4");
263                         }
264 #endif
265                 }
266
267                 [Test]
268                 public void GetString_DoesNotExists ()
269                 {
270                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
271                         Assert.IsNull (rs.GetString ("doesnotexists"), "default");
272                         Assert.IsNull (rs.GetString ("doesnotexists", true), "case");
273                         Assert.IsNull (rs.GetString ("doesnotexists", false), "!case");
274                 }
275
276                 [Test]
277                 public void GetString_NotAString ()
278                 {
279                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
280                         try {
281                                 rs.GetString ("clone");
282                                 Assert.Fail ("#1");
283                         } catch (InvalidOperationException ex) {
284                                 // This particular resource was not a String -
285                                 // call GetObject instead.  Resource name: clone
286                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
287                                 Assert.IsNull (ex.InnerException, "#3");
288                                 Assert.IsNotNull (ex.Message, "#4");
289                                 Assert.IsTrue (ex.Message.IndexOf ("String") != -1, "#5");
290                                 Assert.IsTrue (ex.Message.IndexOf ("GetObject") != -1, "#6");
291                                 Assert.IsTrue (ex.Message.IndexOf ("clone") != -1, "#7");
292                         }
293                 }
294
295                 [Test]
296                 public void GetString_Disposed ()
297                 {
298                         CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
299                         rs.Dispose ();
300                         try {
301                                 rs.GetString ("doesnotexists");
302                                 Assert.Fail ("#1");
303 #if NET_2_0
304                         } catch (ObjectDisposedException ex) {
305                                 // Cannot access a closed resource set
306                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
307                                 Assert.IsNull (ex.InnerException, "#3");
308                                 Assert.IsNotNull (ex.Message, "#4");
309                         }
310 #else
311                         } catch (InvalidOperationException ex) {
312                                 // Resource table is closed
313                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
314                                 Assert.IsNull (ex.InnerException, "#3");
315                                 Assert.IsNotNull (ex.Message, "#4");
316                         }
317 #endif
318                 }
319         }
320 }