public void AddProtocolMappingSilentlyOverwritesExistingProtocol() { ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(FileSystemResource)); // overwrite, must not complain... ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(AssemblyResource)); IResource res = new ConfigurableResourceLoader().GetResource("beep://Spring.Core.Tests/Spring/TestResource.txt"); Assert.IsNotNull(res, "Resource must not be null"); Assert.AreEqual(typeof(AssemblyResource), res.GetType(), "The original IResource Type associated with the 'beep' protocol " + "must have been overwritten; expecting an AssemblyResource 'cos " + "we registered it last under the 'beep' protocol."); }
/// <summary> /// Returns a <see cref="Spring.Core.IO.IResource"/> that has been /// mapped to the protocol of the supplied <paramref name="resourceName"/>. /// </summary> /// <param name="resourceName">The name of the resource.</param> /// <returns> /// A new <see cref="Spring.Core.IO.IResource"/> instance for the /// supplied <paramref name="resourceName"/>. /// </returns> /// <exception cref="System.UriFormatException"> /// If a <see cref="Spring.Core.IO.IResource"/> <see cref="System.Type"/> /// mapping does not exist for the supplied <paramref name="resourceName"/>. /// </exception> /// <exception cref="System.Exception"> /// In the case of any errors arising from the instantiation of the /// returned <see cref="Spring.Core.IO.IResource"/> instance. /// </exception> /// <seealso cref="ResourceHandlerRegistry.RegisterResourceHandler(string, Type)"/> public IResource GetResource(string resourceName) { string protocol = GetProtocol(resourceName); if (protocol == null) { protocol = DefaultResourceProtocol; resourceName = protocol + ProtocolSeparator + resourceName; } IDynamicConstructor handler = ResourceHandlerRegistry.GetResourceHandler(protocol); if (handler == null) { throw new UriFormatException("Resource handler for the '" + protocol + "' protocol is not defined."); } return((IResource)handler.Invoke(new object[] { resourceName })); }
/// <summary> /// Checks that the supplied <paramref name="resourceName"/> starts /// with one of the protocol names currently mapped by this /// <see cref="ConfigurableResourceLoader"/> instance. /// </summary> /// <param name="resourceName">The name of the resource.</param> /// <returns> /// <see langword="true"/> if the supplied /// <paramref name="resourceName"/> starts with one of the known /// protocols; <see langword="false"/> if not, or if the supplied /// <paramref name="resourceName"/> is itself <see langword="null"/>. /// </returns> public static bool HasProtocol(string resourceName) { string protocol = GetProtocol(resourceName); return(protocol != null && ResourceHandlerRegistry.IsHandlerRegistered(protocol)); }
public void WithIResourceHandlerTypeWithNoValidCtor() { Assert.Throws <ArgumentException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(IncompatibleResource))); }
public void WithWhitespacedProtocolName() { Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler("\t ", GetType())); }
public void WithNonIResourceHandlerType() { Assert.Throws <ArgumentException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", GetType())); }
public void WithNullProtocolName() { Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler(null, GetType())); }
public void WithNullIResourceHandlerType() { Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", (Type)null)); }
public void WithIResourceHandlerTypeWithNoValidCtor() { ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(IncompatibleResource)); }
public void WithNonIResourceHandlerType() { ResourceHandlerRegistry.RegisterResourceHandler("beep", GetType()); }
public void WithWhitespacedProtocolName() { ResourceHandlerRegistry.RegisterResourceHandler("\t ", GetType()); }
public void WithNullIResourceHandlerType() { ResourceHandlerRegistry.RegisterResourceHandler("beep", (Type)null); }
public void WithNullProtocolName() { ResourceHandlerRegistry.RegisterResourceHandler(null, GetType()); }