0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Mapping;
|
|
6
|
|
7 namespace Mapping
|
|
8 {
|
|
9 [TestFixture]
|
|
10 public class NullableAttributeTest
|
|
11 {
|
|
12 public abstract class Object1
|
|
13 {
|
|
14 [NotNull] public abstract string Str1 { get; set; }
|
|
15 [Nullable] public abstract string Str2 { get; set; }
|
|
16 [NullValue("(null)")] public abstract string Str3 { get; set; }
|
|
17 [NullValue(typeof(DBNull))] public abstract string Str4 { get; set; }
|
|
18 }
|
|
19
|
|
20 [Test]
|
|
21 public void TestString1()
|
|
22 {
|
|
23 var om = Map.GetObjectMapper(typeof(Object1));
|
|
24 var o = (Object1)om.CreateInstance();
|
|
25
|
|
26 om.SetValue(o, "Str1", null);
|
|
27 om.SetValue(o, "Str2", null);
|
|
28 om.SetValue(o, "Str3", null);
|
|
29 om.SetValue(o, "Str4", null);
|
|
30
|
|
31 Assert.AreEqual("", o.Str1);
|
|
32 Assert.AreEqual("", o.Str2);
|
|
33 Assert.AreEqual("(null)", o.Str3);
|
|
34 Assert.IsNull (o.Str4);
|
|
35
|
|
36 Assert.IsNotNull(om.GetValue(o, "Str1"));
|
|
37 Assert.IsNull (om.GetValue(o, "Str2"));
|
|
38 Assert.IsNull (om.GetValue(o, "Str3"));
|
|
39 Assert.IsNull (om.GetValue(o, "Str4"));
|
|
40 }
|
|
41
|
|
42 [NullValue(typeof(string), "(null)")]
|
|
43 [NullValue(typeof(bool), false)]
|
|
44 public abstract class Object2
|
|
45 {
|
|
46 [Nullable(false)]
|
|
47 public abstract string Str1 { get; set; }
|
|
48 [NullValue("")]
|
|
49 public abstract string Str2 { get; set; }
|
|
50 public abstract string Str3 { get; set; }
|
|
51 [NullValue(typeof(DBNull))]
|
|
52 public abstract string Str4 { get; set; }
|
|
53 }
|
|
54
|
|
55 [Test]
|
|
56 public void TestString2()
|
|
57 {
|
|
58 ObjectMapper om = Map.GetObjectMapper(typeof(Object2));
|
|
59
|
|
60 Object2 o = (Object2)om.CreateInstance();
|
|
61
|
|
62 om.SetValue(o, "Str1", null);
|
|
63 om.SetValue(o, "Str2", null);
|
|
64 om.SetValue(o, "Str3", null);
|
|
65 om.SetValue(o, "Str4", null);
|
|
66
|
|
67 Assert.AreEqual("", o.Str1);
|
|
68 Assert.AreEqual("", o.Str2);
|
|
69 Assert.AreEqual("(null)", o.Str3);
|
|
70 Assert.IsNull (o.Str4);
|
|
71
|
|
72 Assert.IsNotNull(om.GetValue(o, "Str1"));
|
|
73 Assert.IsNull (om.GetValue(o, "Str2"));
|
|
74 Assert.IsNull (om.GetValue(o, "Str3"));
|
|
75 Assert.IsNull (om.GetValue(o, "Str4"));
|
|
76 }
|
|
77
|
|
78 [Nullable(typeof(string))]
|
|
79 public abstract class Object3
|
|
80 {
|
|
81 [Nullable(false)]
|
|
82 public abstract string Str1 { get; set; }
|
|
83 public abstract string Str2 { get; set; }
|
|
84 [NullValue("(null)")]
|
|
85 public abstract string Str3 { get; set; }
|
|
86 [NullValue(typeof(DBNull))]
|
|
87 public abstract string Str4 { get; set; }
|
|
88 }
|
|
89
|
|
90 [Test]
|
|
91 public void TestString3()
|
|
92 {
|
|
93 ObjectMapper om = Map.GetObjectMapper(typeof(Object3));
|
|
94
|
|
95 Object3 o = (Object3)om.CreateInstance();
|
|
96
|
|
97 om.SetValue(o, "Str1", null);
|
|
98 om.SetValue(o, "Str2", null);
|
|
99 om.SetValue(o, "Str3", null);
|
|
100 om.SetValue(o, "Str4", null);
|
|
101
|
|
102 Assert.AreEqual("", o.Str1);
|
|
103 Assert.AreEqual("", o.Str2);
|
|
104 Assert.AreEqual("(null)", o.Str3);
|
|
105 Assert.IsNull (o.Str4);
|
|
106
|
|
107 Assert.IsNotNull(om.GetValue(o, "Str1"));
|
|
108 Assert.IsNull (om.GetValue(o, "Str2"));
|
|
109 Assert.IsNull (om.GetValue(o, "Str3"));
|
|
110 Assert.IsNull (om.GetValue(o, "Str4"));
|
|
111 }
|
|
112
|
|
113 [NullValue("(null)")]
|
|
114 public abstract class Object4
|
|
115 {
|
|
116 [Nullable(false)]
|
|
117 public abstract string Str1 { get; set; }
|
|
118 [NullValue("")]
|
|
119 public abstract string Str2 { get; set; }
|
|
120 public abstract string Str3 { get; set; }
|
|
121 [NullValue(typeof(DBNull))]
|
|
122 public abstract string Str4 { get; set; }
|
|
123 }
|
|
124
|
|
125 [Test]
|
|
126 public void TestString4()
|
|
127 {
|
|
128 ObjectMapper om = Map.GetObjectMapper(typeof(Object2));
|
|
129
|
|
130 Object2 o = (Object2)om.CreateInstance();
|
|
131
|
|
132 om.SetValue(o, "Str1", null);
|
|
133 om.SetValue(o, "Str2", "2");
|
|
134 om.SetValue(o, "Str3", null);
|
|
135 om.SetValue(o, "Str4", null);
|
|
136
|
|
137 Assert.AreEqual("", o.Str1);
|
|
138 Assert.AreEqual("2", o.Str2);
|
|
139 Assert.AreEqual("(null)", o.Str3);
|
|
140 Assert.IsNull (o.Str4);
|
|
141
|
|
142 Assert.IsNotNull( om.GetValue(o, "Str1"));
|
|
143 Assert.AreEqual ("2", om.GetValue(o, "Str2"));
|
|
144 Assert.IsNull ( om.GetValue(o, "Str3"));
|
|
145 Assert.IsNull ( om.GetValue(o, "Str4"));
|
|
146 }
|
|
147
|
|
148 public class Object5
|
|
149 {
|
|
150 public int Int1;
|
|
151 [Nullable]
|
|
152 public int Int2;
|
|
153 [NullValue(int.MinValue)]
|
|
154 public int Int3;
|
|
155 }
|
|
156
|
|
157 [Test]
|
|
158 public void TestPrimitive()
|
|
159 {
|
|
160 ObjectMapper om = Map.GetObjectMapper(typeof(Object5));
|
|
161
|
|
162 Object5 o = (Object5)om.CreateInstance();
|
|
163
|
|
164 om.SetValue(o, "Int1", null);
|
|
165 om.SetValue(o, "Int2", null);
|
|
166 om.SetValue(o, "Int3", null);
|
|
167
|
|
168 Assert.AreEqual(0, o.Int1);
|
|
169 Assert.AreEqual(0, o.Int2);
|
|
170 Assert.AreEqual(int.MinValue, o.Int3);
|
|
171
|
|
172 Assert.IsNotNull(om.GetValue(o, "Int1"));
|
|
173 Assert.IsNull (om.GetValue(o, "Int2"));
|
|
174 Assert.IsNull (om.GetValue(o, "Int3"));
|
|
175 }
|
|
176
|
|
177 [NullValue("(null)")]
|
|
178 public class Object6
|
|
179 {
|
|
180 public string String1;
|
|
181 }
|
|
182
|
|
183 [NullValue("(derived null)")]
|
|
184 public class Object6Derived : Object6
|
|
185 {
|
|
186 }
|
|
187
|
|
188 [Test]
|
|
189 public void TestDerivedNull()
|
|
190 {
|
|
191 ObjectMapper om = Map.GetObjectMapper(typeof(Object6Derived));
|
|
192 Object6Derived o = (Object6Derived) om.CreateInstance();
|
|
193
|
|
194 om.SetValue(o, "String1", null);
|
|
195 Console.WriteLine(o.String1);
|
|
196 }
|
|
197 }
|
|
198 }
|