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

@@ -28,10 +28,10 @@ using Spring.Util;
#endregion
namespace Spring
namespace Spring.Core.IO
{
/// <summary>
/// A <see cref="IResource"/> adapter implementation encapsulating a simply string.
/// A <see cref="IResource"/> adapter implementation encapsulating a simple string.
/// </summary>
/// <author>Erich Eichinger</author>
public class StringResource : AbstractResource
@@ -111,5 +111,21 @@ namespace Spring
{
get { return true; }
}
/// <summary>
/// Gets the encoding used to create a byte stream of the <see cref="Content"/> string.
/// </summary>
public Encoding Encoding
{
get { return _encoding; }
}
/// <summary>
/// Gets the content encapsulated by this <see cref="StringResource"/>.
/// </summary>
public string Content
{
get { return _content; }
}
}
}

View File

@@ -622,6 +622,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Core\IO\StringResource.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Core\IO\UrlResource.cs"
SubType = "Code"

View File

@@ -269,6 +269,7 @@
<Compile Include="Core\IO\IResourceLoader.cs" />
<Compile Include="Core\IO\ResourceConverter.cs" />
<Compile Include="Core\IO\ResourceHandlerRegistry.cs" />
<Compile Include="Core\IO\StringResource.cs" />
<Compile Include="Core\IO\UrlResource.cs" />
<Compile Include="Core\MethodNameMatchCriteria.cs" />
<Compile Include="Core\MethodGenericArgumentsCountCriteria.cs" />

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" />