0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Mapping;
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.Reflection.Extension;
|
|
8 using BLToolkit.Reflection.MetadataProvider;
|
|
9
|
|
10 namespace Mapping
|
|
11 {
|
|
12 [TestFixture]
|
|
13 public class MapperMemberAttributeTest
|
|
14 {
|
|
15 class MemberMapper1 : MemberMapper
|
|
16 {
|
|
17 public override object GetValue(object o)
|
|
18 {
|
|
19 return 45;
|
|
20 }
|
|
21 }
|
|
22
|
|
23 public class Object1
|
|
24 {
|
|
25 // MapIgnore set to false
|
|
26 //
|
|
27 [MemberMapper(typeof(MemberMapper1))]
|
|
28 public int Int;
|
|
29
|
|
30 // MapIgnore set to DebugSwitch value
|
|
31 //
|
|
32 [MapIgnore(DebugSwitch), MemberMapper(typeof(MemberMapper1))]
|
|
33 public int MapIgnore;
|
|
34
|
|
35 // MapIgnore set to true
|
|
36 //
|
|
37 [MemberMapper(typeof(MemberMapper1)), MapIgnore]
|
|
38 public int MapIgnore2;
|
|
39
|
|
40 [MapIgnore(false), MemberMapper(typeof(MemberMapper1))]
|
|
41 public int MapNotIgnore;
|
|
42
|
|
43 private const bool DebugSwitch = true;
|
|
44 }
|
|
45
|
|
46 [Test]
|
|
47 public void Test1()
|
|
48 {
|
|
49 ObjectMapper om = Map.GetObjectMapper(typeof(Object1));
|
|
50
|
|
51 Object1 o = new Object1();
|
|
52
|
|
53 om.SetValue(o, "Int", 456);
|
|
54
|
|
55 Assert.AreEqual(456, o.Int);
|
|
56 Assert.AreEqual(45, om.GetValue(o, "Int"));
|
|
57
|
|
58 Assert.IsNull(om["MapIgnore"]);
|
|
59 Assert.IsNull(om["MapIgnore2"]);
|
|
60 Assert.IsNotNull(om["MapNotIgnore"]);
|
|
61 }
|
|
62
|
|
63 [MemberMapper(typeof(int), typeof(MemberMapper1))]
|
|
64 public class Object2
|
|
65 {
|
|
66 public int Int;
|
|
67 }
|
|
68
|
|
69 [Test]
|
|
70 public void Test2()
|
|
71 {
|
|
72 ObjectMapper om = Map.GetObjectMapper(typeof(Object2));
|
|
73
|
|
74 Object2 o = new Object2();
|
|
75
|
|
76 om.SetValue(o, "Int", 456);
|
|
77
|
|
78 Assert.AreEqual(456, o.Int);
|
|
79 Assert.AreEqual(45, om.GetValue(o, "Int"));
|
|
80 }
|
|
81
|
|
82 [MemberMapper(typeof(CustomNum.Mapper))]
|
|
83 public class CustomNum
|
|
84 {
|
|
85 public int Num;
|
|
86
|
|
87 public CustomNum(int num)
|
|
88 {
|
|
89 Num = num;
|
|
90 }
|
|
91
|
|
92 public CustomNum()
|
|
93 {
|
|
94 }
|
|
95
|
|
96 public class Mapper : MemberMapper
|
|
97 {
|
|
98 public override object GetValue(object o)
|
|
99 {
|
|
100 object value = MemberAccessor.GetValue(o);
|
|
101 return value is CustomNum? ((CustomNum)value).Num: 0;
|
|
102 }
|
|
103
|
|
104 public override void SetValue(object o, object value)
|
|
105 {
|
|
106 value = (value is int)? new CustomNum((int)value) : new CustomNum();
|
|
107 MemberAccessor.SetValue(o, value);
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111
|
|
112 public class Object3
|
|
113 {
|
|
114 public CustomNum Num;
|
|
115 }
|
|
116
|
|
117 [Test]
|
|
118 public void TestNumberString()
|
|
119 {
|
|
120 ObjectMapper om = Map.GetObjectMapper(typeof(Object3));
|
|
121 Object3 o = new Object3();
|
|
122
|
|
123 om.SetValue(o, "Num", 123);
|
|
124
|
|
125 Assert.AreEqual(123, o.Num.Num);
|
|
126 }
|
|
127
|
|
128 #region Custom mapping schema test
|
|
129
|
|
130 public class CustomString
|
|
131 {
|
|
132 public string Str;
|
|
133
|
|
134 public CustomString(string str)
|
|
135 {
|
|
136 Str = str;
|
|
137 }
|
|
138
|
|
139 public CustomString()
|
|
140 {
|
|
141 Str = string.Empty;
|
|
142 }
|
|
143
|
|
144 public class Mapper : MemberMapper
|
|
145 {
|
|
146 public override object GetValue(object o)
|
|
147 {
|
|
148 object value = MemberAccessor.GetValue(o);
|
|
149 return value is CustomString? ((CustomString)value).Str: null;
|
|
150 }
|
|
151
|
|
152 public override void SetValue(object o, object value)
|
|
153 {
|
|
154 value = (value is string)? new CustomString((string)value) : new CustomString();
|
|
155 MemberAccessor.SetValue(o, value);
|
|
156 }
|
|
157 }
|
|
158 }
|
|
159
|
|
160 public class Object4
|
|
161 {
|
|
162 public CustomString Str;
|
|
163 }
|
|
164
|
|
165 public class TestMappingSchema : MappingSchema
|
|
166 {
|
|
167 private class TestMapMetadataProvider : MetadataProviderBase
|
|
168 {
|
|
169 public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
170 {
|
|
171 if (member.Type == typeof(CustomString))
|
|
172 {
|
|
173 isSet = true;
|
|
174 return false;
|
|
175 }
|
|
176
|
|
177 return base.GetMapIgnore(typeExtension, member, out isSet);
|
|
178 }
|
|
179 }
|
|
180
|
|
181 private class TestObjectMapper : ObjectMapper
|
|
182 {
|
|
183 protected override MemberMapper CreateMemberMapper(MapMemberInfo mapMemberInfo)
|
|
184 {
|
|
185 if (mapMemberInfo.Type == typeof(CustomString))
|
|
186 {
|
|
187 MemberMapper mm = new CustomString.Mapper();
|
|
188 mm.Init(mapMemberInfo);
|
|
189 return mm;
|
|
190 }
|
|
191
|
|
192 return base.CreateMemberMapper(mapMemberInfo);
|
|
193 }
|
|
194
|
|
195 protected override MetadataProviderBase CreateMetadataProvider()
|
|
196 {
|
|
197 MetadataProviderBase provider = base.CreateMetadataProvider();
|
|
198 provider.AddProvider(new TestMapMetadataProvider());
|
|
199 return provider;
|
|
200 }
|
|
201
|
|
202 }
|
|
203
|
|
204 protected override ObjectMapper CreateObjectMapperInstance(Type type)
|
|
205 {
|
|
206 return new TestObjectMapper();
|
|
207 }
|
|
208 }
|
|
209
|
|
210 [Test]
|
|
211 public void TestCustomString()
|
|
212 {
|
|
213 MappingSchema save = Map.DefaultSchema;
|
|
214
|
|
215 Map.DefaultSchema = new TestMappingSchema();
|
|
216
|
|
217 ObjectMapper om = Map.GetObjectMapper(typeof(Object4));
|
|
218
|
|
219 Object4 o = new Object4();
|
|
220
|
|
221 om.SetValue(o, "Str", "Test");
|
|
222
|
|
223 Assert.AreEqual("Test", o.Str.Str);
|
|
224
|
|
225 Map.DefaultSchema = save;
|
|
226 }
|
|
227
|
|
228 #endregion
|
|
229 }
|
|
230 }
|