Permalink
Browse files

Fixes bugs and nits in Stray's implementation of fallback providers

  • Loading branch information...
tschneidereit committed Oct 4, 2012
1 parent ec0db70 commit a9344e245e84a7231967779774719c8a21513301

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -10,13 +10,13 @@ package org.swiftsuspenders.dependencyproviders
import flash.utils.Dictionary;
import org.swiftsuspenders.Injector;
- import org.swiftsuspenders.typedescriptions.TypeDescription;
- public class ClassProvider implements FallbackDependencyProvider
+ public class ClassProvider implements DependencyProvider
{
//---------------------- Private / Protected Properties ----------------------//
private var _responseType : Class;
+
//---------------------- Public Methods ----------------------//
public function ClassProvider(responseType : Class)
{
@@ -38,10 +38,5 @@ package org.swiftsuspenders.dependencyproviders
public function destroy() : void
{
}
-
- public function satisfies(type:Class, description:TypeDescription): Boolean
- {
- return (description.ctor != null);
- }
}
}
@@ -1,10 +1,25 @@
+/*
+ * Copyright (c) 2012 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
package org.swiftsuspenders.dependencyproviders
{
- import org.swiftsuspenders.typedescriptions.TypeDescription;
-
public interface FallbackDependencyProvider extends DependencyProvider
{
- function satisfies(type : Class, description : TypeDescription):Boolean;
+ /**
+ * Instructs the fallback provider to interpret the next call to <code>apply</code> as
+ * being a request for the given <code>mappingId</code>. The provider must return
+ * <code>false</code> if it can't satisfy a request for the given mappingId.
+ *
+ * While this is far from ideal, it's the cleanest low-overhead way to implement multiple-
+ * type fallback providers in the injector's architecture.
+ *
+ * @param mappingId The type + mapping name to provide a result for
+ * @return True if the provider can provide a result, false otherwise
+ */
+ function prepareNextRequest(mappingId : String) : Boolean;
}
-
}
@@ -11,7 +11,7 @@ package org.swiftsuspenders.dependencyproviders
import flash.utils.getQualifiedClassName;
import org.swiftsuspenders.Injector;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.typedescriptions.PreDestroyInjectionPoint;
import org.swiftsuspenders.typedescriptions.TypeDescription;
@@ -5,7 +5,7 @@
* in accordance with the terms of the license agreement accompanying it.
*/
-package org.swiftsuspenders
+package org.swiftsuspenders.errors
{
public class InjectorError extends Error
{
@@ -7,11 +7,9 @@
package org.swiftsuspenders.errors
{
- import org.swiftsuspenders.InjectorError;
-
public class InjectorInterfaceConstructionError extends InjectorError
{
- public function InjectorInterfaceConstructionError(message:*="", id:*=0)
+ public function InjectorInterfaceConstructionError(message : * = "", id : * = 0)
{
super(message, id);
}
@@ -7,11 +7,9 @@
package org.swiftsuspenders.errors
{
- import org.swiftsuspenders.InjectorError;
-
public class InjectorMissingMappingError extends InjectorError
{
- public function InjectorMissingMappingError(message:*="", id:*=0)
+ public function InjectorMissingMappingError(message : * = "", id : * = 0)
{
super(message, id);
}
@@ -8,7 +8,7 @@
package org.swiftsuspenders.mapping
{
import org.swiftsuspenders.Injector;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.dependencyproviders.ClassProvider;
import org.swiftsuspenders.dependencyproviders.DependencyProvider;
import org.swiftsuspenders.dependencyproviders.ForwardingProvider;
@@ -55,7 +55,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*
* @see #toSingleton()
*/
@@ -76,7 +76,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*
* @see #toProvider()
*/
@@ -98,7 +98,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*
* @see #toProvider()
*/
@@ -118,7 +118,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*
* @see #toProvider()
*/
@@ -136,7 +136,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*/
public function toProvider(provider : DependencyProvider) : UnsealedMapping
{
@@ -169,7 +169,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*/
public function softly() : ProviderlessMapping
{
@@ -190,7 +190,7 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Sealed mappings can't be changed in any way
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings can't be changed in any way
*/
public function locally() : ProviderlessMapping
{
@@ -217,7 +217,7 @@ package org.swiftsuspenders.mapping
*
* @returns An internally created object that can be used as the key for unseal
*
- * @throws org.swiftsuspenders.InjectorError Can't be invoked on a mapping that's already sealed
+ * @throws org.swiftsuspenders.errors.InjectorError Can't be invoked on a mapping that's already sealed
*
* @see #unseal()
*/
@@ -240,8 +240,8 @@ package org.swiftsuspenders.mapping
*
* @return The <code>InjectionMapping</code> the method is invoked on
*
- * @throws org.swiftsuspenders.InjectorError Has to be invoked with the unique key object returned by an earlier call to <code>seal</code>
- * @throws org.swiftsuspenders.InjectorError Can't unseal a mapping that's not sealed
+ * @throws org.swiftsuspenders.errors.InjectorError Has to be invoked with the unique key object returned by an earlier call to <code>seal</code>
+ * @throws org.swiftsuspenders.errors.InjectorError Can't unseal a mapping that's not sealed
*
* @see #seal()
*/
@@ -12,7 +12,7 @@ package org.swiftsuspenders.reflection
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.typedescriptions.ConstructorInjectionPoint;
import org.swiftsuspenders.typedescriptions.MethodInjectionPoint;
import org.swiftsuspenders.typedescriptions.NoParamsConstructorInjectionPoint;
@@ -11,7 +11,7 @@ package org.swiftsuspenders.reflection
import flash.utils.describeType;
import flash.utils.getQualifiedClassName;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.typedescriptions.ConstructorInjectionPoint;
import org.swiftsuspenders.typedescriptions.MethodInjectionPoint;
@@ -12,10 +12,9 @@ package org.swiftsuspenders.typedescriptions
import flash.utils.Dictionary;
import org.swiftsuspenders.Injector;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorMissingMappingError;
import org.swiftsuspenders.dependencyproviders.DependencyProvider;
import org.swiftsuspenders.utils.SsInternal;
- import org.swiftsuspenders.errors.InjectorMissingMappingError;
public class MethodInjectionPoint extends InjectionPoint
{
@@ -11,10 +11,9 @@ package org.swiftsuspenders.typedescriptions
import flash.utils.getQualifiedClassName;
import org.swiftsuspenders.Injector;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorMissingMappingError;
import org.swiftsuspenders.dependencyproviders.DependencyProvider;
import org.swiftsuspenders.utils.SsInternal;
- import org.swiftsuspenders.errors.InjectorMissingMappingError;
public class PropertyInjectionPoint extends InjectionPoint
{
@@ -10,7 +10,7 @@ package org.swiftsuspenders.typedescriptions
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;
- import org.swiftsuspenders.InjectorError;
+ import org.swiftsuspenders.errors.InjectorError;
public class TypeDescription
{
@@ -15,12 +15,14 @@ package org.swiftsuspenders
import org.swiftsuspenders.support.injectees.childinjectors.ChildInjectorCreatingProvider;
import org.swiftsuspenders.support.injectees.childinjectors.InjectorInjectee;
import org.swiftsuspenders.support.injectees.childinjectors.LeftRobotFoot;
+ import org.swiftsuspenders.support.injectees.childinjectors.NestedInjectorInjectee;
import org.swiftsuspenders.support.injectees.childinjectors.RightRobotFoot;
import org.swiftsuspenders.support.injectees.childinjectors.RobotAnkle;
import org.swiftsuspenders.support.injectees.childinjectors.RobotBody;
import org.swiftsuspenders.support.injectees.childinjectors.RobotFoot;
import org.swiftsuspenders.support.injectees.childinjectors.RobotLeg;
import org.swiftsuspenders.support.injectees.childinjectors.RobotToes;
+ import org.swiftsuspenders.support.providers.ProviderThatCanDoInterfaces;
import org.swiftsuspenders.support.types.Clazz;
import org.swiftsuspenders.support.types.Interface;
import org.swiftsuspenders.utils.SsInternal;
@@ -186,29 +188,29 @@ package org.swiftsuspenders
[Test]
public function injectorCanCreateChildInjectorDuringInjection():void
{
- injector.fallbackProvider = ClassProvider;
injector.map(Injector).toProvider(new ChildInjectorCreatingProvider());
- injector.map(InjectorInjectee).toType(InjectorInjectee);
+ injector.map(InjectorInjectee);
+ injector.map(NestedInjectorInjectee);
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',
injectee.injector.parentInjector == injector);
Assert.assertTrue('injectorInjectee.nestedInjectee is grandchild of main injector',
- injectee.nestedInjectee.nestedInjectee.injector.parentInjector.parentInjector.parentInjector == injector);
+ injectee.nestedInjectee.injector.parentInjector.parentInjector == injector);
}
[Test]
public function satisfies_with_fallbackProvider_trickles_down_to_children():void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
const childInjector:Injector = injector.createChildInjector();
Assert.assertTrue(childInjector.satisfies(Clazz));
}
[Test]
public function getInstance_with_fallbackProvider_trickles_down_to_children():void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
const childInjector:Injector = injector.createChildInjector();
Assert.assertTrue(childInjector.getInstance(Clazz) != null);
}
@@ -99,7 +99,7 @@ package org.swiftsuspenders
provider.destroy();
assertThat(singleton, hasPropertyWithValue("preDestroyCalled", true));
}
- [Test(expects="org.swiftsuspenders.InjectorError")]
+ [Test(expects="org.swiftsuspenders.errors.InjectorError")]
public function usingDestroyedSingletonProviderThrows() : void
{
const provider : SingletonProvider = new SingletonProvider(Clazz, injector);
@@ -17,6 +17,7 @@ package org.swiftsuspenders
import org.hamcrest.object.notNullValue;
import org.swiftsuspenders.dependencyproviders.ClassProvider;
import org.swiftsuspenders.dependencyproviders.SingletonProvider;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.mapping.InjectionMapping;
import org.swiftsuspenders.support.types.Clazz;
import org.swiftsuspenders.support.types.Interface;
@@ -123,14 +124,14 @@ package org.swiftsuspenders
assertThat(testedMethods, hasProperties(methods));
}
- [Test(expects='org.swiftsuspenders.InjectorError')]
+ [Test(expects='org.swiftsuspenders.errors.InjectorError')]
public function unmappingASealedMappingThrows() : void
{
injector.map(Interface).seal();
injector.unmap(Interface);
}
- [Test(expects='org.swiftsuspenders.InjectorError')]
+ [Test(expects='org.swiftsuspenders.errors.InjectorError')]
public function doubleSealingAMappingThrows() : void
{
injector.map(Interface).seal();
@@ -144,14 +145,14 @@ package org.swiftsuspenders
assertThat(config.seal(), notNullValue());
}
- [Test(expects='org.swiftsuspenders.InjectorError')]
+ [Test(expects='org.swiftsuspenders.errors.InjectorError')]
public function unsealingAMappingWithoutKeyThrows() : void
{
injector.map(Interface).seal();
injector.map(Interface).unseal(null);
}
- [Test(expects='org.swiftsuspenders.InjectorError')]
+ [Test(expects='org.swiftsuspenders.errors.InjectorError')]
public function unsealingAMappingWithWrongKeyThrows() : void
{
injector.map(Interface).seal();
Oops, something went wrong.

0 comments on commit a9344e2

Please sign in to comment.