Merge pull request #1843 from info-lvsys/patch-1
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / AssociationAttributeTest.cs
1 //
2 // AssociationAttributeTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.ComponentModel.Design;
32 using System.Text;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.ComponentModel.DataAnnotations
37 {
38         [TestFixture]
39         public class AssociationAttributeTest
40         {
41                 [Test]
42                 public void Constructor ()
43                 {
44                         AssociationAttribute attr;
45
46                         attr = new AssociationAttribute (null, "key1,key2", "key3,key4");
47                         Assert.AreEqual (null, attr.Name, "#A1-1");
48                         Assert.AreEqual ("key1,key2", attr.ThisKey, "#A1-2");
49                         Assert.AreEqual ("key3,key4", attr.OtherKey, "#A1-3");
50                         Assert.IsNotNull (attr.OtherKeyMembers, "#A2-1");
51
52                         int count = 0;
53                         var list = new List<string> ();
54                         foreach (string m in attr.OtherKeyMembers) {
55                                 count++;
56                                 list.Add (m);
57                         }
58                         Assert.AreEqual (2, count, "#A2-2");
59                         Assert.AreEqual ("key3", list [0], "#A2-3");
60                         Assert.AreEqual ("key4", list [1], "#A2-4");
61
62                         Assert.IsNotNull (attr.ThisKeyMembers, "#A3-1");
63
64                         count = 0;
65                         list = new List<string> ();
66                         foreach (string m in attr.ThisKeyMembers) {
67                                 count++;
68                                 list.Add (m);
69                         }
70                         Assert.AreEqual (2, count, "#A3-2");
71                         Assert.AreEqual ("key1", list [0], "#A3-3");
72                         Assert.AreEqual ("key2", list [1], "#A3-4");
73
74                         attr = new AssociationAttribute ("name", null, "key3,key4");
75                         Assert.AreEqual ("name", attr.Name, "#B1-1");
76                         Assert.AreEqual (null, attr.ThisKey, "#B1-2");
77                         Assert.AreEqual ("key3,key4", attr.OtherKey, "#B1-3");
78                         Assert.IsNotNull (attr.OtherKeyMembers, "#B2-1");
79
80                         count = 0;
81                         list = new List<string> ();
82                         foreach (string m in attr.OtherKeyMembers) {
83                                 count++;
84                                 list.Add (m);
85                         }
86                         Assert.AreEqual (2, count, "#B2-2");
87                         Assert.AreEqual ("key3", list [0], "#B2-3");
88                         Assert.AreEqual ("key4", list [1], "#B2-4");
89
90                         // this is just sad...
91                         try {
92                                 var m = attr.ThisKeyMembers;
93                                 Assert.Fail ("#B2-5");
94                         } catch (NullReferenceException) {
95                                 // success
96                         }
97
98                         attr = new AssociationAttribute ("name", " key1  ,   key 2  ,, ,key    3  ", "       ");
99                         Assert.IsNotNull (attr.ThisKeyMembers, "#C1");
100
101                         count = 0;
102                         list = new List<string> ();
103                         foreach (string m in attr.ThisKeyMembers) {
104                                 count++;
105                                 list.Add (m);
106                         }
107
108                         // It seems all the whitespace is removed from key names
109                         Assert.AreEqual (5, count, "#C2-1");
110                         Assert.AreEqual ("key1", list [0], "#C2-2");
111                         Assert.AreEqual ("key2", list [1], "#C2-3");
112                         Assert.AreEqual (String.Empty, list [2], "#C2-4");
113                         Assert.AreEqual (String.Empty, list [3], "#C2-5");
114                         Assert.AreEqual ("key3", list [4], "#C2-6");
115
116                         Assert.IsNotNull (attr.OtherKeyMembers, "#C3");
117                         count = 0;
118                         list = new List<string> ();
119                         foreach (string m in attr.OtherKeyMembers) {
120                                 count++;
121                                 list.Add (m);
122                         }
123                         Assert.AreEqual (1, count, "#C4-1");
124                         Assert.AreEqual (String.Empty, list [0], "#C4-2");
125                 }
126         }
127 }