public void Resolve_with_protocol_and_special_home_character()
        {
            FileInfo fileInfo = CreateFileForTheCurrentDirectory();

            CustomUriBuilder builder = new CustomUriBuilder("file://~/" + TemporaryFileName, null);

            Assert.AreEqual(fileInfo.FullName, builder.Uri.LocalPath,
                "The file name with file://~ must have resolved to a file " +
                "in the current directory of the currently executing domain.");
        }
        public void Resolve_with_protocol_and_special_home_character_in_parent_directory()
        {
            FileInfo file = new FileInfo(Path.GetFullPath(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TemporaryFileName)));

            CustomUriBuilder builder = new CustomUriBuilder("file://~/../" + TemporaryFileName, string.Empty);

            string fileNameOneDirectoryUp = file.Directory.Parent.FullName + "\\" + TemporaryFileName;
            Assert.AreEqual(fileNameOneDirectoryUp, builder.Uri.LocalPath,
                "The file name with file://~/.. must have resolved to a file " +
                "in the parent directory of the currently executing domain.");
        }
        public void Url_should_raise_exception_FileNotFoundException_on_FileInfo_property()
        {
            string uri = "http://www.apache.org/";

            CustomUriBuilder builder = new CustomUriBuilder(uri, AppDomain.CurrentDomain.BaseDirectory);

            using (IResource resource = loader.Create(builder.Uri))
            {
                Assert.IsNotNull(resource);

                Assert.Throws<ResourceException>(delegate { var file = resource.FileInfo; });
            }
        }
        public void Test_resource_creation_with_absolute_path()
        {
            string file = "file://"+Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\Fixtures\Utilities\sample.txt");

            CustomUriBuilder builder = new CustomUriBuilder(file, AppDomain.CurrentDomain.BaseDirectory);

             using (IResource resource = loader.Create(builder.Uri))
             {
                Assert.IsNotNull(resource);

                using (TextReader reader = new StreamReader(resource.Stream))
                {
                    string line = reader.ReadLine();
                    Assert.That(line, Is.EqualTo("hello"));
                }
             }
        }
        public void Test_resource_creation_in_same_base_directory_with_Relative()
        {
            string file = "SqlMap_MSSQL_SqlClient.config";

            CustomUriBuilder builder = new CustomUriBuilder(file, AppDomain.CurrentDomain.BaseDirectory);

            using (IResource resource = loader.Create(builder.Uri))
            {
                Assert.IsNotNull(resource);

                using (TextReader reader = new StreamReader(resource.Stream))
                {
                    string line = reader.ReadLine();
                    Assert.That(line, Is.EqualTo(@"<?xml version=""1.0"" encoding=""utf-8""?>"));
                }
            }
        }
        public void Test_resource_creation_with_absolute_path_and_slash()
        {
            string file = "file:///" + MyBatis.Common.Resources.Resources.ApplicationBase + Path.DirectorySeparatorChar + "SqlMap_MSSQL_SqlClient.config";

            CustomUriBuilder builder = new CustomUriBuilder(file, AppDomain.CurrentDomain.BaseDirectory);

            using (IResource resource = loader.Create(builder.Uri))
            {
                Assert.IsNotNull(resource);

                using (TextReader reader = new StreamReader(resource.Stream))
                {
                    string line = reader.ReadLine();
                    Assert.That(line, Is.EqualTo(@"<?xml version=""1.0"" encoding=""utf-8""?>"));
                }
            }
        }
        /// <summary>
        /// Return an <see cref="IResource"/> for the
        /// specified string address.
        /// </summary>
        /// <param name="resource">The string adress.</param>
        /// <returns>
        /// An appropriate <see cref="IResource"/>.
        /// </returns>
        public static IResource GetResource(string resource)
        {
            Contract.Require.That(resource, Is.Not.Null & Is.Not.Empty).When("retrieving resource argument in GetResource method");

            CustomUriBuilder builder = new CustomUriBuilder(resource, Resources.DefaultBasePath);

            IResourceLoader loader = resourceLoaders[builder.Uri.Scheme];
            IResource       res    = loader.Create(builder.Uri);

            if (loader is FileResourceLoader)
            {
                FileResourceLoadEventArgs evnt = new FileResourceLoadEventArgs();
                evnt.FileInfo = res.FileInfo;

                LoadFileResource(loader, evnt);
            }
            return(res);
        }
        /// <summary>
        /// Return an <see cref="IResource"/> for the
        /// specified string address.
        /// </summary>
        /// <param name="resource">The string adress.</param>
        /// <returns>
        /// An appropriate <see cref="IResource"/>.
        /// </returns>
        public static IResource GetResource(string resource)
        {
            Contract.Require.That(resource, Is.Not.Null & Is.Not.Empty).When("retrieving resource argument in GetResource method");

            CustomUriBuilder builder = new CustomUriBuilder(resource, Resources.DefaultBasePath);

            IResourceLoader loader = resourceLoaders[builder.Uri.Scheme];
            IResource res = loader.Create(builder.Uri);

            if (loader is FileResourceLoader)
            {
                FileResourceLoadEventArgs evnt = new FileResourceLoadEventArgs();
                evnt.FileInfo = res.FileInfo;

                LoadFileResource(loader, evnt);
            }
            return res;
        }
        public void Non_existing_resource_should_raise_ResourceException()
        {
            string file = "/Something/file1.txt";

            Assert.Throws<ResourceException>(delegate { CustomUriBuilder builder = new CustomUriBuilder(file, AppDomain.CurrentDomain.BaseDirectory); });
        }
        public void Test_resource_creation_with_special_uri_character()
        {
            string path = Path.GetFullPath("dir#");
            Directory.CreateDirectory(path);
            string filePath = Path.Combine(path, "file.txt");
            using (StreamWriter text = File.CreateText(filePath))
            {
                text.WriteLine("hello world");
            }

            CustomUriBuilder builder = new CustomUriBuilder(filePath, AppDomain.CurrentDomain.BaseDirectory);

            using (IResource resource = loader.Create(builder.Uri))
            {
                Assert.IsNotNull(resource);

                using (TextReader reader = new StreamReader(resource.Stream))
                {
                    Assert.That(reader.ReadLine(), Is.EqualTo("hello world"));
                }
            }
        }
        public void Test_resource_creation_with_relative_path()
        {
            string file = @"../../Fixtures/Utilities/sample.txt";

            CustomUriBuilder builder = new CustomUriBuilder(file, AppDomain.CurrentDomain.BaseDirectory);

            using (IResource resource = loader.Create(builder.Uri))
            {
                Assert.IsNotNull(resource);

                using (TextReader reader = new StreamReader(resource.Stream))
                {
                    string line = reader.ReadLine();
                    Assert.That(line, Is.EqualTo("hello"));
                }
            }
        }