File: XmlDataDocumentTest2.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (309 lines) | stat: -rw-r--r-- 9,411 bytes parent folder | download | duplicates (2)
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
//
// XmlDataDocumentTest2.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// 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.
//


using System;
using System.Data;
using System.IO;
using System.Xml;
using NUnit.Framework;

namespace MonoTests.System.Xml
{
	[TestFixture]
	public class XmlDataDocumentTest2 : Assertion
	{
		string xml = "<NewDataSet><table><row><col1>1</col1><col2>2</col2></row></table></NewDataSet>";

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void TestCtorNullArgs ()
		{
			new XmlDataDocument (null);
		}

		[Test]
		public void TestDefaultCtor ()
		{
			XmlDataDocument doc = new XmlDataDocument ();
			AssertNotNull (doc.DataSet);
			AssertEquals ("NewDataSet", doc.DataSet.DataSetName);
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestMultipleLoadError ()
		{
			DataSet ds = new DataSet ();
			ds.ReadXml (new XmlTextReader (xml, XmlNodeType.Document, null));
			// If there is already data element, Load() fails.
			XmlDataDocument doc = new XmlDataDocument (ds);
			doc.LoadXml (xml);
		}

		[Test]
		public void TestMultipleLoadNoError ()
		{
			DataSet ds = new DataSet ();
			DataTable dt = new DataTable ();
			dt.Columns.Add ("col1");
			ds.Tables.Add (dt);

			XmlDataDocument doc = new XmlDataDocument (ds);
			doc.LoadXml (xml);
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void TestMultipleDataDocFromDataSet ()
		{
			DataSet ds = new DataSet ();
			XmlDataDocument doc = new XmlDataDocument (ds);
			XmlDataDocument doc2 = new XmlDataDocument (ds);
		}

		[Test]
		public void TestLoadXml ()
		{
			XmlDataDocument doc = new XmlDataDocument ();
			doc.LoadXml ("<NewDataSet><TestTable><TestRow><TestColumn>1</TestColumn></TestRow></TestTable></NewDataSet>");

			doc = new XmlDataDocument ();
			doc.LoadXml ("<test>value</test>");
		}

		[Test]
		public void TestCreateElementAndRow ()
		{
			DataSet ds = new DataSet ("set");
			DataTable dt = new DataTable ("tab1");
			dt.Columns.Add ("col1");
			dt.Columns.Add ("col2");
			ds.Tables.Add (dt);
			DataTable dt2 = new DataTable ("child");
			dt2.Columns.Add ("ref");
			dt2.Columns.Add ("val");
			ds.Tables.Add (dt2);
			DataRelation rel = new DataRelation ("rel",
				dt.Columns [0], dt2.Columns [0]);
			rel.Nested = true;
			ds.Relations.Add (rel);
			XmlDataDocument doc = new XmlDataDocument (ds);
			doc.LoadXml ("<set><tab1><col1>1</col1><col2/><child><ref>1</ref><val>aaa</val></child></tab1></set>");
			AssertEquals (1, ds.Tables [0].Rows.Count);
			AssertEquals (1, ds.Tables [1].Rows.Count);

			// document element - no mapped row
			XmlElement el = doc.DocumentElement;
			AssertNull (doc.GetRowFromElement (el));

			// tab1 element - has mapped row
			el = el.FirstChild as XmlElement;
			DataRow row = doc.GetRowFromElement (el);
			AssertNotNull (row);
			AssertEquals (DataRowState.Added, row.RowState);

			// col1 - it is column. no mapped row
			el = el.FirstChild as XmlElement;
			row = doc.GetRowFromElement (el);
			AssertNull (row);

			// col2 - it is column. np mapped row
			el = el.NextSibling as XmlElement;
			row = doc.GetRowFromElement (el);
			AssertNull (row);

			// child - has mapped row
			el = el.NextSibling as XmlElement;
			row = doc.GetRowFromElement (el);
			AssertNotNull (row);
			AssertEquals (DataRowState.Added, row.RowState);

			// created (detached) table 1 element (used later)
			el = doc.CreateElement ("tab1");
			row = doc.GetRowFromElement (el);
			AssertEquals (DataRowState.Detached, row.RowState);
			AssertEquals (1, dt.Rows.Count); // not added yet

			// adding a node before setting EnforceConstraints
			// raises an error
			try {
				doc.DocumentElement.AppendChild (el);
				Fail ("Invalid Operation should occur; EnforceConstraints prevents addition.");
			} catch (InvalidOperationException) {
			}

			// try again...
			ds.EnforceConstraints = false;
			AssertEquals (1, dt.Rows.Count); // not added yet
			doc.DocumentElement.AppendChild (el);
			AssertEquals (2, dt.Rows.Count); // added
			row = doc.GetRowFromElement (el);
			AssertEquals (DataRowState.Added, row.RowState); // changed

			// Irrelevant element
			XmlElement el2 = doc.CreateElement ("hoge");
			row = doc.GetRowFromElement (el2);
			AssertNull (row);

			// created table 2 element (used later)
			el = doc.CreateElement ("child");
			row = doc.GetRowFromElement (el);
			AssertEquals (DataRowState.Detached, row.RowState);

			// Adding it to irrelevant element performs no row state change.
			AssertEquals (1, dt2.Rows.Count); // not added yet
			el2.AppendChild (el);
			AssertEquals (1, dt2.Rows.Count); // still not added
			row = doc.GetRowFromElement (el);
			AssertEquals (DataRowState.Detached, row.RowState); // still detached here
		}

		// bug #54505
		public void TypedDataDocument ()
		{
			string xml = @"<top xmlns=""urn:test"">
  <foo>
    <s>first</s>
    <d>2004-02-14T10:37:03</d>
  </foo>
  <foo>
    <s>second</s>
    <d>2004-02-17T12:41:49</d>
  </foo>
</top>";
			string xmlschema = @"<xs:schema id=""webstore"" targetNamespace=""urn:test"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
  <xs:element name=""top"">
    <xs:complexType>
      <xs:sequence maxOccurs=""unbounded"">
        <xs:element name=""foo"">
          <xs:complexType>
            <xs:sequence maxOccurs=""unbounded"">
              <xs:element name=""s"" type=""xs:string""/>
              <xs:element name=""d"" type=""xs:dateTime""/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>";
			XmlDataDocument doc = new XmlDataDocument ();
			doc.DataSet.ReadXmlSchema (new StringReader (xmlschema));
			doc.LoadXml (xml);
			DataTable foo = doc.DataSet.Tables ["foo"];
			DataRow newRow = foo.NewRow ();
			newRow ["s"] = "new";
			newRow ["d"] = DateTime.Now;
			foo.Rows.Add (newRow);
			doc.Save (new StringWriter ());
		}

		[Test]
		public void DataSet_AddRowAfterInitingXmlDataDocument ()
		{
			DataSet ds = new DataSet ();
			DataTable table = ds.Tables.Add ("table1");
			table.Columns.Add ("col1", typeof (int));

			XmlDataDocument doc = new XmlDataDocument (ds);
			table.Rows.Add (new object[] {1});

			StringWriter sw = new StringWriter ();
			doc.Save (sw);

			DataSet ds1 = ds.Clone ();
			XmlDataDocument doc1 = new XmlDataDocument (ds1);
			StringReader sr = new StringReader (sw.ToString());
			doc1.Load (sr);

			AssertEquals ("#1", 1, ds1.Tables [0].Rows.Count);
			AssertEquals ("#2", 1, ds1.Tables [0].Rows [0][0]);
		}

		[Test]
		public void Rows_With_Null_Values ()
		{
			DataSet ds = new DataSet ();
			DataTable table = ds.Tables.Add ("table1");
			table.Columns.Add ("col1", typeof (int));
			table.Columns.Add ("col2", typeof (int));

			XmlDataDocument doc = new XmlDataDocument (ds);
			table.Rows.Add (new object[] {1});

			StringWriter sw = new StringWriter ();
			doc.Save (sw);

			DataSet ds1 = ds.Clone ();
			XmlDataDocument doc1 = new XmlDataDocument (ds1);
			StringReader sr = new StringReader (sw.ToString());
			doc1.Load (sr);

			AssertEquals ("#1", 1, ds1.Tables [0].Rows [0][0]);
			AssertEquals ("#2", true, ds1.Tables [0].Rows [0].IsNull (1));
		}

		[Test]
		public void DataSet_ColumnNameWithSpaces ()
		{
			DataSet ds = new DataSet ("New Data Set");

			DataTable table1 = ds.Tables.Add ("New Table 1");
			DataTable table2 = ds.Tables.Add ("New Table 2");

			table1.Columns.Add ("col 1" , typeof (int));
			table1.Columns.Add ("col 2" , typeof (int));

			table1.PrimaryKey = new DataColumn[] {ds.Tables [0].Columns [0]};

			// No exception shud be thrown
			XmlDataDocument doc = new XmlDataDocument (ds);

			// Should not fail to save because of "no rows"
			doc.Save (new StringWriter ());

			table1.Rows.Add (new object[] {0});
			table1.Rows.Add (new object[] {1});
			table1.Rows.Add (new object[] {2});

			// No exception shud be thrown
			StringWriter swriter = new StringWriter ();
			doc.Save (swriter);

			StringReader sreader = new StringReader (swriter.ToString ());
			DataSet ds1 = ds.Clone ();
			XmlDataDocument doc1 = new XmlDataDocument (ds1);
			AssertEquals ("#2" , 0, ds1.Tables [0].Rows.Count);
			doc1.Load (sreader);
			AssertEquals ("#3" , 3, ds1.Tables [0].Rows.Count);
		}
	}
}