This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
InjectorTests.as
397 lines (363 loc) · 18.4 KB
/
InjectorTests.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 2009 the original author or authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.swiftsuspenders
{
import mx.collections.ArrayCollection;
import org.flexunit.Assert;
import org.swiftsuspenders.support.injectees.ClassInjectee;
import org.swiftsuspenders.support.injectees.ComplexClassInjectee;
import org.swiftsuspenders.support.injectees.InterfaceInjectee;
import org.swiftsuspenders.support.injectees.MixedParametersConstructorInjectee;
import org.swiftsuspenders.support.injectees.MixedParametersMethodInjectee;
import org.swiftsuspenders.support.injectees.MultipleNamedSingletonsOfSameClassInjectee;
import org.swiftsuspenders.support.injectees.MultipleSingletonsOfSameClassInjectee;
import org.swiftsuspenders.support.injectees.MultipleSingletonsOfSameClassInjectee;
import org.swiftsuspenders.support.injectees.NamedArrayCollectionInjectee;
import org.swiftsuspenders.support.injectees.NamedClassInjectee;
import org.swiftsuspenders.support.injectees.NamedInterfaceInjectee;
import org.swiftsuspenders.support.injectees.OneNamedParameterConstructorInjectee;
import org.swiftsuspenders.support.injectees.OneNamedParameterMethodInjectee;
import org.swiftsuspenders.support.injectees.OneParameterConstructorInjectee;
import org.swiftsuspenders.support.injectees.OneParameterMethodInjectee;
import org.swiftsuspenders.support.injectees.SetterInjectee;
import org.swiftsuspenders.support.injectees.TwoNamedInterfaceFieldsInjectee;
import org.swiftsuspenders.support.injectees.TwoNamedParametersConstructorInjectee;
import org.swiftsuspenders.support.injectees.TwoNamedParametersMethodInjectee;
import org.swiftsuspenders.support.injectees.TwoParametersConstructorInjectee;
import org.swiftsuspenders.support.injectees.TwoParametersMethodInjectee;
import org.swiftsuspenders.support.types.Clazz;
import org.swiftsuspenders.support.types.Clazz2;
import org.swiftsuspenders.support.types.ComplexClazz;
import org.swiftsuspenders.support.types.Interface;
import org.swiftsuspenders.support.types.Interface2;
public class InjectorTests
{
protected var injector:Injector;
[Before]
public function runBeforeEachTest():void
{
injector = new Injector();
}
[Test]
public function unbind():void
{
var injectee:ClassInjectee = new ClassInjectee();
var value:Clazz = new Clazz();
injector.mapValue(Clazz, value);
injector.unmap(Clazz);
try
{
injector.injectInto(injectee);
}
catch(e:Error)
{
}
Assert.assertEquals("Property should not be injected", null, injectee.property);
}
[Test]
public function bindValue():void
{
var injectee:ClassInjectee = new ClassInjectee();
var injectee2:ClassInjectee = new ClassInjectee();
var value:Clazz = new Clazz();
injector.mapValue(Clazz, value);
injector.injectInto(injectee);
Assert.assertStrictlyEquals("Value should have been injected", value, injectee.property );
injector.injectInto(injectee2);
Assert.assertStrictlyEquals("Injected values should be equal", injectee.property, injectee2.property );
}
[Test]
public function bindValueByInterface():void
{
var injectee:InterfaceInjectee = new InterfaceInjectee();
var value:Interface = new Clazz();
injector.mapValue(Interface, value);
injector.injectInto(injectee);
Assert.assertStrictlyEquals("Value should have been injected", value, injectee.property );
}
[Test]
public function bindNamedValue():void
{
var injectee:NamedClassInjectee = new NamedClassInjectee();
var value:Clazz = new Clazz();
injector.mapValue(Clazz, value, NamedClassInjectee.NAME);
injector.injectInto(injectee);
Assert.assertStrictlyEquals("Named value should have been injected", value, injectee.property );
}
[Test]
public function bindNamedValueByInterface():void
{
var injectee:NamedInterfaceInjectee = new NamedInterfaceInjectee();
var value:Interface = new Clazz();
injector.mapValue(Interface, value, NamedClassInjectee.NAME);
injector.injectInto(injectee);
Assert.assertStrictlyEquals("Named value should have been injected", value, injectee.property );
}
[Test]
public function bindValueNested():void
{
var injectee:ComplexClassInjectee = new ComplexClassInjectee();
var value:Clazz = new Clazz();
var complexValue:ComplexClazz = new ComplexClazz();
injector.mapValue(Clazz, value);
injector.mapValue(ComplexClazz, complexValue);
injector.injectInto(injectee);
Assert.assertStrictlyEquals("Complex Value should have been injected", complexValue, injectee.property );
Assert.assertStrictlyEquals("Nested value should have been injected", value, injectee.property.value );
}
[Test]
public function bindMultipleInterfacesToOneSingletonClass():void
{
var injectee:MultipleSingletonsOfSameClassInjectee = new MultipleSingletonsOfSameClassInjectee();
injector.mapSingletonOf(Interface, Clazz);
injector.mapSingletonOf(Interface2, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Singleton Value for 'property1' should have been injected", injectee.property1 );
Assert.assertNotNull("Singleton Value for 'property2' should have been injected", injectee.property2 );
Assert.assertFalse("Singleton Values 'property1' and 'property2' should not be identical", injectee.property1 == injectee.property2 );
}
[Test]
public function bindClass():void
{
var injectee:ClassInjectee = new ClassInjectee();
var injectee2:ClassInjectee = new ClassInjectee();
injector.mapClass(Clazz, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.property );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.property == injectee2.property );
}
[Test]
public function bindClassByInterface():void
{
var injectee:InterfaceInjectee = new InterfaceInjectee();
injector.mapClass(Interface, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.property );
}
[Test]
public function bindNamedClass():void
{
var injectee:NamedClassInjectee = new NamedClassInjectee();
injector.mapClass(Clazz, Clazz, NamedClassInjectee.NAME);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of named Class should have been injected", injectee.property );
}
[Test]
public function bindNamedClassByInterface():void
{
var injectee:NamedInterfaceInjectee = new NamedInterfaceInjectee();
injector.mapClass(Interface, Clazz, NamedClassInjectee.NAME);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of named Class should have been injected", injectee.property );
}
[Test]
public function bindSingleton():void
{
var injectee:ClassInjectee = new ClassInjectee();
var injectee2:ClassInjectee = new ClassInjectee();
injector.mapSingleton(Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.property );
injector.injectInto(injectee2);
Assert.assertStrictlyEquals("Injected values should be equal", injectee.property, injectee2.property );
}
[Test]
public function bindSingletonOf():void
{
var injectee:InterfaceInjectee = new InterfaceInjectee();
var injectee2:InterfaceInjectee = new InterfaceInjectee();
injector.mapSingletonOf(Interface, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.property );
injector.injectInto(injectee2);
Assert.assertStrictlyEquals("Injected values should be equal", injectee.property, injectee2.property );
}
[Test]
public function bindDifferentlyNamedSingletonsBySameInterface():void
{
var injectee:TwoNamedInterfaceFieldsInjectee = new TwoNamedInterfaceFieldsInjectee();
injector.mapSingletonOf(Interface, Clazz, 'Name1');
injector.mapSingletonOf(Interface, Clazz2, 'Name2');
injector.injectInto(injectee);
Assert.assertTrue('Property "property1" should be of type "Clazz"', injectee.property1 is Clazz);
Assert.assertTrue('Property "property2" should be of type "Clazz2"', injectee.property2 is Clazz2);
Assert.assertFalse('Properties "property1" and "property2" should have received different singletons', injectee.property1 == injectee.property2);
}
[Test]
public function performSetterInjection():void
{
var injectee:SetterInjectee = new SetterInjectee();
var injectee2:SetterInjectee = new SetterInjectee();
injector.mapClass(Clazz, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.property );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.property == injectee2.property );
}
[Test]
public function performMethodInjectionWithOneParameter():void
{
var injectee:OneParameterMethodInjectee = new OneParameterMethodInjectee();
var injectee2:OneParameterMethodInjectee = new OneParameterMethodInjectee();
injector.mapClass(Clazz, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected", injectee.getDependency() );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.getDependency() == injectee2.getDependency() );
}
[Test]
public function performMethodInjectionWithOneNamedParameter():void
{
var injectee:OneNamedParameterMethodInjectee = new OneNamedParameterMethodInjectee();
var injectee2:OneNamedParameterMethodInjectee = new OneNamedParameterMethodInjectee();
injector.mapClass(Clazz, Clazz, 'namedDep');
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency() );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.getDependency() == injectee2.getDependency() );
}
[Test]
public function performMethodInjectionWithTwoParameters():void
{
var injectee:TwoParametersMethodInjectee = new TwoParametersMethodInjectee();
var injectee2:TwoParametersMethodInjectee = new TwoParametersMethodInjectee();
injector.mapClass(Clazz, Clazz);
injector.mapClass(Interface, Clazz);
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected for unnamed Clazz parameter", injectee.getDependency() );
Assert.assertNotNull("Instance of Class should have been injected for unnamed Interface parameter", injectee.getDependency2() );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.getDependency() == injectee2.getDependency() );
Assert.assertFalse("Injected values for Interface should be different", injectee.getDependency2() == injectee2.getDependency2() );
}
[Test]
public function performMethodInjectionWithTwoNamedParameters():void
{
var injectee:TwoNamedParametersMethodInjectee = new TwoNamedParametersMethodInjectee();
var injectee2:TwoNamedParametersMethodInjectee = new TwoNamedParametersMethodInjectee();
injector.mapClass(Clazz, Clazz, 'namedDep');
injector.mapClass(Interface, Clazz, 'namedDep2');
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency() );
Assert.assertNotNull("Instance of Class should have been injected for for named Interface parameter", injectee.getDependency2() );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values should be different", injectee.getDependency() == injectee2.getDependency() );
Assert.assertFalse("Injected values for Interface should be different", injectee.getDependency2() == injectee2.getDependency2() );
}
[Test]
public function performMethodInjectionWithMixedParameters():void
{
var injectee:MixedParametersMethodInjectee = new MixedParametersMethodInjectee();
var injectee2:MixedParametersMethodInjectee = new MixedParametersMethodInjectee();
injector.mapClass(Clazz, Clazz, 'namedDep');
injector.mapClass(Clazz, Clazz);
injector.mapClass(Interface, Clazz, 'namedDep2');
injector.injectInto(injectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency() );
Assert.assertNotNull("Instance of Class should have been injected for unnamed Clazz parameter", injectee.getDependency2() );
Assert.assertNotNull("Instance of Class should have been injected for Interface", injectee.getDependency3() );
injector.injectInto(injectee2);
Assert.assertFalse("Injected values for named Clazz should be different", injectee.getDependency() == injectee2.getDependency() );
Assert.assertFalse("Injected values for unnamed Clazz should be different", injectee.getDependency2() == injectee2.getDependency2() );
Assert.assertFalse("Injected values for named Interface should be different", injectee.getDependency3() == injectee2.getDependency3() );
}
[Test]
public function performConstructorInjectionWithOneParameter():void
{
injector.mapClass(Clazz, Clazz);
var injectee:OneParameterConstructorInjectee = injector.instantiate(OneParameterConstructorInjectee);
Assert.assertNotNull("Instance of Class should have been injected for Clazz parameter", injectee.getDependency() );
}
[Test]
public function performConstructorInjectionWithTwoParameters():void
{
injector.mapClass(Clazz, Clazz);
injector.mapValue(String, 'stringDependency');
var injectee:TwoParametersConstructorInjectee = injector.instantiate(TwoParametersConstructorInjectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency());
Assert.assertEquals("The String 'stringDependency' should have been injected for String parameter", injectee.getDependency2(), 'stringDependency');
}
[Test]
public function performConstructorInjectionWithOneNamedParameter():void
{
injector.mapClass(Clazz, Clazz, 'namedDependency');
var injectee:OneNamedParameterConstructorInjectee = injector.instantiate(OneNamedParameterConstructorInjectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency() );
}
[Test]
public function performConstructorInjectionWithTwoNamedParameter():void
{
injector.mapClass(Clazz, Clazz, 'namedDependency');
injector.mapValue(String, 'stringDependency', 'namedDependency2');
var injectee:TwoNamedParametersConstructorInjectee = injector.instantiate(TwoNamedParametersConstructorInjectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency());
Assert.assertEquals("The String 'stringDependency' should have been injected for named String parameter", injectee.getDependency2(), 'stringDependency');
}
[Test]
public function performConstructorInjectionWithMixedParameters():void
{
injector.mapClass(Clazz, Clazz, 'namedDep');
injector.mapClass(Clazz, Clazz);
injector.mapClass(Interface, Clazz, 'namedDep2');
var injectee:MixedParametersConstructorInjectee = injector.instantiate(MixedParametersConstructorInjectee);
Assert.assertNotNull("Instance of Class should have been injected for named Clazz parameter", injectee.getDependency() );
Assert.assertNotNull("Instance of Class should have been injected for unnamed Clazz parameter", injectee.getDependency2() );
Assert.assertNotNull("Instance of Class should have been injected for Interface", injectee.getDependency3() );
}
[Test]
public function performNamedArrayInjection():void
{
var ac : ArrayCollection = new ArrayCollection();
injector.mapValue(ArrayCollection, ac, "namedCollection");
var injectee:NamedArrayCollectionInjectee = injector.instantiate(NamedArrayCollectionInjectee);
Assert.assertNotNull("Instance 'ac' should have been injected for named ArrayCollection parameter", injectee.ac );
Assert.assertEquals("Instance field 'ac' should be identical to local variable 'ac'", ac, injectee.ac);
}
[Test]
public function performMappedRuleInjection():void
{
var rule : InjectionConfig = injector.mapSingletonOf(Interface, Clazz);
injector.mapRule(Interface2, rule);
var injectee:MultipleSingletonsOfSameClassInjectee = injector.instantiate(MultipleSingletonsOfSameClassInjectee);
Assert.assertEquals("Instance field 'property1' should be identical to Instance field 'property2'", injectee.property1, injectee.property2);
}
[Test]
public function performMappedNamedRuleInjection():void
{
var rule : InjectionConfig = injector.mapSingletonOf(Interface, Clazz);
injector.mapRule(Interface2, rule);
injector.mapRule(Interface, rule, 'name1');
injector.mapRule(Interface2, rule, 'name2');
var injectee:MultipleNamedSingletonsOfSameClassInjectee = injector.instantiate(MultipleNamedSingletonsOfSameClassInjectee);
Assert.assertEquals("Instance field 'property1' should be identical to Instance field 'property2'", injectee.property1, injectee.property2);
Assert.assertEquals("Instance field 'property1' should be identical to Instance field 'namedProperty1'", injectee.property1, injectee.namedProperty1);
Assert.assertEquals("Instance field 'property1' should be identical to Instance field 'namedProperty2'", injectee.property1, injectee.namedProperty2);
}
[Test(expects="org.swiftsuspenders.InjectorError")]
public function haltOnMissingDependency():void
{
var injectee:InterfaceInjectee = new InterfaceInjectee();
injector.injectInto(injectee);
}
}
}