hammett 2004/01/19 16:34:39
Modified: avalon-net/csframework/AvalonFrameworkTest
ConsoleLoggerTestCase.cs
avalon-net/csframework/AvalonFramework AvalonFramework.sln
avalon-net/csframework/AvalonFramework/Context
DefaultContext.cs
Added: avalon-net/csframework/AvalonFrameworkTest
ContextTestCase.cs
Log:
Now test cases are passing. DefaultContext moved from Container to Framework. Also
its test case.
Revision Changes Path
1.2 +1 -1
avalon-sandbox/avalon-net/csframework/AvalonFrameworkTest/ConsoleLoggerTestCase.cs
Index: ConsoleLoggerTestCase.cs
===================================================================
RCS file:
/home/cvs/avalon-sandbox/avalon-net/csframework/AvalonFrameworkTest/ConsoleLoggerTestCase.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConsoleLoggerTestCase.cs 22 Sep 2003 23:49:25 -0000 1.1
+++ ConsoleLoggerTestCase.cs 20 Jan 2004 00:34:39 -0000 1.2
@@ -1,2 +1,2 @@
// ============================================================================
// The Apache Software License, Version 1.1
// ============================================================================
//
// Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. The end-user documentation included with the redistribution, if any, must
// include the following acknowledgment: "This product includes software
// developed by the Apache Software Foundation (http://www.apache.org/)."
// Alternately, this acknowledgment may appear in the software itself, if
// and wherever such third-party acknowledgments normally appear.
//
// 4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
// must not be used to endorse or promote products derived from this software
// without prior written permission. For written permission, please contact
// [EMAIL PROTECTED]
//
// 5. Products derived from this software may not be called "Apache", nor may
// "Apache" appear in their name, without prior written permission of the
// Apache Software Foundation.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This software consists of voluntary contributions made by many individuals
// on behalf of the Apache Software Foundation. For more information on the
// Apache Software Foundation, please see <http://www.apache.org/>.
// ============================================================================
namespace Apache.Avalon.Framework.Test
{
using System;
using System.Text;
using System.IO;
using System.Collections;
using Apache.Avalon.Framework;
using NUnit.Framework;
- [TestFixture]
public class ConsoleLoggerTestCase
{
StringWriter _writer;
StringBuilder _buffer;
TextWriter _consoleWriter;
public ConsoleLoggerTestCase()
{
_consoleWriter = Console.Out;
}
[SetUp]
public void Init()
{
_buffer = new StringBuilder();
_writer = new StringWriter(_buffer);
System.Console.SetOut(_writer);
}
[Test]
public void TestCommonUse()
{
ConsoleLogger logger = new ConsoleLogger();
logger.Debug("Simple message");
String content = _buffer.ToString();
Assertion.AssertEquals("[DEBUG] Simple message\r\n", content);
}
[Test]
public void TestFormat()
{
ConsoleLogger logger = new ConsoleLogger();
logger.Debug("The object {0} is not implementing {1}", this,
typeof(ILogger));
String content = _buffer.ToString();
Assertion.AssertEquals("[DEBUG] The object
Apache.Avalon.Framework.Test.ConsoleLoggerTestCase " +
"is not implementing
Apache.Avalon.Framework.ILogger\r\n", content);
}
}
}
\ No newline at end of file
+ [TestFixture]
public class ConsoleLoggerTestCase
{
StringWriter _writer;
StringBuilder _buffer;
TextWriter _consoleWriter;
public ConsoleLoggerTestCase()
{
_consoleWriter = Console.Out;
}
[SetUp]
public void Init()
{
_buffer = new StringBuilder();
_writer = new StringWriter(_buffer);
System.Console.SetOut(_writer);
}
[Test]
public void TestCommonUse()
{
ConsoleLogger logger = new ConsoleLogger();
logger.Debug("Simple message");
String content = _buffer.ToString();
Assertion.AssertEquals("[Debug] '' Simple message\r\n",
content);
}
[Test]
public void TestFormat()
{
ConsoleLogger logger = new ConsoleLogger();
logger.Debug("The object {0} is not implementing {1}", this,
typeof(ILogger));
String content = _buffer.ToString();
Assertion.AssertEquals("[Debug] '' The object
Apache.Avalon.Framework.Test.ConsoleLoggerTestCase " +
"is not implementing
Apache.Avalon.Framework.ILogger\r\n", content);
}
}
}
\ No newline at end of file
1.1
avalon-sandbox/avalon-net/csframework/AvalonFrameworkTest/ContextTestCase.cs
Index: ContextTestCase.cs
===================================================================
// ============================================================================
// The Apache Software License, Version 1.1
// ============================================================================
//
// Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. The end-user documentation included with the redistribution, if any, must
// include the following acknowledgment: "This product includes software
// developed by the Apache Software Foundation (http://www.apache.org/)."
// Alternately, this acknowledgment may appear in the software itself, if
// and wherever such third-party acknowledgments normally appear.
//
// 4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
// must not be used to endorse or promote products derived from this software
// without prior written permission. For written permission, please contact
// [EMAIL PROTECTED]
//
// 5. Products derived from this software may not be called "Apache", nor may
// "Apache" appear in their name, without prior written permission of the
// Apache Software Foundation.
//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
// DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This software consists of voluntary contributions made by many individuals
// on behalf of the Apache Software Foundation. For more information on the
// Apache Software Foundation, please see <http://www.apache.org/>.
// ============================================================================
namespace Apache.Avalon.Framework.Test
{
using System;
using System.Text;
using NUnit.Framework;
using Apache.Avalon.Framework;
/// <summary>
/// Summary description for ContextTestCase.
/// </summary>
[TestFixture]
public class ContextTestCase
{
private class ResolvableString : IResolvable
{
private string m_content;
public ResolvableString( string content )
{
this.m_content = content;
}
public ResolvableString() : this( "This is a ${test}." )
{
}
#region IResolvable Members
public object Resolve(IContext context)
{
int index = this.m_content.IndexOf( "${" );
if ( index < 0 )
{
return this.m_content;
}
StringBuilder buf = new StringBuilder(
this.m_content.Substring( 0, index ) );
while ( index >= 0 && index <= this.m_content.Length )
{
index += 2;
int end = this.m_content.IndexOf( "}", index);
if ( end < 0 )
{
end = this.m_content.Length;
}
buf.Append( context[ this.m_content.Substring(
index, end - index ) ] );
end++;
index = this.m_content.IndexOf( "${", end ) +
2;
if ( index < 2 )
{
index = -1;
buf.Append( this.m_content.Substring(
end, this.m_content.Length - end ) );
}
if ( index >=0 && index <=
this.m_content.Length )
{
buf.Append( this.m_content.Substring(
end, index - end ) );
}
}
return buf.ToString();
}
#endregion
}
[Test]
public void AddContext()
{
DefaultContext context = new DefaultContext();
context.Put( "key1", "value1" );
Assertion.Assert( "value1".Equals( context["key1"] ) );
context.Put( "key1", String.Empty );
Assertion.Assert( String.Empty.Equals( context["key1"] ) );
context.Put( "key1", "value1" );
context.MakeReadOnly();
try
{
context.Put( "key1", String.Empty );
Assertion.Fail( "You are not allowed to change a value
after it has been made read only" );
}
catch ( ContextException )
{
Assertion.Assert( "Value is null", "value1".Equals(
context["key1"] ) );
}
}
[Test]
public void ResolveableObject()
{
DefaultContext context = new DefaultContext();
context.Put( "key1", new ResolvableString() );
context.Put( "test", "Cool Test" );
context.MakeReadOnly();
IContext newContext = (IContext) context;
Assertion.Assert( "Cool Test".Equals( newContext["test"] ) );
Assertion.Assert( ! "This is a ${test}.".Equals(
newContext["key1"] ) );
Assertion.Assert( "This is a Cool Test.".Equals(
newContext["key1"] ) );
}
[Test]
public void CascadingContext()
{
DefaultContext parent = new DefaultContext();
parent.Put( "test", "ok test" );
parent.MakeReadOnly();
DefaultContext child = new DefaultContext( parent );
child.Put( "check", new ResolvableString("This is an
${test}.") );
child.MakeReadOnly();
IContext context = (IContext) child;
Assertion.Assert ( "ok test".Equals( context["test"] ) );
Assertion.Assert ( ! "This is an ${test}.".Equals(
context["check"] ) );
Assertion.Assert ( "This is an ok test.".Equals(
context["check"] ) );
}
[Test]
public void HiddenItems()
{
DefaultContext parent = new DefaultContext();
parent.Put( "test", "test" );
parent.MakeReadOnly();
DefaultContext child = new DefaultContext( parent );
child.Put( "check", "check" );
IContext context = (IContext) child;
Assertion.Assert ( "check".Equals( context["check"] ) );
Assertion.Assert ( "test".Equals( context["test"] ) );
child.Hide( "test" );
try
{
object o = context["test"];
Assertion.Fail( "The item \"test\" was hidden in the
child context, but could still be retrieved via Get()." );
}
catch (ContextException)
{
// Supposed to be thrown.
}
child.MakeReadOnly();
try
{
child.Hide( "test" );
Assertion.Fail( "Hide() did not throw an exception,
even though the context is supposed to be read-only." );
}
catch (ContextException)
{
// Supposed to be thrown.
}
}
}
}
1.5 +16 -0
avalon-sandbox/avalon-net/csframework/AvalonFramework/AvalonFramework.sln
Index: AvalonFramework.sln
===================================================================
RCS file:
/home/cvs/avalon-sandbox/avalon-net/csframework/AvalonFramework/AvalonFramework.sln,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AvalonFramework.sln 19 Jan 2004 01:22:55 -0000 1.4
+++ AvalonFramework.sln 20 Jan 2004 00:34:39 -0000 1.5
@@ -15,6 +15,14 @@
ProjectSection(ProjectDependencies) = postProject
EndProjectSection
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apache.Avalon.Container",
"..\..\cscontainer\AvalonContainer\Apache.Avalon.Container.csproj",
"{0638D63A-8CE2-435B-89B8-E67A3D1904F7}"
+ ProjectSection(ProjectDependencies) = postProject
+ EndProjectSection
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apache.Avalon.Container.Test",
"..\..\cscontainer\AvalonContainerTest\Apache.Avalon.Container.Test.csproj",
"{DE388F53-A538-424E-B289-DB5D05BC0169}"
+ ProjectSection(ProjectDependencies) = postProject
+ EndProjectSection
+EndProject
Global
GlobalSection(SolutionConfiguration) = preSolution
Debug = Debug
@@ -37,6 +45,14 @@
{71F72AE0-E10D-4FA8-88B5-C6D9026E9A3A}.Debug.Build.0 = Debug|.NET
{71F72AE0-E10D-4FA8-88B5-C6D9026E9A3A}.Release.ActiveCfg = Release|.NET
{71F72AE0-E10D-4FA8-88B5-C6D9026E9A3A}.Release.Build.0 = Release|.NET
+ {0638D63A-8CE2-435B-89B8-E67A3D1904F7}.Debug.ActiveCfg = Debug|.NET
+ {0638D63A-8CE2-435B-89B8-E67A3D1904F7}.Debug.Build.0 = Debug|.NET
+ {0638D63A-8CE2-435B-89B8-E67A3D1904F7}.Release.ActiveCfg = Release|.NET
+ {0638D63A-8CE2-435B-89B8-E67A3D1904F7}.Release.Build.0 = Release|.NET
+ {DE388F53-A538-424E-B289-DB5D05BC0169}.Debug.ActiveCfg = Debug|.NET
+ {DE388F53-A538-424E-B289-DB5D05BC0169}.Debug.Build.0 = Debug|.NET
+ {DE388F53-A538-424E-B289-DB5D05BC0169}.Release.ActiveCfg = Release|.NET
+ {DE388F53-A538-424E-B289-DB5D05BC0169}.Release.Build.0 = Release|.NET
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EndGlobalSection
1.2 +172 -19
avalon-sandbox/avalon-net/csframework/AvalonFramework/Context/DefaultContext.cs
Index: DefaultContext.cs
===================================================================
RCS file:
/home/cvs/avalon-sandbox/avalon-net/csframework/AvalonFramework/Context/DefaultContext.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultContext.cs 19 Jan 2004 01:22:55 -0000 1.1
+++ DefaultContext.cs 20 Jan 2004 00:34:39 -0000 1.2
@@ -1,34 +1,34 @@
// ============================================================================
// The Apache Software License, Version 1.1
// ============================================================================
-//
+//
// Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without modifica-
// tion, are permitted provided that the following conditions are met:
-//
+//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
-//
+//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
-//
+//
// 3. The end-user documentation included with the redistribution, if any, must
// include the following acknowledgment: "This product includes software
// developed by the Apache Software Foundation (http://www.apache.org/)."
// Alternately, this acknowledgment may appear in the software itself, if
// and wherever such third-party acknowledgments normally appear.
-//
-// 4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
-// must not be used to endorse or promote products derived from this software
-// without prior written permission. For written permission, please contact
+//
+// 4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
+// must not be used to endorse or promote products derived from this software
+// without prior written permission. For written permission, please contact
// [EMAIL PROTECTED]
-//
+//
// 5. Products derived from this software may not be called "Apache", nor may
// "Apache" appear in their name, without prior written permission of the
// Apache Software Foundation.
-//
+//
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
@@ -39,36 +39,189 @@
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
+//
// This software consists of voluntary contributions made by many individuals
-// on behalf of the Apache Software Foundation. For more information on the
+// on behalf of the Apache Software Foundation. For more information on the
// Apache Software Foundation, please see <http://www.apache.org/>.
// ============================================================================
namespace Apache.Avalon.Framework
{
using System;
+ using System.Collections;
+ using System.Runtime.Serialization;
/// <summary>
- /// TODO: Implement this default context implementation
+ /// Default implementation of IContext.
/// </summary>
public class DefaultContext : IContext
{
- public DefaultContext()
+ [Serializable]
+ private sealed class Hidden
+ {
+ }
+
+ private static Hidden HIDDEN_MAKER = new Hidden();
+
+ private IDictionary m_contextData;
+ private IContext m_parent;
+ private bool m_readOnly;
+
+ /// <summary>
+ /// Create a Context with specified data and parent.
+ /// </summary>
+ /// <param name="contextData">the context data</param>
+ /// <param name="parent">the parent Context (may be null)</param>
+ public DefaultContext( IDictionary contextData, IContext parent )
+ {
+ m_parent = parent;
+ m_contextData = contextData;
+ }
+
+ /// <summary>
+ /// Create a Context with specified data.
+ /// </summary>
+ /// <param name="contextData">the context data</param>
+ public DefaultContext( IDictionary contextData ) : this( contextData,
null )
+ {
+ }
+
+ /// <summary>
+ /// Create a Context with specified parent.
+ /// </summary>
+ /// <param name="parent">the parent Context (may be null)</param>
+ public DefaultContext( IContext parent )
+ : this( Hashtable.Synchronized( new Hashtable() ), parent )
+ {
+ }
+
+ /// <summary>
+ /// Create a Context with no parent.
+ /// </summary>
+ public DefaultContext() : this( (IContext) null )
{
}
#region IContext Members
- public object this[object key]
+ /// <summary>
+ /// Retrieve an item from the Context.
+ /// </summary>
+ /// <param name="key">the key of item</param>
+ /// <returns>the item stored in context</returns>
+ /// <exception cref="ContextException">if item not present</exception>
+ public object this[ object key ]
{
- get
+ get
{
- // TODO: Add Context.this getter implementation
- return null;
+ object data = m_contextData[key];
+
+ if( null != data )
+ {
+ if( data is Hidden )
+ {
+ // Always fail.
+ string message = String.Format("Unable
to locate {0}", key);
+ throw new ContextException( message );
+ }
+
+ if( data is IResolvable )
+ {
+ return ( (IResolvable)data ).Resolve(
this );
+ }
+
+ return data;
+ }
+
+ // If data was null, check the parent
+ if( null == m_parent )
+ {
+ // There was no parent, and no data
+ string message = String.Format("Unable to
resolve context key {0}", key);
+ throw new ContextException( message );
+ }
+
+ return m_parent[ key ];
}
}
#endregion
+
+ /// <summary>
+ /// Helper method fo adding items to Context.
+ /// </summary>
+ /// <param name="key">the items key</param>
+ /// <param name="value">the item</param>
+ /// <exception cref="ContextException">if context is read
only</exception>
+ public void Put( object key, object value )
+ {
+ CheckWriteable();
+ if( null == value )
+ {
+ m_contextData.Remove( key );
+ }
+ else
+ {
+ m_contextData[ key ] = value;
+ }
+ }
+
+ /// <summary>
+ /// Hides the item in the context.
+ /// After Hide(key) has been called, a Get(key)
+ /// will always fail, even if the parent context
+ /// has such a mapping.
+ /// </summary>
+ /// <param name="key">the items key</param>
+ /// <exception cref="ContextException">if context is read
only</exception>
+ public void Hide( object key )
+ {
+ CheckWriteable();
+ m_contextData[ key ] = HIDDEN_MAKER;
+ }
+
+ /// <summary>
+ /// Utility method to retrieve context data.
+ /// </summary>
+ /// <returns>the context data</returns>
+ protected IDictionary GetContextData()
+ {
+ return m_contextData;
+ }
+
+ /// <summary>
+ /// Get parent context if any.
+ /// </summary>
+ /// <returns>the parent Context (may be null)</returns>
+ protected IContext Parent
+ {
+ get
+ {
+ return m_parent;
+ }
+ }
+
+ /// <summary>
+ /// Make the context read-only.
+ /// Any attempt to write to the context via Add()
+ /// will result in an IllegalStateException.
+ /// </summary>
+ public void MakeReadOnly()
+ {
+ m_readOnly = true;
+ }
+
+ /// <summary>
+ /// Utility method to check if context is writeable and if not throw
exception.
+ /// </summary>
+ /// <exception cref="ContextException">if context is read
only</exception>
+ protected void CheckWriteable()
+ {
+ if( m_readOnly )
+ {
+ string message = "Context is read only and can not be
modified";
+ throw new ContextException( message );
+ }
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]