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 ());
216 } catch (ObjectDisposedException ex) {
217 // Cannot access a closed resource set
218 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
219 Assert.IsNull (ex.InnerException, "#3");
220 Assert.IsNotNull (ex.Message, "#4");
225 public void GetObject_DoesNotExists ()
227 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
228 Assert.IsNull (rs.GetObject ("doesnotexists"), "default");
229 Assert.IsNull (rs.GetObject ("doesnotexists", true), "case");
230 Assert.IsNull (rs.GetObject ("doesnotexists", false), "!case");
234 public void GetObject_Disposed ()
236 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
239 rs.GetObject ("doesnotexists");
241 } catch (ObjectDisposedException ex) {
242 // Cannot access a closed resource set
243 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
244 Assert.IsNull (ex.InnerException, "#3");
245 Assert.IsNotNull (ex.Message, "#4");
250 public void GetString_DoesNotExists ()
252 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
253 Assert.IsNull (rs.GetString ("doesnotexists"), "default");
254 Assert.IsNull (rs.GetString ("doesnotexists", true), "case");
255 Assert.IsNull (rs.GetString ("doesnotexists", false), "!case");
259 public void GetString_NotAString ()
261 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
263 rs.GetString ("clone");
265 } catch (InvalidOperationException ex) {
266 // This particular resource was not a String -
267 // call GetObject instead. Resource name: clone
268 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
269 Assert.IsNull (ex.InnerException, "#3");
270 Assert.IsNotNull (ex.Message, "#4");
271 Assert.IsTrue (ex.Message.IndexOf ("String") != -1, "#5");
272 Assert.IsTrue (ex.Message.IndexOf ("GetObject") != -1, "#6");
273 Assert.IsTrue (ex.Message.IndexOf ("clone") != -1, "#7");
278 public void GetString_Disposed ()
280 CloneResourceSet rs = new CloneResourceSet (new ClonableObject ());
283 rs.GetString ("doesnotexists");
285 } catch (ObjectDisposedException ex) {
286 // Cannot access a closed resource set
287 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
288 Assert.IsNull (ex.InnerException, "#3");
289 Assert.IsNotNull (ex.Message, "#4");