390 lines
13 KiB
C#
390 lines
13 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using ln.types.odb.ng;
|
|
using ln.types.odb.ng.storage;
|
|
using ln.types.odb.ng.storage.fs;
|
|
using ln.types.odb.ng.storage.session;
|
|
using ln.types.odb.values;
|
|
using NUnit.Framework;
|
|
using ln.types.odb.ng.mappings;
|
|
using ln.types.odb.ng.diff;
|
|
using System.Runtime.CompilerServices;
|
|
namespace ln.types.test
|
|
{
|
|
[TestFixture()]
|
|
public class ODBTests
|
|
{
|
|
[SetUp()]
|
|
public void Prepare()
|
|
{
|
|
if (Directory.Exists("odb.test"))
|
|
Directory.Delete("odb.test", true);
|
|
}
|
|
|
|
|
|
[TestCase()]
|
|
public void TestDocument()
|
|
{
|
|
Document a = new Document();
|
|
a["feld1"] = new ODBStringValue("1");
|
|
|
|
Document a2 = a.Clone() as Document;
|
|
Document b = new Document();
|
|
a.CloneTo(b);
|
|
|
|
Assert.AreEqual(a, a2);
|
|
Assert.IsTrue(a.CompareTo(a2) == 0);
|
|
|
|
Assert.AreNotEqual(a, b);
|
|
Assert.IsTrue(a.CompareTo(b) == 0);
|
|
|
|
Assert.AreNotEqual(a2, b);
|
|
Assert.IsTrue(a2.CompareTo(b) == 0);
|
|
}
|
|
|
|
[TestCase(ExpectedException = typeof(IOException))]
|
|
public void DoubleOpen()
|
|
{
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
IStorageContainer container2 = new FSStorageContainer("odb.test").Open();
|
|
}
|
|
}
|
|
|
|
[Test()]
|
|
public void StorageTest()
|
|
{
|
|
Guid documentID;
|
|
String documentString;
|
|
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
using (IStorage storage = storageContainer.GetStorage("test1"))
|
|
{
|
|
storage.Open();
|
|
|
|
Document document = new Document();
|
|
|
|
storage.Save(document);
|
|
|
|
Assert.AreNotEqual(Guid.Empty, document.ID);
|
|
|
|
document[new ODBStringValue("FeldA")] = new ODBStringValue("FeldA");
|
|
document[new ODBStringValue("FeldB")] = new ODBStringValue("FeldB");
|
|
Document nestedDocument = new Document();
|
|
nestedDocument["EineZeichenkette"] = new ODBStringValue("Ich bin eine Zeichenkette");
|
|
document[new ODBStringValue("FeldDokument")] = nestedDocument;
|
|
|
|
storage.Save(document);
|
|
|
|
documentString = document.ToString();
|
|
Console.WriteLine("documentString={0}", documentString);
|
|
|
|
Assert.AreEqual(1, storage.GetDocumentIDs().Count());
|
|
|
|
documentID = document.ID;
|
|
}
|
|
}
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
using (IStorage storage = storageContainer.GetStorage("test1"))
|
|
{
|
|
storage.Open();
|
|
|
|
Assert.AreEqual(1, storage.GetDocumentIDs().Count());
|
|
|
|
Document document = storage.Load(storage.GetDocumentIDs().First());
|
|
|
|
Assert.AreEqual(documentString, document.ToString());
|
|
|
|
Assert.AreEqual(documentID, document.ID);
|
|
|
|
Assert.AreEqual(new ODBStringValue("FeldA"), document[new ODBStringValue("FeldA")]);
|
|
Assert.AreEqual(new ODBStringValue("FeldB"), document[new ODBStringValue("FeldB")]);
|
|
|
|
storage.Delete(document.ID);
|
|
|
|
Assert.AreEqual(0, storage.GetDocumentIDs().Count());
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestCase()]
|
|
public void TestStorageRecycling()
|
|
{
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
using (IStorage storage = storageContainer.GetStorage("testRecycle"))
|
|
{
|
|
storage.Open();
|
|
String dataFileName = (storage as SegmentedFileStorage).DataFileName;
|
|
|
|
Document[] documents = new Document[512];
|
|
|
|
for (int n=0;n<documents.Length;n++)
|
|
{
|
|
documents[n] = new Document();
|
|
storage.Save(documents[n]);
|
|
}
|
|
//(storage as SegmentedFileStorage).Sync();
|
|
|
|
long startSize = new FileInfo(dataFileName).Length;
|
|
|
|
for (int n = 0; n < documents.Length; n++)
|
|
{
|
|
storage.Save(documents[n]);
|
|
//(storage as SegmentedFileStorage).Sync();
|
|
|
|
long newSize = new FileInfo(dataFileName).Length;
|
|
long grew = newSize - startSize;
|
|
|
|
Assert.AreEqual(4096, grew);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Person[] persons = new Person[]{
|
|
new Person()
|
|
{
|
|
FirstName="Harald",
|
|
MiddleNames = new string[]{ "Christian", "Joachim" },
|
|
LastName = "Wolff-Thobaben",
|
|
Age=39
|
|
},
|
|
new Person()
|
|
{
|
|
FirstName="Liesschen",
|
|
LastName = "Müller",
|
|
Age=39
|
|
},
|
|
};
|
|
|
|
[TestCase()]
|
|
public void TestClassMapping()
|
|
{
|
|
ClassMapping classMapping = new ClassMapping(typeof(Person));
|
|
|
|
Document doc1 = classMapping.MapValue(Mapper.Default, persons[0]) as Document;
|
|
Document doc2 = doc1.Clone() as Document;
|
|
|
|
Person p2 = classMapping.UnmapValue(Mapper.Default, doc2) as Person;
|
|
|
|
Assert.AreEqual(RuntimeHelpers.GetHashCode(persons[0]), RuntimeHelpers.GetHashCode(p2));
|
|
|
|
Assert.AreEqual(persons[0], p2);
|
|
Assert.IsTrue(Test.FieldsAreEqual(persons[0], p2));
|
|
|
|
Document doc2b = classMapping.MapValue(Mapper.Default, p2) as Document;
|
|
|
|
Assert.AreEqual(doc2, doc2b);
|
|
Assert.IsTrue(doc2.CompareTo(doc2b) == 0);
|
|
|
|
Assert.AreNotEqual(ODBNull.Instance, doc2["Something"]);
|
|
Assert.AreEqual(doc2["Something"], doc2b["Something"]);
|
|
}
|
|
|
|
[TestCase()]
|
|
public void TestMapper()
|
|
{
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
Mapper mapper = new Mapper(storageContainer);
|
|
|
|
mapper.Save<Person>(persons[0]);
|
|
mapper.Save<Person>(persons[1]);
|
|
|
|
Assert.AreEqual(2, mapper.Load<Person>().Count());
|
|
Assert.AreEqual(0, mapper.Load<Person>(Query.Equals<Person>("FirstName", "Wolfgang")).Count());
|
|
Assert.AreEqual(1, mapper.Load<Person>(Query.Equals<Person>("FirstName", "Harald")).Count());
|
|
Assert.AreEqual(1, mapper.Load<Person>(Query.Equals<Person>("FirstName", "Liesschen")).Count());
|
|
|
|
Person harald = mapper.Load<Person>(Query.Equals<Person>("FirstName", "Harald")).FirstOrDefault();
|
|
|
|
Assert.AreEqual(persons[0], harald);
|
|
}
|
|
|
|
}
|
|
|
|
[TestCase()]
|
|
public void TestConcurrentAccess()
|
|
{
|
|
using (IStorageContainer storageContainer = new FSStorageContainer("odb.test").Open())
|
|
{
|
|
using (
|
|
SessionStorageContainer
|
|
ssc1 = new SessionStorageContainer(storageContainer),
|
|
ssc2 = new SessionStorageContainer(storageContainer)
|
|
)
|
|
{
|
|
using (IStorage
|
|
storage1 = ssc1.GetStorage("test2"),
|
|
storage2 = ssc2.GetStorage("test2")
|
|
)
|
|
{
|
|
Document origin = new Document();
|
|
|
|
origin["FeldA"] = new ODBStringValue("OriginalA");
|
|
origin["FeldB"] = new ODBStringValue("OriginalB");
|
|
origin["FeldC"] = new ODBStringValue("OriginalC");
|
|
|
|
storage1.Save(origin);
|
|
|
|
Document secondary = storage2.Load(origin.ID);
|
|
|
|
Assert.IsTrue(origin.CompareTo(secondary) == 0);
|
|
|
|
secondary["FeldB"] = new ODBStringValue("ZweiB");
|
|
storage2.Save(secondary);
|
|
|
|
Assert.IsFalse(origin.CompareTo(secondary) == 0);
|
|
|
|
Document reloaded1 = storage1.Load(origin.ID);
|
|
Assert.AreSame(reloaded1, origin);
|
|
|
|
Assert.IsFalse(secondary.CompareTo(reloaded1) == 0);
|
|
Assert.IsFalse(secondary.CompareTo(origin) == 0);
|
|
|
|
storage1.Refresh(reloaded1);
|
|
|
|
Assert.IsTrue(secondary.CompareTo(reloaded1) == 0);
|
|
Assert.IsTrue(secondary.CompareTo(origin) == 0);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
[TestCase()]
|
|
public void TestDiff()
|
|
{
|
|
Document docA = new Document();
|
|
Document docB, docC, docD;
|
|
Document[] spareDocuments = new Document[16];
|
|
|
|
for (int n = 0; n < spareDocuments.Length; n++)
|
|
{
|
|
spareDocuments[n] = new Document();
|
|
spareDocuments[n]["spareIdx"] = new ODBInteger(n);
|
|
spareDocuments[n]["value"] = new ODBInteger(n);
|
|
spareDocuments[n]["valueB"] = new ODBStringValue("valueB");
|
|
spareDocuments[n]["valueC"] = new ODBStringValue("valueC");
|
|
}
|
|
|
|
ODBList subDocuments = new ODBList();
|
|
docA["documents"] = subDocuments;
|
|
|
|
for (int n = 0; n < 8; n++)
|
|
subDocuments.Add(spareDocuments[n].Clone());
|
|
|
|
docB = docA.Clone() as Document;
|
|
docD = docA.Clone() as Document;
|
|
|
|
for (int n = 8; n < 12; n++)
|
|
{
|
|
(docA["documents"] as ODBList).Add(spareDocuments[n].Clone());
|
|
}
|
|
|
|
for (int n = 12; n < 16; n++)
|
|
{
|
|
(docB["documents"] as ODBList).Add(spareDocuments[n].Clone());
|
|
(docD["documents"] as ODBList).Add(spareDocuments[n].Clone());
|
|
}
|
|
|
|
docC = docA.Clone() as Document;
|
|
|
|
for (int n = 0; n < 4; n++)
|
|
{
|
|
((docC["documents"] as ODBList)[6] as Document)["value"] = new ODBInteger(100 + n);
|
|
(docC["documents"] as ODBList).RemoveAt(2);
|
|
|
|
(docD["documents"] as ODBList).RemoveAt(2);
|
|
}
|
|
|
|
for (int n = 0; n < 2; n++)
|
|
{
|
|
((docD["documents"] as ODBList)[2 + n] as Document)["value"] = new ODBInteger(100 + n);
|
|
}
|
|
|
|
Console.WriteLine("docA:\n{0}", docA.TreeString);
|
|
Console.WriteLine("docB:\n{0}", docB.TreeString);
|
|
Console.WriteLine("docC:\n{0}", docC.TreeString);
|
|
|
|
Diff diffAB = Diff.Construct(docA, docB);
|
|
Diff diffAC = Diff.Construct(docA, docC);
|
|
|
|
Console.WriteLine("diffAB: {0}", diffAB.TreeString);
|
|
Console.WriteLine("diffAC: {0}", diffAC.TreeString);
|
|
|
|
Document docB2 = docA.Clone() as Document;
|
|
Document docC2 = docA.Clone() as Document;
|
|
|
|
diffAB.Apply(docB2);
|
|
diffAC.Apply(docC2);
|
|
|
|
Console.WriteLine("docB2:\n{0}", docB2.TreeString);
|
|
Console.WriteLine("docC2:\n{0}", docB2.TreeString);
|
|
|
|
Assert.AreEqual(docB, docB2);
|
|
Assert.IsTrue(docB.CompareTo(docB2) == 0);
|
|
|
|
Assert.AreEqual(docC, docC2);
|
|
Assert.IsTrue(docC.CompareTo(docC2) == 0);
|
|
|
|
Document docD2 = docA.Clone() as Document;
|
|
diffAB.Apply(docD2);
|
|
diffAC.Apply(docD2);
|
|
|
|
Console.WriteLine("docD:\n{0}", docD.TreeString);
|
|
Console.WriteLine("docD2:\n{0}", docD2.TreeString);
|
|
|
|
Assert.AreEqual(docD, docD2);
|
|
Assert.IsTrue(docD.CompareTo(docD2) == 0);
|
|
|
|
Document docD3 = docA.Clone() as Document;
|
|
diffAB.Apply(docD3);
|
|
diffAC.Apply(docD3);
|
|
|
|
Console.WriteLine("docD3:\n{0}", docD3.TreeString);
|
|
|
|
Assert.AreEqual(docD, docD3);
|
|
Assert.IsTrue(docD.CompareTo(docD3) == 0);
|
|
|
|
|
|
}
|
|
|
|
public class Person
|
|
{
|
|
public readonly Guid ID;
|
|
|
|
public String FirstName = "";
|
|
public String[] MiddleNames = new string[0];
|
|
public String LastName = "";
|
|
|
|
public int Age = 18;
|
|
|
|
public Something Something = new Something();
|
|
|
|
public Person()
|
|
{
|
|
ID = Guid.NewGuid();
|
|
}
|
|
|
|
public override int GetHashCode() => ID.GetHashCode();
|
|
public override bool Equals(object obj) => (obj is Person) && ID.Equals((obj as Person).ID);
|
|
}
|
|
|
|
public class Something
|
|
{
|
|
public Guid ID = Guid.NewGuid();
|
|
}
|
|
|
|
}
|
|
}
|