Example #1
0
        public void AppPackage_Basic()
        {
            string       tempPath = GetTempFileName();
            string       tempDir  = Helper.AddTrailingSlash(Path.GetTempPath());
            string       testPath = tempDir + "Test.txt";
            AppPackage   package  = null;
            MemoryStream ms;
            StreamReader reader;

            try
            {
                // Generate the package

                package = AppPackage.Create(tempPath, AppRef.Parse("appref://myapps/mypackage.zip?version=1.2.3.4"), @"
LaunchType   = Test.MyType:MyAssembly.dll;
LaunchMethod = Foo;
LaunchArgs   = Bar;
");

                using (StreamWriter writer = new StreamWriter(testPath))
                {
                    for (int i = 0; i < 4000; i++)
                    {
                        writer.WriteLine("Hello World!");
                    }
                }

                package.AddFile(testPath, tempDir);

                using (ms = new MemoryStream(4096))
                {
                    for (int i = 0; i < 4096; i++)
                    {
                        ms.WriteByte((byte)i);
                    }

                    ms.Position = 0;
                    package.AddFile("Test.dat", ms);
                }

                package.Close();
                package = null;

                // Verify that the package can be opened and the contents look good.

                package = AppPackage.Open(tempPath);

                Assert.AreEqual(new AppRef("appref://myapps/mypackage.zip?version=1.2.3.4"), package.AppRef);
                Assert.AreEqual(new Version(1, 2, 3, 4), package.Version);

                Assert.AreEqual("Test.MyType:MyAssembly.dll", package.Settings.Get("LaunchType"));
                Assert.AreEqual("Foo", package.Settings.Get("LaunchMethod"));
                Assert.AreEqual("Bar", package.Settings.Get("LaunchArgs"));

                Assert.IsTrue(package.ContainsFile("Test.txt"));
                Assert.IsTrue(package.ContainsFile("Test.dat"));

                ms = new MemoryStream();
                package.CopyFile("Test.txt", ms);
                ms.Position = 0;
                reader      = new StreamReader(ms);
                for (int i = 0; i < 4000; i++)
                {
                    Assert.AreEqual("Hello World!", reader.ReadLine());
                }

                string tempPath2 = Path.GetTempFileName();

                try
                {
                    using (var fs = new FileStream(tempPath2, FileMode.Create, FileAccess.ReadWrite))
                    {
                        package.CopyFile("test.dat", fs);
                        Assert.AreEqual(4096, fs.Length);
                        fs.Position = 0;

                        for (int i = 0; i < 4096; i++)
                        {
                            Assert.AreEqual((byte)i, fs.ReadByte());
                        }
                    }
                }
                finally
                {
                    File.Delete(tempPath2);
                }

                package.Close();
                package = null;
            }
            finally
            {
                if (package != null)
                {
                    package.Close();
                }

                Delete(tempPath);
                Delete(testPath);
            }
        }