Beispiel #1
0
        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.");
        }
Beispiel #2
0
        /// <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 }));
        }
Beispiel #3
0
        /// <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));
        }
Beispiel #4
0
 public void WithIResourceHandlerTypeWithNoValidCtor()
 {
     Assert.Throws <ArgumentException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(IncompatibleResource)));
 }
Beispiel #5
0
 public void WithWhitespacedProtocolName()
 {
     Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler("\t   ", GetType()));
 }
Beispiel #6
0
 public void WithNonIResourceHandlerType()
 {
     Assert.Throws <ArgumentException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", GetType()));
 }
Beispiel #7
0
 public void WithNullProtocolName()
 {
     Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler(null, GetType()));
 }
Beispiel #8
0
 public void WithNullIResourceHandlerType()
 {
     Assert.Throws <ArgumentNullException>(() => ResourceHandlerRegistry.RegisterResourceHandler("beep", (Type)null));
 }
Beispiel #9
0
 public void WithIResourceHandlerTypeWithNoValidCtor()
 {
     ResourceHandlerRegistry.RegisterResourceHandler("beep", typeof(IncompatibleResource));
 }
Beispiel #10
0
 public void WithNonIResourceHandlerType()
 {
     ResourceHandlerRegistry.RegisterResourceHandler("beep", GetType());
 }
Beispiel #11
0
 public void WithWhitespacedProtocolName()
 {
     ResourceHandlerRegistry.RegisterResourceHandler("\t   ", GetType());
 }
Beispiel #12
0
 public void WithNullIResourceHandlerType()
 {
     ResourceHandlerRegistry.RegisterResourceHandler("beep", (Type)null);
 }
Beispiel #13
0
 public void WithNullProtocolName()
 {
     ResourceHandlerRegistry.RegisterResourceHandler(null, GetType());
 }