public void RES_failingVerificationWithCyclicDependencyException() { if ((GetComponentAdapterNature() & RESOLVING) > 0) { Hashtable cycleInstances = new Hashtable(); IObjectReference cycleCheck = new SimpleReference(); object[] wrapperDependencies = new Object[] { cycleInstances, cycleCheck }; IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory()); IComponentAdapter componentAdapter = prepRES_failingVerificationWithCyclicDependencyException(picoContainer); Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType()); Assert.IsTrue(picoContainer.ComponentAdapters.Contains(componentAdapter)); IPicoContainer wrappedPicoContainer = WrapComponentInstances(typeof(CycleDetectorComponentAdapter), picoContainer, wrapperDependencies); try { componentAdapter.Verify(wrappedPicoContainer); Assert.Fail("Thrown PicoVerificationException excpected"); } catch (CyclicDependencyException cycle) { object[] dependencies = cycle.Dependencies; Assert.AreSame(dependencies[0], dependencies[dependencies.Length - 1]); } } }
public CycleDetectorComponentAdapter(IComponentAdapter theDelegate, Hashtable set, IObjectReference reference) : base(theDelegate) { this.set = set; this.reference = reference; }
public void AddOrderedComponentAdapter(IComponentAdapter componentAdapter) { if (!orderedComponentAdapters.Contains(componentAdapter)) { orderedComponentAdapters.Add(componentAdapter); } }
/// <summary> /// Resolve the parameter for the expected type. The method will return <code>null</code> /// If the expected type is not one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns>the instance of the collection type or <code>null</code></returns> public Object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = null; Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { IDictionary dictionary = GetMatchingComponentAdapters(container, adapter, componentKeyType, GetValueType(expectedType)); if (typeof (Array).IsAssignableFrom(collectionType)) { result = GetArrayInstance(container, expectedType, dictionary); } else if (typeof (IDictionary).IsAssignableFrom(collectionType)) { result = GetDictionaryInstance(container, expectedType, dictionary); } else if (typeof (ICollection).IsAssignableFrom(collectionType)) { result = GetCollectionInstance(container, expectedType, dictionary); } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } } return result; }
public void SER_isSerializable() { if ((GetComponentAdapterNature() & SERIALIZABLE) > 0) { IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory()); IComponentAdapter componentAdapter = prepSER_isSerializable(picoContainer); Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType()); Object instance = componentAdapter.GetComponentInstance(picoContainer); Assert.IsNotNull(instance); IComponentAdapter serializedComponentAdapter = null; using (Stream stream = new MemoryStream()) { // Serialize it to memory IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, componentAdapter); // De-Serialize from memory stream.Seek(0, 0); // reset stream to begining serializedComponentAdapter = (IComponentAdapter)formatter.Deserialize(stream); } Assert.AreEqual(componentAdapter.ComponentKey, serializedComponentAdapter.ComponentKey); Object instanceAfterSerialization = serializedComponentAdapter.GetComponentInstance(picoContainer); Assert.IsNotNull(instanceAfterSerialization); Assert.AreSame(instance.GetType(), instanceAfterSerialization.GetType()); } }
/// <summary> /// Resolve the parameter for the expected type. The method will return <code>null</code> /// If the expected type is not one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns>the instance of the collection type or <code>null</code></returns> public Object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = null; Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { IDictionary dictionary = GetMatchingComponentAdapters(container, adapter, componentKeyType, GetValueType(expectedType)); if (typeof(Array).IsAssignableFrom(collectionType)) { result = GetArrayInstance(container, expectedType, dictionary); } else if (typeof(IDictionary).IsAssignableFrom(collectionType)) { result = GetDictionaryInstance(container, expectedType, dictionary); } else if (typeof(ICollection).IsAssignableFrom(collectionType)) { result = GetCollectionInstance(container, expectedType, dictionary); } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } } return(result); }
/// <summary> /// Verify a successful dependency resolution of the parameter for the expected type. The /// method will only return if the expected type is one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> public void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { Type valueType = GetValueType(expectedType); ICollection componentAdapters = GetMatchingComponentAdapters(container, adapter, componentKeyType, valueType).Values; if (componentAdapters.Count == 0) { if (!emptyCollection) { throw new PicoIntrospectionException(expectedType.FullName + " not resolvable, no components of type " + GetValueType(expectedType).FullName + " available"); } } else { foreach (IComponentAdapter componentAdapter in componentAdapters) { componentAdapter.Verify(container); } } } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } return; }
public void RegisterComponent() { IComponentAdapter componentSpecification = CreateComponentAdapter(); picoContainer.RegisterComponent(componentSpecification); Assert.IsTrue(picoContainer.ComponentAdapters.Contains(componentSpecification)); }
public void UnregisterComponent() { IComponentAdapter componentSpecification = CreateComponentAdapter(); picoContainer.RegisterComponent(componentSpecification); picoContainer.UnregisterComponent(typeof(ITouchable)); Assert.IsFalse(picoContainer.ComponentAdapters.Contains(componentSpecification)); }
public void TestEquals() { IComponentAdapter componentAdapter = CreateComponentAdapterFactory().CreateComponentAdapter(typeof(ITouchable), typeof(SimpleTouchable), null); Assert.AreEqual(componentAdapter, componentAdapter); }
public void SingleUseComponentCanBeInstantiatedByDefaultIComponentAdapter() { IComponentAdapter componentAdapter = CreateComponentAdapterFactory() .CreateComponentAdapter("o", typeof(object), null); Object component = componentAdapter.GetComponentInstance(null); Assert.IsNotNull(component); }
public void DEF_verifyWithoutDependencyWorks() { IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory()); IComponentAdapter componentAdapter = prepDEF_verifyWithoutDependencyWorks(picoContainer); Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType()); componentAdapter.Verify(picoContainer); }
public void ComponentParameterRespectsExpectedType() { IMutablePicoContainer picoContainer = new DefaultPicoContainer(); IComponentAdapter adapter = picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable)); Assert.IsNull( ComponentParameter.DEFAULT.ResolveInstance(picoContainer, adapter, typeof(TestFixtureAttribute))); }
public void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { if (!expectedType.IsInstanceOfType(constantValue)) { throw new PicoIntrospectionException(expectedType.FullName + " is not assignable from " + constantValue.GetType().FullName); } }
public void ConstantParameterRespectsExpectedType() { IMutablePicoContainer picoContainer = new DefaultPicoContainer(); IParameter parameter = new ConstantParameter(new SimpleTouchable()); IComponentAdapter adapter = picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable)); Assert.IsFalse(parameter.IsResolvable(picoContainer, adapter, typeof(TestFixtureAttribute))); }
public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter != null) { return container.GetComponentInstance(componentAdapter.ComponentKey); } return null; }
public void InstantiateComponentWithNoDependencies() { IComponentAdapter componentAdapter = CreateComponentAdapterFactory() .CreateComponentAdapter(typeof(ITouchable), typeof(SimpleTouchable), null); Object comp = componentAdapter.GetComponentInstance(null); Assert.IsNotNull(comp); Assert.IsTrue(comp is SimpleTouchable); }
public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter != null) { return(container.GetComponentInstance(componentAdapter.ComponentKey)); } return(null); }
/// <summary> /// Check for a successful dependency resolution of the parameter for the expected type. The /// dependency can only be satisfied if the expected type is one of the collection types /// {@link Array},{@link Collection}or {@link Map}. An empty collection is only a valid /// resolution, if the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns><code>true</code> if matching components were found or an empty collective type is allowed</returns> public bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { Type collectionType = GetCollectionType(expectedType); Type valueType = GetValueType(expectedType); return (collectionType != null && (emptyCollection || GetMatchingComponentAdapters(container, adapter, componentKeyType, valueType).Count > 0)); }
public void UnRegisterComponent() { IComponentAdapter componentAdapter = CreateComponentAdapterFactory().CreateComponentAdapter(typeof(ITouchable), typeof(SimpleTouchable), null); picoContainer.RegisterComponent(componentAdapter); Assert.IsNotNull(picoContainer.UnregisterComponent(typeof(ITouchable))); Assert.IsFalse(picoContainer.ComponentAdapters.Contains(componentAdapter)); }
public IComponentAdapter RegisterComponentImplementation(object componentKey, Type componentImplementation, IParameter[] parameters) { IComponentAdapter componentAdapter = componentAdapterFactory.CreateComponentAdapter(componentKey, componentImplementation, parameters); RegisterComponent(componentAdapter); return(componentAdapter); }
public virtual void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter == null) { throw new PicoIntrospectionException(expectedType.Name + " is not resolvable"); } componentAdapter.Verify(container); }
public void CannotInstantiateAnUnregisteredComponent() { IComponentAdapter componentAdapter = CreateComponentAdapter(); picoContainer.RegisterComponent(componentAdapter); object o = picoContainer.ComponentInstances; picoContainer.UnregisterComponent(typeof(ITouchable)); Assert.IsTrue(picoContainer.ComponentInstances.Count == 0); }
public IComponentAdapter GetComponentAdapter(object componentKey) { IComponentAdapter adapter = (IComponentAdapter)componentKeyToAdapterMap[componentKey]; if (adapter == null && parent != null) { adapter = parent.GetComponentAdapter(componentKey); } return(adapter); }
private IComponentAdapter ResolveAdapter(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter result = GetTargetAdapter(container, expectedType, adapter); if (result != null && !expectedType.IsAssignableFrom(result.ComponentImplementation)) { return(null); } return(result); }
public void UnregisterAfterInstantiateComponents() { IComponentAdapter componentAdapter = CreateComponentAdapter(); picoContainer.RegisterComponent(componentAdapter); object o = picoContainer.ComponentInstances; picoContainer.UnregisterComponent(typeof(ITouchable)); Assert.IsNull(picoContainer.GetComponentInstance(typeof(ITouchable))); }
public override object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = base.ResolveInstance(container, adapter, expectedType); if (result == null && collectionParameter != null) { result = collectionParameter.ResolveInstance(container, adapter, expectedType); } return(result); }
private IComponentAdapter ResolveAdapter(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter result = GetTargetAdapter(container, expectedType, adapter); if (result != null && !expectedType.IsAssignableFrom(result.ComponentImplementation)) { return null; } return result; }
public virtual bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { try { Verify(container, adapter, expectedType); return(true); } catch (PicoIntrospectionException) { return(false); } }
public void ComponentParameterExcludesSelf() { DefaultPicoContainer pico = new DefaultPicoContainer(); IComponentAdapter adapter = pico.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable)); Assert.IsNotNull(pico.GetComponentInstance(typeof(ITouchable))); ITouchable touchable = (ITouchable)ComponentParameter.DEFAULT.ResolveInstance(pico, adapter, typeof(ITouchable)); Assert.IsNull(touchable); }
public virtual bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { try { Verify(container, adapter, expectedType); return true; } catch (PicoIntrospectionException) { return false; } }
public override bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { if (!base.IsResolvable(container, adapter, expectedType)) { if (collectionParameter != null) { return(collectionParameter.IsResolvable(container, adapter, expectedType)); } return(false); } return(true); }
public void CanInstantiateReplacedComponent() { IComponentAdapter componentAdapter = CreateComponentAdapter(); picoContainer.RegisterComponent(componentAdapter); object o = picoContainer.ComponentInstances; picoContainer.UnregisterComponent(typeof(ITouchable)); picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(AlternativeTouchable)); Assert.AreEqual(1, picoContainer.ComponentInstances.Count, "Container should container 1 component"); }
public void DEF_verifyDoesNotInstantiate() { IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory()); IComponentAdapter componentAdapter = prepDEF_verifyDoesNotInstantiate(picoContainer); Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType()); IComponentAdapter notInstantiatablecomponentAdapter = new NotInstantiatableComponentAdapter(componentAdapter); IPicoContainer wrappedPicoContainer = WrapComponentInstances(typeof(NotInstantiatableComponentAdapter), picoContainer, null); notInstantiatablecomponentAdapter.Verify(wrappedPicoContainer); }
public void ShouldInstantiateArrayOfStrings() { CollectionComponentParameter ccp = new CollectionComponentParameter(); Mock componentAdapterMock = new DynamicMock(typeof (IComponentAdapter)); componentAdapterMock.ExpectAndReturn("ComponentKey", "x", null); componentAdapterMock.ExpectAndReturn("ComponentKey", "x", null); Mock containerMock = new DynamicMock(typeof (IPicoContainer)); containerMock.ExpectAndReturn("ComponentAdapters", new Hashtable(), null); IComponentAdapter[] adapters = new IComponentAdapter[] { new InstanceComponentAdapter("y", "Hello"), new InstanceComponentAdapter("z", "World") }; containerMock.ExpectAndReturn("GetComponentAdaptersOfType", adapters, new IsEqual(typeof (string))); containerMock.ExpectAndReturn("GetComponentInstance", "World", new IsEqual("z")); containerMock.ExpectAndReturn("GetComponentInstance", "Hello", new IsEqual("y")); containerMock.ExpectAndReturn("Parent", null, null); ArrayList expected = new ArrayList(new string[] {"Hello", "World"}); expected.Sort(); object[] array = (object[]) ccp.ResolveInstance((IPicoContainer) containerMock.MockInstance, (IComponentAdapter) componentAdapterMock.MockInstance, typeof (string[])); ArrayList actual = new ArrayList(array); actual.Sort(); Assert.AreEqual(expected.ToArray(), actual.ToArray()); // Verify mocks componentAdapterMock.Verify(); containerMock.Verify(); }
public override bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { if (!base.IsResolvable(container, adapter, expectedType)) { if (collectionParameter != null) { return collectionParameter.IsResolvable(container, adapter, expectedType); } return false; } return true; }
public override object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = base.ResolveInstance(container, adapter, expectedType); if (result == null && collectionParameter != null) { result = collectionParameter.ResolveInstance(container, adapter, expectedType); } return result; }
public virtual IComponentAdapter RegisterComponent(IComponentAdapter IComponentAdapter) { return delegateContainer.RegisterComponent(IComponentAdapter); }
public virtual void AddOrderedComponentAdapter(IComponentAdapter componentAdapter) { delegateContainer.AddOrderedComponentAdapter(componentAdapter); }
public NotInstantiatableComponentAdapter(IComponentAdapter componentAdapter) : base(componentAdapter) { }
/// <summary> /// Constructor /// </summary> /// <param name="theDelegate">The component adapter to decorate</param> public CachingComponentAdapter(IComponentAdapter theDelegate) : this(theDelegate, new SimpleReference()) { }
/// <summary> /// Evaluate whether the given component adapter will be part of the collective type. /// </summary> /// <param name="adapter">a <code>ComponentAdapter</code> value</param> /// <returns><code>true</code> if the adapter takes part</returns> protected virtual bool Evaluate(IComponentAdapter adapter) { return adapter != null; // use parameter, prevent compiler warning }
public ImplementationHidingComponentAdapter(IComponentAdapter theDelegate) : base(theDelegate) { }
/** * Check wether the given Parameter can be statisfied by the container. * * @return <code>true</code> if the Parameter can be verified. * @see org.picocontainer.Parameter#isResolvable(org.picocontainer.PicoContainer, * org.picocontainer.ComponentAdapter, java.lang.Class) */ public virtual bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { return ResolveAdapter(container, adapter, expectedType) != null; }
public SynchronizedComponentAdapter(IComponentAdapter theDelegate) : base(theDelegate) { }
/// <summary> /// Constructor /// </summary> /// <param name="theDelegate">The component adapter to decorate</param> /// <param name="reference">Object to store the instance in. See <see cref="IObjectReference"/> for an explanation.</param> public CachingComponentAdapter(IComponentAdapter theDelegate, SimpleReference reference) : base(theDelegate) { instanceReference = reference; }
public override void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { try { base.Verify(container, adapter, expectedType); } catch (UnsatisfiableDependenciesException e) { if (collectionParameter != null) { collectionParameter.Verify(container, adapter, expectedType); return; } throw e; } }
/// <summary> /// Collect the matching ComponentAdapter instances. /// </summary> /// <param name="container">container container to use for dependency resolution</param> /// <param name="adapter">ComponentAdapter to exclude</param> /// <param name="keyType">the compatible type of the key</param> /// <param name="valueType">the compatible type of the component</param> /// <returns>IDictionary with the ComponentAdapter instances and their component keys as map key.</returns> protected IDictionary GetMatchingComponentAdapters(IPicoContainer container, IComponentAdapter adapter, Type keyType, Type valueType) { IDictionary adapterMap = new Hashtable(); IPicoContainer parent = container.Parent; if (parent != null) { IDictionary matchingComponentAdapters = GetMatchingComponentAdapters(parent, adapter, keyType, valueType); foreach (DictionaryEntry entry in matchingComponentAdapters) { adapterMap.Add(entry.Key, entry.Value); } } ICollection allAdapters = container.ComponentAdapters; foreach (IComponentAdapter componentAdapter in allAdapters) { adapterMap.Remove(componentAdapter.ComponentKey); } IList adapterList = container.GetComponentAdaptersOfType(valueType); foreach (IComponentAdapter componentAdapter in adapterList) { object key = componentAdapter.ComponentKey; if (adapter != null && key.Equals(adapter.ComponentKey)) { continue; } if (keyType.IsAssignableFrom(key.GetType()) && Evaluate(componentAdapter)) { adapterMap.Add(key, componentAdapter); } } return adapterMap; }
protected override bool Evaluate(IComponentAdapter componentAdapter) { return !"Tom".Equals(componentAdapter.ComponentKey); }
public CollectingComponentAdapter(IComponentAdapter theDelegate, IList list) : base(theDelegate) { this.list = list; }
private IComponentAdapter GetTargetAdapter(IPicoContainer container, Type expectedType, IComponentAdapter excludeAdapter) { if (componentKey != null) { // key tells us where to look so we follow return container.GetComponentAdapter(componentKey); } else if (excludeAdapter == null) { return container.GetComponentAdapterOfType(expectedType); } else { Object excludeKey = excludeAdapter.ComponentKey; IComponentAdapter byKey = container.GetComponentAdapter(expectedType); if (byKey != null) { if (byKey.ComponentKey.Equals(excludeKey)) { return null; } return byKey; } IList found = container.GetComponentAdaptersOfType(expectedType); IComponentAdapter exclude = null; foreach (IComponentAdapter work in found) { if (work.ComponentKey.Equals(excludeKey)) { exclude = work; } } found.Remove(exclude); if (found.Count == 0) { if (container.Parent != null) { return container.Parent.GetComponentAdapterOfType(expectedType); } else { return null; } } else if (found.Count == 1) { return (IComponentAdapter) found[0]; } else { Type[] foundTypes = new Type[found.Count]; for (int i = 0; i < foundTypes.Length; i++) { foundTypes[i] = ((IComponentAdapter) found[i]).ComponentImplementation; } throw new AmbiguousComponentResolutionException(expectedType, foundTypes); } } }
public UnsatisfiableDependenciesException(IComponentAdapter instantiatingComponentAdapter, IList failedDependencies) { this.instantiatingComponentAdapter = instantiatingComponentAdapter; this.failedDependencies = failedDependencies; }
public IComponentAdapter RegisterComponent(IComponentAdapter componentAdapter) { object componentKey = componentAdapter.ComponentKey; if (componentKeyToAdapterMap.Contains(componentKey)) { throw new DuplicateComponentKeyRegistrationException(componentKey); } componentAdapter.Container = this; componentAdapters.Add(componentAdapter); componentKeyToAdapterMap.Add(componentKey, componentAdapter); return componentAdapter; }
private object FindInstance(IComponentAdapter componentAdapter) { // check wether this is our adapter // we need to check this to ensure up-down dependencies cannot be followed bool isLocal = componentAdapters.Contains(componentAdapter); if (isLocal) { Object instance = componentAdapter.GetComponentInstance(this); AddOrderedComponentAdapter(componentAdapter); return instance; } else if (parent != null) { return parent.GetComponentInstance(componentAdapter.ComponentKey); } // TODO: decide .. exception or null? // exceptrion: mx: +1, joehni +1 return null; }
public ImplementationHidingComponentAdapter(IComponentAdapter theDelegate, bool strict) : base(theDelegate) { this.strict = strict; }