[Mono-list] MarshalByRefObject and Activator NUnit v2.0 test classes
Jean-Marc André
jean-marc.andre@polymtl.ca
Wed, 12 Feb 2003 16:17:18 -0500
This is a multi-part message in MIME format.
--------------080908050204050508000903
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Hi,
Here are 2 test classes for System.Activator and System.MarshalByRefObject.
I am currently working on a RemontingServices test class...
Since these are my 2 first test classes, I would really appreciate any
feedback (I already sent ActivatorTest.cs but nobody told me about so...).
The both test classes are using TcpChannel so they have to be compiled
with a reference to System.Runtime.Remoting.dll
Tanks,
JM
--------------080908050204050508000903
Content-Type: text/plain;
name="ActivatorTest.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ActivatorTest.cs"
// System.Activator NUnit v2.0 test class
//
// Jean-Marc Andre (jean-marc.Andre@polymtl.ca)
//
//
using System;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using NUnit.Framework;
namespace MonoTests.System.ActivatorTestInternal
{
// We need a COM class to test the Activator class
[ComVisible(true)]
public class COMTest: MarshalByRefObject
{
public COMTest()
{
id = 0;
}
// This property is visible
[ComVisible(true)]
public int Id
{
get { return id; }
set { id = value; }
}
public COMTest(int id)
{
this.id = id;
}
private int id;
public bool constructorFlag = false;
}
}
namespace MonoTests.System
{
using MonoTests.System.ActivatorTestInternal;
[TestFixture]
public class ActivatorTest
{
public ActivatorTest()
{}
[Test]
[Ignore("Activator.CreateComInstanceForm is not yet implemented")]
// This test is ignored for the moment because
// CreateComInstanceFrom() is not implemented yet
// by the mono Activator class
public void CreateComInstanceFrom()
{
ObjectHandle objHandle = Activator.CreateComInstanceFrom(strAssembly , "COMTest");
COMTest objCOMTest = (COMTest) objHandle.Unwrap();
objCOMTest.Id = 10;
Assertion.AssertEquals("#A01",10,objCOMTest.Id);
}
[Test]
// This method tests CreateInstance()
public void CreateInstance()
{
COMTest objCOMTest;
// object CreateInstance(Type type)
objCOMTest = (COMTest) Activator.CreateInstance(typeof(COMTest));
Assertion.AssertEquals("#A02", "MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType()).ToString());
// ObjectHandle CreateInstance(string, string)
ObjectHandle objHandle;
objHandle = Activator.CreateInstance(null , "MonoTests.System.ActivatorTestInternal.COMTest");
objCOMTest = (COMTest) objHandle.Unwrap();
objCOMTest.Id = 2;
Assertion.AssertEquals("#A03", 2, objCOMTest.Id);
// object CreateInstance(Type, bool)
objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), false);
Assertion.AssertEquals("#A04", "MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType()).ToString());
// object CreateInstance(Type, object[])
object[] objArray = new object[1];
objArray[0] = 7;
objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), objArray);
Assertion.AssertEquals("#A05", 7, objCOMTest.Id);
// Todo: Implemente the test methods for
// all the overriden functions using activationAttribute
}
// This method tests GetObject from the Activator class
[Test]
public void GetObject()
{
// object GetObject(Type, string)
// This will provide a COMTest object on tcp://localhost:1234/COMTestUri
COMTest objCOMTest = new COMTest(8);
TcpChannel chnServer = new TcpChannel(1234);
ChannelServices.RegisterChannel(chnServer);
RemotingServices.SetObjectUriForMarshal(objCOMTest, "COMTestUri");
RemotingServices.Marshal(objCOMTest);
// This will get the remoting object
object objRem = Activator.GetObject(typeof(COMTest), "tcp://localhost:1234/COMTestUri");
Assertion.Assert("#A07",objRem != null);
COMTest remCOMTest = (COMTest) objRem;
Assertion.AssertEquals("#A08", 8, remCOMTest.Id);
// Todo: Implemente the test methods for
// all the overriden function using activationAttribute
chnServer.StopListening(null);
ChannelServices.UnregisterChannel(chnServer);
//RemotingServices.Disconnect(objCOMTest);
}
// This method tests the CreateInstanceFrom methods
// of the Activator class
[Test]
public void CreateInstanceFrom()
{
COMTest objCOMTest;
ObjectHandle objHandle;
objHandle = Activator.CreateInstanceFrom(strAssembly , "MonoTests.System.ActivatorTestInternal.COMTest");
Assertion.Assert("#A09", objHandle != null);
objCOMTest = (COMTest) objHandle.Unwrap();
// Todo: Implemente the test methods for
// all the overriden function using activationAttribute
}
// The name of the assembly file may be incorrect.
// I used it to test these classes but you should
// replace it with the name of the mono tests assembly file
// The name of the assembly is used to get an object through
// Activator.CreateInstance(), Activator.CreateComInstanceFrom()...
private string strAssembly = "corlib_test.dll";
}
}
--------------080908050204050508000903
Content-Type: text/plain;
name="MarshalByRefObjectTest.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="MarshalByRefObjectTest.cs"
// MarshalByRefObjectTest.cs Test class for
// System.MarshalByRefObject class
//
// Jean-Marc Andre (jean-marc.andre@polymtl.ca)
//
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Serialization;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
// Just an internal test namespace for
// the MarshalByRefObjectTest class
namespace MonoTests.System.MarshalByRefObjectTestInternal
{
// Object from this class can be marshaled
public class MarshalObject: MarshalByRefObject
{
public MarshalObject(){}
public MarshalObject(int id)
{
this.id = id;
}
// This method override the default one
// so we can set some properties of the lifetime
// of the remot object
public override object InitializeLifetimeService()
{
ILease lease = (ILease) base.InitializeLifetimeService();
// By default InitialLeaseTime is set to 5 minutes
// we set it at 10 seconds
if(lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.FromSeconds(10);
}
return lease;
}
public int Id
{
get { return id;}
}
private int id = 0;
} // MarshalObject
} // MonoTests.System.MarshalByRefObjectTestInternal
namespace MonoTests.System
{
using MonoTests.System.MarshalByRefObjectTestInternal;
using NUnit.Framework;
// The main test class
[TestFixture]
public class MarshalByRefObjectTest
{
public MarshalByRefObjectTest()
{
}
// This method test the CreateObjRef
[Test]
public void CreateObjRef()
{
MarshalObject objMarshal = new MarshalObject();
RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal1");
RemotingServices.Marshal(objMarshal);
ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));
Assertion.AssertEquals("#A01", objRef.URI, RemotingServices.GetObjectUri(objMarshal));
// TODO: When implemented in the mono RemotingServices class
//RemotingServices.Disconnect(objMarshal);
}
[Test]
[ExpectedException(typeof(RemotingException))]
public void CreateObjRefThrowException()
{
MarshalObject objMarshal = new MarshalObject();
ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));
}
// This method both tests InitializeLifetimeService()
// and GetLifetimeService()
[Test]
public void LifetimeService()
{
MarshalObject objMarshal = new MarshalObject();
RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal2");
RemotingServices.Marshal(objMarshal);
objMarshal.InitializeLifetimeService();
ILease lease = (ILease) objMarshal.GetLifetimeService();
Assertion.AssertEquals("#A02", lease.InitialLeaseTime, TimeSpan.FromSeconds(10));
// TODO: When implemented in the mono RemotingServices class
//RemotingServices.Disconnect(objMarshal);
}
// Here we test if we a published object can be get
// through a TcpChannel
[Test]
public void GetObject()
{
MarshalObject objMarshal = new MarshalObject(1);
RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal3");
RemotingServices.Marshal(objMarshal);
TcpChannel chn = new TcpChannel(1234);
ChannelServices.RegisterChannel(chn);
object objRem = Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1234/MarshalByRefObjectTest.objMarshal3");
MarshalObject objMarshalRem = (MarshalObject) objRem;
Assertion.AssertEquals("#A03", 1, objMarshalRem.Id);
// TODO: When implemented in the mono RemotingServices class
//RemotingServices.Disconnect(objMarshal);
chn.StopListening(null);
ChannelServices.UnregisterChannel(chn);
}
}
}
--------------080908050204050508000903--