0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.DataAccess;
|
|
7 using BLToolkit.Mapping;
|
|
8 using BLToolkit.Reflection.Extension;
|
|
9
|
|
10 namespace Mapping
|
|
11 {
|
|
12 [TestFixture, Category("Mapping")]
|
|
13 public class MapFieldAttributeTest
|
|
14 {
|
|
15 [MapField("MapName", "Field1")]
|
|
16 public class Object1
|
|
17 {
|
|
18 public int Field1;
|
|
19 [MapField("intfld")]
|
|
20 public int Field2;
|
|
21 }
|
|
22
|
|
23 [Test]
|
|
24 public void Test1()
|
|
25 {
|
|
26 ObjectMapper om = Map.GetObjectMapper(typeof(Object1));
|
|
27
|
|
28 Object1 o = (Object1)om.CreateInstance();
|
|
29
|
|
30 om.SetValue(o, "mapname", 123);
|
|
31 om.SetValue(o, "intfld", 234);
|
|
32
|
|
33 Assert.AreEqual(123, o.Field1);
|
|
34 Assert.AreEqual(234, o.Field2);
|
|
35 }
|
|
36
|
|
37 [MapValue(true, "Y")]
|
|
38 [MapValue(false, "N")]
|
|
39 public class Object2
|
|
40 {
|
|
41 public bool Field1;
|
|
42 public int Field2;
|
|
43 }
|
|
44
|
|
45 public class Object3
|
|
46 {
|
|
47 public Object2 Object2 = new Object2();
|
|
48 public Object4 Object4;
|
|
49 }
|
|
50
|
|
51 [MapField("fld1", "Object3.Object2.Field1")]
|
|
52 [MapField("fld2", "Object3.Object4.Str1")]
|
|
53 public class Object4
|
|
54 {
|
|
55 public Object3 Object3 = new Object3();
|
|
56 public string Str1;
|
|
57 }
|
|
58
|
|
59 [Test]
|
|
60 public void Test2()
|
|
61 {
|
|
62 ObjectMapper om = Map.GetObjectMapper(typeof(Object4));
|
|
63
|
|
64 Object4 o = (Object4)om.CreateInstance();
|
|
65
|
|
66 om.SetValue(o, "fld1", "Y");
|
|
67 om.SetValue(o, "Object3.Object2.Field2", 123);
|
|
68 om.SetValue(o, "fld2", "str");
|
|
69
|
|
70 Assert.AreEqual(true, o.Object3.Object2.Field1);
|
|
71 Assert.AreEqual(123, o.Object3.Object2.Field2);
|
|
72 Assert.IsNull ( o.Object3.Object4);
|
|
73
|
|
74 Assert.AreEqual("Y", om.GetValue(o, "fld1"));
|
|
75 Assert.AreEqual(123, om.GetValue(o, "Object3.Object2.Field2"));
|
|
76 Assert.IsNull ( om.GetValue(o, "fld2"));
|
|
77 }
|
|
78
|
|
79 [Test]
|
|
80 public void Test3()
|
|
81 {
|
|
82 ObjectMapper om = Map.GetObjectMapper(typeof(Object4));
|
|
83 MemberMapper mm = om["Object3.Object2.Field1", true];
|
|
84
|
|
85 Assert.IsNotNull(mm);
|
|
86 }
|
|
87
|
|
88 [MapField("fld2", "Object3.Object4.Str1")]
|
|
89 [TypeExtension(FileName="Map.xml")]
|
|
90 public class Object5
|
|
91 {
|
|
92 public Object3 Object3 = new Object3();
|
|
93 public string Str1;
|
|
94 }
|
|
95
|
|
96 [Test]
|
|
97 public void Test4()
|
|
98 {
|
|
99 ObjectMapper om = Map.GetObjectMapper(typeof(Object5));
|
|
100 MemberMapper mm1 = om["Object3.Object2.Field1", true];
|
|
101 MemberMapper mm2 = om["Object3.Object4.Str1", true];
|
|
102
|
|
103 Assert.IsNotNull(mm1);
|
|
104 Assert.IsNotNull(mm2);
|
|
105 }
|
|
106
|
|
107 public class Entity
|
|
108 {
|
|
109 [PrimaryKey]
|
|
110 public int Id;
|
|
111 }
|
|
112
|
|
113 public class Name
|
|
114 {
|
|
115 public string LastName;
|
|
116 public string FirstName;
|
|
117 public string MiddleName;
|
|
118 }
|
|
119
|
|
120 [MapField("PhoneId", "Id")]
|
|
121 public class PhoneEntity : Entity
|
|
122 {
|
|
123 public string Phone;
|
|
124 }
|
|
125
|
|
126 [MapField("PhoneId", "Phone.Id")]
|
|
127 [MapField("LastName", "Name.LastName")]
|
|
128 [MapField("FirstName", "Name.FirstName")]
|
|
129 [MapField("MiddleName", "Name.MiddleName")]
|
|
130 public class PersonEntity : Entity
|
|
131 {
|
|
132 public PhoneEntity Phone;
|
|
133 public Name Name = new Name();
|
|
134 }
|
|
135
|
|
136 [Test]
|
|
137 public void InnerTest1()
|
|
138 {
|
|
139 ObjectMapper om = ObjectMapper<PersonEntity>.Instance;
|
|
140 List<string> list = new List<MemberMapper>(om)
|
|
141 .ConvertAll<string>(delegate(MemberMapper mm) { return mm.Name; });
|
|
142
|
|
143 Assert.That(list.Count == 5);
|
|
144 Assert.That(list.Contains("PhoneId"));
|
|
145 Assert.That(list.Contains("Phone"), Is.False);
|
|
146 Assert.That(list.Contains("LastName"));
|
|
147 Assert.That(list.Contains("Name"), Is.False);
|
|
148 }
|
|
149
|
|
150 [MapField("PhoneId", "Phone.Id")]
|
|
151 public class PersonEntity2 : Entity
|
|
152 {
|
|
153 [MapIgnore]
|
|
154 public PhoneEntity Phone = new PhoneEntity();
|
|
155 [MapField(Format="{0}")]
|
|
156 public Name Name = new Name();
|
|
157 }
|
|
158
|
|
159 [Test]
|
|
160 public void InnerTest2()
|
|
161 {
|
|
162 ObjectMapper om = ObjectMapper<PersonEntity2>.Instance;
|
|
163 List<string> list = new List<MemberMapper>(om)
|
|
164 .ConvertAll<string>(delegate(MemberMapper mm) { return mm.Name; });
|
|
165
|
|
166 Assert.That(list.Count == 5);
|
|
167 Assert.That(list.Contains("PhoneId"));
|
|
168 Assert.That(list.Contains("Phone"), Is.False);
|
|
169 Assert.That(list.Contains("LastName"));
|
|
170 Assert.That(list.Contains("Name"), Is.False);
|
|
171 }
|
|
172
|
|
173 [MapField("PhoneId", "Phone.Id")]
|
|
174 [MapField("Name", Format="{0}")]
|
|
175 public class PersonEntity3 : Entity
|
|
176 {
|
|
177 [MapIgnore]
|
|
178 public PhoneEntity Phone = new PhoneEntity();
|
|
179 public Name Name = new Name();
|
|
180 }
|
|
181
|
|
182 [Test]
|
|
183 public void InnerTest3()
|
|
184 {
|
|
185 ObjectMapper om = ObjectMapper<PersonEntity3>.Instance;
|
|
186 List<string> list = new List<MemberMapper>(om)
|
|
187 .ConvertAll<string>(delegate(MemberMapper mm) { return mm.Name; });
|
|
188
|
|
189 Assert.That(list.Count == 5);
|
|
190 Assert.That(list.Contains("PhoneId"));
|
|
191 Assert.That(list.Contains("Phone"), Is.False);
|
|
192 Assert.That(list.Contains("LastName"));
|
|
193 Assert.That(list.Contains("Name"), Is.False);
|
|
194 }
|
|
195 }
|
|
196 }
|