public void OpensReaderWithDefaults()
 {
     EncodedResource r = new EncodedResource( new StringResource("test") );
     StreamReader reader = (StreamReader)r.OpenReader();
     Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
     Assert.AreEqual("test", reader.ReadToEnd());
 }
        public void OpensReaderWithDefaults()
        {
            EncodedResource r      = new EncodedResource(new StringResource("test"));
            StreamReader    reader = (StreamReader)r.OpenReader();

            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            Assert.AreEqual("test", reader.ReadToEnd());
        }
        public void HashcodeIsCalculatedUsingResourceOnly()
        {
            StringResource  testResource = new StringResource("test");
            EncodedResource er1          = new EncodedResource(testResource, Encoding.ASCII, true);
            EncodedResource er2          = new EncodedResource(testResource, Encoding.UTF8, false);

            Assert.AreEqual(testResource.GetHashCode(), er1.GetHashCode());
            Assert.AreEqual(er1.GetHashCode(), er2.GetHashCode());
        }
        public void HashcodeIsCalculatedUsingResourceOnly()
        {
            StringResource testResource = new StringResource("test");
            EncodedResource er1 = new EncodedResource(testResource, Encoding.ASCII, true);
            EncodedResource er2 = new EncodedResource(testResource, Encoding.UTF8, false);

            Assert.AreEqual(testResource.GetHashCode(), er1.GetHashCode());
            Assert.AreEqual(er1.GetHashCode(), er2.GetHashCode());
        }
        public void OpensReaderWithoutAutoDetectEncoding()
        {
            string expected = "test";
            Encoding utf32 = new UTF32Encoding(false, true);
            byte[] resourceData = GetBytes(expected, utf32);
            EncodedResource r = new EncodedResource(new InputStreamResource(new MemoryStream(resourceData), "description"), Encoding.UTF8, false);
            StreamReader reader = (StreamReader)r.OpenReader();
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();
//            Assert.AreEqual("\uFFFD\uFFFD\0\0t\0\0\0e\0\0\0s\0\0\0t\0\0\0", actual);
            Assert.AreEqual(Encoding.UTF8.GetString(resourceData), actual);
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
        }
        public void OpensReaderWithAutoDetectEncoding()
        {
            string expected = "test";
            Encoding utf32 = new UTF32Encoding(false, true);
            byte[] resourceData = GetBytes(expected, utf32);
            resourceData = (byte[])ArrayUtils.Concat(utf32.GetPreamble(), resourceData);
            EncodedResource r = new EncodedResource( new InputStreamResource( new MemoryStream( resourceData), "description" ), Encoding.UTF8, true);
            StreamReader reader = (StreamReader)r.OpenReader();
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();
            Assert.AreEqual( "\uFEFF" + expected , actual);
// interestingly the line below is *not* true!
//            Assert.AreEqual(utf32.GetString(resourceData), actual);
            Assert.AreEqual(utf32, reader.CurrentEncoding);
        }
Beispiel #7
0
        /// <summary>
        /// Determine whether <paramref name="obj"/> equals this instance.
        /// </summary>
        /// <returns>
        /// <c>true</c> if obj is an <see cref="EncodedResource"/> and both
        /// , <see cref="Resource"/> and <see cref="Encoding"/> are equal.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (!(obj is EncodedResource))
            {
                return(false);
            }

            EncodedResource other = (EncodedResource)obj;

            return(Equals(this.resource, other.resource) &&
                   Equals(this.encoding, other.encoding));
        }
        public void OpensReaderWithoutAutoDetectEncoding()
        {
            string   expected = "test";
            Encoding utf32    = new UTF32Encoding(false, true);

            byte[]          resourceData = GetBytes(expected, utf32);
            EncodedResource r            = new EncodedResource(new InputStreamResource(new MemoryStream(resourceData), "description"), Encoding.UTF8, false);
            StreamReader    reader       = (StreamReader)r.OpenReader();

            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();

//            Assert.AreEqual("\uFFFD\uFFFD\0\0t\0\0\0e\0\0\0s\0\0\0t\0\0\0", actual);
            Assert.AreEqual(Encoding.UTF8.GetString(resourceData), actual);
            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
        }
        public void OpensReaderWithAutoDetectEncoding()
        {
            string   expected = "test";
            Encoding utf32    = new UTF32Encoding(false, true);

            byte[] resourceData = GetBytes(expected, utf32);
            resourceData = (byte[])ArrayUtils.Concat(utf32.GetPreamble(), resourceData);
            EncodedResource r      = new EncodedResource(new InputStreamResource(new MemoryStream(resourceData), "description"), Encoding.UTF8, true);
            StreamReader    reader = (StreamReader)r.OpenReader();

            Assert.AreEqual(Encoding.UTF8.EncodingName, reader.CurrentEncoding.EncodingName);
            string actual = reader.ReadToEnd();

            Assert.AreEqual("\uFEFF" + expected, actual);
// interestingly the line below is *not* true!
//            Assert.AreEqual(utf32.GetString(resourceData), actual);
            Assert.AreEqual(utf32, reader.CurrentEncoding);
        }
        /// <summary>
        /// TBD
        /// </summary>
        public static void GetScriptBlocks(EncodedResource encodedResource, IList<string> blockCollector, params Regex[] blockDelimiterPatterns)
        {
            AssertUtils.ArgumentNotNull(blockCollector, "blockCollector");

            using (TextReader sr = encodedResource.OpenReader())
            {
                string script = sr.ReadToEnd();

                // the first pattern that finds a match will be used, if any
                Regex patternToUse = BLOCKDELIM_DEFAULT_EXP;
                if (blockDelimiterPatterns != null)
                {
                    foreach (Regex pattern in blockDelimiterPatterns)
                    {
                        if (pattern.IsMatch(script))
                        {
                            patternToUse = pattern;
                            break;
                        }
                    }
                }

                Split(script, patternToUse, blockCollector);
            }
        }
        /// <summary>
        /// Execute the given script
        /// </summary>
        private static void ExecuteSqlScriptInternal(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
        {
            AssertUtils.ArgumentNotNull(adoTemplate, "adoTemplate");
            AssertUtils.ArgumentNotNull(resource, "resource");

            if (!CollectionUtils.HasElements(blockDelimiter))
            {
                blockDelimiter = BLOCKDELIM_ALL_EXP;
            }

            List<string> statements = new List<string>();
            try
            {
                GetScriptBlocks(resource, statements, blockDelimiter);
            }
            catch (Exception ex)
            {
                throw new DataAccessResourceFailureException("Failed to open SQL script from " + resource, ex);
            }

            foreach (string statement in statements)
            {
                try
                {
                    adoTemplate.ExecuteNonQuery(CommandType.Text, statement);
                }
                catch (DataAccessException dae)
                {
                    if (!continueOnError)
                    {
                        throw;
                    }
                    Log.Warn(string.Format("SQL statement failed:{0}", statement), dae);
                }
            }
        }
 /// <summary>
 /// Execute the given script
 /// </summary>
 public static void ExecuteSqlScript(IAdoOperations adoTemplate, EncodedResource resource, bool continueOnError, params Regex[] blockDelimiter)
 {
     ExecuteSqlScriptInternal(adoTemplate, resource, continueOnError, blockDelimiter);
 }