removed HttpContext deps

further Spring/J syncs (introduced INamespaceParserResolver (just internal yet))
fixed ParserContext.ContainingObjectDefinition handling
fixed SPRNET-1211
This commit is contained in:
eeichinger
2009-05-25 03:07:01 +00:00
parent 34fea21fdd
commit 2a203a7f7c
44 changed files with 2397 additions and 975 deletions

View File

@@ -1784,7 +1784,8 @@ namespace Spring.Objects.Factory.Xml
</objects>";
stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
IObjectFactory factory = new XmlObjectFactory(new InputStreamResource(stream, string.Empty));
factory.GetObject("foo");
ITestObject to = (ITestObject) factory.GetObject("foo");
Assert.IsNotNull( to.Spouse );
}
[Test(Description="SPR-1313")]

View File

@@ -803,6 +803,7 @@
<Content Include="Data\Spring\Objects\Factory\Xml\collections.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\array-autowire.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\objectNameGeneration.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\simple-constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\expressions.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\default-autowire.xml" />

View File

@@ -404,5 +404,23 @@ namespace Spring.Util
return ((int)dex.Key).CompareTo(dey.Key);
}
[Test]
public void FindFirstMatchReturnsNullIfAnyInputIsEmpty()
{
Assert.IsNull( CollectionUtils.FindFirstMatch(null, null) );
Assert.IsNull( CollectionUtils.FindFirstMatch(new string[0], new string[0]));
Assert.IsNull( CollectionUtils.FindFirstMatch(null, new string[] { "x" }));
Assert.IsNull( CollectionUtils.FindFirstMatch(new string[] { "x" }, null));
}
[Test]
public void FindFirstMatchReturnsFirstMatch()
{
ArrayList source = new ArrayList();
string[] candidates = new string[] { "G", "B", "H" };
source.AddRange( new string[] { "A", "B", "C" } );
Assert.AreEqual( "B" , CollectionUtils.FindFirstMatch(source, candidates));
}
}
}

View File

@@ -126,6 +126,11 @@ namespace Spring.Validation
{
throw new NotImplementedException();
}
public bool IsObjectNameInUse(string objectName)
{
return this.objects[objectName] != null;
}
}
}

View File

@@ -39,7 +39,7 @@ namespace Spring.Core.IO
[TestFixtureSetUp]
public void SetUpFixture()
{
testVirtualEnvironment = new VirtualEnvironmentMock("/some.request", "somepathinfo", "/", true);
testVirtualEnvironment = new VirtualEnvironmentMock("/some.request", "somepathinfo", null, "/", true);
}
[TestFixtureTearDown]

View File

@@ -44,7 +44,7 @@ namespace Spring.Objects.Factory.Support
// we need to create WOF within a valid HttpContext environment 'cause we will
// make use of 'request' and 'session' scope.
using (new VirtualEnvironmentMock("/somedir/some.file", null, "/", true))
using (new VirtualEnvironmentMock("/somedir/some.file", null, null, "/", true))
{
wof = new WebObjectFactory("/somedir/", false);
}

View File

@@ -0,0 +1,60 @@
#region License
/*
* Copyright 2002-2009 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
using System;
using NUnit.Framework;
namespace Spring.Objects.Factory.Support
{
/// <summary>
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
public class WebObjectUtilsTests
{
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void GetPageTypeWithNullPageName()
{
WebObjectUtils.GetPageType(null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void GetPageTypeWithEmptyStringPageName()
{
WebObjectUtils.GetPageType(string.Empty);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void GetPageTypeWithWhitespacedPageName()
{
WebObjectUtils.GetPageType(" ");
}
[Test]
[ExpectedException(typeof(ObjectCreationException))]
public void CreatePageInstanceWhenNotRunningInServerContext()
{
WebObjectUtils.CreatePageInstance("foo.aspx");
}
}
}

View File

@@ -0,0 +1,181 @@
#region License
/*
* Copyright 2002-2009 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
using System;
using System.Collections;
using System.Xml;
using NUnit.Framework;
using Spring.Collections;
using Spring.Core.IO;
using Spring.Objects.Factory.Support;
using Spring.Objects.Factory.Xml;
using Spring.TestSupport;
using Spring.Util;
namespace Spring.Objects.Factory
{
/// <summary>
/// </summary>
/// <author>Erich Eichinger</author>
[TestFixture]
public class WebObjectDefinitionReaderTests
{
public class TestWebObjectDefinitionReader : WebObjectDefinitionReader
{
public TestWebObjectDefinitionReader(string contextVirtualPath, IObjectDefinitionRegistry registry, XmlResolver resolver)
: base(contextVirtualPath, registry, resolver)
{}
}
[Test]
public void ControlDefinitionsGetMarkedAbstract()
{
const string CONTEXTPATH = "/ContextPath/";
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object type='MyControl.ascx' />
</objects>";
WebObjectFactory objectFactory = new WebObjectFactory(CONTEXTPATH, false);
TestWebObjectDefinitionReader reader = new TestWebObjectDefinitionReader(objectFactory.ContextPath, objectFactory, new XmlUrlResolver());
using (VirtualEnvironmentMock env = new VirtualEnvironmentMock(CONTEXTPATH + "test.aspx", null, null, CONTEXTPATH, true))
{
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyControl.ascx"] = typeof(Spring.Web.UI.UserControl);
reader.LoadObjectDefinitions(new StringResource(xml));
}
Assert.IsTrue(objectFactory.ContainsObjectDefinition("Spring.Web.UI.UserControl"));
Assert.IsTrue(objectFactory.GetObjectDefinition("Spring.Web.UI.UserControl").IsAbstract);
}
[Test]
public void ParsesPagePathIntoObjectNameIfNeitherIdNorNameAttributeSpecified()
{
const string CONTEXTPATH = "/ContextPath/";
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object type='MyPage.aspx' />
<object type='~/MyControl.ascx' />
</objects>";
WebObjectFactory objectFactory = new WebObjectFactory(CONTEXTPATH, false);
TestWebObjectDefinitionReader reader = new TestWebObjectDefinitionReader(objectFactory.ContextPath, objectFactory, new XmlUrlResolver());
using (VirtualEnvironmentMock env = new VirtualEnvironmentMock(CONTEXTPATH + "test.aspx", null, null, CONTEXTPATH, true))
{
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyPage.aspx"] = typeof (Spring.Web.UI.Page);
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyControl.ascx"] = typeof (Spring.Web.UI.UserControl);
reader.LoadObjectDefinitions(new StringResource(xml));
}
Assert.IsTrue(objectFactory.ContainsObjectDefinition("/MyPage.aspx"));
Assert.AreEqual(typeof(Spring.Web.UI.Page), objectFactory.GetType("/MyPage.aspx"));
Assert.IsTrue(objectFactory.ContainsObjectDefinition("Spring.Web.UI.UserControl"));
}
[Test]
public void DoesNotGenerateObjectNameIfIdAttributeSpecified()
{
const string CONTEXTPATH = "/ContextPath/";
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object id='mypage' type='MyPage.aspx' />
<object id='mycontrol' type='MyControl.ascx' />
</objects>";
WebObjectFactory objectFactory = new WebObjectFactory(CONTEXTPATH, false);
TestWebObjectDefinitionReader reader = new TestWebObjectDefinitionReader(objectFactory.ContextPath, objectFactory, new XmlUrlResolver());
using (VirtualEnvironmentMock env = new VirtualEnvironmentMock(CONTEXTPATH + "test.aspx", null, null, CONTEXTPATH, true))
{
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyPage.aspx"] = typeof (Spring.Web.UI.Page);
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyControl.ascx"] = typeof (Spring.Web.UI.UserControl);
reader.LoadObjectDefinitions(new StringResource(xml));
}
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mypage"));
Assert.AreEqual(typeof(Spring.Web.UI.Page), objectFactory.GetType("mypage"));
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mycontrol"));
}
[Test]
public void DoesNotGenerateObjectNameIfNameAttributeSpecified()
{
const string CONTEXTPATH = "/ContextPath/";
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object name='mypage' type='MyPage.aspx' />
<object name='mycontrol' type='MyControl.ascx' />
</objects>";
WebObjectFactory objectFactory = new WebObjectFactory(CONTEXTPATH, false);
TestWebObjectDefinitionReader reader = new TestWebObjectDefinitionReader(objectFactory.ContextPath, objectFactory, new XmlUrlResolver());
using (VirtualEnvironmentMock env = new VirtualEnvironmentMock(CONTEXTPATH + "test.aspx", null, null, CONTEXTPATH, true))
{
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyPage.aspx"] = typeof(Spring.Web.UI.Page);
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyControl.ascx"] = typeof(Spring.Web.UI.UserControl);
reader.LoadObjectDefinitions(new StringResource(xml));
}
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mypage"));
Assert.AreEqual(typeof(Spring.Web.UI.Page), objectFactory.GetType("mypage"));
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mycontrol"));
}
[Test]
public void DoesNotGenerateObjectNameIfIdAndNameAttributeSpecified()
{
const string CONTEXTPATH = "/";
const string xml =
@"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
<object id='mypage' name='mypageAlias' type='~/MyPage.aspx' />
<object id='mycontrol' name='mycontrolAlias' type='~/MyControl.ascx' />
</objects>";
WebObjectFactory objectFactory = new WebObjectFactory(CONTEXTPATH, false);
TestWebObjectDefinitionReader reader = new TestWebObjectDefinitionReader(objectFactory.ContextPath, objectFactory, new XmlUrlResolver());
using (VirtualEnvironmentMock env = new VirtualEnvironmentMock(CONTEXTPATH + "test.aspx", null, null, CONTEXTPATH, true))
{
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyPage.aspx"] = typeof(Spring.Web.UI.Page);
env.VirtualPath2ArtifactsTable[CONTEXTPATH + "MyControl.ascx"] = typeof(Spring.Web.UI.UserControl);
reader.LoadObjectDefinitions(new StringResource(xml));
}
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mypage"));
Assert.IsTrue(objectFactory.ContainsObject("mypageAlias"));
Assert.IsTrue(objectFactory.ContainsObjectDefinition("mycontrol"));
Assert.IsTrue(objectFactory.ContainsObject("mycontrolAlias"));
}
}
}

View File

@@ -104,6 +104,8 @@
<Compile Include="Objects\Factory\Support\WebObjectFactoryTests.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Objects\Factory\Support\WebObjectUtilsTests.cs" />
<Compile Include="Objects\Factory\Xml\WebObjectDefinitionReaderTests.cs" />
<Compile Include="TestSupport\NUnitAdapter.cs" />
<Compile Include="TestSupport\SessionMock.cs" />
<Compile Include="TestSupport\TestPage.cs">

View File

@@ -2,6 +2,9 @@ using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using Spring.Collections;
using Spring.Util;
@@ -15,20 +18,30 @@ namespace Spring.TestSupport
{
private readonly IVirtualEnvironment _prevEnvironment;
private readonly string _currentVirtualFilePath;
private readonly string _pathInfo;
private string _currentVirtualFilePath;
private string _pathInfo;
private HttpValueCollection _query;
private string _currentExecutionFilePath;
private readonly string _applicationVirtualPath;
private ISessionState _session = new SessionMock();
private IDictionary _requestVariables = new CaseInsensitiveHashtable(); //CollectionsUtil.CreateCaseInsensitiveHashtable();
private NameValueCollection requestParams = new NameValueCollection();
private IDictionary virtualPath2ArtifactsTable = new CaseInsensitiveHashtable();
public VirtualEnvironmentMock(string currentVirtualFilePath, string pathInfo, string applicationVirtualPath, bool autoInitialize)
public VirtualEnvironmentMock(string currentVirtualFilePath, string pathInfo, string queryText, string applicationVirtualPath, bool autoInitialize)
{
_currentVirtualFilePath = currentVirtualFilePath;
_currentExecutionFilePath = currentVirtualFilePath;
_pathInfo = (pathInfo == null || pathInfo.Length == 0) ? "" : "/" + pathInfo.TrimStart('/'); // prevent null string and ensure '/' prefixed
_query = new HttpValueCollection(queryText);
_applicationVirtualPath = "/" + ("" + applicationVirtualPath).Trim('/');
if (!_applicationVirtualPath.EndsWith("/")) _applicationVirtualPath = _applicationVirtualPath + "/";
// if (!_currentVirtualFilePath.StartsWith(_applicationVirtualPath))
// {
// throw new ArgumentException("currentVirtualFilePath must begin with applicationVirtualPath");
// }
_prevEnvironment = VirtualEnvironment.SetInstance(this);
if (autoInitialize)
{
@@ -36,6 +49,11 @@ namespace Spring.TestSupport
}
}
public IDictionary VirtualPath2ArtifactsTable
{
get { return virtualPath2ArtifactsTable; }
}
public string ApplicationVirtualPath
{
get { return _applicationVirtualPath; }
@@ -43,9 +61,19 @@ namespace Spring.TestSupport
public string CurrentVirtualPath
{
get
{
return _currentVirtualFilePath + _pathInfo;
get { return _currentVirtualFilePath + _pathInfo; }
}
public string CurrentVirtualPathAndQuery
{
get
{
string result = _currentVirtualFilePath + _pathInfo;
if (_query.Count > 0)
{
result = result + "?" + _query.ToString();
}
return result;
}
}
@@ -60,14 +88,58 @@ namespace Spring.TestSupport
set { this._currentExecutionFilePath = value; }
}
public NameValueCollection QueryString
{
get { return _query; }
}
public string MapPath(string virtualPath)
{
string basePath = Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath);
string resultPath = WebUtils.CreateAbsolutePath(this.ApplicationVirtualPath, virtualPath);
string resultPath = WebUtils.CreateAbsolutePath(this.CurrentVirtualFilePath, virtualPath);
resultPath = basePath.TrimEnd('\\') + "\\" + resultPath.Replace('/', '\\').TrimStart('\\');
return resultPath;
}
public IDisposable RewritePath(string newVirtualPath, bool rebaseClientPath)
{
IDisposable ctx = new RewriteContext(CurrentVirtualPathAndQuery, false, this);
int index = newVirtualPath.IndexOf('?');
if (index >= 0)
{
string newQueryString = (index < (newVirtualPath.Length - 1)) ? newVirtualPath.Substring(index + 1) : string.Empty;
_query = new HttpValueCollection(newQueryString);
newVirtualPath = newVirtualPath.Substring(0, index);
}
_currentVirtualFilePath = newVirtualPath;
return ctx;
}
public Type GetCompiledType(string virtualPath)
{
object o = virtualPath2ArtifactsTable[virtualPath];
if (o == null)
throw new FileNotFoundException(virtualPath);
else if (o is Type)
return (Type) o;
else
return o.GetType();
}
public object CreateInstanceFromVirtualPath(string virtualPath, Type requiredBaseType)
{
object o = virtualPath2ArtifactsTable[virtualPath];
if (o == null)
throw new FileNotFoundException(virtualPath);
else if (o is Type)
return Activator.CreateInstance((Type)o);
else
return o;
}
public ISessionState Session
{
get { return _session; }
@@ -80,9 +152,152 @@ namespace Spring.TestSupport
set { _requestVariables = value; }
}
public NameValueCollection RequestParams
{
get { return requestParams; }
}
public void Dispose()
{
VirtualEnvironment.SetInstance(_prevEnvironment);
}
private class RewriteContext : IDisposable
{
private string originalPath;
private bool rebaseClientPath;
private VirtualEnvironmentMock runtime;
public RewriteContext(string originalPath, bool rebaseClientPath, VirtualEnvironmentMock runtime)
{
this.originalPath = originalPath;
this.rebaseClientPath = rebaseClientPath;
this.runtime = runtime;
}
public void Dispose()
{
if (originalPath != null)
{
this.runtime.RewritePath(originalPath, rebaseClientPath);
}
}
}
private class HttpValueCollection : NameValueCollection
{
public HttpValueCollection(string queryText)
{
FillFromString(queryText, false, Encoding.UTF8);
}
public override string ToString()
{
return ToString(true);
}
private void FillFromString(string s, bool urlencoded, Encoding encoding)
{
int num = (s != null) ? s.Length : 0;
for (int i = 0; i < num; i++)
{
int startIndex = i;
int num4 = -1;
while (i < num)
{
char ch = s[i];
if (ch == '=')
{
if (num4 < 0)
{
num4 = i;
}
}
else if (ch == '&')
{
break;
}
i++;
}
string str = null;
string str2 = null;
if (num4 >= 0)
{
str = s.Substring(startIndex, num4 - startIndex);
str2 = s.Substring(num4 + 1, (i - num4) - 1);
}
else
{
str2 = s.Substring(startIndex, i - startIndex);
}
if (urlencoded)
{
base.Add(HttpUtility.UrlDecode(str, encoding), HttpUtility.UrlDecode(str2, encoding));
}
else
{
base.Add(str, str2);
}
if ((i == (num - 1)) && (s[i] == '&'))
{
base.Add(null, string.Empty);
}
}
}
internal virtual string ToString(bool urlencoded)
{
StringBuilder builder = new StringBuilder();
int count = this.Count;
for (int i = 0; i < count; i++)
{
string str3;
string key = this.GetKey(i);
if (urlencoded)
{
key = HttpUtility.UrlEncodeUnicode(key);
}
string str2 = ((key != null) && (key.Length > 0)) ? (key + "=") : "";
ArrayList list = (ArrayList)base.BaseGet(i);
int num3 = (list != null) ? list.Count : 0;
if (i > 0)
{
builder.Append('&');
}
if (num3 == 1)
{
builder.Append(str2);
str3 = (string)list[0];
if (urlencoded)
{
str3 = HttpUtility.UrlEncodeUnicode(str3);
}
builder.Append(str3);
}
else if (num3 == 0)
{
builder.Append(str2);
}
else
{
for (int j = 0; j < num3; j++)
{
if (j > 0)
{
builder.Append('&');
}
builder.Append(str2);
str3 = (string)list[j];
if (urlencoded)
{
str3 = HttpUtility.UrlEncodeUnicode(str3);
}
builder.Append(str3);
}
}
}
return builder.ToString();
}
}
}
}

View File

@@ -39,34 +39,6 @@ namespace Spring.Util
{
private const string ExpectedPageName = "foo";
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetPageTypeWithNullPageName()
{
WebObjectUtils.GetPageType(null);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetPageTypeWithEmptyStringPageName()
{
WebObjectUtils.GetPageType(string.Empty);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetPageTypeWithWhitespacedPageName()
{
WebObjectUtils.GetPageType(" ");
}
[Test]
[ExpectedException(typeof (ObjectCreationException))]
public void CreatePageInstanceWhenNotRunningInServerContext()
{
WebObjectUtils.CreatePageInstance("foo.aspx");
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetPageNameWithNullUrl()
@@ -180,7 +152,7 @@ namespace Spring.Util
public void CombineVirtualPathsInRootWeb()
{
// emulate root website context
using( new VirtualEnvironmentMock("/somedir/some.file", null, "/", true) )
using( new VirtualEnvironmentMock("/somedir/some.file", null, null, "/", true) )
{
CombineVirtualPathsSuite( "/" );
}
@@ -190,7 +162,7 @@ namespace Spring.Util
public void CombineVirtualPathsInChildWeb()
{
// emulate child website context
using( new VirtualEnvironmentMock("/somedir/some.file", null, "/myapp", true) )
using( new VirtualEnvironmentMock("/somedir/some.file", null, null, "/myapp", true) )
{
CombineVirtualPathsSuite( "/myapp/" );
}