migrate all tests to use MCX3 embedded Mini OMS

This commit is contained in:
Michael Becker 2024-08-25 13:42:28 -04:00
parent 1b531ef5fc
commit ba6ee8e5ae
9 changed files with 125 additions and 39 deletions

View File

@ -22,6 +22,14 @@ namespace Mocha.Core;
public abstract class LibraryPlugin : Plugin public abstract class LibraryPlugin : Plugin
{ {
protected virtual void LoadInternal(Stream stream, Library library)
{
throw new NotSupportedException();
}
public void Load(Stream stream, Library library)
{
LoadInternal(stream, library);
}
protected abstract void LoadInternal(string filename, Library library); protected abstract void LoadInternal(string filename, Library library);
public void Load(string filename, Library library) public void Load(string filename, Library library)
{ {

View File

@ -1060,6 +1060,61 @@ public abstract class Oms
return LibraryHandle.Empty; return LibraryHandle.Empty;
} }
/// <summary>
/// Loads the specified library file into memory. To add it to the tenant,
/// first <see cref="SelectTenant" /> the desired tenant, and then call
/// <see cref="AddLibraryReference" />.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public LibraryHandle LoadLibrary(Stream stream)
{
// some examples:
// .mcl compiled binary library file (fastest?)
// /path/to/directory containing a bunch of XML / JSON / YAML files
// .mcz ZIP archive of XML / JSON / YAML files (slowest?)
LibraryHandle lh = LibraryHandle.Create();
Library lib = new Library();
// _libraries[name] = lh;
// get a list of all library loaders we have implemented
LibraryPlugin[] plugins = Plugin.Get<LibraryPlugin>();
foreach (LibraryPlugin plugin in plugins)
{
Console.WriteLine("found plugin: {0}", plugin.GetType().FullName);
try
{
// if passed a directory, will enumerate all the
// .xml / .json / .yaml files in the directory and
// subdirectories
plugin.Load(stream, lib);
break;
}
catch (NotSupportedException ex)
{
}
}
// if the files have not already been compiled, we need to do two passes.
// - the first pass loads all instances into our tenant
// - the second pass applies the attributes and relationships, which depend on the
// presence of the instances
// compiling to .mcl binary library format does this all ahead of time and should
// be faster.
// let's be lazy and just look at the file extension to determine how to parse
// once all the instances have been loaded, tell the in-memory library manager
// to do its second pass "linking" the objects
lib.Link();
InitializeLibraryInternal(lh, lib);
return lh;
}
/// <summary> /// <summary>
/// Loads the specified library file into memory. To add it to the tenant, /// Loads the specified library file into memory. To add it to the tenant,
/// first <see cref="SelectTenant" /> the desired tenant, and then call /// first <see cref="SelectTenant" /> the desired tenant, and then call

View File

@ -24,9 +24,9 @@ using MBS.Core;
/// Implements <see cref="Oms" /> completely in-memory with a bare minimum schema definition which /// Implements <see cref="Oms" /> completely in-memory with a bare minimum schema definition which
/// includes Class, Attribute and subclasses, Relationship, and File for attachments. /// includes Class, Attribute and subclasses, Relationship, and File for attachments.
/// </summary> /// </summary>
public class MiniOms : MemoryOms public class HardcodedMiniOms : MemoryOms
{ {
private MiniOms() private HardcodedMiniOms()
{ {
} }
@ -42,7 +42,7 @@ public class MiniOms : MemoryOms
public List<MiniOmsModule> Modules { get; } = new List<MiniOmsModule>(); public List<MiniOmsModule> Modules { get; } = new List<MiniOmsModule>();
private MiniOms(MiniOmsModule[] modules = null) private HardcodedMiniOms(MiniOmsModule[] modules = null)
{ {
if (modules == null) if (modules == null)
{ {

View File

@ -34,10 +34,11 @@ public class McxMiniLibraryPlugin : LibraryPlugin
return ext.Equals(".mcx") || ext.Equals(".mcl"); return ext.Equals(".mcx") || ext.Equals(".mcl");
} }
protected override void LoadInternal(string filename, Library library) protected override void LoadInternal(Stream stream, Library library)
{ {
FileStream fs = File.Open(filename, FileMode.Open); base.LoadInternal(stream, library);
BinaryReader r = new BinaryReader(fs);
BinaryReader r = new BinaryReader(stream);
string signature = r.ReadFixedString(4); string signature = r.ReadFixedString(4);
if (!signature.Equals("MCX!")) if (!signature.Equals("MCX!"))
@ -170,4 +171,10 @@ public class McxMiniLibraryPlugin : LibraryPlugin
} }
} }
protected override void LoadInternal(string filename, Library library)
{
FileStream fs = File.Open(filename, FileMode.Open);
LoadInternal(fs, library);
}
} }

View File

@ -24,11 +24,6 @@ namespace Mocha.Core.Tests;
[TestFixture] [TestFixture]
public class BasicTests : OmsTestsBase public class BasicTests : OmsTestsBase
{ {
protected override Oms CreateOms()
{
return new MiniOms();
}
[Test] [Test]
public void PrerequisitesDefined() public void PrerequisitesDefined()

View File

@ -0,0 +1,26 @@
using Mocha.Core.OmsImplementations;
namespace Mocha.Core.Tests;
public class EmbeddedMiniOms : MemoryOms
{
protected override void InitializeInternal()
{
base.InitializeInternal();
TenantHandle th = CreateTenant("super");
SelectTenant(th);
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
Stream? stream = asm.GetManifestResourceStream("Mocha.Core.Tests.Resources.net.alcetech.Mocha.System.mcl");
if (stream != null)
{
LibraryHandle lh = LoadLibrary(stream);
this.AddLibraryReference(lh);
}
else
{
throw new InvalidOperationException("Manifest resource stream not found!");
}
}
}

View File

@ -23,7 +23,12 @@ public abstract class OmsTestsBase
{ {
public Oms Oms { get; private set; } public Oms Oms { get; private set; }
protected virtual Oms CreateOms() { return new MiniOms(); } protected Oms CreateOms()
{
EmbeddedMiniOms oms = new EmbeddedMiniOms();
oms.Initialize();
return oms;
}
protected readonly Guid TEST_CLASS_GUID = new Guid("{5821fc28-6411-4339-a7d2-56dc05591501}"); protected readonly Guid TEST_CLASS_GUID = new Guid("{5821fc28-6411-4339-a7d2-56dc05591501}");
protected readonly Guid TEST_CLASS2_GUID = new Guid("{6351ecb7-da5d-4029-ba3a-196a6dc980e9}"); protected readonly Guid TEST_CLASS2_GUID = new Guid("{6351ecb7-da5d-4029-ba3a-196a6dc980e9}");

View File

@ -22,11 +22,6 @@ namespace Mocha.Core.Tests;
public class RelationshipTests : OmsTestsBase public class RelationshipTests : OmsTestsBase
{ {
protected override Oms CreateOms()
{
return new MiniOms();
}
protected readonly Guid TEST_REL3_GUID = new Guid("{e1cacab7-c8a9-480d-b2b6-5b25c6c549e0}"); protected readonly Guid TEST_REL3_GUID = new Guid("{e1cacab7-c8a9-480d-b2b6-5b25c6c549e0}");
protected readonly Guid TEST_REL4_GUID = new Guid("{82b8f531-fe32-4c36-ad3a-9e0f79bc95b4}"); protected readonly Guid TEST_REL4_GUID = new Guid("{82b8f531-fe32-4c36-ad3a-9e0f79bc95b4}");
protected readonly Guid TEST_REL5_GUID = new Guid("{b80995cf-6a2d-44c2-9c93-ebb80c9be802}"); protected readonly Guid TEST_REL5_GUID = new Guid("{b80995cf-6a2d-44c2-9c93-ebb80c9be802}");

View File

@ -25,11 +25,6 @@ namespace Mocha.Core.Tests;
public class WorkSetTests : OmsTestsBase public class WorkSetTests : OmsTestsBase
{ {
protected override Oms CreateOms()
{
return new MiniOms();
}
[Test] [Test]
public void NoValidClassConstraints() public void NoValidClassConstraints()
{ {