Author: alan.dean
Date: Sun Jun 15 10:20:07 2008
New Revision: 96
Added:
trunk/src/Common.UnitTests/IO/
trunk/src/Common.UnitTests/IO/Stream.ExtensionMethods.UnitTests.cs
trunk/src/Common/IO/
trunk/src/Common/IO/Stream.ExtensionMethods.cs
Modified:
trunk/src/Common.UnitTests/Common.UnitTests.csproj
trunk/src/Common/Common.csproj
trunk/src/Website.HttpTests/Net/HttpWebResponse.ExtensionMethods.cs
trunk/src/Website.HttpTests/Website.HttpTests.csproj
Log:
Refactored extension methods.
Modified: trunk/src/Common.UnitTests/Common.UnitTests.csproj
=========================================================================== ===
--- trunk/src/Common.UnitTests/Common.UnitTests.csproj (original)
+++ trunk/src/Common.UnitTests/Common.UnitTests.csproj Sun Jun 15 10:20:07 2008
@@ -47,6 +47,7 @@
<Compile Include="..\altnetuk.cs">
<Link>Properties\altnetuk.cs</Link>
</Compile>
+ <Compile Include="IO\Stream.ExtensionMethods.UnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\Cryptography\MD5Digest.UnitTests.cs" />
<Compile Include="String.ExtensionMethods.UnitTests.cs" />
Added: trunk/src/Common.UnitTests/IO/Stream.ExtensionMethods.UnitTests.cs
=========================================================================== ===
--- (empty file)
+++ trunk/src/Common.UnitTests/IO/Stream.ExtensionMethods.UnitTests.cs Sun Jun 15 10:20:07 2008
@@ -0,0 +1,79 @@
+// <copyright file="Stream.ExtensionMethods.UnitTests.cs" company="Alt.Net UK">
+// The MIT License
+
+// Copyright (c) 2008 Alt.Net UK
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+// </copyright>
+// <author>Alan Dean</author>
+// <email>alan.d...@thoughtpad.net</email>
+
+namespace AltNetUK.IO
+{
+ using System;
+ using System.IO;
+
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ /// <summary>
+ /// Contains unit tests of the <see cref="T:AltNetUK.IO.StreamExtensionMethods"/> class.
+ /// </summary>
+ [TestClass]
+ public class StringExtensionMethodsUnitTests
+ {
+ #region public static void ReadString(this Stream)
+
+ [TestMethod]
+ public void ReadString_StreamNull_Test()
+ {
+ Assert.IsNull((null as Stream).ReadString());
+ }
+
+ [TestMethod]
+ public void ReadString_StreamEmpty_Test()
+ {
+ using (MemoryStream stream = new MemoryStream())
+ {
+ Assert.AreEqual<string>(String.Empty, stream.ReadString());
+ }
+ }
+
+ [TestMethod]
+ public void ReadString_Stream_Test()
+ {
+ string expected = "test";
+
+ using (MemoryStream stream = new MemoryStream())
+ {
+ using (StreamWriter writer = new StreamWriter(stream))
+ {
+ writer.Write(expected);
+ writer.Flush();
+ stream.Position = 0;
+
+ string actual = stream.ReadString();
+
+ Assert.AreEqual<string>(expected, actual);
+ }
+ }
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
Modified: trunk/src/Common/Common.csproj
=========================================================================== ===
--- trunk/src/Common/Common.csproj (original)
+++ trunk/src/Common/Common.csproj Sun Jun 15 10:20:07 2008
@@ -48,6 +48,7 @@
<Compile Include="..\altnetuk.cs">
<Link>Properties\altnetuk.cs</Link>
</Compile>
+ <Compile Include="IO\Stream.ExtensionMethods.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\Cryptography\MD5Digest.cs" />
<Compile Include="String.ExtensionMethods.cs" />
Added: trunk/src/Common/IO/Stream.ExtensionMethods.cs
=========================================================================== ===
--- (empty file)
+++ trunk/src/Common/IO/Stream.ExtensionMethods.cs Sun Jun 15 10:20:07 2008
@@ -0,0 +1,60 @@
+// <copyright file="Stream.ExtensionMethods.cs" company="Alt.Net UK">
+// The MIT License
+
+// Copyright (c) 2008 Alt.Net UK
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+// </copyright>
+// <author>Alan Dean</author>
+// <email>alan.d...@thoughtpad.net</email>
+
+namespace AltNetUK.IO
+{
+ using System;
+ using System.IO;
+
+ /// <summary>
+ /// Extends the <see cref="T:System.IO.Stream"/> class.
+ /// <para>This class cannot be inherited.</para>
+ /// </summary>
+ public static class StreamExtensionMethods
+ {
+ #region public static void ReadString(this Stream)
+ /// <summary>
+ /// Returns the <see cref="T:System.IO.Stream"/> content.
+ /// </summary>
+ /// <param name="arg">The source <see cref="T:System.IO.Stream"/> instance.</param>
+ /// <returns>The stream content as a <see langword="string"/>.</returns>
+ public static string ReadString(this Stream arg)
+ {
+ string obj = null;
+
+ if ((arg != null) && arg.CanRead)
+ {
+ using (StreamReader reader = new StreamReader(arg))
+ {
+ obj = reader.ReadToEnd();
+ }
+ }
+
+ return obj;
+ }
+ #endregion
+ }
+}
\ No newline at end of file
Modified: trunk/src/Website.HttpTests/Net/HttpWebResponse.ExtensionMethods.cs
=========================================================================== ===
--- trunk/src/Website.HttpTests/Net/HttpWebResponse.ExtensionMethods.cs (original)
+++ trunk/src/Website.HttpTests/Net/HttpWebResponse.ExtensionMethods.cs Sun Jun 15 10:20:07 2008
@@ -32,7 +32,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
- using HtmlAgilityPack;
+ using AltNetUK.IO;
/// <summary>
/// Extends the <see cref="T:System.Net.HttpWebResponse"/> class.
@@ -52,13 +52,7 @@
throw new ArgumentNullException("arg");
}
- using (Stream stream = arg.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(stream))
- {
- Assert.AreEqual<long>(0, reader.ReadToEnd().Length);
- }
- }
+ Assert.AreEqual<long>(0, arg.GetResponseStream().ReadString().Length);
}
#endregion
}
Modified: trunk/src/Website.HttpTests/Website.HttpTests.csproj
=========================================================================== ===
--- trunk/src/Website.HttpTests/Website.HttpTests.csproj (original)
+++ trunk/src/Website.HttpTests/Website.HttpTests.csproj Sun Jun 15 10:20:07 2008
@@ -50,6 +50,9 @@
<Compile Include="..\altnetuk.cs">
<Link>Properties\altnetuk.cs</Link>
</Compile>
+ <Compile Include="Net\HttpWebResponse.ExtensionMethods.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="UserRegistration.Xhtml.Tests.cs" />
<Compile Include="UserRegistration.Html.Tests.cs" />
<Compile Include="Net\IAssertContent.cs" />
@@ -59,7 +62,6 @@
<Compile Include="Default.Aspx.Tests.cs" />
<Compile Include="HtmlAgilityPack\HtmlDocument.ExtensionMethods.cs" />
<Compile Include="Net\HttpAssert.cs" />
- <Compile Include="Net\HttpWebResponse.ExtensionMethods.cs" />
<Compile Include="Root.Tests.cs" />
<Compile Include="Home.Html.Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />