Exemplo n.º 1
0
        public void Build_BasedOnPathAndMessage_ReturnBuildErrorMessage()
        {
            // Setup
            const string filePath     = "<file path>";
            const string errorMessage = "test test 1,2,3";

            // Call
            string message = new FileWriterErrorMessageBuilder(filePath).Build(errorMessage);

            // Assert
            string expectedMessage = $"Fout bij het schrijven naar bestand '{filePath}': {errorMessage}";

            Assert.AreEqual(expectedMessage, message);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new file with the basic database structure for a Riskeer database at
        /// <paramref name="databaseFilePath"/>.
        /// </summary>
        /// <param name="databaseFilePath">Path of the new database file.</param>
        /// <exception cref="ArgumentException">Thrown when either:
        /// <list type="bullet">
        /// <item><paramref name="databaseFilePath"/> is invalid</item>
        /// <item><paramref name="databaseFilePath"/> points to an existing file</item>
        /// </list></exception>
        /// <exception cref="StorageException">Thrown when executing <c>DatabaseStructure</c> script fails.</exception>
        public static void CreateDatabaseStructure(string databaseFilePath)
        {
            IOUtils.ValidateFilePath(databaseFilePath);

            if (File.Exists(databaseFilePath))
            {
                string message = $"File '{databaseFilePath}' already exists.";
                throw new ArgumentException(message);
            }

            SQLiteConnection.CreateFile(databaseFilePath);
            string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath, false);

            try
            {
                using (var dbContext = new SQLiteConnection(connectionString, true))
                {
                    dbContext.Open();
                    using (SQLiteTransaction transaction = dbContext.BeginTransaction())
                        using (SQLiteCommand command = dbContext.CreateCommand())
                        {
                            command.CommandText = Resources.DatabaseStructure;
                            command.ExecuteNonQuery();

                            transaction.Commit();
                        }
                }
            }
            catch (SQLiteException exception)
            {
                string message = new FileWriterErrorMessageBuilder(databaseFilePath).Build(Resources.Error_writing_structure_to_database);
                throw new StorageException(message, new UpdateStorageException("", exception));
            }
            finally
            {
                SQLiteConnectionHelper.ForcefullyDisposeSQLiteConnection();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a configured instance of <see cref="StorageException"/> when writing to the storage file failed.
        /// </summary>
        /// <param name="databaseFilePath">The path of the file that was attempted to connect with.</param>
        /// <param name="errorMessage">The critical error message.</param>
        /// <param name="innerException">Exception that caused this exception to be thrown.</param>
        /// <returns>Returns a new <see cref="StorageException"/>.</returns>
        private static StorageException CreateStorageWriterException(string databaseFilePath, string errorMessage, Exception innerException)
        {
            string message = new FileWriterErrorMessageBuilder(databaseFilePath).Build(errorMessage);

            return(new StorageException(message, innerException));
        }