Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
Added trace on overwriting of pre-existing result to InjectionConfig
Browse files Browse the repository at this point in the history
Additionally, added some more tests for InjectionConfig.

Closes #39
  • Loading branch information
tschneidereit committed Jan 23, 2011
1 parent 7e125f2 commit f334f17
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/org/swiftsuspenders/InjectionConfig.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package org.swiftsuspenders
{
import flash.utils.getQualifiedClassName;

import org.swiftsuspenders.injectionresults.InjectionResult;

public class InjectionConfig
Expand Down Expand Up @@ -67,6 +69,14 @@ package org.swiftsuspenders

public function setResult(result : InjectionResult) : void
{
if (m_result != null && result != null)
{
trace('Warning: Injector already has a rule for type "' +
getQualifiedClassName(request) + '", named "' + injectionName + '".\n ' +
'If you have overwritten this mapping intentionally you can use ' +
'"injector.unmap()" prior to your replacement mapping in order to ' +
'avoid seeing this message.');
}
m_result = result;
}

Expand Down
41 changes: 23 additions & 18 deletions test/org/swiftsuspenders/ApplicationDomainTests.as
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,29 @@ package org.swiftsuspenders
//no need to assert anything here, this method is only needed to make Flexunit happy
}

[Test(async, timeout=5000)]
public function mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive() : void
{
loadSupportSWFIntoDomainWithCallback(new ApplicationDomain(), function() : void
{
var childInjector : Injector = injector.createChildInjector(_loaderDomain);
childInjector.mapClass(Clazz,
Class(_loaderDomain.getDefinition(getQualifiedClassName(Clazz))));
});
Async.handleEvent(this, _timer, TimerEvent.TIMER,
mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive_result, 5000);
}
private function mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive_result(
...args) : void
{
Assert.assertTrue('Mapping a class from a child ApplicationDomain doesn\'t prevent ' +
'it from being collected', weaklyKeyedDictionaryIsEmpty());
}
/**
* disabled for now because I can't find a way to force the player to perform both the
* mark and the sweep parts of GC in a reliable way inside the test harness, resulting in
* false negatives.
*/
// [Test(async, timeout=5000)]
// public function mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive() : void
// {
// loadSupportSWFIntoDomainWithCallback(new ApplicationDomain(), function() : void
// {
// var childInjector : Injector = injector.createChildInjector(_loaderDomain);
// childInjector.mapClass(Clazz,
// Class(_loaderDomain.getDefinition(getQualifiedClassName(Clazz))));
// });
// Async.handleEvent(this, _timer, TimerEvent.TIMER,
// mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive_result, 5000);
// }
// private function mappingsInReleasedChildInjectorDontKeepChildAppDomainAlive_result(
// ...args) : void
// {
// Assert.assertTrue('Mapping a class from a child ApplicationDomain doesn\'t prevent ' +
// 'it from being collected', weaklyKeyedDictionaryIsEmpty());
// }


private function loadSupportSWFIntoDomainWithCallback(
Expand Down
1 change: 0 additions & 1 deletion test/org/swiftsuspenders/ChildInjectorTests.as
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ package org.swiftsuspenders
{
injector.mapRule(Injector, new InjectorCopyRule());
injector.mapClass(InjectorInjectee, InjectorInjectee);
injector.mapClass(InjectorInjectee, InjectorInjectee);
var injectee : InjectorInjectee = injector.getInstance(InjectorInjectee);
Assert.assertNotNull('Injection has been applied to injectorInjectee', injectee.injector);
Assert.assertTrue('injectorInjectee.injector is child of main injector',
Expand Down
46 changes: 37 additions & 9 deletions test/org/swiftsuspenders/InjectionConfigTests.as
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.swiftsuspenders
{
import flash.utils.Dictionary;

import org.flexunit.Assert;
import flexunit.framework.Assert;

import org.swiftsuspenders.injectionresults.InjectClassResult;
import org.swiftsuspenders.injectionresults.InjectOtherRuleResult;
import org.swiftsuspenders.injectionresults.InjectSingletonResult;
import org.swiftsuspenders.injectionresults.InjectValueResult;
import org.swiftsuspenders.support.types.Clazz;

import org.swiftsuspenders.support.types.ClazzExtension;

public class InjectionConfigTests
{
private var injector:Injector;

[Before]
public function setup():void
{
injector = new Injector()
injector = new Injector();
}

[After]
Expand Down Expand Up @@ -71,18 +72,45 @@ package org.swiftsuspenders
var returnedResponse:Object = config.getResponse(injector);
var secondResponse:Object = config.getResponse(injector);

Assert.assertStrictlyEquals( returnedResponse, secondResponse )
Assert.assertStrictlyEquals( returnedResponse, secondResponse );
}

[Test]
public function sameNamedSingletonIsReturnedOnSecondResponse():void
{
var config : InjectionConfig = new InjectionConfig(Clazz, "named");
config.setResult(new InjectSingletonResult(Clazz));
var returnedResponse:Object = config.getResponse(injector);
var secondResponse:Object = config.getResponse(injector);

Assert.assertStrictlyEquals( returnedResponse, secondResponse )

Assert.assertStrictlyEquals( returnedResponse, secondResponse );
}

[Test]
public function callingSetResultBetweenUsagesChangesResponse():void
{
var config : InjectionConfig = new InjectionConfig(Clazz, '');
config.setResult(new InjectSingletonResult(Clazz));
var returnedResponse:Object = config.getResponse(injector);
config.setResult(null);
config.setResult(new InjectClassResult(Clazz));
var secondResponse:Object = config.getResponse(injector);

Assert.assertFalse('First result doesn\'t equal second result',
returnedResponse == secondResponse );
}

[Test]
public function injectionTypeOtherRuleReturnsOtherRulesResponse():void
{
var config : InjectionConfig = new InjectionConfig(Clazz, "");
var otherConfig : InjectionConfig = new InjectionConfig(ClazzExtension, "");
otherConfig.setResult(new InjectClassResult(ClazzExtension));
config.setResult(new InjectOtherRuleResult(otherConfig));
var returnedResponse:Object = config.getResponse(injector);

Assert.assertTrue( returnedResponse is Clazz);
Assert.assertTrue( returnedResponse is ClazzExtension);
}
}
}

0 comments on commit f334f17

Please sign in to comment.