PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/BurnSystems/tests/BurnSystems.UnitTests/Collections/XmlListTests.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 415 lines | 326 code | 77 blank | 12 comment | 10 complexity | f55985a3346f4afcaa0bfae448c135fb MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using BurnSystems.Interfaces;
  6using System.Xml.Linq;
  7using System.Globalization;
  8using NUnit.Framework;
  9using BurnSystems.Collections;
 10
 11namespace BurnSystems.UnitTests.Collections
 12{
 13    /// <summary>
 14    /// Stores all tests that have been created for the xmllist
 15    /// </summary>
 16    [TestFixture]
 17    public class XmlListTests
 18    {
 19        /// <summary>
 20        /// Stores the testentity
 21        /// </summary>
 22        public class Entity
 23        {
 24            public string Name
 25            {
 26                get;
 27                set;
 28            }
 29
 30            public string Prename
 31            {
 32                get;
 33                set;
 34            }
 35
 36            public int Age
 37            {
 38                get;
 39                set;
 40            }
 41
 42            public override bool Equals(object obj)
 43            {
 44                var entity = obj as Entity;
 45                if (entity == null)
 46                {
 47                    return false;
 48                }
 49
 50                return entity.Name == this.Name
 51                    && entity.Prename == this.Prename
 52                    && entity.Age == this.Age;
 53            }
 54
 55            public override int GetHashCode()
 56            {
 57                return
 58                    this.Name.GetHashCode() ^
 59                    this.Prename.GetHashCode() ^
 60                    this.Age.GetHashCode();
 61            }
 62        }
 63
 64        public class EntityConverter : IXElementConverter<Entity>
 65        {
 66            public Entity Convert(System.Xml.Linq.XElement element)
 67            {
 68                var entity = new Entity()
 69                {
 70                    Name = element.Element("name").Value,
 71                    Prename = element.Element("prename").Value,
 72                    Age = System.Convert.ToInt32(element.Element("age").Value, CultureInfo.InvariantCulture)
 73                };
 74
 75                return entity;
 76            }
 77
 78            public System.Xml.Linq.XElement Convert(Entity entity)
 79            {
 80                return
 81                    new XElement(
 82                        "entity",
 83                        new XElement("name", entity.Name),
 84                        new XElement("prename", entity.Prename),
 85                        new XElement("age", entity.Age.ToString(CultureInfo.InvariantCulture)));
 86            }
 87        }
 88
 89        public static XmlList<Entity> EmptyRootElement
 90        {
 91            get
 92            {
 93                var document = new XDocument(
 94                    new XElement("elements"));
 95
 96                return new XmlList<Entity>(
 97                    document.Element("elements"),
 98                    new EntityConverter());
 99            }
100        }
101
102        public static XElement AttributeListDocument
103        {
104            get
105            {
106                var document = new XDocument(
107                    new XElement("elements",
108                        new XElement(
109                            "item",
110                            new XAttribute("value", 12)),
111                        new XElement(
112                            "item",
113                            new XAttribute("value", 25)),
114                        new XElement(
115                            "item",
116                            new XAttribute("value", 68))));
117
118                return document.Element("elements");
119            }
120        }
121
122        public static XElement ElementListDocument
123        {
124            get
125            {
126                var document = new XDocument(
127                    new XElement("elements",
128                        new XElement(
129                            "item",
130                            12),
131                        new XElement(
132                            "item",
133                            25),
134                        new XElement(
135                            "item",
136                            68)));
137
138                return document.Element("elements");
139            }
140        }
141
142        public Entity Person1
143        {
144            get
145            {
146                return new Entity()
147                {
148                    Name = "John",
149                    Prename = "Miller",
150                    Age = 23
151                };
152            }
153        }
154
155        public Entity Person2
156        {
157            get
158            {
159                return new Entity()
160                {
161                    Name = "Mike",
162                    Prename = "Wayne",
163                    Age = 25
164                };
165            }
166        }
167
168        public Entity Person3
169        {
170            get
171            {
172                return new Entity()
173                {
174                    Name = "Barack",
175                    Prename = "Bush",
176                    Age = 50
177                };
178            }
179        }
180
181        [Test]
182        public void CreateEmptyList()
183        {
184            var list = EmptyRootElement;
185
186            Assert.That(list.Count, Is.EqualTo(0));
187        }
188
189        [Test]
190        public void AddAndRetrieveItems()
191        {
192            var list = EmptyRootElement;
193
194            list.Add(this.Person1);
195            list.Add(this.Person2);
196
197            Assert.That(list.Count, Is.EqualTo(2));
198            Assert.That(list[0], Is.EqualTo(Person1));
199            Assert.That(list[1], Is.EqualTo(Person2));
200
201            list.Add(this.Person3);
202
203            Assert.That(list.Count, Is.EqualTo(3));
204        }
205
206        [Test]
207        public void AddAndRemoveItems()
208        {
209            var list = EmptyRootElement;
210
211            list.Add(this.Person1);
212            list.Add(this.Person2);
213            list.Add(this.Person3);
214
215            Assert.That(list.Count, Is.EqualTo(3));
216
217            list.RemoveAt(1);
218
219            Assert.That(list.Count, Is.EqualTo(2));
220            Assert.That(list[0], Is.EqualTo(Person1));
221            Assert.That(list[1], Is.EqualTo(Person3));
222        }
223
224        [Test]
225        public void InsertItems()
226        {
227            var list = EmptyRootElement;
228
229            list.Add(this.Person1);
230            list.Add(this.Person3);
231            list.Insert(1, this.Person2);
232
233            Assert.That(list.Count, Is.EqualTo(3));
234            Assert.That(list[0], Is.EqualTo(Person1));
235            Assert.That(list[1], Is.EqualTo(Person2));
236            Assert.That(list[2], Is.EqualTo(Person3));
237        }
238
239        [Test]
240        public void Remove()
241        {
242            var list = EmptyRootElement;
243
244            list.Add(this.Person1);
245            list.Add(this.Person2);
246            list.Add(this.Person3);
247
248            Assert.That(list.Remove(this.Person2), Is.True);
249            Assert.That(list.Count, Is.EqualTo(2));
250            Assert.That(list[0], Is.EqualTo(Person1));
251            Assert.That(list[1], Is.EqualTo(Person3));
252
253            list.Remove(this.Person1);
254            list.Remove(this.Person3);
255
256            Assert.That(list.Count, Is.EqualTo(0));
257
258            Assert.That(list.Remove(this.Person2), Is.False);
259        }
260
261        [Test]
262        public void IndexOf()
263        {
264            var list = EmptyRootElement;
265
266            list.Add(this.Person1);
267            list.Add(this.Person2);
268            list.Add(this.Person3);
269
270            Assert.That(list.Count, Is.EqualTo(3));
271            Assert.That(list.IndexOf(this.Person1), Is.EqualTo(0));
272            Assert.That(list.IndexOf(this.Person2), Is.EqualTo(1));
273            Assert.That(list.IndexOf(this.Person3), Is.EqualTo(2));
274
275            list.Remove(this.Person2);
276            Assert.That(list.IndexOf(this.Person2), Is.EqualTo(-1));
277        }
278
279        [Test]
280        public void Setter()
281        {
282            var list = EmptyRootElement;
283
284            list.Add(this.Person1);
285            list.Add(this.Person2);
286
287            Assert.That(list.Count, Is.EqualTo(2));
288            Assert.That(list.IndexOf(this.Person1), Is.EqualTo(0));
289            Assert.That(list.IndexOf(this.Person2), Is.EqualTo(1));
290
291            list[1] = this.Person3;
292
293            Assert.That(list.Count, Is.EqualTo(2));
294            Assert.That(list[1], Is.EqualTo(Person3));
295        }
296
297        [Test]
298        public void Clear()
299        {
300            var list = EmptyRootElement;
301
302            list.Add(this.Person1);
303            list.Add(this.Person2);
304
305            Assert.That(list.Count, Is.EqualTo(2));
306
307            list.Clear();
308
309            Assert.That(list.Count, Is.EqualTo(0));
310            Assert.That(list.IndexOf(this.Person2), Is.EqualTo(-1));
311        }
312
313        [Test]
314        public void IsReadOnly()
315        {
316            var list = EmptyRootElement;
317
318            Assert.That(list.IsReadOnly, Is.False);
319        }
320
321        [Test]
322        public void Enumerator()
323        {
324            var list = EmptyRootElement;
325
326            list.Add(this.Person1);
327            list.Add(this.Person2);
328
329            var count = 0;
330            foreach (var element in list)
331            {
332                count++;
333
334                Assert.That(element.Equals(this.Person1) || element.Equals(this.Person2), Is.True);
335                Assert.That(element.Equals(this.Person3), Is.False);
336            }
337
338            Assert.That(count, Is.EqualTo(2));
339        }
340
341        [Test]
342        public void LinqTests()
343        {
344            var list = EmptyRootElement;
345
346            list.Add(this.Person1);
347            list.Add(this.Person2);
348            list.Add(this.Person3);
349
350            Assert.That(list.Any(x => x.Age == 25), Is.True);
351            Assert.That(list.Any(x => x.Age == 99), Is.False);
352
353            Assert.That(list.All(x => x.Age > 10), Is.True);
354
355            Assert.That(list.Select(x => x.Name + " " + x.Prename).Count(), Is.EqualTo(3));
356        }
357
358        [Test]
359        public void CopyToTests()
360        {
361            var list = EmptyRootElement;
362
363            list.Add(this.Person1);
364            list.Add(this.Person2);
365            list.Add(this.Person3);
366
367            var array = new Entity[10];
368
369            list.CopyTo(array, 1);
370
371            Assert.That(array[1], Is.EqualTo(this.Person1));
372            Assert.That(array[2], Is.EqualTo(this.Person2));
373            Assert.That(array[3], Is.EqualTo(this.Person3));
374
375            list.CopyTo(array, 3);
376            Assert.That(array[3], Is.EqualTo(this.Person1));
377            Assert.That(array[4], Is.EqualTo(this.Person2));
378            Assert.That(array[5], Is.EqualTo(this.Person3));
379        }
380
381        /// <summary>
382        /// Tests whether the list for attributes is working
383        /// </summary>
384        [Test]
385        public void TestListForAttributes()
386        {
387            var element = AttributeListDocument;
388            var values = XmlList<int>.GetListForAttributes(element, "item", "value");
389
390            Assert.That(values.Sum(), Is.EqualTo(105));
391            Assert.That(values.Count, Is.EqualTo(3));
392
393            values.Add(10);
394            Assert.That(values.Sum(), Is.EqualTo(115));
395            Assert.That(values.Count, Is.EqualTo(4));
396        }
397
398        /// <summary>
399        /// Tests whether the list for attributes is working
400        /// </summary>
401        [Test]
402        public void TestListForElements()
403        {
404            var element = ElementListDocument;
405            var values = XmlList<int>.GetListForElements(element, "item");
406
407            Assert.That(values.Sum(), Is.EqualTo(105));
408            Assert.That(values.Count, Is.EqualTo(3));
409
410            values.Add(10);
411            Assert.That(values.Sum(), Is.EqualTo(115));
412            Assert.That(values.Count, Is.EqualTo(4));
413        }
414    }
415}