public void PackRoundTripTest() { // TODO: Create new package based on a directory (See the Load test) var packageRoot = WriteTestPackage("RoundTripPackage"); Package target = new Package(packageRoot, true); Package unpackedTarget = null; using (System.IO.MemoryStream packedStream = new System.IO.MemoryStream()) { target.Pack(packedStream); packedStream.Seek(0, System.IO.SeekOrigin.Begin); System.IO.Directory.Delete(System.IO.Path.Combine(PackageTestRoot, "RoundTripPackage"), true); unpackedTarget = new Package(packedStream); unpackedTarget.Unpack(PackageTestRoot); } Assert.IsNotNull(unpackedTarget); Assert.AreEqual(target.ID, unpackedTarget.ID); Assert.AreEqual(target.Name, unpackedTarget.Name); Assert.AreEqual(target.Files.Count(), unpackedTarget.Files.Count()); Assert.AreEqual(target.References.Count(), unpackedTarget.References.Count()); Assert.AreEqual(target.ComponentLocations.Count(), unpackedTarget.ComponentLocations.Count()); Assert.AreEqual(target.TypeLocations.Count(), unpackedTarget.TypeLocations.Count()); for (int i = 0; i < target.Files.Count(); ++i) { var targetFile = target.Files.ElementAt(i); var unpackedFile = unpackedTarget.Files.ElementAt(i); Assert.AreEqual(targetFile.ID, unpackedFile.ID); } for (int i = 0; i < target.References.Count(); ++i) { Assert.AreEqual(target.References.ElementAt(i), unpackedTarget.References.ElementAt(i)); } for (int i = 0; i < target.ComponentLocations.Count(); ++i) { Assert.AreEqual(target.ComponentLocations.ElementAt(i), unpackedTarget.ComponentLocations.ElementAt(i)); } for (int i = 0; i < target.TypeLocations.Count(); ++i) { Assert.AreEqual(target.TypeLocations.ElementAt(i), unpackedTarget.TypeLocations.ElementAt(i)); } // and lets make sure the files came through too. Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "RoundTripPackage.manifest"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "coest.xml"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "coest1.xml"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Data", "randomfile.something"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Components", "Importer.dll"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "Types", "DictionaryTermWeights.dll"))); Assert.IsTrue(System.IO.File.Exists(System.IO.Path.Combine(packageRoot, "somerandomfile.xml"))); }
public void SaveManifestTest() { var name = "TestPackage"; var packageRoot = WriteTestPackage(name); // Load the package Package target = new Package(packageRoot, true); var manifestFile = System.IO.Path.Combine(PackageTestRoot, name, "TestPackage.manifest"); var newManifestFile = System.IO.Path.Combine(PackageTestRoot, name, name + ".manifest"); // Move the manifest to a backup System.IO.File.Move(manifestFile, manifestFile + ".bak"); // Compare the written manifest with the m anifest that is saved in the test resources Assert.IsFalse(System.IO.File.Exists(newManifestFile)); // Force the save of the manifest target.SaveManifest(); // Compare the written manifest with the m anifest that is saved in the test resources Assert.IsTrue(System.IO.File.Exists(newManifestFile)); var backup = System.IO.File.ReadAllText(manifestFile + ".bak", Encoding.UTF8); var manifest = System.IO.File.ReadAllText(newManifestFile, Encoding.UTF8); Assert.IsTrue(backup.SequenceEqual(manifest)); }
public void SetDirectoryHasTypes() { string packageName = "SetDirectoryHasTypesPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); string components = System.IO.Path.Combine(target.Location, "Components"); string types = System.IO.Path.Combine(target.Location, "Types"); string data = System.IO.Path.Combine(target.Location, "Files"); System.IO.Directory.CreateDirectory(components); System.IO.Directory.CreateDirectory(types); System.IO.Directory.CreateDirectory(data); WriteFile(System.IO.Path.Combine(components, "Importer.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Components.Importer.dll"); WriteFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Types.DictionaryTermWeights.dll"); target.AddFile(System.IO.Path.Combine(components, "Importer.dll")); target.AddFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll")); Assert.AreEqual(0, target.ComponentLocations.Count()); Assert.AreEqual(0, target.TypeLocations.Count()); target.SetDirectoryHasTypes(types, true); Assert.AreEqual(1, target.TypeLocations.Count()); Assert.AreEqual(types, target.TypeLocations.ElementAtOrDefault(0)); target.SetDirectoryHasTypes(types, false); Assert.AreEqual(0, target.TypeLocations.Count()); Assert.AreEqual(0, target.ComponentLocations.Count()); }
public void AddFileTest() { string packageName = "AddFileTestPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); string components = System.IO.Path.Combine(target.Location, "Components"); string types = System.IO.Path.Combine(target.Location, "Types"); string data = System.IO.Path.Combine(target.Location, "Files"); System.IO.Directory.CreateDirectory(components); System.IO.Directory.CreateDirectory(types); System.IO.Directory.CreateDirectory(data); WriteFile(System.IO.Path.Combine(data, "coest.xml"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Data.coest.xml"); WriteFile(System.IO.Path.Combine(target.Location, "randomfile.something"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Data.randomfile.something"); WriteFile(System.IO.Path.Combine(components, "Importer.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Components.Importer.dll"); WriteFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll"), "TraceLab.Core.Test.PackageSystem.PackageSystemTestResources.Types.DictionaryTermWeights.dll"); target.AddFile(System.IO.Path.Combine(data, "coest.xml")); target.AddFile(System.IO.Path.Combine(target.Location, "randomfile.something")); target.AddFile(System.IO.Path.Combine(components, "Importer.dll")); target.AddFile(System.IO.Path.Combine(types, "DictionaryTermWeights.dll")); // TODO: Verify that files are added correctly Assert.AreEqual(4, target.Files.Count()); }
public void PackageLoadTest() { string packageName = "TestPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); Package target = new Package(packageDirectory, true); Assert.AreEqual("A939607B-7159-4466-88C4-A869044F81C6", target.ID); Assert.AreEqual("TestPackage", target.Name); Assert.AreEqual(2, target.References.Count()); Assert.AreEqual("1886E5F6-27CA-4D42-AB51-7B7BBD9E97C1", target.References.ElementAt(0).ID); Assert.AreEqual("E6D01E22-7C9E-4B75-B7C3-6FC8B33A9593", target.References.ElementAt(1).ID); Assert.AreEqual(1, target.ComponentLocations.Count()); Assert.AreEqual("./Components", target.ComponentLocations.ElementAt(0)); Assert.AreEqual(1, target.TypeLocations.Count()); Assert.AreEqual("./Types", target.TypeLocations.ElementAt(0)); Assert.AreEqual(6, target.Files.Count()); foreach (PackageFile file in target.Files) { Assert.AreEqual(target, file.Owner); string filePath = target.GetAbsolutePath(file); using (System.IO.FileStream reader = System.IO.File.OpenRead(filePath)) { Assert.AreEqual(reader.Length, file.UncompressedSize, "File does not match expected uncompressed size: " + file.Path); Assert.AreEqual(Package.ComputeHash(reader), file.UncompressedHash, "File does not match expected uncompressed hash: " + file.Path); reader.Seek(0, System.IO.SeekOrigin.Begin); // We can't validate the specific size of the compressed data or the hash of the compressed data // because the compression algorithm can change based on the runtime. And we don't need to validate the round-trip since // PackageFile already tests that. } } }
public void PackageConstructorTest() { string packageName = "AnEmptyPackage"; string packageDirectory = System.IO.Path.Combine(PackageTestRoot, packageName); System.IO.Directory.CreateDirectory(packageDirectory); Package target = new Package(packageDirectory, false); Guid id; Assert.IsTrue(Guid.TryParse(target.ID, out id)); Assert.AreEqual(packageName, target.Name); Assert.AreEqual(System.IO.Path.Combine(PackageTestRoot, packageName), target.Location); Assert.AreEqual(0, target.Files.Count()); Assert.AreEqual(0, target.References.Count()); Assert.AreEqual(0, target.TypeLocations.Count()); Assert.AreEqual(0, target.ComponentLocations.Count()); }
/// <summary> /// Unpacks this package into the specified directory. /// </summary> /// <param name="baseDirectory">The directory to use.</param> public static Package Unpack(string rootLocation, Stream packageStream) { XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; XmlReader reader = XmlReader.Create(packageStream, settings); XPathDocument doc = new XPathDocument(reader); // Read the name so we can know where to create it. var nav = doc.CreateNavigator(); var nameNode = nav.SelectSingleNode("/Package/@Name"); string name = nameNode.Value; string packageDir = System.IO.Path.Combine(rootLocation, name); System.IO.Directory.CreateDirectory(packageDir); Package newPackage = new Package(packageDir, false); newPackage.ReadPackage(nav); newPackage.SaveManifest(); return newPackage; }