示例#1
0
        public override void DatabaseSpecificTearDown()
        {
            //legacy API database connection close
            SqlCeContextGuardian.CloseBackgroundConnection();

            TestHelper.ClearDatabase();
        }
 public static bool VerifyConnection(string connectionString)
 {
     using (var conn = SqlCeContextGuardian.Open(connectionString))
     {
         return(conn.State == ConnectionState.Open);
     }
 }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandType"></param>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(
            string connectionString,
            CommandType commandType,
            string commandText,
            params SqlCeParameter[] commandParameters
            )
        {
            object retVal;

            try
            {
                using (SqlCeConnection conn = SqlCeContextGuardian.Open(connectionString))
                {
                    using (SqlCeCommand cmd = new SqlCeCommand(commandText, conn))
                    {
                        AttachParameters(cmd, commandParameters);
                        Debug.WriteLine("---------------------------------SCALAR-------------------------------------");
                        Debug.WriteLine(commandText);
                        Debug.WriteLine("----------------------------------------------------------------------------");
                        retVal = cmd.ExecuteScalar();
                    }
                }

                return(retVal);
            }
            catch (Exception ee)
            {
                throw new SqlCeProviderException("Error running Scalar: \nSQL Statement:\n" + commandText + "\n\nException:\n" + ee.ToString());
            }
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandType"></param>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static SqlCeDataReader ExecuteReader(
            string connectionString,
            CommandType commandType,
            string commandText,
            params SqlCeParameter[] commandParameters
            )
        {
            try
            {
                Debug.WriteLine("---------------------------------READER-------------------------------------");
                Debug.WriteLine(commandText);
                Debug.WriteLine("----------------------------------------------------------------------------");
                SqlCeDataReader reader;
                SqlCeConnection conn = SqlCeContextGuardian.Open(connectionString);

                try
                {
                    SqlCeCommand cmd = new SqlCeCommand(commandText, conn);
                    AttachParameters(cmd, commandParameters);
                    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    conn.Close();
                    throw;
                }

                return(reader);
            }
            catch (Exception ee)
            {
                throw new SqlCeProviderException("Error running Reader: \nSQL Statement:\n" + commandText + "\n\nException:\n" + ee.ToString());
            }
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="commandType"></param>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        public static int ExecuteNonQuery(
            string connectionString,
            CommandType commandType,
            string commandText,
            params SqlCeParameter[] commandParameters
            )
        {
            try
            {
                int rowsAffected;
                using (SqlCeConnection conn = SqlCeContextGuardian.Open(connectionString))
                {
                    // this is for multiple queries in the installer
                    if (commandText.Trim().StartsWith("!!!"))
                    {
                        commandText = commandText.Trim().Trim('!');
                        string[] commands   = commandText.Split('|');
                        string   currentCmd = String.Empty;

                        foreach (string cmd in commands)
                        {
                            try
                            {
                                currentCmd = cmd;
                                if (!String.IsNullOrWhiteSpace(cmd))
                                {
                                    SqlCeCommand c = new SqlCeCommand(cmd, conn);
                                    c.ExecuteNonQuery();
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine("*******************************************************************");
                                Debug.WriteLine(currentCmd);
                                Debug.WriteLine(e);
                                Debug.WriteLine("*******************************************************************");
                            }
                        }
                        return(1);
                    }
                    else
                    {
                        Debug.WriteLine("----------------------------------------------------------------------------");
                        Debug.WriteLine(commandText);
                        Debug.WriteLine("----------------------------------------------------------------------------");
                        SqlCeCommand cmd = new SqlCeCommand(commandText, conn);
                        AttachParameters(cmd, commandParameters);
                        rowsAffected = cmd.ExecuteNonQuery();
                    }
                }

                return(rowsAffected);
            }
            catch (Exception ee)
            {
                throw new SqlCeProviderException("Error running NonQuery: \nSQL Statement:\n" + commandText + "\n\nException:\n" + ee.ToString());
            }
        }
示例#6
0
 private void CloseDbConnections()
 {
     //Ensure that any database connections from a previous test is disposed.
     //This is really just double safety as its also done in the TearDown.
     if (ApplicationContext != null && DatabaseContext != null && DatabaseContext.Database != null)
     {
         DatabaseContext.Database.Dispose();
     }
     SqlCeContextGuardian.CloseBackgroundConnection();
 }
示例#7
0
        public static bool VerifyConnection(string connectionString)
        {
            bool isConnected = false;

            using (SqlCeConnection conn = SqlCeContextGuardian.Open(connectionString))
            {
                isConnected = conn.State == ConnectionState.Open;
            }

            return(isConnected);
        }
示例#8
0
        private void CloseDbConnections()
        {
            // just to be sure, although it's also done in TearDown
            if (ApplicationContext != null &&
                ApplicationContext.DatabaseContext != null &&
                ApplicationContext.DatabaseContext.ScopeProvider != null)
            {
                ApplicationContext.DatabaseContext.ScopeProvider.Reset();
            }

            SqlCeContextGuardian.CloseBackgroundConnection();
        }
        protected void PrepareConnection()
        {
            if (_connection == null)
            {
                _connection = SqlCeContextGuardian.Open(ConnectionStringProvider.GetConncetionString());
            }

            if (_connection.State != ConnectionState.Open)
            {
                _connection.Open();
            }
        }
示例#10
0
        public override void DatabaseSpecificTearDown()
        {
            //legacy API database connection close
            SqlCeContextGuardian.CloseBackgroundConnection();

            string filePath = string.Concat(Path, "\\UmbracoPetaPocoTests.sdf");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="commandType"></param>
 /// <param name="commandText"></param>
 /// <param name="commandParameters"></param>
 /// <returns></returns>
 public static SqlCeDataReader ExecuteReader(
     string connectionString,
     CommandType commandType,
     string commandText,
     params SqlCeParameter[] commandParameters
     )
 {
     try
     {
         var conn = SqlCeContextGuardian.Open(connectionString);
         return(ExecuteReaderTry(conn, null, commandText, commandParameters));
     }
     catch (Exception ee)
     {
         throw new SqlCeProviderException("Error running Reader: \nSQL Statement:\n" + commandText + "\n\nException:\n" + ee.ToString());
     }
 }
示例#12
0
        public virtual void TearDown()
        {
            if (ApplicationContext != null)
            {
                if (DatabaseContext != null && DatabaseContext.Database != null)
                {
                    DatabaseContext.Database.Dispose();
                }
                //reset the app context
                ApplicationContext.ApplicationCache.ClearAllCache();
            }

            SqlSyntaxContext.SqlSyntaxProvider = null;

            //legacy API database connection close - because a unit test using PetaPoco db-layer can trigger the usage of SqlHelper we need to ensure that a possible connection is closed.
            SqlCeContextGuardian.CloseBackgroundConnection();

            ApplicationContext.Current = null;
            Resolution.IsFrozen        = false;
            RepositoryResolver.Reset();
            SqlSyntaxProvidersResolver.Reset();

            TestHelper.CleanContentDirectories();

            string path = TestHelper.CurrentAssemblyDirectory;

            AppDomain.CurrentDomain.SetData("DataDirectory", null);

            SettingsForTests.Reset();
            UmbracoSettings.ResetSetters();

            try
            {
                string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error <BaseDatabaseFactoryTest>("Could not remove the old database file", ex);

                //We will swallow this exception! That's because a sub class might require further teardown logic.
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="commandType"></param>
 /// <param name="commandText"></param>
 /// <param name="commandParameters"></param>
 public static int ExecuteNonQuery(
     string connectionString,
     CommandType commandType,
     string commandText,
     params SqlCeParameter[] commandParameters
     )
 {
     try
     {
         using (var conn = SqlCeContextGuardian.Open(connectionString))
         {
             return(ExecuteNonQueryTry(conn, null, commandText, commandParameters));
         }
     }
     catch (Exception ee)
     {
         throw new SqlCeProviderException("Error running NonQuery: \nSQL Statement:\n" + commandText + "\n\nException:\n" + ee.ToString());
     }
 }
示例#14
0
        public virtual void TearDown()
        {
            SqlSyntaxContext.SqlSyntaxProvider = null;
            Resolution.Reset();

            TestHelper.CleanContentDirectories();

            Path = TestHelper.CurrentAssemblyDirectory;
            AppDomain.CurrentDomain.SetData("DataDirectory", null);

            //legacy API database connection close
            SqlCeContextGuardian.CloseBackgroundConnection();

            string filePath = string.Concat(Path, "\\UmbracoPetaPocoTests.sdf");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
示例#15
0
        public virtual void Initialize()
        {
            TestHelper.SetupLog4NetForTests();
            TestHelper.InitializeContentDirectories();

            string path = TestHelper.CurrentAssemblyDirectory;

            AppDomain.CurrentDomain.SetData("DataDirectory", path);

            //Ensure that any database connections from a previous test is disposed. This is really just double safety as its also done in the TearDown.
            if (ApplicationContext != null && DatabaseContext != null)
            {
                DatabaseContext.Database.Dispose();
            }
            SqlCeContextGuardian.CloseBackgroundConnection();

            try
            {
                //Delete database file before continueing
                string filePath = string.Concat(path, "\\UmbracoPetaPocoTests.sdf");
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (Exception)
            {
                //if this doesn't work we have to make sure everything is reset! otherwise
                // well run into issues because we've already set some things up
                TearDown();
                throw;
            }

            RepositoryResolver.Current = new RepositoryResolver(
                new RepositoryFactory());

            SqlSyntaxProvidersResolver.Current = new SqlSyntaxProvidersResolver(
                new List <Type> {
                typeof(MySqlSyntaxProvider), typeof(SqlCeSyntaxProvider), typeof(SqlServerSyntaxProvider)
            })
            {
                CanResolveBeforeFrozen = true
            };

            //Get the connectionstring settings from config
            var settings = ConfigurationManager.ConnectionStrings[Core.Configuration.GlobalSettings.UmbracoConnectionName];

            ConfigurationManager.AppSettings.Set(Core.Configuration.GlobalSettings.UmbracoConnectionName, @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\UmbracoPetaPocoTests.sdf");

            //Create the Sql CE database
            var engine = new SqlCeEngine(settings.ConnectionString);

            engine.CreateDatabase();

            Resolution.Freeze();
            ApplicationContext.Current = new ApplicationContext(
                //assign the db context
                new DatabaseContext(new DefaultDatabaseFactory()),
                //assign the service context
                new ServiceContext(new PetaPocoUnitOfWorkProvider(), new FileUnitOfWorkProvider(), new PublishingStrategy()))
            {
                IsReady = true
            };

            InitializeDatabase();

            //ensure the configuration matches the current version for tests
            SettingsForTests.ConfigurationStatus = UmbracoVersion.Current.ToString(3);
        }