Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Last active August 29, 2015 14:03
Show Gist options
  • Save DinisCruz/bcd6422c4736f69ea896 to your computer and use it in GitHub Desktop.
Save DinisCruz/bcd6422c4736f69ea896 to your computer and use it in GitHub Desktop.
PoCs of scriptingTM's AppDomain from NUnit's AppDomain
var apiCassini = nUnitTests_Cassini_TeamMentor.apiCassini;
if ("TeamMentor.Schemas".assembly().isNull())
apiCassini.webRoot().mapPath("bin//TeamMentor.Schemas.dll").assembly();
var o2Proxy = apiCassini.appDomain().o2Proxy();
var tmConfig = o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", new object[] {});
tmConfig.prop("WindowsAuthentication").prop("ReaderGroup","Value from NUnit AppDomain");
o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"set_Current", new object[] { tmConfig });
return tmConfig.toXml();
//O2Ref:TeamMentor.UnitTests.TM_Website.dll
//O2Ref:FluentSharp.NUnit.dll
//O2Ref:FluentSharp.CassiniDev.dll
//using FluentSharp.CassiniDev
var apiCassini = nUnitTests_Cassini_TeamMentor.apiCassini;
var o2Proxy = apiCassini.appDomain().o2Proxy();
var tmConfig = o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", new object[] {});
//O2Ref:TeamMentor.Schemas.dll
//using TeamMentor.CoreLib
tmConfig.cast<TeamMentor.CoreLib.TMConfig>()
.WindowsAuthentication.Enabled = true;
return o2Proxy.staticInvocation("TeamMentor.Users",
"TeamMentor.CoreLib.TM_Config_Utils",
"windowsAuthentication_Enabled",
new object[] { tmConfig });
//O2Ref:TeamMentor.UnitTests.TM_Website.dll
//O2Ref:FluentSharp.NUnit.dll
//O2Ref:FluentSharp.CassiniDev.dll
//using FluentSharp.CassiniDev
[Test] public void Confirm_That_We_Can_Access_Variables_And_Methods_Across_AppDomains()
{
apiCassini.url().GET().assert_Contains("TeamMentor"); // make one request so that TM Startup is invoked
if ("TeamMentor.Schemas".assembly().isNull()) //ensure that this TeamMentor.Schemas.dll exists in the current AppDomain
apiCassini.webRoot().mapPath("bin//TeamMentor.Schemas.dll").assembly();
"TeamMentor.Schemas.dll".assembly().assert_Not_Null();
var o2Proxy = apiCassini.appDomain().o2Proxy().assert_Not_Null();
Func<object> get_TMConfig = () => o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", null);
Action<object> set_TMConfig = (value)=> o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"set_Current", new [] {value});
var tmConfig = get_TMConfig().assert_Not_Null();
var originalValue = "TM_Reader";
var newValue = "TM_Reader".add_5_RandomLetters();
tmConfig.prop("WindowsAuthentication").prop<string>("ReaderGroup") // get current value
.assert_Is_Equal_To(originalValue);
tmConfig.prop("WindowsAuthentication").prop("ReaderGroup", newValue); // change it it
var tmConfig_BeforeSave = get_TMConfig().assert_Not_Null(); // get another copy of TMConfig
set_TMConfig(tmConfig); // push current changes to server
var tmConfig_AfterSave = get_TMConfig().assert_Not_Null(); // get a copy of the modified TMconfig
tmConfig_BeforeSave.prop("WindowsAuthentication").prop<string>("ReaderGroup")
.assert_Is_Equal_To(originalValue);
tmConfig_AfterSave .prop("WindowsAuthentication").prop<string>("ReaderGroup")
.assert_Is_Equal_To(newValue);
// "TM Site".add_IE().open(apiCassini.url())
// .parentForm_WaitForClose();
}
[Test] public void Create_User_Via_MarshalByRefObject_Proxies()
{
var tmFileStorage = (TM_FileStorage)o2Proxy.staticInvocation("TeamMentor.FileStorage",
"TeamMentor.FileStorage.TM_FileStorage",
"get_Current", null);
var tmConfig = (TMConfig )o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", null);
tmFileStorage.assert_Not_Null();
tmConfig .assert_Not_Null();
TMConfig .Current = tmConfig;
TM_FileStorage .Current = tmFileStorage;
TM_UserData .Current = tmFileStorage.UserData;
TM_Xml_Database.Current = tmFileStorage.TMXmlDatabase;
var currentUsers = TM_UserData.Current.TMUsers.assert_Not_Empty();
var sizeBefore = currentUsers.size();
// this shows the interresting problem
var tmUsers = TM_UserData.Current.TMUsers; // note how the existing ones are MarshalByRef objects
tmUsers.Add(new TMUser()); // but the new one will not be
var tmUser = TM_UserData.Current.createUser(); // call ExtensionMethod TeamMentor.UserData.Users_Creation.createUser((this TM_UserData userData)
var sizeAfter = TM_UserData.Current.TMUsers.size(); // there is no difference because the .Add was not propagated into the MarshalByRef object
sizeAfter.assert_Is(sizeBefore); // confirming that the sizes are the same
tmFileStorage.users_Load(); // triggering a load will clear the TMUsers list since the loaded TMUsers objects cannot be added
// because they are not MarshalByRef objects
TM_UserData.Current.TMUsers.size().assert_Is(0); // this confirms that the TMUsers is now Empty
}
[Test] public void Confirm_That_We_Can_Access_TM_StartUp_As_MarshalByRefObject()
{
var tmStartUp = (TM_StartUp)o2Proxy.staticInvocation("TeamMentor.AspNet",
"TeamMentor.CoreLib.TM_StartUp",
"get_Current", new object[] {});
//check that we got a valid MarshalByRef object
tmStartUp.assert_Not_Null();
//check that we can get the Version property (which is of type: System.String)
tmStartUp.Version.assert_Not_Null()
.assert_Is(typeof(TM_StartUp).assembly().version());
//check that we can get the TMEngine property (which is of type: TeamMentor.CoreLib.TM_Engine )
tmStartUp.TMEngine.assert_Not_Null();
//check that we can get the TrackingApplication property (which is of type: TeamMentor.CoreLib.Tracking_Application )
tmStartUp.TrackingApplication.assert_Not_Null();
//check that we can get the TmFileStorage property (which is of type: TeamMentor.FileStorage.TM_FileStorage )
tmStartUp.TmFileStorage.assert_Not_Null();
//check that (from tmStartUp.TmFileStorage) we can access the main TM (in memory) Database Objects
tmStartUp.TmFileStorage.TMXmlDatabase .assert_Not_Null();
tmStartUp.TmFileStorage.UserData .assert_Not_Null();
tmStartUp.TmFileStorage.Server .assert_Not_Null();
tmStartUp.TmFileStorage.WebRoot .assert_Folder_Exists();
tmStartUp.TmFileStorage.Path_UserData .assert_Folder_Exists();
tmStartUp.TmFileStorage.Path_SiteData .assert_Folder_Exists();
tmStartUp.TmFileStorage.Path_XmlDatabase .assert_Folder_Exists();
tmStartUp.TmFileStorage.Path_XmlLibraries .assert_Folder_Exists();
tmStartUp.TmFileStorage.GuidanceExplorers_Paths .assert_Not_Null();//.assert_Not_Empty();
tmStartUp.TmFileStorage.GuidanceItems_FileMappings.assert_Not_Null();//.assert_Not_Empty();
}
[Test] public void Create_User_Via_MarshalByRefObject_Proxies()
{
var tmFileStorage = (TM_FileStorage)o2Proxy.staticInvocation("TeamMentor.FileStorage",
"TeamMentor.FileStorage.TM_FileStorage",
"get_Current", null);
var tmConfig = (TMConfig )o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", null);
tmFileStorage.assert_Not_Null();
tmConfig .assert_Not_Null();
var sizeBefore = tmFileStorage.UserData.TMUsers.size();
var tmUser = (TMUser)o2Proxy.staticInvocation("TeamMentor.Users",
"TeamMentor.UserData.Users_Creation",
"createUser", new object[] {tmFileStorage.UserData});
tmUser.assert_Not_Null(); // confirm that we received a valid TMUser object
tmUser.UserName.assert_Valid(); // and that the UserName value is set
tmUser.UserID.assert_Bigger_Than(1); // and that the UserID is a valid number
var sizeAfter = tmFileStorage.UserData.TMUsers.size(); // get an updated value of TMUsers.size
sizeAfter.assert_Is(sizeBefore + 1);
}
[Test] public void Confirm_That_We_Can_Access_Variables_Across_AppDomains_Using_Reflection()
{
Func<object> get_TMConfig = () => o2Proxy.staticInvocation("TeamMentor.Schemas",
"TeamMentor.CoreLib.TMConfig",
"get_Current", null);
var tmConfig = get_TMConfig().assert_Not_Null(); // this is an MarshalByRefObject
var originalValue = "TM_Reader";
var newValue = "TM_Reader".add_5_RandomLetters();
tmConfig.prop("WindowsAuthentication").prop<string>("ReaderGroup") // get current value
.assert_Is_Equal_To(originalValue);
tmConfig.prop("WindowsAuthentication").prop("ReaderGroup", newValue); // change it it, with no need to call
// TMConfig.set_Current(tmConfig)
var tmConfig_AfterChange = get_TMConfig().assert_Not_Null(); // get a copy of the modified TMconfig
tmConfig_AfterChange.prop("WindowsAuthentication").prop<string>("ReaderGroup")
.assert_Is_Equal_To(newValue); // confirm change
tmConfig.prop("WindowsAuthentication").prop("ReaderGroup", originalValue); // restore originalValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment