internal static void CurrentTransactionIsNull()
        {
#if DEBUG
            Console.WriteLine("CurrentTransactionIsNull");
#endif
            var sourcePath =
                "Data" +
                Path.DirectorySeparatorChar +
                "copy-file-source.txt";

            var managedPath =
                "Data" +
                Path.DirectorySeparatorChar +
                "copy-file-is-new-no-current-transaction.txt";

            var manager = new CopyFileManager(sourcePath,
                                              managedPath, overwrite: true);

            FileStream stream = (FileStream)FileManagerReflection.Invoke(
                manager,
                "OnPrepareFileStream",
                new string[] { managedPath });

            ExceptionAssert.IsThrown(
                () => { manager.EnlistVolatile(EnlistmentOptions.None); },
                expectedType: typeof(InvalidOperationException),
                expectedMessage: String.Format(
                    "Cannot enlist resource {0}: no ambient transaction detected.",
                    managedPath));
        }
            public static void OnRollbackNoScope()
            {
#if DEBUG
                Console.WriteLine("FileAlreadyExists_OnRollbackNoScope");
#endif
                var sourcePath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-source.txt";

                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-already-exists-on-rollback-no-scope.txt";

                // Simulate a preparation

                bool overwrite = true;
                var  manager   = new CopyFileManager(sourcePath,
                                                     managedPath, overwrite: overwrite);

                FileStream stream = (FileStream)FileManagerReflection.Invoke(
                    manager,
                    "OnPrepareFileStream",
                    new string[] { managedPath });

                FileManagerReflection.SetStream(manager, stream);
                stream = null;

                Assert.AreEqual(overwrite, manager.CanOverwrite);
                Assert.AreEqual(true, manager.FileAlreadyExists);

                // Simulate a rollback

                FileManagerReflection.Invoke(
                    manager,
                    "OnRollback",
                    null);

                // Expected results

                Assert.IsTrue(File.Exists(managedPath));
                using (stream = File.OpenRead(managedPath))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        var content = reader.ReadLine();
                        Assert.AreEqual("existing-file", content);
                    }
                }
            }
            public static void OnRollbackNoScope(bool overwrite)
            {
#if DEBUG
                Console.WriteLine("FileIsNew_OnRollbackNoScope");
#endif
                var sourcePath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-source.txt";

                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-is-new-on-rollback-no-scope.txt";

                // Simulate a preparation

                var manager = new CopyFileManager(sourcePath,
                                                  managedPath, overwrite: overwrite);

                FileStream stream = (FileStream)FileManagerReflection.Invoke(
                    manager,
                    "OnPrepareFileStream",
                    new string[] { managedPath });

                FileManagerReflection.SetStream(manager, stream);
                stream = null;

                Assert.AreEqual(overwrite, manager.CanOverwrite);
                Assert.AreEqual(false, manager.FileAlreadyExists);

                // Simulate a rollback

                FileManagerReflection.Invoke(
                    manager,
                    "OnRollback",
                    null);

                // Expected results

                // Dispose the manager, so that
                // the following call to File.Exists
                // has the required permissions
                // to investigate the managed file.
                manager.Dispose();

                Assert.IsTrue(!File.Exists(managedPath));
            }
            public static void OnCommit(bool overwrite)
            {
#if DEBUG
                Console.WriteLine("FileIsNew_OnCommit");
#endif
                var sourcePath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-source.txt";

                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-is-new-on-commit.txt";

                var manager = new CopyFileManager(sourcePath,
                                                  managedPath, overwrite: overwrite);

                List <FileManager> managers = new List <FileManager>
                {
                    manager
                };

                void results()
                {
                    Assert.IsTrue(File.Exists(managedPath));
                    var sourceBytes = File.ReadAllBytes(sourcePath);
                    var destBytes   = File.ReadAllBytes(managedPath);

                    Assert.AreEqual(sourceBytes.Length, destBytes.Length);

                    for (int i = 0; i < sourceBytes.Length; i++)
                    {
                        Assert.AreEqual(sourceBytes[i], destBytes[i]);
                    }
                }

                void rolledBack(Exception e)
                {
                }

                TransactionScopeHelper.Using(
                    managers,
                    results,
                    rolledBack);
            }
            /// <summary>
            /// Tests the constructor when parameter <i>managedPath</i> is <b>null</b>.
            /// </summary>
            public static void ManagedPathIsNull()
            {
#if DEBUG
                Console.WriteLine("Constructor_DestinationFileNameIsNull");
#endif
                string sourcePath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "copy-file-managed-path-is-null.txt";

                string managedPath = null;

                ArgumentExceptionAssert.IsThrown(
                    () =>
                {
                    var manager =
                        new CopyFileManager(sourcePath, managedPath, overwrite: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "managedPath");
            }
        public static void Dispose(bool overwrite)
        {
#if DEBUG
            Console.WriteLine("Dispose");
#endif
            var sourcePath =
                "Data" +
                Path.DirectorySeparatorChar +
                "copy-file-source.txt";

            string managedPath =
                overwrite
                ?
                "Data" +
                Path.DirectorySeparatorChar +
                "copy-file-is-new-dispose.txt"
                :
                "Data" +
                Path.DirectorySeparatorChar +
                "copy-file-is-new-dispose-cannot-overwrite.txt";

            // Assure that the managed file is new
            // in case of overwrite: false
            if (!overwrite)
            {
                File.Delete(managedPath);
            }

            // Simulate a preparation

            var manager = new CopyFileManager(sourcePath,
                                              managedPath, overwrite: overwrite);

            Assert.AreEqual(false, FileManagerReflection.GetField(manager, "disposed"));

            FileStream stream = (FileStream)FileManagerReflection.Invoke(
                manager,
                "OnPrepareFileStream",
                new string[] { managedPath });

            FileManagerReflection.SetStream(manager, stream);
            stream = null;

            // Dispose the manager

            manager.Dispose();

            // Expected results

            Assert.AreEqual(true, FileManagerReflection.GetField(manager, "disposed"));

            // Dispose the manager, again

            ExceptionAssert.IsNotThrown(
                () => { manager.Dispose(); }
                );

            // Delete the managed file in case of overwrite: false
            if (!overwrite)
            {
                File.Delete(managedPath);
            }
        }
Exemple #7
0
        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <param name="divDeviceTagParams"></param>
        /// <param name="callback"></param>
        public IDeviceCore InitialDevice(List <DeviceTagParam> divDeviceTagParams, DataChangeEventHandler callback) //初始化设备
        {
            DeviceTags = divDeviceTagParams;
            CallBack   = callback;

            IDeviceCore deviceCore = null;

            #region 初始化主通讯设备

            switch (CommParam.CommInterface)
            {
            case DeviceCommInterface.CNC_Fanuc:     //Fanuc CNC
                deviceCore = new FocasManager(this, callback);
                break;

            case DeviceCommInterface.FanucRobot:      //Fanuc机器人
                deviceCore = new ModbusTCPManager(this, callback);
                break;

            case DeviceCommInterface.ModulaTCP:      //Modula 自动货柜
                deviceCore = new SocketClientManager(this, callback);
                break;

            case DeviceCommInterface.ABPLC:      //Modula 自动货柜
                deviceCore = new HslAllenBradleyNetManager(this, callback);
                break;

            case DeviceCommInterface.ZeissTCP:      //蔡司 三坐标
                int    serverIndex = this.CommParam.CommAddress.IndexOf('|', 0);
                string serverAdd   = this.CommParam.CommAddress.Substring(serverIndex + 1);
                serverIndex = serverAdd.IndexOf('|', 0);
                serverAdd   = serverAdd.Substring(serverIndex + 1);
                DeviceManager deviceComm = new DeviceManager(this.DevicePKNO + "_S", DeviceCommInterface.ZeissTCP, serverAdd);
                deviceCore = new SocketClientManager(this, callback);
                var serverManager = new SocketServerManager(deviceComm, callback);      //新的Server端
                break;

            case DeviceCommInterface.OPC_Classic:     //OPC Server
                //deviceCore = new OpcClassicManager(this, callback);
                break;

            case DeviceCommInterface.TCP_Custom:      //自定义TCP协议
                deviceCore = new SocketClientManager(this, callback);
                break;

            case DeviceCommInterface.TCP_Modbus:      //Modbus TCP
                deviceCore = new ModbusTCPManager(this, callback);
                break;

            case DeviceCommInterface.TCP_Server:      //TCP 服务器
                deviceCore = new SocketServerManager(this, callback);
                break;

            case DeviceCommInterface.WebApi:      //WebApi
                deviceCore = new WebApiManager(this, callback);
                break;

            case DeviceCommInterface.DataBase:     //数据库通讯
                deviceCore = new DatabaseManager(this, callback);
                break;

            case DeviceCommInterface.ShareFile:     //数据库通讯
                deviceCore = new CopyFileManager();
                break;
            }

            #endregion

            if (deviceCore != null)
            {
                if (!DeviceHelper.CommDevices.Contains(this))
                {
                    DeviceHelper.CommDevices.Add(this);                                           //添加到通讯设备中
                }
                DeviceCores.Add(deviceCore);
            }

            return(deviceCore);
        }