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 |
#region license
// Copyright (c) 2005 - 2007 Ayende Rahien (ayende@ayende.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Ayende Rahien nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.IO;
using System.Xml;
using Ayende.NHibernateQueryAnalyzer.SchemaEditing;
using NHibernate.Mapping.Hbm;
using MbUnit.Framework;
using System.Reflection;
namespace Ayende.NHibernateQueryAnalyzer.Tests.SchemaEditing
{
[TestFixture]
public class SchemaEditorTests
{
protected SchemaEditor schemaEditor;
private const string ClassName = "Ayende.NHibernateQueryAnalyzer.Core.Project, Ayende.NHibernateQueryAnalyzer";
#region MappingXml = file(Project.hbm.xml);
private static string MappingXml = new StreamReader(
Assembly.GetExecutingAssembly().
GetManifestResourceStream("Ayende.NHibernateQueryAnalyzer.UnitTests.Mapping.xml"))
.ReadToEnd();
#endregion
[SetUp]
public void SetUp()
{
this.schemaEditor = new SchemaEditor(typeof (hibernatemapping), new SchemaEditorNodeTestFactory());
this.schemaEditor.Read(new XmlTextReader(new StringReader(MappingXml)));
}
[Test]
public void RootElement()
{
Assert.IsNotNull(this.schemaEditor.Root);
Assert.AreEqual("hibernatemapping",this.schemaEditor.Root.GetType().Name);
}
[Test]
public void RootSchema()
{
ISchemaEditorNode node = schemaEditor.RootNode;
Assert.IsNotNull(node, "Null value returned for the root node");
Assert.IsTrue(node.HasValue,"Root node has no value!");
Assert.AreEqual("hibernate-mapping", node.Name, "Bad root node name");
Assert.AreEqual(schemaEditor.Root, schemaEditor.RootNode.Value);
}
[Test]
public void GraphNodesValues()
{
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0];
Assert.AreEqual("class", node.Name, "Can't get the class's name");
Assert.AreEqual(ClassName, node.ActiveAttributes["name"].Value, "Couldn't get the expected property value");
}
[Test]
public void DeepGraphNodeTraversal()
{
// /hibernate-mapping/class/id/generator
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0].ActiveNodes[0];
Assert.AreEqual("generator", node.Name, "Wrong node name");
Assert.AreEqual("class", node.ActiveAttributes[0].Name);
Assert.AreEqual("identity", node.ActiveAttributes[0].Value);
}
[Test]
public void DeepDelete()
{
ISchemaEditorNode @class = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
ISchemaEditorNode id = schemaEditor.CreateChild(@class, typeof(id));
/*ISchemaEditorNode generator = */schemaEditor.CreateChild(id, typeof(generator));
schemaEditor.RemoveChild(@class, id);
Assert.IsNull(@class.ActiveNodes["id"]);
}
[Test]
public void MissingAttributesAreNull()
{
// /hibernate-mapping/class/id
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0];
Assert.IsNull(node.ActiveAttributes["column"]);
}
[Test]
public void CountActiveNodesForMapping()
{
Assert.AreEqual(1,schemaEditor.RootNode.ActiveNodes.Count);
}
[Test]
public void AllAttributesWithNonExistingAttributeReturnNull()
{
// /hibernate-mapping/class/id
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0];
Assert.IsNull(node.Attributes["non-exisitng-attributes"], "AllAttributes returned non null value for non existing attributes");
}
[Test]
public void AllAttributesReturnBothValidAndInvalidAttributes()
{
// /hibernate-mapping/class/id
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0];
Assert.IsNotNull(node.Attributes["name"], "Couldn't find attribute's 'name' which HasValue==true");
Assert.IsNotNull(node.Attributes["column"], "Couldn't find attribute's 'column' which HasValue==false");
}
[Test]
public void ChangingValueToNull()
{
// /hibernate-mapping/class/id/generator
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0].ActiveNodes[0];
AttributeFieldReference classAttribute = (AttributeFieldReference)node.ActiveAttributes[0];
int prevCount = node.ActiveAttributes.Count;
Assert.IsTrue(node.ActiveAttributes[0].HasValue, "Attribute doesn't report that it has a value");
classAttribute.HasValueChanged += new AttributeFieldReference.AttributeFieldReferenceEventHandler(nodeAttributes_EventHanlder_False);
classAttribute.Value = null;
Assert.AreEqual(prevCount - 1, node.ActiveAttributes.Count, "Active attributes count was not decrease when setting attribute to null");
}
private void nodeAttributes_EventHanlder_False(AttributeFieldReference sender, AttributeFieldReferenceEventArgs e)
{
Assert.IsFalse(e.HasValue, "Attribute's has value was not changed");
}
private void nodeAttributes_EventHanlder_True(AttributeFieldReference sender, AttributeFieldReferenceEventArgs e)
{
Assert.IsTrue(e.HasValue, "Attribute's has value was not changed");
}
[Test]
public void SetAttributeValueToNonNull()
{
// /hibernate-mapping/class/id
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0].ActiveNodes[0];
((AttributeFieldReference)node.Attributes["column"]).HasValueChanged += new AttributeFieldReference.AttributeFieldReferenceEventHandler(nodeAttributes_EventHanlder_True);
node.Attributes["column"].Value = "id";
Assert.IsNotNull(node.ActiveAttributes["column"], "Attribute was not placed in the Attributes collection");
}
[Test]
public void ActiveAttributesInAttributes()
{
ISchemaEditorNode node = schemaEditor.RootNode.ActiveNodes[0];
Assert.IsTrue(node.ActiveAttributes.Count>0);
foreach (AttributeFieldReference attribute in node.ActiveAttributes)
CollectionAssert.Contains(schemaEditor.RootNode.ActiveNodes[0].Attributes,attribute);
}
[Test]
public void AddNode()
{
ISchemaEditorNode node = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
hibernatemapping hm = schemaEditor.RootNode.Value as hibernatemapping;
Assert.IsNotNull(node, "Returned a null node");
CollectionAssert.Contains(schemaEditor.RootNode.ActiveNodes,node);
Assert.IsNotNull(node.Value, "Newly created node with a null value");
CollectionAssert.Contains(hm.Items, node.Value);
Assert.AreEqual(typeof (@class), node.Value.GetType());
}
[Test]
public void RemoveNode()
{
ISchemaEditorNode node = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
hibernatemapping hm = schemaEditor.RootNode.Value as hibernatemapping;
CollectionAssert.Contains(schemaEditor.RootNode.ActiveNodes,node);
schemaEditor.RemoveChild(schemaEditor.RootNode, node);
CollectionAssert.DoesNotContain(schemaEditor.RootNode.ActiveNodes,node);
CollectionAssert.DoesNotContain(hm.Items,node.Value);
}
[Test]
[ExpectedException(typeof (InvalidOperationException))]
public void RemoveNodeWhenNotExisting()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(idbag));
schemaEditor.RemoveChild(id,clazz);
}
[Test]
[ExpectedException(typeof (InvalidOperationException))]
public void AddRootNode()
{
schemaEditor.RootNode.FieldReference.AddValue(null);
}
[Test]
[ExpectedException(typeof (InvalidOperationException))]
public void RemoveRootNode()
{
schemaEditor.RootNode.FieldReference.RemoveValue(null);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void AddInvalidNode()
{
schemaEditor.CreateChild(schemaEditor.RootNode, typeof(int));
}
[Test]
public void AddNodeDeep()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id));
@class clazzValue = clazz.Value as @class;
Assert.IsNotNull(id, "Returned a null node");
CollectionAssert.Contains(clazz.ActiveNodes,id);
Assert.IsNotNull(id.Value, "Newly created node with a null value");
Assert.AreSame(id.Value,clazzValue.Item1/*id or composite-id*/,"Didn't set id in class");
Assert.AreEqual(typeof (id), id.Value.GetType());
}
[Test]
public void RemoveNodeDeep()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id));
@class clazzValue = clazz.Value as @class;
schemaEditor.RemoveChild(clazz,id);
CollectionAssert.DoesNotContain(clazz.ActiveNodes,id);
Assert.IsNull(clazzValue.Item/*id or composite-id*/,"Didn't reset id in class");
}
[Test]
public void AddNodeDeepArray()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
oneToOne = schemaEditor.CreateChild(clazz,typeof(onetoone));
@class clazzValue = clazz.Value as @class;
Assert.IsNotNull(oneToOne, "Returned a null node");
CollectionAssert.Contains(clazz.ActiveNodes,oneToOne);
Assert.IsNotNull(oneToOne.Value, "Newly created node with a null value");
CollectionAssert.Contains(clazzValue.Items, oneToOne.Value);
Assert.AreEqual(typeof (onetoone), oneToOne.Value.GetType());
}
[Test]
public void RemoveNodeDeepArray()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
oneToOne = schemaEditor.CreateChild(clazz,typeof(onetoone));
@class clazzValue = clazz.Value as @class;
schemaEditor.RemoveChild(clazz, oneToOne);
CollectionAssert.DoesNotContain(clazz.ActiveNodes,oneToOne);
CollectionAssert.DoesNotContain(clazzValue.Items,oneToOne.Value);
}
/// <summary>
/// Tests the node fields.
/// </summary>
[Test]
public void NodeFields()
{
ISchemaEditorNode node = schemaEditor.RootNode;
Assert.IsNotNull(node.NodeFields[typeof(@class)]);
Assert.IsNotNull(node.NodeFields[typeof(joinedsubclass)]);
}
[Test]
public void AddNodeArraySingleSingleArray()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id)),
generator = schemaEditor.CreateChild(id,typeof(generator)),
param = schemaEditor.CreateChild(generator,typeof(param));
@class clazzValue = clazz.Value as @class;
Assert.AreSame(
((id)clazzValue.Item1). generator. param[0],
clazz.ActiveNodes[0]. ActiveNodes[0]. ActiveNodes[0].Value,
"Can't repreduce object graph on three level copy"
);
Assert.AreSame(param, clazz.ActiveNodes[0]. ActiveNodes[0]. ActiveNodes[0], "return node was not put into the object graph");
}
[Test]
public void AddNodeArrayArraySingle()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
array = schemaEditor.CreateChild(clazz,typeof(array)),
key = schemaEditor.CreateChild(array,typeof(key));
@class clazzValue = clazz.Value as @class;
Assert.AreSame(
((array)clazzValue. Items[0]). key,
clazz.ActiveNodes[0]. ActiveNodes[0]. Value,
"Can't repreduce object graph on three level copy"
);
Assert.AreSame(key, clazz.ActiveNodes[0].ActiveNodes[0], "return node was not put into the object graph");
}
[Test]
public void RequirementsAreObserved()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id));
Assert.IsTrue(id.HasSchemaError,"Didn't find that node cotains schema's error");
Assert.AreEqual(1,id.FieldReference.AmountRequired,"didn't recognize a require tag");
Assert.AreEqual(1, id.FieldReference.AmountExisting,"didn't count propertly");
Assert.AreEqual(0, id.NodeFields[typeof(generator)].AmountExisting,"didn't count propertly");
Assert.AreEqual(1, id.NodeFields[typeof(generator)].AmountRequired,"didn't count propertly");
}
[Test]
public void HasSchemaErrors()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id));
SchemaErrorCollection errors = SchemaEditor.CheckSchemaCompliance(id);
Assert.IsTrue(id.HasSchemaError,"Didn't detect errors");
Assert.AreEqual(1,errors.Count,"Wrong number of errors");
}
[Test]
public void CorrectNodeFields()
{
ISchemaEditorNode @class = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
int count=0;
string names = "";
foreach (NodeFieldReference reference in @class.NodeFields)
{
count+=reference.Names.Length;
foreach (string s in reference.Names)
{
names += s + " - ";
}
}
Assert.AreEqual(28,count,"Didn't get the right count of fields");
}
[Test]
public void HasAttributeSchemaError()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
SchemaErrorCollection errors = SchemaEditor.CheckSchemaCompliance(clazz);
Assert.AreEqual(typeof(AttributeSchemaError),errors[0].GetType());
Assert.AreEqual("Attribute 'name' on element 'class' is required!",errors[0].Message);
}
[Test]
public void HasElementSchemaError()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class));
SchemaErrorCollection errors = SchemaEditor.CheckSchemaCompliance(clazz);
Assert.AreEqual(typeof(ElementSchemaError),errors[1].GetType());
StringAssert.Like(errors[1].Message,
@"Some of the elements \((id|composite-id), (id|composite-id)\) are required to appear on 'class' 1 time\(s\), but appears 0 time\(s\)!");
}
[Test]
public void HadRecursiveSchemaError()
{
ISchemaEditorNode clazz = schemaEditor.CreateChild(schemaEditor.RootNode, typeof(@class)),
id = schemaEditor.CreateChild(clazz,typeof(id));
SchemaErrorCollection errors = SchemaEditor.CheckSchemaCompliance(clazz);
Assert.IsTrue(id.HasSchemaError,"Didn't recognize error");
Assert.IsTrue(clazz.HasSchemaError,"Didn't recognize error");
Assert.AreEqual(2,errors.Count,"Wrong number of errors");
}
[Test]
public void SerializingProduceSameAsInput()
{
string actual = schemaEditor.ToString();
Assert.AreEqual(MappingXml, actual);
}
}
} |