made StringResource a 1st class citizen of Spring.Core

This commit is contained in:
eeichinger
2008-07-23 10:35:38 +00:00
parent b8dd1d2c64
commit d9c328e13d
7 changed files with 137 additions and 9 deletions

View File

@@ -0,0 +1,106 @@
#region License
/*
* Copyright <20> 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#region Imports
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
#endregion
namespace Spring.Core.IO
{
/// <summary>
///
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
public class StringResourceTests
{
[Test]
public void EnsureDefaults()
{
Encoding enc = Encoding.Default;
string FOO_CONTENT = "foo";
string FOO_DESCRIPTION = "foo description";
StringResource r = new StringResource(FOO_CONTENT);
Assert.AreEqual(FOO_CONTENT, r.Content);
Assert.AreEqual(enc, r.Encoding);
Assert.AreEqual(string.Empty, r.Description);
enc = new UTF7Encoding();
r = new StringResource(FOO_CONTENT, enc, FOO_DESCRIPTION);
Assert.AreEqual(FOO_CONTENT, r.Content);
Assert.AreEqual(enc, r.Encoding);
Assert.AreEqual(FOO_DESCRIPTION, r.Description);
}
[Test]
public void ReturnsCorrectEncodedStream()
{
string FOO_CONTENT = "foo\u4567";
StringResource r = new StringResource(FOO_CONTENT, Encoding.GetEncoding("utf-16"));
Assert.AreEqual(FOO_CONTENT, r.Content);
Stream istm = r.InputStream;
Assert.IsTrue(istm.CanRead);
byte[] chars = new byte[istm.Length];
istm.Read(chars, 0, chars.Length);
istm.Close();
string result = Encoding.GetEncoding("utf-16").GetString( chars );
Assert.AreEqual(FOO_CONTENT, result);
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void DoesntSupportRelativeResources()
{
StringResource r = new StringResource(string.Empty);
r.CreateRelative("foo");
}
[Test]
public void AcceptsNullContent()
{
Encoding utf7 = new UTF7Encoding();
StringResource r = new StringResource(null, utf7);
Assert.AreEqual(string.Empty, r.Content);
Stream stm = r.InputStream;
Assert.IsTrue(stm.CanRead);
Assert.IsNotNull(stm);
Assert.AreEqual(0, stm.Length);
stm.Close();
}
[Test]
public void AlwaysExists()
{
StringResource r = new StringResource(null);
Assert.IsTrue(r.Exists);
r = new StringResource(string.Empty);
Assert.IsTrue(r.Exists);
r = new StringResource("foo");
Assert.IsTrue(r.Exists);
}
}
}

View File

@@ -21,7 +21,7 @@
#region Imports
using NUnit.Framework;
using Spring.Core.IO;
using Spring.Objects.Factory.Support;
#endregion

View File

@@ -188,11 +188,6 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "StringResource.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "TestResource.txt"
BuildAction = "EmbeddedResource"
@@ -512,6 +507,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Core\IO\StringResourceTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Core\IO\TestResource.txt"
BuildAction = "EmbeddedResource"

View File

@@ -237,6 +237,7 @@
<Compile Include="Core\IO\InputStreamResourceTests.cs" />
<Compile Include="Core\IO\ResourceConverterTests.cs" />
<Compile Include="Core\IO\ResourceHandlerRegistryTests.cs" />
<Compile Include="Core\IO\StringResourceTests.cs" />
<Compile Include="Core\IO\UrlResourceTest.cs" />
<Compile Include="Core\MethodGenericArgumentsCountCriteriaTests.cs" />
<Compile Include="Core\MethodParametersCountCriteriaTests.cs" />
@@ -646,7 +647,6 @@
<Compile Include="StreamHelperDecorator.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="StringResource.cs" />
<Compile Include="Threading\AsyncTestMethod.cs" />
<Compile Include="Threading\AsyncTestTask.cs" />
<Compile Include="Threading\CallContextStorageTests.cs" />

View File

@@ -1,115 +0,0 @@
#region License
/*
* Copyright <20> 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#region Imports
using System;
using System.IO;
using System.Text;
using Spring.Core.IO;
using Spring.Util;
#endregion
namespace Spring
{
/// <summary>
/// A <see cref="IResource"/> adapter implementation encapsulating a simply string.
/// </summary>
/// <author>Erich Eichinger</author>
public class StringResource : AbstractResource
{
#region Fields
private readonly string _description;
private readonly string _content;
private readonly Encoding _encoding;
#endregion
/// <summary>
/// Creates a new instance of the <see cref="StringResource"/> class.
/// </summary>
public StringResource(string content)
: this(content, Encoding.Default, null)
{
}
/// <summary>
/// Creates a new instance of the <see cref="StringResource"/> class.
/// </summary>
public StringResource(string content, Encoding encoding)
: this(content, encoding, null)
{
}
/// <summary>
/// Creates a new instance of the <see cref="StringResource"/> class.
/// </summary>
public StringResource(string content, Encoding encoding, string description)
{
AssertUtils.ArgumentNotNull(encoding, "encoding");
_content = content==null ? string.Empty : content;
_encoding = encoding;
_description = description == null ? string.Empty : description;
}
/// <summary>
/// Get the <see cref="System.IO.Stream"/> to
/// for accessing this resource.
/// </summary>
public override Stream InputStream
{
get
{
return new MemoryStream(_encoding.GetBytes(_content), false);
}
}
/// <summary>
/// Returns a description for this resource.
/// </summary>
/// <value>
/// A description for this resource.
/// </value>
/// <seealso cref="Spring.Core.IO.IResource.Description"/>
public override string Description
{
get { return _description; }
}
/// <summary>
/// This implementation always returns true
/// </summary>
public override bool IsOpen
{
get { return true; }
}
/// <summary>
/// This implemementation always returns true
/// </summary>
public override bool Exists
{
get { return true; }
}
}
}