public void NestedObjectCreationExceptionIsWrappedForOneTimeOnly() { // Arrange var requests = new[] { new object(), new object(), new object() }; var requestQueue = new Queue <object>(requests); var firstRequest = requestQueue.Dequeue(); var expectedInnerException = new ObjectCreationException(); var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => requestQueue.Count > 0 ? c.Resolve(requestQueue.Dequeue()) : throw expectedInnerException }; var sut = new TerminatingWithPathSpecimenBuilder(builder); // Cause sut to be executed recursively for multiple times. var context = new SpecimenContext(sut); // Act & assert var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(firstRequest, context)); Assert.Equal(expectedInnerException, actualEx.InnerException); }
/// <summary> /// Resolve a reference to another object in the factory. /// </summary> /// <param name="name"> /// The name of the object that is having the value of one of its properties resolved. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="argumentName"> /// The name of the property the value of which is being resolved. /// </param> /// <param name="reference"> /// The runtime reference containing the value of the property. /// </param> /// <returns>A reference to another object in the factory.</returns> protected virtual object ResolveReference(IObjectDefinition definition, string name, string argumentName, RuntimeObjectReference reference) { #region Instrumentation if (log.IsDebugEnabled) { log.Debug( string.Format(CultureInfo.InvariantCulture, "Resolving reference from property '{0}' in object '{1}' to object '{2}'.", argumentName, name, reference.ObjectName)); } #endregion try { if (reference.IsToParent) { if (null == objectFactory.ParentObjectFactory) { throw new ObjectCreationException(definition.ResourceDescription, name, string.Format( "Can't resolve reference to '{0}' in parent factory: " + "no parent factory available.", reference.ObjectName)); } return(objectFactory.ParentObjectFactory.GetObject(reference.ObjectName)); } return(objectFactory.GetObject(reference.ObjectName)); } catch (ObjectsException ex) { throw ObjectCreationException.GetObjectCreationException(ex, name, argumentName, definition.ResourceDescription, reference.ObjectName); } }
public void SutIsException() { // Arrange Type expectedBase = typeof(Exception); // Act var sut = new ObjectCreationException(); // Assert Assert.IsAssignableFrom(expectedBase, sut); }
public void MessageWillBeDefineWhenDefaultConstructorIsUsed() { // Arrange var sut = new ObjectCreationException(); // Act var result = sut.Message; // Assert Assert.NotNull(result); }
public void InnerExceptionWillMatchConstructorArgument() { // Arrange var expectedException = new ArgumentOutOfRangeException(); var sut = new ObjectCreationException("Anonymous message.", expectedException); // Act var result = sut.InnerException; // Assert Assert.Equal <Exception>(expectedException, result); }
public void MessageWillMatchConstructorArgument() { // Arrange string expectedMessage = "Anonymous exception message"; var sut = new ObjectCreationException(expectedMessage); // Act var result = sut.Message; // Assert Assert.Equal(expectedMessage, result); }
public void SutIsException() { // Fixture setup Type expectedBase = typeof(Exception); // Exercise system var sut = new ObjectCreationException(); // Verify outcome Assert.IsAssignableFrom(expectedBase, sut); // Teardown }
public void MessageWillBeDefineWhenDefaultConstructorIsUsed() { // Fixture setup var sut = new ObjectCreationException(); // Exercise system var result = sut.Message; // Verify outcome Assert.NotNull(result); // Teardown }
public void InnerExceptionWillMatchConstructorArgument() { // Fixture setup var expectedException = new ArgumentOutOfRangeException(); var sut = new ObjectCreationException("Anonymous message.", expectedException); // Exercise system var result = sut.InnerException; // Verify outcome Assert.Equal <Exception>(expectedException, result); // Teardown }
public void MessageWillMatchConstructorArgument() { // Fixture setup string expectedMessage = "Anonymous exception message"; var sut = new ObjectCreationException(expectedMessage); // Exercise system var result = sut.Message; // Verify outcome Assert.Equal <string>(expectedMessage, result); // Teardown }
public void ShouldWrapObjectCreationExceptionsFromInnerBuilders() { // Arrange var request = new object(); var exceptionToThrow = new ObjectCreationException("Creation failed."); var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => throw exceptionToThrow }; var sut = new TerminatingWithPathSpecimenBuilder(builder); var context = new DelegatingSpecimenContext(); // Act & assert var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context)); Assert.NotEqual(exceptionToThrow, actualEx); }
public void ShouldWrapObjectCreationExceptionsFromInnerBuilders() { // Fixture setup var request = new object(); var exceptionToThrow = new ObjectCreationException("Creation failed."); var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => throw exceptionToThrow }; var sut = new TerminatingWithPathSpecimenBuilder(builder); var context = new DelegatingSpecimenContext(); // Exercise system and verify outcome var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context)); Assert.NotEqual(exceptionToThrow, actualEx); // Teardown }
/// <summary> /// Resolves an inner object definition. /// </summary> /// <param name="name"> /// The name of the object that surrounds this inner object definition. /// </param> /// <param name="innerObjectName"> /// The name of the inner object definition... note: this is a synthetic /// name assigned by the factory (since it makes no sense for inner object /// definitions to have names). /// </param> /// <param name="argumentName"> /// The name of the property the value of which is being resolved. /// </param> /// <param name="definition"> /// The definition of the inner object that is to be resolved. /// </param> /// <param name="singletonOwner"> /// <see langword="true"/> if the owner of the property is a singleton. /// </param> /// <returns> /// The resolved object as defined by the inner object definition. /// </returns> protected virtual object ResolveInnerObjectDefinition(string name, string innerObjectName, string argumentName, IObjectDefinition definition, bool singletonOwner) { RootObjectDefinition mod = objectFactory.GetMergedObjectDefinition(innerObjectName, definition); // Check given bean name whether it is unique. If not already unique, // add counter - increasing the counter until the name is unique. String actualInnerObjectName = innerObjectName; if (mod.IsSingleton) { actualInnerObjectName = AdaptInnerObjectName(innerObjectName); } mod.IsSingleton = singletonOwner; object instance; object result; try { //SPRNET-986 ObjectUtils.EmptyObjects -> null instance = objectFactory.InstantiateObject(actualInnerObjectName, mod, null, false, false); result = objectFactory.GetObjectForInstance(instance, actualInnerObjectName, actualInnerObjectName, mod); } catch (ObjectsException ex) { throw ObjectCreationException.GetObjectCreationException(ex, name, argumentName, definition.ResourceDescription, innerObjectName); } if (singletonOwner && instance is IDisposable) { // keep a reference to the inner object instance, to be able to destroy // it on factory shutdown... objectFactory.DisposableInnerObjects.Add(instance); } return(result); }