public static void GenerateDatabaseDatTest(int listLength, DBKind dbKind)
        {
            var typeSettingList     = CreateTypeSettingList(listLength);
            var dataSettingList     = CreateDataSettingList(listLength);
            var instance            = new DatabaseMergedData(typeSettingList, dataSettingList);
            var changedPropertyList = new List <string>();

            instance.PropertyChanged += (sender, args) => { changedPropertyList.Add(args.PropertyName); };

            DatabaseDat result       = null;
            var         errorOccured = false;

            try
            {
                result = instance.GenerateDatabaseDat(dbKind);
            }
            catch (Exception ex)
            {
                logger.Exception(ex);
                errorOccured = true;
            }

            // エラーが発生しないこと
            Assert.IsFalse(errorOccured);

            // データ数が正しいこと
            Assert.AreEqual(result.SettingList.Count, listLength);
            // DB種別が一致すること
            Assert.AreEqual(result.DBKind, dbKind);

            for (var i = 0; i < listLength; i++)
            {
                // 内容が一致すること
                Assert.AreEqual(result.SettingList[i].TypeId, dataSettingList[i].TypeId);
            }

            // プロパティ変更通知が発火していないこと
            Assert.AreEqual(changedPropertyList.Count, 0);
        }
Example #2
0
        public static void DBDataReadTest(DatabaseMergedData resultData, DBKind dbKind)
        {
            DatabaseMergedDataReader reader = null;

            DatabaseDatFilePath     datFilePath     = null;
            DatabaseProjectFilePath projectFilePath = null;

            if (dbKind == DBKind.User)
            {
                datFilePath =
                    (UserDatabaseDatFilePath)$@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\Database.dat";
                projectFilePath =
                    (UserDatabaseProjectFilePath)
                    $@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\Database.project";
                reader = new DatabaseMergedDataReader(
                    (UserDatabaseDatFilePath)datFilePath,
                    (UserDatabaseProjectFilePath)projectFilePath);
            }
            else if (dbKind == DBKind.Changeable)
            {
                datFilePath =
                    (ChangeableDatabaseDatFilePath)
                    $@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\CDatabase.dat";
                projectFilePath =
                    (ChangeableDatabaseProjectFilePath)
                    $@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\CDatabase.project";
                reader = new DatabaseMergedDataReader(
                    (ChangeableDatabaseDatFilePath)datFilePath,
                    (ChangeableDatabaseProjectFilePath)projectFilePath);
            }
            else if (dbKind == DBKind.System)
            {
                datFilePath =
                    (SystemDatabaseDatFilePath)
                    $@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\SysDatabase.dat";
                projectFilePath =
                    (SystemDatabaseProjectFilePath)
                    $@"{DatabaseMergedDataTestItemGenerator.TestWorkRootDir}\SysDatabase.project";
                reader = new DatabaseMergedDataReader(
                    (SystemDatabaseDatFilePath)datFilePath,
                    (SystemDatabaseProjectFilePath)projectFilePath);
            }
            else
            {
                Assert.Fail();
            }

            Assert.NotNull(datFilePath);
            Assert.NotNull(projectFilePath);
            Assert.NotNull(reader);

            var readResult   = false;
            var errorMessage = "";

            try
            {
                reader.ReadSync();
                readResult = true;
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
            }


            // 正しく読めること
            if (!readResult)
            {
                throw new InvalidOperationException(
                          $"Error Occured. Message : {errorMessage}");
            }

            Console.WriteLine("Read Test Clear.");

            {
                // DatabaseDat 一致チェック
                var readResultDataBytes = reader.Data.GenerateDatabaseDat().ToBinary();

                // 元のデータと一致すること
                using (var stream = new FileStream(datFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var bufLength = (int)stream.Length;
                    var buf       = new byte[bufLength];
                    stream.Read(buf, 0, bufLength);

                    if (readResultDataBytes.Length != bufLength)
                    {
                        throw new InvalidOperationException(
                                  $"Data Length Not Match. " +
                                  $"(answerLength: {bufLength}, readResultLength: {readResultDataBytes.Length})");
                    }

                    for (long i = 0; i < 0; i++)
                    {
                        if (readResultDataBytes[i] != buf[i])
                        {
                            throw new InvalidOperationException(
                                      $"Data Byte Not Match. (index: {i}, answer: {buf[i]}," +
                                      $" readResult: {readResultDataBytes[i]})");
                        }
                    }
                }

                // 意図したデータと一致すること
                var resultDataBytes = resultData.GenerateDatabaseDat().ToBinary().ToArray();

                if (resultDataBytes.Length != readResultDataBytes.Length)
                {
                    throw new InvalidOperationException(
                              $"Data Length Not Match. " +
                              $"(answerLength: {resultDataBytes.Length}, readResultLength: {readResultDataBytes.Length})");
                }

                for (long i = 0; i < 0; i++)
                {
                    if (resultDataBytes[i] != readResultDataBytes[i])
                    {
                        throw new InvalidOperationException(
                                  $"Data Byte Not Match. (index: {i}, answer: {resultDataBytes[i]}," +
                                  $" readResult: {readResultDataBytes[i]})");
                    }
                }
            }

            {
                // DatabaseProject 一致チェック
                var readResultDataBytes = reader.Data.GenerateDatabaseProject().ToBinary();

                // 元のデータと一致すること
                using (var stream = new FileStream(projectFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var bufLength = (int)stream.Length;
                    var buf       = new byte[bufLength];
                    stream.Read(buf, 0, bufLength);

                    if (readResultDataBytes.Length != bufLength)
                    {
                        throw new InvalidOperationException(
                                  $"Data Length Not Match. " +
                                  $"(answerLength: {bufLength}, readResultLength: {readResultDataBytes.Length})");
                    }

                    for (long i = 0; i < 0; i++)
                    {
                        if (readResultDataBytes[i] != buf[i])
                        {
                            throw new InvalidOperationException(
                                      $"Data Byte Not Match. (index: {i}, answer: {buf[i]}," +
                                      $" readResult: {readResultDataBytes[i]})");
                        }
                    }
                }

                // 意図したデータと一致すること
                var resultDataBytes = resultData.GenerateDatabaseProject().ToBinary().ToArray();

                if (resultDataBytes.Length != readResultDataBytes.Length)
                {
                    throw new InvalidOperationException(
                              $"Data Length Not Match. " +
                              $"(answerLength: {resultDataBytes.Length}, readResultLength: {readResultDataBytes.Length})");
                }

                for (long i = 0; i < 0; i++)
                {
                    if (resultDataBytes[i] != readResultDataBytes[i])
                    {
                        throw new InvalidOperationException(
                                  $"Data Byte Not Match. (index: {i}, answer: {resultDataBytes[i]}," +
                                  $" readResult: {readResultDataBytes[i]})");
                    }
                }
            }

            Assert.True(true);
        }