Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit a9344e245e84a7231967779774719c8a21513301 1 parent ec0db70
@tschneidereit authored
Showing with 191 additions and 284 deletions.
  1. +117 −158 src/org/swiftsuspenders/Injector.as
  2. +2 −7 src/org/swiftsuspenders/dependencyproviders/ClassProvider.as
  3. +19 −4 src/org/swiftsuspenders/dependencyproviders/FallbackDependencyProvider.as
  4. +1 −1  src/org/swiftsuspenders/dependencyproviders/SingletonProvider.as
  5. +1 −1  src/org/swiftsuspenders/{ → errors}/InjectorError.as
  6. +1 −3 src/org/swiftsuspenders/errors/InjectorInterfaceConstructionError.as
  7. +1 −3 src/org/swiftsuspenders/errors/InjectorMissingMappingError.as
  8. +11 −11 src/org/swiftsuspenders/mapping/InjectionMapping.as
  9. +1 −1  src/org/swiftsuspenders/reflection/DescribeTypeJSONReflector.as
  10. +1 −1  src/org/swiftsuspenders/reflection/DescribeTypeReflector.as
  11. +1 −2  src/org/swiftsuspenders/typedescriptions/MethodInjectionPoint.as
  12. +1 −2  src/org/swiftsuspenders/typedescriptions/PropertyInjectionPoint.as
  13. +1 −1  src/org/swiftsuspenders/typedescriptions/TypeDescription.as
  14. +7 −5 test/org/swiftsuspenders/ChildInjectorTests.as
  15. +1 −1  test/org/swiftsuspenders/DependencyProviderTests.as
  16. +5 −4 test/org/swiftsuspenders/InjectionMappingTests.as
  17. +8 −66 test/org/swiftsuspenders/InjectorTests.as
  18. +0 −7 test/org/swiftsuspenders/support/injectees/childinjectors/NestedInjectorInjectee.as
  19. +10 −3 test/org/swiftsuspenders/support/providers/MoodyProvider.as
  20. +2 −3 test/org/swiftsuspenders/support/providers/ProviderThatCanDoInterfaces.as
View
275 src/org/swiftsuspenders/Injector.as
@@ -16,8 +16,12 @@ package org.swiftsuspenders
import flash.utils.getQualifiedClassName;
import org.swiftsuspenders.dependencyproviders.DependencyProvider;
+ import org.swiftsuspenders.dependencyproviders.FallbackDependencyProvider;
import org.swiftsuspenders.dependencyproviders.LocalOnlyProvider;
import org.swiftsuspenders.dependencyproviders.SoftDependencyProvider;
+ import org.swiftsuspenders.errors.InjectorError;
+ import org.swiftsuspenders.errors.InjectorInterfaceConstructionError;
+ import org.swiftsuspenders.errors.InjectorMissingMappingError;
import org.swiftsuspenders.mapping.InjectionMapping;
import org.swiftsuspenders.mapping.MappingEvent;
import org.swiftsuspenders.reflection.DescribeTypeJSONReflector;
@@ -27,11 +31,8 @@ package org.swiftsuspenders
import org.swiftsuspenders.typedescriptions.InjectionPoint;
import org.swiftsuspenders.typedescriptions.PreDestroyInjectionPoint;
import org.swiftsuspenders.typedescriptions.TypeDescription;
- import org.swiftsuspenders.utils.TypeDescriptor;
import org.swiftsuspenders.utils.SsInternal;
- import org.swiftsuspenders.errors.InjectorInterfaceConstructionError;
- import org.swiftsuspenders.errors.InjectorMissingMappingError;
- import org.swiftsuspenders.dependencyproviders.FallbackDependencyProvider;
+ import org.swiftsuspenders.utils.TypeDescriptor;
use namespace SsInternal;
@@ -168,21 +169,30 @@ package org.swiftsuspenders
* injections are started.
*/
public class Injector extends EventDispatcher
- {
+ {
//---------------------- Private / Protected Properties ----------------------//
private static var INJECTION_POINTS_CACHE : Dictionary = new Dictionary(true);
-
+
private var _parentInjector : Injector;
private var _applicationDomain:ApplicationDomain;
private var _classDescriptor : TypeDescriptor;
private var _mappings : Dictionary;
private var _mappingsInProcess : Dictionary;
- private var _defaultProviders : Dictionary;
private var _managedObjects : Dictionary;
private var _reflector : Reflector;
-
- private static const _baseTypes:Array = [Array, Boolean, Class, Function, int, Number, Object, String, uint];
-
+ private var _fallbackProvider : FallbackDependencyProvider;
+ private var _blockParentFallbackProvider : Boolean = false;
+
+ private static const _baseTypes:Array = initBaseTypeMappingIds(
+ [Object, Array, Class, Function, Boolean, Number, int, uint, String]);
+
+ private static function initBaseTypeMappingIds(types : Array) : Array
+ {
+ return types.map(function(type : Class, index : uint, list : Array) : String
+ {
+ return getQualifiedClassName(type) + '|';
+ });
+ }
//---------------------- Internal Properties ----------------------//
@@ -194,7 +204,6 @@ package org.swiftsuspenders
{
_mappings = new Dictionary();
_mappingsInProcess = new Dictionary();
- _defaultProviders = new Dictionary();
_managedObjects = new Dictionary();
try
{
@@ -239,8 +248,8 @@ package org.swiftsuspenders
* @param type The <code>class</code> describing the mapping
* @param name The name, as a case-sensitive string, to further describe the mapping
*
- * @throws org.swiftsuspenders.InjectorError Descriptions that are not mapped can't be unmapped
- * @throws org.swiftsuspenders.InjectorError Sealed mappings have to be unsealed before unmapping them
+ * @throws org.swiftsuspenders.errors.InjectorError Descriptions that are not mapped can't be unmapped
+ * @throws org.swiftsuspenders.errors.InjectorError Sealed mappings have to be unsealed before unmapping them
*
* @see #map()
* @see org.swiftsuspenders.mapping.InjectionMapping
@@ -270,21 +279,15 @@ package org.swiftsuspenders
* Indicates whether the injector can supply a response for the specified dependency either
* by using a mapping of its own or by querying one of its ancestor injectors.
*
- * @param type The dependency under query
+ * @param type The type of the dependency under query
+ * @param name The name of the dependency under query
*
* @return <code>true</code> if the dependency can be satisfied, <code>false</code> if not
*/
public function satisfies(type : Class, name : String = '') : Boolean
{
- if(getProvider(getQualifiedClassName(type) + '|' + name) != null)
- {
- return true;
- }
- if(name.length > 0)
- {
- return false;
- }
- return getDefaultProvider(type, true) != null;
+ const mappingId : String = getQualifiedClassName(type) + '|' + name;
+ return getProvider(mappingId, true) != null;
}
/**
@@ -294,17 +297,15 @@ package org.swiftsuspenders
* <p>In contrast to <code>#satisfies()</code>, <code>satisfiesDirectly</code> only informs
* about mappings on this injector itself, without querying its ancestor injectors.</p>
*
- * @param type The dependency under query
+ * @param type The type of the dependency under query
+ * @param name The name of the dependency under query
*
* @return <code>true</code> if the dependency can be satisfied, <code>false</code> if not
*/
public function satisfiesDirectly(type : Class, name : String = '') : Boolean
{
- if (hasDirectMapping(type, name))
- {
- return true;
- }
- return (getDefaultProvider(type, false) != null);
+ return hasDirectMapping(type, name)
+ || getDefaultProvider(getQualifiedClassName(type) + '|' + name, false) != null;
}
/**
@@ -316,11 +317,13 @@ package org.swiftsuspenders
* This restriction is in place to prevent accidential changing of mappings in ancestor
* injectors where only the child's response is meant to be altered.</p>
*
- * @param type The dependency to return the mapping for
- *
+ * @param type The type of the dependency to return the mapping for
+ * @param name The name of the dependency to return the mapping for
+ *
* @return The mapping for the specified dependency class
*
- * @throws org.swiftsuspenders.InjectorError When no mapping was found for the specified dependency
+ * @throws org.swiftsuspenders.errors.InjectorMissingMappingError when no mapping was found
+ * for the specified dependency
*/
public function getMapping(type : Class, name : String = '') : InjectionMapping
{
@@ -328,8 +331,8 @@ package org.swiftsuspenders
var mapping : InjectionMapping = _mappings[mappingId];
if (!mapping)
{
- throw new InjectorMissingMappingError('Error while retrieving an injector mapping: ' +
- 'No mapping defined for dependency ' + mappingId);
+ throw new InjectorMissingMappingError('Error while retrieving an injector mapping: '
+ + 'No mapping defined for dependency ' + mappingId);
}
return mapping;
}
@@ -339,7 +342,8 @@ package org.swiftsuspenders
*
* @param target The instance to inject into
*
- * @throws org.swiftsuspenders.InjectorError The <code>Injector</code> must have mappings for all injection points
+ * @throws org.swiftsuspenders.errors.InjectorError The <code>Injector</code> must have mappings
+ * for all injection points
*
* @see #map()
*/
@@ -352,9 +356,6 @@ package org.swiftsuspenders
/**
* Instantiates the class identified by the given <code>type</code> and <code>name</code>.
*
- * <p>If no <code>InjectionMapping</code> is found for the given <code>type</code> and no
- * <code>name</code> is given, the class is simply instantiated and then injected into.</p>
- *
* <p>The parameter <code>targetType</code> is only useful if the
* <code>InjectionMapping</code> used to satisfy the request might vary its result based on
* that <code>targetType</code>. An Example of that would be a provider returning a logger
@@ -364,32 +365,70 @@ package org.swiftsuspenders
* @param name The name, as a case-sensitive string, to use for mapping resolution
* @param targetType The type of the instance that is dependent on the returned value
*
- * @return The created instance
+ * @return The mapped or created instance
+ *
+ * @throws org.swiftsuspenders.errors.InjectorMissingMappingError if no mapping was found
+ * for the specified dependency and no <code>fallbackProvider</code> is set.
*/
public function getInstance(type : Class, name : String = '', targetType : Class = null) : *
{
const mappingId : String = getQualifiedClassName(type) + '|' + name;
- var provider : DependencyProvider = getProvider(mappingId);
- if (!provider && (name.length == 0))
- {
- provider = getDefaultProvider(type);
- }
+ const provider : DependencyProvider =
+ getProvider(mappingId) || getDefaultProvider(mappingId, true);
if(provider)
{
const ctor : ConstructorInjectionPoint = _classDescriptor.getDescription(type).ctor;
return provider.apply(targetType, this, ctor ? ctor.injectParameters : null);
}
+
+ var fallbackMessage:String = _fallbackProvider
+ ? "the fallbackProvider, '" + _fallbackProvider + "', was unable to fulfill this request."
+ : "the injector has no fallbackProvider.";
- var fallbackMessage:String = " The injector has no fallbackProvider (not necessarily a bad thing).";
- if(_fallbackProvider)
+ throw new InjectorMissingMappingError('No mapping found for request ' + mappingId
+ + ' and ' + fallbackMessage);
+ }
+
+ /**
+ * Returns an instance of the given type. If the Injector has a mapping for the type, that
+ * is used for getting the instance. If not, a new instance of the class is created and
+ * injected into.
+ *
+ * @param type The type to get an instance of
+ * @return The instance that was created or retrieved from the mapped provider
+ *
+ * @throws org.swiftsuspenders.errors.InjectorMissingMappingError if no mapping is found
+ * for one of the type's dependencies and no <code>fallbackProvider</code> is set
+ * @throws org.swiftsuspenders.errors.InjectorInterfaceConstructionError if the given type
+ * is an interface and no mapping was found
+ */
+ public function getOrCreateNewInstance(type : Class) : *
+ {
+ return satisfies(type) && getInstance(type) || instantiateUnmapped(type);
+ }
+
+ /**
+ * Creates an instance of the given type and injects into it.
+ *
+ * @param type The type to instantiate
+ * @return The new instance, with all of its dependencies fulfilled
+ *
+ * @throws org.swiftsuspenders.errors.InjectorMissingMappingError if no mapping is found
+ * for one of the type's dependencies and no <code>fallbackProvider</code> is set
+ */
+ public function instantiateUnmapped(type : Class) : *
+ {
+ if(!canBeInstantiated(type))
{
- fallbackMessage = "The fallbackProvider, " + _fallbackProvider + " was unable to fulfill this request.";
+ throw new InjectorInterfaceConstructionError(
+ "Can't instantiate interface " + getQualifiedClassName(type));
}
-
- throw new InjectorMissingMappingError('No mapping found for request ' + mappingId
- + '. ' + fallbackMessage);
-
- return null;
+ const description : TypeDescription = _classDescriptor.getDescription(type);
+ const instance : * = description.ctor.createInstance(type, this);
+ hasEventListener(InjectionEvent.POST_INSTANTIATE) && dispatchEvent(
+ new InjectionEvent(InjectionEvent.POST_INSTANTIATE, instance, type));
+ applyInjectionPoints(instance, type, description);
+ return instance;
}
/**
@@ -438,8 +477,9 @@ package org.swiftsuspenders
}
_mappings = new Dictionary();
_mappingsInProcess = new Dictionary();
- _defaultProviders = new Dictionary();
_managedObjects = new Dictionary();
+ _fallbackProvider = null;
+ _blockParentFallbackProvider = false;
}
/**
@@ -519,84 +559,35 @@ package org.swiftsuspenders
{
return _reflector.describeInjections(type);
}
-
- // ADDED BY STRAY
-
- private var _fallbackProvider:Object;
-
- public function get fallbackProvider():Object
- {
- return _fallbackProvider;
- }
-
- public function set fallbackProvider(value:Object):void
- {
- if(value === _fallbackProvider)
- {
- return;
- }
- _defaultProviders = new Dictionary();
-
- if((value == null) || (value is FallbackDependencyProvider) || (value is Class && _reflector.typeImplements(value as Class, FallbackDependencyProvider)))
- {
- _fallbackProvider = value;
- }
- else
- {
- throw new InjectorError(
- "The fallbackProvider must be a Class or an"
- + " instance implementing FallbackDependencyProvider, but was " + value);
- }
- }
- public function instantiateUnmapped(type : Class) : *
+ public function hasMapping(type : Class, name : String = '') : Boolean
{
- if(! canBeInstantiated( type ))
- {
- throw new InjectorInterfaceConstructionError(
- "Can't instantiate interface " + getQualifiedClassName(type));
- }
- const description : TypeDescription = _classDescriptor.getDescription(type);
- const instance : * = description.ctor.createInstance(type, this);
- hasEventListener(InjectionEvent.POST_INSTANTIATE) && dispatchEvent(
- new InjectionEvent(InjectionEvent.POST_INSTANTIATE, instance, type));
- applyInjectionPoints(instance, type, description);
- return instance;
+ return getProvider(getQualifiedClassName(type) + '|' + name) != null;
}
- public function getOrCreateNewInstance(type : Class) : *
+ public function hasDirectMapping(type : Class, name : String = '') : Boolean
{
- if(satisfies(type))
- {
- return getInstance(type);
- }
-
- return instantiateUnmapped(type);
+ return _mappings[getQualifiedClassName(type) + '|' + name] != null;
}
-
- public function hasMapping(type : Class, name : String = ''):Boolean
+
+ public function get fallbackProvider() : FallbackDependencyProvider
{
- return getProvider(getQualifiedClassName(type) + '|' + name) != null;
+ return _fallbackProvider;
}
-
- public function hasDirectMapping(type : Class, name : String = ''):Boolean
+
+ public function set fallbackProvider(provider : FallbackDependencyProvider) : void
{
- return providerMappings[getQualifiedClassName(type) + '|' + name] != null;
+ _fallbackProvider = provider;
}
-
- private var _blockParentFallbackProvider:Boolean = false;
- public function get blockParentFallbackProvider():Boolean
+ public function get blockParentFallbackProvider() : Boolean
{
return _blockParentFallbackProvider;
}
- public function set blockParentFallbackProvider(value:Boolean):void
+ public function set blockParentFallbackProvider(value : Boolean) : void
{
- if (value !== _blockParentFallbackProvider)
- {
- _blockParentFallbackProvider = value;
- }
+ _blockParentFallbackProvider = value;
}
//---------------------- Internal Methods ----------------------//
@@ -611,7 +602,8 @@ package org.swiftsuspenders
return description.ctor != null;
}
- SsInternal function getProvider(mappingId : String) : DependencyProvider
+ SsInternal function getProvider(
+ mappingId : String, fallbackToDefault : Boolean = true) : DependencyProvider
{
var softProvider : DependencyProvider;
var injector : Injector = this;
@@ -640,59 +632,26 @@ package org.swiftsuspenders
{
return softProvider;
}
- return null;
+ return fallbackToDefault ? getDefaultProvider(mappingId, true) : null;
}
- SsInternal function getDefaultProvider(type : Class, consultParents : Boolean = true) : DependencyProvider
+ SsInternal function getDefaultProvider(
+ mappingId : String, consultParents : Boolean) : DependencyProvider
{
- if((!_fallbackProvider) && (_blockParentFallbackProvider || (!(consultParents && _parentInjector))))
- {
- return null;
- }
-
//No meaningful way to automatically create base types without names
- if(_baseTypes.indexOf(type) > -1)
+ if (_baseTypes.indexOf(mappingId) > -1)
{
return null;
}
-
- return getNearestDefaultProviderForType(type, consultParents);
- }
-
- SsInternal function getNearestDefaultProviderForType(type : Class, consultParents : Boolean = true) : DependencyProvider
- {
- if(!_fallbackProvider && !_parentInjector)
- {
- return null;
- }
-
- if(!_fallbackProvider && consultParents)
- {
- return _parentInjector.getNearestDefaultProviderForType( type );
- }
-
- if(_defaultProviders[type])
- {
- return _defaultProviders[type];
- }
-
- const provider:FallbackDependencyProvider = _fallbackProvider is Class
- ? new _fallbackProvider(type)
- : _fallbackProvider as FallbackDependencyProvider;
-
- const description : TypeDescription = _classDescriptor.getDescription(type);
- if(provider.satisfies(type, description))
+ if (_fallbackProvider && _fallbackProvider.prepareNextRequest(mappingId))
{
- _defaultProviders[type] = provider;
- return provider;
+ return _fallbackProvider;
}
-
- if(_parentInjector && consultParents)
- {
- return _parentInjector.getNearestDefaultProviderForType( type );
+ if (consultParents && !_blockParentFallbackProvider && _parentInjector)
+ {
+ return _parentInjector.getDefaultProvider(mappingId, consultParents);
}
-
return null;
}
View
9 src/org/swiftsuspenders/dependencyproviders/ClassProvider.as
@@ -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);
- }
}
}
View
23 src/org/swiftsuspenders/dependencyproviders/FallbackDependencyProvider.as
@@ -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;
}
-
}
View
2  src/org/swiftsuspenders/dependencyproviders/SingletonProvider.as
@@ -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;
View
2  src/org/swiftsuspenders/InjectorError.as → ...g/swiftsuspenders/errors/InjectorError.as
@@ -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
{
View
4 src/org/swiftsuspenders/errors/InjectorInterfaceConstructionError.as
@@ -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);
}
View
4 src/org/swiftsuspenders/errors/InjectorMissingMappingError.as
@@ -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);
}
View
22 src/org/swiftsuspenders/mapping/InjectionMapping.as
@@ -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()
*/
View
2  src/org/swiftsuspenders/reflection/DescribeTypeJSONReflector.as
@@ -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;
View
2  src/org/swiftsuspenders/reflection/DescribeTypeReflector.as
@@ -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;
View
3  src/org/swiftsuspenders/typedescriptions/MethodInjectionPoint.as
@@ -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
{
View
3  src/org/swiftsuspenders/typedescriptions/PropertyInjectionPoint.as
@@ -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
{
View
2  src/org/swiftsuspenders/typedescriptions/TypeDescription.as
@@ -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
{
View
12 test/org/swiftsuspenders/ChildInjectorTests.as
@@ -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,21 +188,21 @@ 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));
}
@@ -208,7 +210,7 @@ package org.swiftsuspenders
[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);
}
View
2  test/org/swiftsuspenders/DependencyProviderTests.as
@@ -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);
View
9 test/org/swiftsuspenders/InjectionMappingTests.as
@@ -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();
View
74 test/org/swiftsuspenders/InjectorTests.as
@@ -23,6 +23,7 @@ package org.swiftsuspenders
import org.hamcrest.object.isFalse;
import org.hamcrest.object.instanceOf;
import org.swiftsuspenders.dependencyproviders.OtherMappingProvider;
+ import org.swiftsuspenders.errors.InjectorError;
import org.swiftsuspenders.mapping.InjectionMapping;
import org.swiftsuspenders.mapping.MappingEvent;
import org.swiftsuspenders.support.injectees.ClassInjectee;
@@ -426,7 +427,7 @@ package org.swiftsuspenders
[Test]
public function use_fallbackProvider_for_unmapped_dependency_if_given():void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
const injectee : ClassInjectee = new ClassInjectee();
injector.injectInto(injectee);
assertThat(injectee.property, isA(Clazz));
@@ -552,18 +553,11 @@ package org.swiftsuspenders
Assert.assertFalse('injectee1.property is not the same instance as injectee2.property',
injectee1.property == injectee2.property);
}
-
- [Test(expects="org.swiftsuspenders.errors.InjectorMissingMappingError")]
- public function getInstance_on_unmapped_interface_with_fallback_ClassProvider_throws_InjectorMissingMappingError() : void
- {
- injector.fallbackProvider = ClassProvider;
- injector.getInstance(Interface);
- }
[Test(expects="org.swiftsuspenders.errors.InjectorInterfaceConstructionError")]
public function instantiateUnmapped_on_interface_throws_InjectorInterfaceConstructionError() : void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
injector.instantiateUnmapped(Interface);
}
@@ -618,7 +612,7 @@ package org.swiftsuspenders
Assert.assertNotNull(injector.getInstance(Interface));
}
- [Test(expects="org.swiftsuspenders.InjectorError")]
+ [Test(expects="org.swiftsuspenders.errors.InjectorError")]
public function local_mappings_arent_shared_with_child_injectors() : void
{
const childInjector : Injector = injector.createChildInjector();
@@ -945,14 +939,6 @@ package org.swiftsuspenders
}
[Test]
- public function set_and_get_fallbackProvider_by_class() : void
- {
- const provider : Class = ClassProvider;
- injector.fallbackProvider = provider;
- assertThat(injector.fallbackProvider, equalTo(provider));
- }
-
- [Test]
public function satisfies_isTrue_if_fallbackProvider_satisifies() : void
{
injector.fallbackProvider = new MoodyProvider(true);
@@ -966,61 +952,17 @@ package org.swiftsuspenders
assertThat(injector.satisfies(Clazz), isFalse());
}
- [Test(expects="org.swiftsuspenders.InjectorError")]
- public function error_thrown_if_fallbackProvider_instance_doesnt_implement_FallbackDependencyProvider() : void
- {
- injector.fallbackProvider = new FactoryProvider(Clazz);
- }
-
- [Test(expects="org.swiftsuspenders.InjectorError")]
- public function error_thrown_if_fallbackProvider_class_doesnt_implement_FallbackDependencyProvider() : void
- {
- injector.fallbackProvider = FactoryProvider;
- }
-
[Test]
- public function changing_the_fallbackProvider_cleans_defaultProvider_cache() : void
+ public function satisfies_returns_false_without_error_if_fallback_provider_cannot_satisfy_request() : void
{
- injector.fallbackProvider = ClassProvider;
- assertThat(injector.satisfies(Clazz), isTrue());
injector.fallbackProvider = new MoodyProvider(false);
- assertThat(injector.satisfies(Clazz), isFalse());
- }
-
- [Test]
- public function setting_the_fallbackProvider_to_null_cleans_defaultProvider_cache() : void
- {
- injector.fallbackProvider = ClassProvider;
- assertThat(injector.satisfies(Clazz), isTrue());
- injector.fallbackProvider = null;
- assertThat(injector.satisfies(Clazz), isFalse());
- }
-
- [Test]
- public function satisfies_returns_false_for_named_unmapped_dependency_with_class_defaultProvider() : void
- {
- injector.fallbackProvider = ClassProvider;
- assertThat(injector.satisfies(Clazz, "named"), isFalse());
- }
-
- [Test]
- public function satisfies_returns_false_for_named_unmapped_dependency_with_instance_defaultProvider() : void
- {
- injector.fallbackProvider = new MoodyProvider(true);
- assertThat(injector.satisfies(Clazz, "named"), isFalse());
- }
-
- [Test]
- public function satisfies_returns_false_without_error_if_interface_requested_from_ClassProvider() : void
- {
- injector.fallbackProvider = ClassProvider;
assertThat(injector.satisfies(Interface), isFalse());
}
[Test]
public function satisfies_returns_true_without_error_if_interface_requested_from_ProviderThatCanDoInterfaces() : void
{
- injector.fallbackProvider = ProviderThatCanDoInterfaces;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
assertThat(injector.satisfies(Interface), isTrue());
}
@@ -1125,7 +1067,7 @@ package org.swiftsuspenders
[Test]
public function satisfies_doesnt_use_fallbackProvider_from_ancestors_if_blockParentFallbackProvider_is_set() : void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
const childInjector:Injector = injector.createChildInjector();
childInjector.blockParentFallbackProvider = true;
assertThat(childInjector.satisfies(Clazz), isFalse());
@@ -1134,7 +1076,7 @@ package org.swiftsuspenders
[Test(expects="org.swiftsuspenders.errors.InjectorMissingMappingError")]
public function getInstance_doesnt_use_fallbackProvider_from_ancestors_if_blockParentFallbackProvider_is_set() : void
{
- injector.fallbackProvider = ClassProvider;
+ injector.fallbackProvider = new ProviderThatCanDoInterfaces(Clazz);
const childInjector:Injector = injector.createChildInjector();
childInjector.blockParentFallbackProvider = true;
childInjector.getInstance(Clazz);
View
7 test/org/swiftsuspenders/support/injectees/childinjectors/NestedInjectorInjectee.as
@@ -12,12 +12,5 @@ package org.swiftsuspenders.support.injectees.childinjectors
public class NestedInjectorInjectee
{
[Inject] public var injector : Injector;
- public var nestedInjectee : NestedNestedInjectorInjectee;
-
- [PostConstruct]
- public function createAnotherChildInjector() : void
- {
- nestedInjectee = injector.getInstance(NestedNestedInjectorInjectee);
- }
}
}
View
13 test/org/swiftsuspenders/support/providers/MoodyProvider.as
@@ -1,9 +1,16 @@
+/*
+ * 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.support.providers
{
- import org.swiftsuspenders.Injector;
import flash.utils.Dictionary;
+
+ import org.swiftsuspenders.Injector;
import org.swiftsuspenders.dependencyproviders.FallbackDependencyProvider;
- import org.swiftsuspenders.typedescriptions.TypeDescription;
public class MoodyProvider implements FallbackDependencyProvider
{
@@ -20,7 +27,7 @@ package org.swiftsuspenders.support.providers
// FallbackDependencyProvider Implementation
//---------------------------------------
- public function satisfies(type : Class, description:TypeDescription):Boolean
+ public function prepareNextRequest(mappingId : String) : Boolean
{
return _satisfies;
}
View
5 test/org/swiftsuspenders/support/providers/ProviderThatCanDoInterfaces.as
@@ -11,7 +11,6 @@ package org.swiftsuspenders.support.providers
import org.swiftsuspenders.Injector;
import org.swiftsuspenders.dependencyproviders.FallbackDependencyProvider;
- import org.swiftsuspenders.typedescriptions.TypeDescription;
public class ProviderThatCanDoInterfaces implements FallbackDependencyProvider
{
@@ -33,14 +32,14 @@ package org.swiftsuspenders.support.providers
public function apply(
targetType : Class, activeInjector : Injector, injectParameters : Dictionary) : Object
{
- return new Object();
+ return new _responseType();
}
public function destroy() : void
{
}
- public function satisfies(type:Class, description:TypeDescription): Boolean
+ public function prepareNextRequest(mappingId : String) : Boolean
{
return true;
}
Please sign in to comment.
Something went wrong with that request. Please try again.