Ejemplo n.º 1
0
        public void TestMethodDownloadAsync()
        {
            ResetLog();//重设日志
            bool isDone = false;
            bool isFail = false;

            //使用这个线程它也不会重新跳回原来的线程
            Task.Run(() =>
            {
                Runing.Increment.Log.Info("TestMethodDownload():当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);

                Console.WriteLine("UnitTestXML.GenerateXML():生成xml文件");
                IDFHelper.CreatConfigFileWithXml("../Debug/", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");

                //FileHelper.CleanDirectory("../test/Temp");

                IDF.Update("http://127.0.0.1:22333/test/IDFTest.zip", "../test/Temp", "../test/Target", "../test/Backup")
                .OnDownloadSuccess((obj) =>
                {
                    Runing.Increment.Log.Info("TestMethodDownload():进入OnDownloadSuccess当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);
                    isDone = true;
                })
                .OnError((e) =>
                {
                    Runing.Increment.Log.Info("TestMethodDownload():进入OnError当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);
                    isDone = true;
                    isFail = true;
                }).Go();
            });
            while (!isDone)
            {
                Thread.Sleep(15);
            }
            Assert.IsFalse(isFail);
        }
Ejemplo n.º 2
0
        public void TestMethodDownload()
        {
            ResetLog();//重设日志
            //SynchronizationContext contex = new SynchronizationContext();
            //SynchronizationContext.SetSynchronizationContext(contex);

            //这样调用它没有跳回原来的线程
            Runing.Increment.Log.Info("TestMethodDownload():当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("UnitTestXML.GenerateXML():生成xml文件");
            IDFHelper.CreatConfigFileWithXml("../Debug/", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");

            //FileHelper.CleanDirectory("../test/Temp");

            bool isDone = false;

            IDF.Update("http://127.0.0.1:22333/test/IDFTest.zip", "../test/Temp", "../test/Target", "../test/Backup")
            .OnDownloadSuccess((obj) =>
            {
                Runing.Increment.Log.Info("TestMethodDownload():进入OnDownloadSuccess当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);
                isDone = true;
            })
            .OnError((e) =>
            {
                Runing.Increment.Log.Info("TestMethodDownload():进入OnError当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);

                Assert.Fail();   //下载错误,认为不通过
                isDone = true;
            }).Go();

            while (!isDone)
            {
                Thread.Sleep(15);
            }
        }
Ejemplo n.º 3
0
        private void button1_Click(object sender, EventArgs ea)
        {
            ResetLog();//重设日志

            Runing.Increment.Log.Info("TestMethodDownload():当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("UnitTestXML.GenerateXML():生成xml文件");
            IDFHelper.CreatConfigFileWithXml("../Debug/", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");

            //FileHelper.CleanDirectory("../test/Temp");

            //bool isDone = false;
            IDF.Update("http://127.0.0.1:22333/test/IDFTest.zip", "../test/Temp", "../test/Target", "../test/Backup")
            .OnDownloadSuccess((obj) =>
            {
                Runing.Increment.Log.Info("TestMethodDownload():进入OnDownloadSuccess当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);
                //isDone = true;
            })
            .OnError((e) =>
            {
                Runing.Increment.Log.Info("TestMethodDownload():进入OnError当前执行线程id=" + Thread.CurrentThread.ManagedThreadId);

                //isDone = true;
            }).Go();
        }
Ejemplo n.º 4
0
        public void TestMethodCreatXML()
        {
            ResetLog();//重设日志

            for (int i = 0; i < 20; i++)
            {
                Runing.Increment.Log.Info("UnitTest.TestMethodCreatXML():生成xml文件");
                IDFHelper.CreatConfigFileWithXml("./", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");
            }
        }
Ejemplo n.º 5
0
        public void TestMethodRecoverFile()
        {
            ResetLog();//重设日志

            if (Directory.Exists("../test/Target"))
            {
                FileInfo[] fis = new DirectoryInfo("../test/Target").GetFiles("*", SearchOption.AllDirectories);
                for (int i = 0; i < fis.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        File.Delete(fis[i].FullName);
                    }
                }
            }
            Thread.Sleep(500);

            //干扰文件项
            var ws = File.CreateText(new FileInfo("../Debug/TestFile.txt").FullName);

            ws.Write("123456789123456789");
            ws.Close();

            ws = File.CreateText(new FileInfo("../test/Target/TestFile.txt").FullName);
            ws.Write("12345");
            ws.Close();

            Runing.Increment.Log.Info("UnitTest1.TestMethodRecoverFile():生成xml文件");
            IDFHelper.CreatConfigFileWithXml("../Debug/", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");

            //记录备份前目标文件的md5值
            DirectoryInfo backupBeforeDir = new DirectoryInfo("../test/Target");

            FileInfo[] backupBeforeFiles = backupBeforeDir.GetFiles("*", SearchOption.AllDirectories);
            Dictionary <string, string> backupBeforeMD5 = new Dictionary <string, string>();

            for (int i = 0; i < backupBeforeFiles.Length; i++)
            {
                FileInfo file = backupBeforeFiles[i];
                backupBeforeMD5.Add(file.FullName, MD5Helper.FileMD5(file.FullName));
            }

            bool isDone = false;

            IDF.Update("http://127.0.0.1:22333/test/IDFTest.zip", "../test/Temp", "../test/Target", "../test/Backup")
            .OnMoveFileDone((obj, success) =>
            {
                string str = File.ReadAllText(new FileInfo("../test/Target/TestFile.txt").FullName);
                Assert.IsTrue(str == "123456789123456789");

                obj.RecoverFile();
                isDone = true;
            })
            .OnDownloadSuccess((obj) =>
            {
                //关闭那些程序
                obj.MoveFile();
            })
            .OnError((e) =>
            {
                isDone = true;
            }).Go();

            while (!isDone)
            {
                Thread.Sleep(50);
            }

            //记录备份后目标文件的md5值
            Runing.Increment.Log.Info($"UnitTest1.TestMethodRecoverFile(): 开始检查备份前后的文件...");
            DirectoryInfo backupAfterDir = new DirectoryInfo("../test/Target");

            FileInfo[] backupAfterFiles = backupAfterDir.GetFiles("*", SearchOption.AllDirectories);
            Assert.IsTrue(backupBeforeMD5.Count == backupAfterFiles.Length);

            for (int i = 0; i < backupAfterFiles.Length; i++)
            {
                FileInfo file = backupAfterFiles[i];
                Assert.IsTrue(backupBeforeMD5[file.FullName] == MD5Helper.FileMD5(file.FullName));
            }

            string str2 = File.ReadAllText(new FileInfo("../test/Target/TestFile.txt").FullName);

            Assert.IsTrue(str2 == "12345");
        }
Ejemplo n.º 6
0
        public void TestMethodMoveFile2()
        {
            ResetLog();//重设日志

            Runing.Increment.Log.Info("UnitTest1.TestMethodMoveFile2():生成xml文件");
            IDFHelper.CreatConfigFileWithXml("../Debug/", "http://127.0.0.1:22333/Debug/", "../test/IDFTest.zip");

            //FileHelper.CleanDirectory("../test/Temp");
            //FileHelper.CleanDirectory("../test/Target");
            //FileHelper.CleanDirectory("../test/Backup");

            if (Directory.Exists("../test/Target"))
            {
                FileInfo[] fis = new DirectoryInfo("../test/Target").GetFiles("*.*", SearchOption.AllDirectories);
                for (int i = 0; i < fis.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        File.Delete(fis[i].FullName);
                    }
                }
            }

            Thread.Sleep(500);

            bool isDone = false;

            IDF.Update("http://127.0.0.1:22333/test/IDFTest.zip", "../test/Temp", "../test/Target", "../test/Backup")
            .OnMoveFileDone((obj, success) =>
            {
                Runing.Increment.Log.Info("移动文件成功后回调");
                isDone = true;
            })
            .OnDownloadSuccess((obj) =>
            {
                //关闭那些程序
                //异步的移动文件
                Task.Run(() => { obj.MoveFile(); });
            })
            .OnError((e) =>
            {
                isDone = true;
            }).Go();

            while (!isDone)
            {
                Thread.Sleep(50);
            }

            var xml = XmlHelper.CreatXml();

            var          fs    = File.Open(new FileInfo("../test/IDFTest.zip").FullName, FileMode.Open, FileAccess.Read);
            ZipFile      zip   = ZipFile.Read(fs);
            ZipEntry     ze    = zip.Entries.First();//第一个实体
            MemoryStream xmlms = new MemoryStream();

            ze.Extract(xmlms);
            xmlms.Position = 0;
            xml.Load(xmlms); //从下载文件流中读xml
            OriginFolder originFolder = new OriginFolder();
            var          node         = xml.DocumentElement.SelectSingleNode("./" + typeof(OriginFolder).Name);

            originFolder.FromXml(node);//从xml文件根节点反序列化
            fs.Close();

            int index = 0;

            foreach (var item in originFolder.fileItemDict.Values)
            {
                index++;
                Runing.Increment.Log.Info($"测试{index}:测试校验文件" + item.relativePath);
                string itemTarFilePath = Path.Combine(new DirectoryInfo("../test/Target").FullName, item.relativePath);
                Assert.IsTrue(MD5Helper.FileMD5(itemTarFilePath) == item.MD5);
            }
        }