2 // ResourceSetTest.cs: NUnit Test Cases for System.Resources.ResourceSet
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using System.Resources;
34 using NUnit.Framework;
36 namespace MonoTests.System.Resources {
38 public class DefaultResourceSet : ResourceSet {
40 public DefaultResourceSet ()
44 public Hashtable GetTable ()
49 public IResourceReader GetReader ()
55 public class ClonableObject : ICloneable {
59 private ClonableObject (int value)
64 public ClonableObject ()
73 public object Clone ()
75 object clone = new ClonableObject (n);
77 return (ClonableObject) clone;
81 public class CloneResourceSet : ResourceSet {
83 public CloneResourceSet (ClonableObject c)
85 Table.Add ("clone", c);
90 public class ResourceSetTest {
92 [Test] // ctor (IResourceReader)
93 public void Constructor1_Reader_Null ()
96 new ResourceSet ((IResourceReader) null);
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");
106 [Test] // ctor (Stream)
107 public void Constructor2_Stream_Closed ()
109 MemoryStream ms = new MemoryStream ();
113 new ResourceSet (ms);
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");
124 [Test] // ctor (Stream)
125 public void Constructor2_Stream_Null ()
128 new ResourceSet ((Stream) null);
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");
138 [Test] // ctor (String)
139 public void Constructor3_FileName_Null ()
142 new ResourceSet ((string) null);
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");
152 [Test] // ctor (String)
153 public void Constructor3_FileName_Empty ()
156 new ResourceSet (string.Empty);
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");
168 public void Defaults ()
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");
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");
183 public void Clonable ()
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");
194 public void Dispose ()
196 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
202 public void GetEnumerator ()
204 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
205 Assert.IsNotNull (rs.GetEnumerator ());
209 public void GetEnumerator_Disposed ()
211 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
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");
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");
234 public void GetObject_DoesNotExists ()
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");
243 public void GetObject_Disposed ()
245 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
248 rs.GetObject ("doesnotexists");
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");
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");
268 public void GetString_DoesNotExists ()
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");
277 public void GetString_NotAString ()
279 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
281 rs.GetString ("clone");
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");
296 public void GetString_Disposed ()
298 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
301 rs.GetString ("doesnotexists");
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");
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");