0
|
1 using System;
|
|
2 using System.Collections;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.TypeBuilder;
|
|
8
|
|
9 namespace TypeBuilder
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class LazyInstanceAttributeTest
|
|
13 {
|
|
14 public abstract class AbstractObject
|
|
15 {
|
|
16 public AbstractObject(InitContext init)
|
|
17 {
|
|
18 if (init.MemberParameters != null && init.MemberParameters.Length == 1)
|
|
19 Field = (int)init.MemberParameters[0];
|
|
20 else
|
|
21 Field = 77;
|
|
22 }
|
|
23
|
|
24 public int Field;
|
|
25 }
|
|
26
|
|
27 public class InnerObject
|
|
28 {
|
|
29 public InnerObject(InitContext init)
|
|
30 {
|
|
31 if (init.MemberParameters != null && init.MemberParameters.Length == 1)
|
|
32 Field = (int)init.MemberParameters[0];
|
|
33 else
|
|
34 Field = 44;
|
|
35 }
|
|
36
|
|
37 public int Field;
|
|
38 }
|
|
39
|
|
40 public class TestField
|
|
41 {
|
|
42 public TestField()
|
|
43 {
|
|
44 Value = 10;
|
|
45 }
|
|
46
|
|
47 public TestField(int p1, float p2)
|
|
48 {
|
|
49 Value = p1 + (int)p2;
|
|
50 }
|
|
51
|
|
52 public TestField(TestField p1)
|
|
53 {
|
|
54 Value = 77;
|
|
55 }
|
|
56
|
|
57 public int Value;
|
|
58 }
|
|
59
|
|
60 public abstract class TestObject1
|
|
61 {
|
|
62 [LazyInstance]
|
|
63 public abstract ArrayList List { get; set; }
|
|
64
|
|
65 [LazyInstance]
|
|
66 public abstract string Str { get; set; }
|
|
67
|
|
68 [LazyInstance]
|
|
69 public abstract string this[int i] { get; set; }
|
|
70
|
|
71 [LazyInstance]
|
|
72 public abstract TestField Field { get; set; }
|
|
73
|
|
74 [LazyInstance]
|
|
75 public abstract InnerObject InnerObject { get; set; }
|
|
76
|
|
77 [LazyInstance]
|
|
78 public abstract AbstractObject AbstractObject { get; set; }
|
|
79 }
|
|
80
|
|
81 [Test]
|
|
82 public void NoParamTest()
|
|
83 {
|
|
84 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
|
|
85
|
|
86 Assert.IsNotNull(o.List);
|
|
87 Assert.AreEqual("", o.Str);
|
|
88 Assert.AreEqual(10, o.Field.Value);
|
|
89 Assert.AreEqual(44, o.InnerObject.Field);
|
|
90 }
|
|
91
|
|
92 [AttributeUsage(AttributeTargets.Property)]
|
|
93 public class TestParameterAttribute : ParameterAttribute
|
|
94 {
|
|
95 public TestParameterAttribute()
|
|
96 : base(new TestField())
|
|
97 {
|
|
98 }
|
|
99 }
|
|
100
|
|
101 public abstract class TestObject2
|
|
102 {
|
|
103 [LazyInstance, Parameter(10)]
|
|
104 public abstract ArrayList List { get; set; }
|
|
105
|
|
106 [LazyInstance, Parameter("test")]
|
|
107 public abstract string Str { get; set; }
|
|
108
|
|
109 [LazyInstance, Parameter(20)]
|
|
110 public abstract string this[int i] { get; set; }
|
|
111
|
|
112 [LazyInstance, Parameter(20, 30)]
|
|
113 public abstract TestField Field1 { get; set; }
|
|
114
|
|
115 [LazyInstance, TestParameter]
|
|
116 public abstract TestField Field2 { get; set; }
|
|
117
|
|
118 [LazyInstance, Parameter(55)]
|
|
119 public abstract InnerObject InnerObject { get; set; }
|
|
120
|
|
121 [LazyInstance, Parameter(88)]
|
|
122 public abstract AbstractObject AbstractObject { get; set; }
|
|
123 }
|
|
124
|
|
125 [Test]
|
|
126 public void ParamTest()
|
|
127 {
|
|
128 TestObject2 o = (TestObject2)TypeAccessor.CreateInstance(typeof(TestObject2));
|
|
129
|
|
130 Assert.AreEqual(10, o.List.Capacity);
|
|
131 Assert.AreEqual("test", o.Str);
|
|
132 Assert.AreEqual(50, o.Field1.Value);
|
|
133 Assert.AreEqual(77, o.Field2.Value);
|
|
134 Assert.AreEqual(55, o.InnerObject.Field);
|
|
135 }
|
|
136
|
|
137 [LazyInstances]
|
|
138 public abstract class TestObject3
|
|
139 {
|
|
140 public abstract string Str1 { get; set; }
|
|
141 [LazyInstance(false), Parameter("")]
|
|
142 public abstract string Str2 { get; set; }
|
|
143 }
|
|
144
|
|
145 [Test]
|
|
146 public void LazyInstancesTest()
|
|
147 {
|
|
148 TestObject3 o = (TestObject3)TypeAccessor.CreateInstance(typeof(TestObject3));
|
|
149
|
|
150 Assert.AreEqual("", o.Str1);
|
|
151 Assert.AreEqual("", o.Str2);
|
|
152
|
|
153 o.Str1 = null;
|
|
154 o.Str2 = null;
|
|
155
|
|
156 Assert.AreEqual("", o.Str1);
|
|
157 Assert.AreEqual(null, o.Str2);
|
|
158 }
|
|
159
|
|
160 [LazyInstances(false)]
|
|
161 public abstract class TestObject4 : TestObject3
|
|
162 {
|
|
163 }
|
|
164
|
|
165 [Test]
|
|
166 public void LazyInstancesFalseTest()
|
|
167 {
|
|
168 TestObject4 o = (TestObject4)TypeAccessor.CreateInstance(typeof(TestObject4));
|
|
169
|
|
170 Assert.AreEqual("", o.Str1);
|
|
171 Assert.AreEqual("", o.Str2);
|
|
172
|
|
173 o.Str1 = null;
|
|
174 o.Str2 = null;
|
|
175
|
|
176 Assert.AreEqual(null, o.Str1);
|
|
177 Assert.AreEqual(null, o.Str2);
|
|
178 }
|
|
179
|
|
180 [LazyInstances(typeof(string))]
|
|
181 public abstract class TestObject5
|
|
182 {
|
|
183 public abstract string Str { get; set; }
|
|
184 public abstract ArrayList List { get; set; }
|
|
185 }
|
|
186
|
|
187 [Test]
|
|
188 public void LazyInstancesTypeTest()
|
|
189 {
|
|
190 TestObject5 o = (TestObject5)TypeAccessor.CreateInstance(typeof(TestObject5));
|
|
191
|
|
192 Assert.IsNotNull(o.Str);
|
|
193 Assert.IsNotNull(o.List);
|
|
194
|
|
195 o.Str = null;
|
|
196 o.List = null;
|
|
197
|
|
198 Assert.IsNotNull(o.Str);
|
|
199 Assert.AreEqual (null, o.List);
|
|
200 }
|
|
201 }
|
|
202 }
|