1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
//
// ResXFileRefTest.cs: Unit Tests for ResXFileRef.
//
// Authors:
// Andreia Gaita (avidigal@novell.com)
// Gary Barnett (gary.barnett.mono@gmail.com)
using System;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.Resources;
using System.Runtime.Serialization;
using System.Collections;
using NUnit.Framework;
using System.ComponentModel.Design;
using System.Runtime.Serialization.Formatters.Binary;
namespace MonoTests.System.Resources {
[TestFixture]
public class ResXDataNodeTest : ResourcesTestHelper
{
string _tempDirectory;
string _otherTempDirectory;
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorEx1 ()
{
ResXDataNode d = new ResXDataNode (null, (object)null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorEx2A ()
{
ResXDataNode d = new ResXDataNode (null, new ResXFileRef ("filename", "typename"));
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorEx2B ()
{
ResXDataNode d = new ResXDataNode ("aname", (ResXFileRef) null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorEx3 ()
{
ResXDataNode d = new ResXDataNode ("", (object) null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorEx4 ()
{
ResXDataNode d = new ResXDataNode ("", (ResXFileRef) null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorEx5 ()
{
ResXDataNode d = new ResXDataNode ("", new ResXFileRef ("filename", "typename"));
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorEx6 ()
{
ResXDataNode d = new ResXDataNode ("name", new notserializable ());
}
[Test]
public void Name ()
{
ResXDataNode node = new ResXDataNode ("startname", (object) null);
Assert.AreEqual ("startname", node.Name, "#A1");
node.Name = "newname";
Assert.AreEqual ("newname", node.Name, "#A2");
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void NameCantBeNull ()
{
ResXDataNode node = new ResXDataNode ("startname", (object) null);
node.Name = null;
}
[Test, ExpectedException (typeof (ArgumentException))]
public void NameCantBeEmpty ()
{
ResXDataNode node = new ResXDataNode ("name", (object) null);
node.Name = "";
}
[Test]
public void FileRef ()
{
ResXFileRef fileRef = new ResXFileRef ("fileName", "Type.Name");
ResXDataNode node = new ResXDataNode ("name", fileRef);
Assert.AreEqual (fileRef, node.FileRef, "#A1");
}
[Test]
public void Comment ()
{
ResXDataNode node = new ResXDataNode ("name", (object) null);
node.Comment = "acomment";
Assert.AreEqual ("acomment", node.Comment, "#A1");
}
[Test]
public void CommentNullToStringEmpty ()
{
ResXDataNode node = new ResXDataNode ("name", (object) null);
node.Comment = null;
Assert.AreEqual (String.Empty, node.Comment, "#A1");
}
[Test]
public void WriteRead1 ()
{
serializable ser = new serializable ("aaaaa", "bbbbb");
ResXDataNode dn = new ResXDataNode ("test", ser);
dn.Comment = "comment";
string resXFile = GetResXFileWithNode (dn, "resx.resx");
bool found = false;
ResXResourceReader rr = new ResXResourceReader (resXFile);
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
while (en.MoveNext ()) {
ResXDataNode node = ((DictionaryEntry)en.Current).Value as ResXDataNode;
if (node == null)
break;
serializable o = node.GetValue ((AssemblyName []) null) as serializable;
if (o != null) {
found = true;
Assert.AreEqual (ser, o, "#A1");
Assert.AreEqual ("comment", node.Comment, "#A3");
}
}
rr.Close ();
Assert.IsTrue (found, "#A2 - Serialized object not found on resx");
}
[Test]
public void ConstructorResXFileRef()
{
ResXDataNode node = GetNodeFileRefToIcon ();
Assert.IsNotNull (node.FileRef, "#A1");
Assert.AreEqual (typeof (Icon).AssemblyQualifiedName, node.FileRef.TypeName, "#A2");
Assert.AreEqual ("test", node.Name, "#A3");
}
[Test]
public void NullObjectGetValueTypeNameIsNull ()
{
ResXDataNode node = new ResXDataNode ("aname", (object) null);
Assert.IsNull (node.GetValueTypeName ((AssemblyName []) null), "#A1");
}
[Test]
public void NullObjectWrittenToResXOK ()
{
ResXDataNode node = new ResXDataNode ("aname", (object) null);
ResXDataNode returnedNode = GetNodeFromResXReader (node);
Assert.IsNotNull (returnedNode, "#A1");
Assert.IsNull (returnedNode.GetValue ((AssemblyName []) null), "#A2");
}
[Test]
public void NullObjectReturnedFromResXGetValueTypeNameReturnsObject ()
{
ResXDataNode node = new ResXDataNode ("aname", (object) null);
ResXDataNode returnedNode = GetNodeFromResXReader (node);
Assert.IsNotNull (returnedNode, "#A1");
Assert.IsNull (returnedNode.GetValue ((AssemblyName []) null), "#A2");
string type = returnedNode.GetValueTypeName ((AssemblyName []) null);
Assert.AreEqual (typeof (object).AssemblyQualifiedName, type, "#A3");
}
[Test]
public void DoesNotRequireResXFileToBeOpen_Serializable ()
{
serializable ser = new serializable ("aaaaa", "bbbbb");
ResXDataNode dn = new ResXDataNode ("test", ser);
string resXFile = GetResXFileWithNode (dn,"resx.resx");
ResXResourceReader rr = new ResXResourceReader (resXFile);
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
rr.Close ();
Assert.IsNotNull (node,"#A1");
serializable o = node.GetValue ((AssemblyName []) null) as serializable;
Assert.IsNotNull (o, "#A2");
}
[Test]
public void DoesNotRequireResXFileToBeOpen_TypeConverter ()
{
ResXDataNode dn = new ResXDataNode ("test", 34L);
string resXFile = GetResXFileWithNode (dn,"resx.resx");
ResXResourceReader rr = new ResXResourceReader (resXFile);
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
rr.Close ();
Assert.IsNotNull (node, "#A1");
object o = node.GetValue ((AssemblyName []) null);
Assert.IsInstanceOfType (typeof (long), o, "#A2");
Assert.AreEqual (34L, o, "#A3");
}
[Test,ExpectedException (typeof(TypeLoadException))]
public void AssemblyNamesPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter ()
{
string aName = "DummyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
AssemblyName [] assemblyNames = new AssemblyName [] { new AssemblyName (aName) };
string resXFile = GetFileFromString ("test.resx", convertableResXWithoutAssemblyName);
using (ResXResourceReader rr = new ResXResourceReader (resXFile, assemblyNames)) {
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
Assert.IsNotNull (node, "#A1");
//should raise exception
object o = node.GetValue ((AssemblyName []) null);
}
}
[Test]
public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_TypeConverter ()
{
ResXDataNode dn = new ResXDataNode ("test", 34L);
string resXFile = GetResXFileWithNode (dn,"resx.resx");
ResXResourceReader rr = new ResXResourceReader (resXFile, new ReturnIntITRS ());
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
Assert.IsNotNull (node, "#A1");
object o = node.GetValue ((AssemblyName []) null);
Assert.IsInstanceOfType (typeof (long), o, "#A2");
Assert.AreEqual (34L, o, "#A3");
rr.Close ();
}
[Test]
public void ITRSPassedToResourceReaderDoesNotAffectResXDataNode_Serializable ()
{
serializable ser = new serializable ("aaaaa", "bbbbb");
ResXDataNode dn = new ResXDataNode ("test", ser);
string resXFile = GetResXFileWithNode (dn,"resx.resx");
ResXResourceReader rr = new ResXResourceReader (resXFile, new ReturnSerializableSubClassITRS ());
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode node = ((DictionaryEntry) en.Current).Value as ResXDataNode;
Assert.IsNotNull (node, "#A1");
object o = node.GetValue ((AssemblyName []) null);
Assert.IsNotInstanceOfType (typeof (serializableSubClass), o, "#A2");
Assert.IsInstanceOfType (typeof (serializable), o, "#A3");
rr.Close ();
}
[Test]
public void BasePathSetOnResXResourceReaderDoesAffectResXDataNode ()
{
ResXFileRef fileRef = new ResXFileRef ("file.name", "type.name");
ResXDataNode node = new ResXDataNode("anode", fileRef);
string resXFile = GetResXFileWithNode (node, "afilename.xxx");
using (ResXResourceReader rr = new ResXResourceReader (resXFile)) {
rr.BasePath = "basePath";
rr.UseResXDataNodes = true;
IDictionaryEnumerator en = rr.GetEnumerator ();
en.MoveNext ();
ResXDataNode returnedNode = ((DictionaryEntry) en.Current).Value as ResXDataNode;
Assert.IsNotNull (node, "#A1");
Assert.AreEqual (Path.Combine ("basePath", "file.name"), returnedNode.FileRef.FileName, "#A2");
}
}
[TearDown]
protected override void TearDown ()
{
//teardown
if (Directory.Exists (_tempDirectory))
Directory.Delete (_tempDirectory, true);
base.TearDown ();
}
string GetResXFileWithNode (ResXDataNode node, string filename)
{
string fullfileName;
_tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest");
_otherTempDirectory = Path.Combine (_tempDirectory, "in");
if (!Directory.Exists (_otherTempDirectory)) {
Directory.CreateDirectory (_otherTempDirectory);
}
fullfileName = Path.Combine (_tempDirectory, filename);
using (ResXResourceWriter writer = new ResXResourceWriter (fullfileName)) {
writer.AddResource (node);
}
return fullfileName;
}
private string GetFileFromString (string filename, string filecontents)
{
_tempDirectory = Path.Combine (Path.GetTempPath (), "ResXDataNodeTest");
_otherTempDirectory = Path.Combine (_tempDirectory, "in");
if (!Directory.Exists (_otherTempDirectory)) {
Directory.CreateDirectory (_otherTempDirectory);
}
string filepath = Path.Combine (_tempDirectory, filename);
StreamWriter writer = new StreamWriter(filepath,false);
writer.Write (filecontents);
writer.Close ();
return filepath;
}
static string convertableResXWithoutAssemblyName =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<root>
<resheader name=""resmimetype"">
<value>text/microsoft-resx</value>
</resheader>
<resheader name=""version"">
<value>2.0</value>
</resheader>
<resheader name=""reader"">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name=""writer"">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name=""test"" type=""DummyAssembly.Convertable"">
<value>im a name im a value</value>
</data>
</root>";
}
}
|