Example #1
0
        //Opens add test form
        private void menuAddTest_Click(object sender, System.EventArgs e)
        {
            if (null != this.database)
            {
                string sqlStmt = "SELECT name FROM sqlite_master WHERE type='table' AND name='TestCase';";

                SqliteVm vm = new SqliteVm(this.database, true);

                if (Sqlite.Row == vm.Execute(sqlStmt))
                {
                    //Hack...if the form is not visible then the user must have closed it->create a new one
                    //If it is visible bring it to focus
                    if (!this.addTestForm.Visible)
                    {
                        this.addTestForm = new SqliteGUI.NewAddTestForm();
                        this.addTestForm.LoadForm(database);
                        this.addTestForm.MdiParent = this;
                        this.addTestForm.Show();
                    }
                    else
                    {
                        this.addTestForm.Activate();
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("TestCase table does not exist!",
                                                         "Error",
                                                         System.Windows.Forms.MessageBoxButtons.OK,
                                                         System.Windows.Forms.MessageBoxIcon.Hand);
                }
                vm.SqlFinalize();
            }
        }
Example #2
0
        public TestExecutionRun(string dbPath, ITestExecutorCollection exec, bool generateDump = false)
        {
            _executors = exec;
            _generateDump = generateDump;
            _unitTestDb = new SqliteDb();
            _unitTestDb.Open(dbPath);

            _unitTestVm = new SqliteVm(_unitTestDb, false);
            _unitTestResultVm = new SqliteVm(_unitTestDb, false);
        }
Example #3
0
        public TestExecutionRun(string dbPath, ITestExecutorCollection exec, bool generateDump = false)
        {
            _executors    = exec;
            _generateDump = generateDump;
            _unitTestDb   = new SqliteDb();
            _unitTestDb.Open(dbPath);

            _unitTestVm       = new SqliteVm(_unitTestDb, false);
            _unitTestResultVm = new SqliteVm(_unitTestDb, false);
        }
Example #4
0
        private void NewDatabase(bool template)
        {
            saveFileDialog1.Filter           = "db files (*.db)|*.db|All files (*.*)|*.*";
            saveFileDialog1.RestoreDirectory = true;
            SqliteDb db = null;

            string dbName = "";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                dbName = saveFileDialog1.FileName;
                if (!IsReadOnly(dbName))
                {
                    //Close the existing database
                    if (null != this.database)
                    {
                        this.CloseDatabase();
                    }

                    //Delete the file
                    this.DeleteFile(dbName);

                    //Open the database. SQLite will create the file for us
                    db = new SqliteDb();
                    db.Open(dbName);

                    //Create a template database for the unit test infrastructure
                    if (template && (db != null))
                    {
                        SqliteVm templateVm = new SqliteVm(db, true);
                        templateVm.Execute("CREATE TABLE TestCase (ExecuteSequence INTEGER, TestName TEXT, ParamSets TEXT, Description TEXT, TestType TEXT, Prerequisite TEXT)");
                        templateVm.Execute("CREATE TABLE Params (ParamSet INTEGER, ParamName TEXT, ParamValue TEXT)");
                        templateVm.Execute("CREATE TABLE CommonParams (ParamName text, ParamValue text)");
                        templateVm.Execute("CREATE TABLE ApiTestResults (Description TEXT, ParamSet INTEGER, ContentType TEXT, Result TEXT)");
                        templateVm.Execute("CREATE TABLE HttpTestResults (Description TEXT, ParamSet INTEGER, ContentType TEXT, Result BLOB)");
                        templateVm.SqlFinalize();
                        templateVm = null;
                    }
                    db.Close();

                    //Open the new database
                    this.OpenDatabase(dbName);
                }
                else
                {
                    string errMsg;
                    errMsg = String.Format("File {0} is readOnly and cannot be replaced", dbName);
                    System.Windows.Forms.MessageBox.Show(errMsg,
                                                         "Error",
                                                         System.Windows.Forms.MessageBoxButtons.OK,
                                                         System.Windows.Forms.MessageBoxIcon.Hand);
                    this.NewDatabase(template);
                }
            }
        }
Example #5
0
 public override void Dispose()
 {
     _unitTestVm.SqlFinalize();
     _unitTestVm = null;
     try
     {
         _unitTestDb.Close();
     }
     catch { }
     _unitTestDb = null;
 }
Example #6
0
        protected PlatformApiTestExecutor(string opName, string apiType, string unitTestVm)
        {
            _opName         = opName;
            _apiType        = apiType;
            _unitTestVmPath = unitTestVm;

            _unitTestDb = new SqliteDb();
            _unitTestDb.Open(_unitTestVmPath);

            _unitTestVm = new SqliteVm(_unitTestDb, true);
        }
Example #7
0
        protected PlatformApiTestExecutor(string opName, string apiType, string unitTestVm)
        {
            _opName = opName;
            _apiType = apiType;
            _unitTestVmPath = unitTestVm;

            _unitTestDb = new SqliteDb();
            _unitTestDb.Open(_unitTestVmPath);

            _unitTestVm = new SqliteVm(_unitTestDb, true);
        }
Example #8
0
        public static bool ReadCommonParameterValue(this SqliteVm vm, string paramName, NameValueCollection result)
        {
            int stat = vm.Execute("Select ParamValue from CommonParams WHERE ParamName=\"{0}\"", paramName);

            if (stat == Sqlite.Row)
            {
                result.Add(paramName, vm.GetString("ParamValue"));
                return(true);
            }
            return(false);
        }
Example #9
0
 public override void Dispose()
 {
     _unitTestVm.SqlFinalize();
     _unitTestVm = null;
     try
     {
         _unitTestDb.Close();
     }
     catch { }
     _unitTestDb = null;
 }
Example #10
0
        private void OpenDatabase(string fName)
        {
            if (database != null)
            {
                CloseDatabase();
            }

            if (fName.Length > 0)
            {
                database = new SqliteDb();
                int err = database.Open(fName);
                if (err != 0)
                {
                    database = null;
                }
            }

            if (database != null)
            {
                databaseName = fName;

                treeView1.BeginUpdate();

                // Clear the TreeView each time the method is called.
                treeView1.Nodes.Clear();

                TreeNode databaseNode = new TreeNode(fName);

                SqliteVm vm        = new SqliteVm(database, true);
                string   tableList = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";

                int fini = vm.Execute(tableList);
                while (fini == Sqlite.Row)
                {
                    string tableName = vm.GetString("name");

                    databaseNode.Nodes.Add(tableName);

                    fini = vm.NextRow();
                }
                vm.SqlFinalize();
                vm = null;

                treeView1.Nodes.Add(databaseNode);

                treeView1.EndUpdate();
                //Enable menu items
                this.menuItem3.Enabled      = true;
                this.menuAddTest.Enabled    = true;
                this.saveAsMenuItem.Enabled = true;
                this.saveDbMenuItem.Enabled = true;
            }
        }
Example #11
0
 public void Dispose()
 {
     _unitTestResultVm.SqlFinalize();
     _unitTestResultVm = null;
     _unitTestVm.SqlFinalize();
     _unitTestVm = null;
     try
     {
         _unitTestDb.Close();
     }
     catch { }
     _unitTestDb = null;
 }
Example #12
0
 public void Dispose()
 {
     _unitTestResultVm.SqlFinalize();
     _unitTestResultVm = null;
     _unitTestVm.SqlFinalize();
     _unitTestVm = null;
     try
     {
         _unitTestDb.Close();
     }
     catch { }
     _unitTestDb = null;
 }
Example #13
0
        public static bool ReadParameterValue(this SqliteVm vm, int paramSetId, string paramName, NameValueCollection result, bool bIsPath = false)
        {
            int stat = vm.Execute("Select ParamValue from Params WHERE ParamSet={0} AND ParamName=\"{1}\"", paramSetId, paramName);

            if (stat == Sqlite.Row)
            {
                string str = vm.GetString("ParamValue");
                if (bIsPath)
                {
                    str = CommonUtility.GetPath(str);
                }
                result.Add(paramName, str);
                return(true);
            }
            return(false);
        }
Example #14
0
        public int Execute(ref int testsRun, ITestLogger logger, bool isEnterprise)
        {
            int    exitStatus = 0;
            string dbPath     = CommonUtility.GetDbPath(this.DumpFile);
            string dbName     = CommonUtility.GetPath(dbPath);

            if (File.Exists(dbName))
            {
                var db = new SqliteDb();
                db.Open(dbName);

                var vm = new SqliteVm(db, true);

                int status = vm.Execute("Select TestName, TestType from TestCase where TestType=\"{0}\" order by ExecuteSequence", this.ApiType);

                //NOTE: We can't share the SqliteVm instance among our executor objects as this messes up query results
                //we must be able to re-create a new SqliteVm for each executor, so we pass down the db path
                SetupExecutors(dbName);

                while (status == Sqlite.Row)
                {
                    string testName = vm.GetString("TestName");
                    string testType = vm.GetString("TestType");

                    Console.WriteLine("Executing {0} test: {1}", testType, testName);
                    using (var run = new TestExecutionRun(dbPath, this))
                    {
                        try
                        {
                            exitStatus += run.RunTests(testName, logger, ref testsRun);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            exitStatus += 1;
                        }
                    }
                    status = vm.NextRow();
                }
                vm.SqlFinalize();
                vm = null;
                db = null;
            }
            return(exitStatus);
        }
Example #15
0
        public static NameValueCollection SetCommonParams(int paramSet, SqliteDb db)
        {
            NameValueCollection result = null;
            var vm = new SqliteVm(db, false);

            try
            {
                result = new NameValueCollection();

                vm.ReadParameterValue(paramSet, "OPERATION", result);
                vm.ReadCommonParameterValue("VERSION", result);
                vm.ReadCommonParameterValue("CREDENTIALS", result);
                vm.ReadCommonParameterValue("LOCALE", result);

                vm = null;
            }
            catch (MgException ex)
            {
                throw new UnitTestException(string.Format("Exception from MapGuide:\n{0}", ex.GetDetails()));
            }
            catch (Exception ex)
            {
                try
                {
                    vm = null;
                    vm = new SqliteVm(db, true);
                    vm.ReadParameterValue("VERSION", result);
                    vm.ReadParameterValue("CREDENTIALS", result);
                    vm.ReadParameterValue("LOCALE", result);
                }
                catch (Exception ex2)
                {
                }
            }
            return(result);
        }
Example #16
0
        private void SqlExecute_Click(object sender, System.EventArgs e)
        {
            if (richTextBox1.Text.Length > 0 && database != null)
            {
                SqliteVm vm     = new SqliteVm(database, true);
                string   sql    = richTextBox1.Text;
                int      sqlErr = Sqlite.Error;
                try
                {
                    sqlErr = vm.Execute(sql);
                }
                catch (SEHException)
                {
                    // Do nothing.  Execution failed...
                }

                if (sqlErr != Sqlite.Done && sqlErr != Sqlite.Ok)
                {
                    string errMsg = vm.GetErrMsg();
                    errMsg += "\n";
                    errMsg += vm.GetQueryTail();
                    MessageBox.Show(errMsg, "Sqlite Execution Error");

                    vm.SqlFinalize();
                    vm = null;
                }
                else
                {
                    vm.SqlFinalize();
                    vm = null;

                    // reload database
                    OpenDatabase(databaseName);
                }
            }
        }
Example #17
0
        public static int Execute(this SqliteVm vm, string sql, params object[] args)
        {
            string formattedSql = string.Format(sql, args);

            return(vm.Execute(formattedSql));
        }
Example #18
0
        private static void AddLogFileEntry(ITestLogger logger, string operation, int paramSetId, SqliteVm vm)
        {
            logger.Write("\nParamSet: {0}\n", paramSetId);
            int status = vm.Execute("Select * from Params where ParamSet={0}", paramSetId);

            while (status == Sqlite.Row)
            {
                string paramName  = vm.GetString("ParamName");
                string paramValue = vm.GetString("ParamValue");

                logger.Write("{0}: {1}\n", paramName, paramValue);
                status = vm.NextRow();
            }
        }
Example #19
0
        private static void AddLogFileEntry(ITestLogger logger, string operation, int paramSetId, SqliteVm vm)
        {
            logger.Write("\nParamSet: {0}\n", paramSetId);
            int status = vm.Execute("Select * from Params where ParamSet={0}", paramSetId);

            while (status == Sqlite.Row)
            {
                string paramName = vm.GetString("ParamName");
                string paramValue = vm.GetString("ParamValue");

                logger.Write("{0}: {1}\n", paramName, paramValue);
                status = vm.NextRow();
            }
        }
Example #20
0
        public override int ValidateRequest(SqliteDotNet.SqliteDb db, string testName, int paramSetId, string operation, TestResult actualResult, ITestLogger logger)
        {
            int      exitStatus = 0;
            string   outcome    = "pass";
            SqliteVm vm         = new SqliteVm(db, false);

            object expectedResult = null;

            //If we have an exception we need to remove the stack trace because different line numbers will fail the test
            object resultData = CommonUtility.RemoveStackTraceFromResult(actualResult.ResultData);
            //Get the mime type based on the content type in the result
            string mimeType = actualResult.ContentType;

            //If we have exception message we need to remove any parts that may contain system dependent information
            //Ex. file paths
            resultData = CommonUtility.ProcessExceptionMessage(resultData);
            //Get the file extension that will be used for a dump
            string actualExtension = CommonUtility.GetExtensionFromMimeType(mimeType);

            //If we have an ALWAYSPASS parameter defined for the operation then skip the whole validation process
            //This parameter should only be used for clean up operations that are no related with the tests
            if (vm.Execute("Select ParamValue from Params where ParamName=\"ALWAYSPASS\" and ParamSet={0}", paramSetId) != Sqlite.Row)
            {
                //TestName is Test_[ServiceType]
                string type     = testName.Substring(testName.IndexOf("_") + 1);
                string filePath = CommonUtility.GetPath(string.Format("../../TestData/{0}/DumpFiles/{0}ApiTest", type));
                string fileName = string.Format("{0}_{1}.{2}", filePath, paramSetId, actualExtension);

                if (this.TestExecutionMode == "dump")
                {
                    //File.WriteAllText(fileName, resultData);
                    throw new NotImplementedException("The .net test runner does not support dumping of test results. Please use the PHP test runner for this purpose");
                }
                else
                {
                    //This section is special case handling for the operations that return different data after each call
                    resultData = CommonUtility.SpecialDataHandling(operation, resultData, mimeType);

                    if (this.TestExecutionMode == "generate")
                    {
                        throw new NotImplementedException("The .net test runner does not support test update/generation. Please use the PHP test runner for this purpose");

                        /*
                         * //Get the sample result that is stored in the database. If we are using file on disk for validation
                         * //then do not overwrite the filename in the database
                         * //To distinguish between sample data and filename all filenames should be prefixed with "@@"
                         * int status = vm.Execute("Select Result from ApiTestResults where ParamSet={0}", paramSetId);
                         * string sampleResult = vm.GetString("Result");
                         *
                         * if (!sampleResult.StartsWith("@@"))
                         * {
                         *  //Insert the sample data as a BLOB
                         *  //Update the row for that param set or create a new row if we do not have it yet
                         *
                         *  string responseBody = "";
                         *  if (status == Sqlite.Row)
                         *  {
                         *      vm.Prepare("update ApiTestResults set Result = :blob where ParamSet={0}", paramSetId);
                         *  }
                         *  else
                         *  {
                         *      Console.WriteLine("A new row has been created in ApiTestResults table to store the result for operation {0}", paramSetId);
                         *      Console.WriteLine("Please update the description field for that row later");
                         *      vm.Prepare("INSERT INTO ApiTestResults(ParamSet, Result) VALUES({0}, :blob)", paramSetId);
                         *  }
                         *
                         *  byte[] bytes = Encoding.UTF8.GetBytes(resultData);
                         *  vm.BindBlob(":blob", bytes);
                         *  vm.Execute();
                         *
                         *  if (mimeType != null)
                         *  {
                         *      vm.Execute("UPDATE ApiTestResults SET ContentType=\"{0}\" WHERE ParamSet={1}", mimeType, paramSetId);
                         *  }
                         *
                         *  File.WriteAllText(fileName, resultData);
                         * }
                         */
                    }
                    else if (this.TestExecutionMode == "validate" || this.TestExecutionMode == "show")
                    {
                        string resultContent = "";

                        //Get the sample result and the expected content type from the database
                        int          status = vm.Execute("Select Result, ContentType from ApiTestResults where ParamSet={0}", paramSetId);
                        string       expectedContentType = vm.GetString("ContentType");
                        string       expectedExtension   = CommonUtility.GetExtensionFromMimeType(expectedContentType);
                        SqliteGcBlob blob = vm.GetBlob("Result");

                        byte[] b = blob.Read();
                        if (b != null)
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                            {
                                expectedResult = Encoding.UTF8.GetString(b);
                            }
                            else
                            {
                                expectedResult = b;
                            }
                        }
                        else
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                            {
                                expectedResult = string.Empty;
                            }
                            else
                            {
                                expectedResult = null;
                            }
                        }
                        string strExpectedResult = expectedResult as string;
                        //If we are validating from a file then get the contents of that file
                        //File names should be prefixed with "@@" to distinguish them from BLOB data
                        if (strExpectedResult != null && strExpectedResult.StartsWith("@@"))
                        {
                            string sampleDataFile = strExpectedResult.Substring(2);
                            sampleDataFile = CommonUtility.GetPath(sampleDataFile);
                            expectedResult = File.ReadAllText(sampleDataFile);
                        }

                        if (this.TestExecutionMode == "validate")
                        {
                            bool   bEqual        = false;
                            byte[] bExpected     = expectedResult as byte[];
                            byte[] bActual       = resultData as byte[];
                            string strResultData = resultData as string;

                            //FIXME: We're not processing DWF content properly to do this check properly. So just
                            //pass these for now
                            if (operation == "GETDRAWINGLAYER" || operation == "GETDRAWINGSECTION")
                            {
                                bEqual = true;
                            }
                            else
                            {
                                if (strExpectedResult != null && strResultData != null)
                                {
                                    //Normalize line endings on LF before comparsion (in case the SQLite GUI recorded CRLFs)
                                    string normStrResultData     = strResultData.Replace("\r\n", "\n");
                                    string normStrExpectedResult = strExpectedResult.Replace("\r\n", "\n");

                                    bEqual = normStrResultData.Equals(normStrExpectedResult, StringComparison.InvariantCultureIgnoreCase);
                                }
                                else if (bExpected != null && bActual != null)
                                {
                                    bEqual = CommonUtility.ByteArraysEqual(bExpected, bActual, operation, testName);
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: {0} - {1} - Encountered disparate data types between expected and actual results. Expecting test failure :(", testName, operation));
                                }
                            }
                            //If the results are different and special validation fails then the operation failed ->mark it red
                            if (!bEqual && !CommonUtility.SpecialValidation(operation, resultData, expectedResult))
                            {
                                outcome    = "fail";
                                exitStatus = 1;

                                if (expectedExtension != "xml" && expectedExtension != "html" && expectedExtension != "txt")
                                {
                                    expectedResult = "Unable to display binary data";
                                }

                                if (actualExtension != "xml" && actualExtension != "html" && actualExtension != "txt")
                                {
                                    resultData = "Unable to display binary data";
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("The .net test runner does not support the given test execution mode (" + this.TestExecutionMode + "). Please use the PHP test runner for this purpose");

                            /*
                             * type = testName.Substring(testName.IndexOf("_") + 1);
                             * string showPath = CommonUtility.GetPath(string.Format("../../TestData/{0}/ShowFiles/{0}ApiTest", type));
                             * string showName = string.Format("{0}_{1}.{2}", showPath, paramSetId, actualExtension);
                             * File.WriteAllText(showName, expectedResult);
                             */
                        }
                    }
                }
            }

            if (outcome == "fail")
            {
                Console.WriteLine("****{0} {1} {2} failed.\n", testName, paramSetId, operation);
                string str = string.Format("\n****ACTUAL RESULT****\n{0}\n****EXPECTED RESULT****\n{1}\n********\n\n\n", resultData, expectedResult);
                //Console.WriteLine(str);
                //Console.WriteLine("<FAIL>");
                logger.Write(str);
            }

            vm.SqlFinalize();
            vm = null;

            return(exitStatus);
        }
Example #21
0
        //Opens add test form
        private void menuAddTest_Click(object sender, System.EventArgs e)
        {
            if (null != this.database)
            {
                string sqlStmt = "SELECT name FROM sqlite_master WHERE type='table' AND name='TestCase';";

                SqliteVm vm = new SqliteVm(this.database, true);

                if (Sqlite.Row == vm.Execute(sqlStmt))
                {
                    //Hack...if the form is not visible then the user must have closed it->create a new one
                    //If it is visible bring it to focus
                    if (!this.addTestForm.Visible)
                    {
                        this.addTestForm = new SqliteGUI.NewAddTestForm();
                        this.addTestForm.LoadForm(database);
                        this.addTestForm.MdiParent = this;
                        this.addTestForm.Show();
                    }
                    else
                    {
                        this.addTestForm.Activate();
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("TestCase table does not exist!",
                        "Error",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Hand);
                }
                vm.SqlFinalize();
            }
        }
Example #22
0
        public void WriteBack()
        {
            this.vm = new SqliteVm(this.database, true);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                int sqErr = Sqlite.Ok;

                DataRow row = dataTable.Rows[i];

                switch (row.RowState)
                {
                case DataRowState.Added:
                {
                    string insertStmt = String.Format("INSERT INTO {0} (", dataTable.TableName);
                    for (int j = 1; j < dataTable.Columns.Count; j++)
                    {
                        // ignore binary for now...
                        if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                        {
                            continue;
                        }

                        if (j > 1)
                        {
                            insertStmt += ",";
                        }

                        insertStmt += dataTable.Columns[j].ColumnName;
                    }

                    insertStmt += ") VALUES (";

                    for (int j = 1; j < dataTable.Columns.Count; j++)
                    {
                        // ignore binary for now...
                        if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                        {
                            continue;
                        }

                        if (j > 1)
                        {
                            insertStmt += ",";
                        }

                        insertStmt += String.Format("\"{0}\"", row[j].ToString());
                    }

                    insertStmt += ");";
                    sqErr       = vm.Execute(insertStmt);
                }
                break;

                case DataRowState.Deleted:
                {
                    // hack... rejectchanges to get the row back...
                    row.RejectChanges();
                    object obj = row[0];
                    if (obj != null)
                    {
                        string rowId = obj.ToString();
                        if (rowId != null && rowId.Length > 0)
                        {
                            string deleteStmt =
                                String.Format("DELETE FROM {0} WHERE rowid = {1};",
                                              dataTable.TableName, rowId);
                            sqErr = vm.Execute(deleteStmt);
                        }
                    }
                    row.Delete();
                }
                break;

                case DataRowState.Modified:
                {
                    int nCols = dataTable.Columns.Count;

                    string updateStmt = String.Format("UPDATE {0} SET ", dataTable.TableName);

                    for (int j = 1; j < nCols; j++)
                    {
                        // ignore binary for now...
                        if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                        {
                            continue;
                        }

                        if (j > 1)
                        {
                            updateStmt += ",";
                        }

                        updateStmt += String.Format("{0}=\"{1}\"",
                                                    dataTable.Columns[j].ColumnName,
                                                    row[j].ToString());
                    }

                    updateStmt += String.Format(" WHERE rowid = {0};", row[0].ToString());
                    sqErr       = vm.Execute(updateStmt);
                }
                break;

                case DataRowState.Unchanged:
                {
                    // do nothing
                    ;
                    ;
                }
                break;

                default:
                    break;
                }
            }

            vm.SqlFinalize();
            vm = null;
        }
Example #23
0
        private void OpenDatabase(string fName)
        {
            if (database != null)
            {
                CloseDatabase();
            }

            if (fName.Length > 0)
            {
                database = new SqliteDb();
                int err = database.Open(fName);
                if (err != 0)
                {
                    database = null;
                }
            }

            if (database != null)
            {
                databaseName = fName;

                treeView1.BeginUpdate();

                // Clear the TreeView each time the method is called.
                treeView1.Nodes.Clear();

                TreeNode databaseNode = new TreeNode(fName);

                SqliteVm vm = new SqliteVm(database, true);
                string tableList = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";

                int fini = vm.Execute(tableList);
                while (fini == Sqlite.Row)
                {
                    string tableName = vm.GetString("name");

                    databaseNode.Nodes.Add(tableName);

                    fini = vm.NextRow();
                }
                vm.SqlFinalize();
                vm = null;

                treeView1.Nodes.Add(databaseNode);

                treeView1.EndUpdate();
                //Enable menu items
                this.menuItem3.Enabled = true;
                this.menuAddTest.Enabled = true;
                this.saveAsMenuItem.Enabled = true;
                this.saveDbMenuItem.Enabled = true;
            }
        }
Example #24
0
        //Loads the form
        public void LoadForm(SqliteDb newDatabase)
        {
            //Create a new virtual machine
            this.database = newDatabase;
            int status = Sqlite.Ok;
            this.vm = new SqliteVm(newDatabase, true);

            //Get all tests from the test case table and add the in the dropdown box
            status = this.vm.Execute("Select TestName from TestCase");
            while (Sqlite.Row == status)
            {
                this.testNameList.Items.Add(this.vm.GetString("TestName"));
                status = this.vm.NextRow();
            }
            //If we have any tests in the list select the first one by default
            if (this.testNameList.Items.Count > 0)
            {
                this.testNameList.SelectedIndex = 0;
                this.testName = this.testNameList.SelectedItem.ToString();
            }
        }
Example #25
0
        public int Execute(ref int testsRun, ITestLogger logger, bool isEnterprise)
        {
            int exitStatus = 0;
            string dbPath = CommonUtility.GetDbPath(this.DumpFile);
            string dbName = CommonUtility.GetPath(dbPath);

            if (File.Exists(dbName))
            {
                var db = new SqliteDb();
                db.Open(dbName);

                var vm = new SqliteVm(db, true);

                int status = vm.Execute("Select TestName, TestType from TestCase where TestType=\"{0}\" order by ExecuteSequence", this.ApiType);

                //NOTE: We can't share the SqliteVm instance among our executor objects as this messes up query results
                //we must be able to re-create a new SqliteVm for each executor, so we pass down the db path
                SetupExecutors(dbName);

                while (status == Sqlite.Row)
                {
                    string testName = vm.GetString("TestName");
                    string testType = vm.GetString("TestType");

                    Console.WriteLine("Executing {0} test: {1}", testType, testName);
                    using (var run = new TestExecutionRun(dbPath, this))
                    {
                        try
                        {
                            exitStatus += run.RunTests(testName, logger, ref testsRun);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            exitStatus += 1;
                        }
                    }
                    status = vm.NextRow();
                }
                vm.SqlFinalize();
                vm = null;
                db = null;
            }
            return exitStatus;
        }
        public override int ValidateRequest(SqliteDotNet.SqliteDb db, string testName, int paramSetId, string operation, TestResult actualResult, ITestLogger logger)
        {
            int exitStatus = 0;
            string outcome = "pass";
            SqliteVm vm = new SqliteVm(db, false);

            object expectedResult = null;

            //If we have an exception we need to remove the stack trace because different line numbers will fail the test
            object resultData = CommonUtility.RemoveStackTraceFromResult(actualResult.ResultData);
            //Get the mime type based on the content type in the result
            string mimeType = actualResult.ContentType;
            //If we have exception message we need to remove any parts that may contain system dependent information
            //Ex. file paths
            resultData = CommonUtility.ProcessExceptionMessage(resultData);
            //Get the file extension that will be used for a dump
            string actualExtension = CommonUtility.GetExtensionFromMimeType(mimeType);

            //If we have an ALWAYSPASS parameter defined for the operation then skip the whole validation process
            //This parameter should only be used for clean up operations that are no related with the tests
            if (vm.Execute("Select ParamValue from Params where ParamName=\"ALWAYSPASS\" and ParamSet={0}", paramSetId) != Sqlite.Row)
            {
                //TestName is Test_[ServiceType]
                string type = testName.Substring(testName.IndexOf("_") + 1);
                string filePath = CommonUtility.GetPath(string.Format("../../TestData/{0}/DumpFiles/{0}ApiTest", type));
                string fileName = string.Format("{0}_{1}.{2}", filePath, paramSetId, actualExtension);

                if (this.TestExecutionMode == "dump")
                {
                    //File.WriteAllText(fileName, resultData);
                    throw new NotImplementedException("The .net test runner does not support dumping of test results. Please use the PHP test runner for this purpose");
                }
                else
                {
                    //This section is special case handling for the operations that return different data after each call
                    resultData = CommonUtility.SpecialDataHandling(operation, resultData, mimeType);

                    if (this.TestExecutionMode == "generate")
                    {
                        throw new NotImplementedException("The .net test runner does not support test update/generation. Please use the PHP test runner for this purpose");
                        /*
                        //Get the sample result that is stored in the database. If we are using file on disk for validation
                        //then do not overwrite the filename in the database
                        //To distinguish between sample data and filename all filenames should be prefixed with "@@"
                        int status = vm.Execute("Select Result from ApiTestResults where ParamSet={0}", paramSetId);
                        string sampleResult = vm.GetString("Result");

                        if (!sampleResult.StartsWith("@@"))
                        {
                            //Insert the sample data as a BLOB
                            //Update the row for that param set or create a new row if we do not have it yet

                            string responseBody = "";
                            if (status == Sqlite.Row)
                            {
                                vm.Prepare("update ApiTestResults set Result = :blob where ParamSet={0}", paramSetId);
                            }
                            else
                            {
                                Console.WriteLine("A new row has been created in ApiTestResults table to store the result for operation {0}", paramSetId);
                                Console.WriteLine("Please update the description field for that row later");
                                vm.Prepare("INSERT INTO ApiTestResults(ParamSet, Result) VALUES({0}, :blob)", paramSetId);
                            }

                            byte[] bytes = Encoding.UTF8.GetBytes(resultData);
                            vm.BindBlob(":blob", bytes);
                            vm.Execute();

                            if (mimeType != null)
                            {
                                vm.Execute("UPDATE ApiTestResults SET ContentType=\"{0}\" WHERE ParamSet={1}", mimeType, paramSetId);
                            }

                            File.WriteAllText(fileName, resultData);
                        }
                         */
                    }
                    else if (this.TestExecutionMode == "validate" || this.TestExecutionMode == "show")
                    {
                        string resultContent = "";

                        //Get the sample result and the expected content type from the database
                        int status = vm.Execute("Select Result, ContentType from ApiTestResults where ParamSet={0}", paramSetId);
                        string expectedContentType = vm.GetString("ContentType");
                        string expectedExtension = CommonUtility.GetExtensionFromMimeType(expectedContentType);
                        SqliteGcBlob blob = vm.GetBlob("Result");

                        byte[] b = blob.Read();
                        if (b != null)
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                                expectedResult = Encoding.UTF8.GetString(b);
                            else
                                expectedResult = b;
                        }
                        else
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                                expectedResult = string.Empty;
                            else
                                expectedResult = null;
                        }
                        string strExpectedResult = expectedResult as string;
                        //If we are validating from a file then get the contents of that file
                        //File names should be prefixed with "@@" to distinguish them from BLOB data
                        if (strExpectedResult != null && strExpectedResult.StartsWith("@@"))
                        {
                            string sampleDataFile = strExpectedResult.Substring(2);
                            sampleDataFile = CommonUtility.GetPath(sampleDataFile);
                            expectedResult = File.ReadAllText(sampleDataFile);
                        }

                        if (this.TestExecutionMode == "validate")
                        {
                            bool bEqual = false;
                            byte[] bExpected = expectedResult as byte[];
                            byte[] bActual = resultData as byte[];
                            string strResultData = resultData as string;

                            //FIXME: We're not processing DWF content properly to do this check properly. So just
                            //pass these for now
                            if (operation == "GETDRAWINGLAYER" || operation == "GETDRAWINGSECTION")
                            {
                                bEqual = true;
                            }
                            else
                            {
                                if (strExpectedResult != null && strResultData != null)
                                {
                                    bEqual = strResultData.Equals(strExpectedResult, StringComparison.InvariantCultureIgnoreCase);
                                }
                                else if (bExpected != null && bActual != null)
                                {
                                    bEqual = CommonUtility.ByteArraysEqual(bExpected, bActual, operation, testName);
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: {0} - {1} - Encountered disparate data types between expected and actual results. Expecting test failure :(", testName, operation));
                                }
                            }
                            //If the results are different and special validation fails then the operation failed ->mark it red
                            if (!bEqual && !CommonUtility.SpecialValidation(operation, resultData, expectedResult))
                            {
                                outcome = "fail";
                                exitStatus = 1;

                                if (expectedExtension != "xml" && expectedExtension != "html" && expectedExtension != "txt")
                                {
                                    expectedResult = "Unable to display binary data";
                                }

                                if (actualExtension != "xml" && actualExtension != "html" && actualExtension != "txt")
                                {
                                    resultData = "Unable to display binary data";
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("The .net test runner does not support the given test execution mode (" + this.TestExecutionMode + "). Please use the PHP test runner for this purpose");
                            /*
                            type = testName.Substring(testName.IndexOf("_") + 1);
                            string showPath = CommonUtility.GetPath(string.Format("../../TestData/{0}/ShowFiles/{0}ApiTest", type));
                            string showName = string.Format("{0}_{1}.{2}", showPath, paramSetId, actualExtension);
                            File.WriteAllText(showName, expectedResult);
                             */
                        }
                    }
                }
            }

            if (outcome == "fail")
            {
                Console.WriteLine("****{0} {1} {2} failed.\n", testName, paramSetId, operation);
                string str = string.Format("\n****ACTUAL RESULT****\n{0}\n****EXPECTED RESULT****\n{1}\n********\n\n\n", resultData, expectedResult);
                //Console.WriteLine(str);
                //Console.WriteLine("<FAIL>");
                logger.Write(str);
            }

            vm.SqlFinalize();
            vm = null;

            return exitStatus;
        }
Example #27
0
        public void LoadTable(SqliteDb newDatabase, string tableName)
        {
            this.Name = tableName;
            this.SetDatabase(newDatabase);

            DataGridTableStyle tableStyle = new DataGridTableStyle();

            tableStyle.MappingName = tableName;
            if (tableName == "TestCase")
            {
                tableStyle.PreferredRowHeight *= 3;
            }
            else if (tableName == "ApiTestResults" || tableName == "HttpTestResults")
            {
                tableStyle.PreferredRowHeight *= 2;
            }

            int gridWidth = 0;

            vm = new SqliteVm(database, true);
            if (vm != null)
            {
                dataTable = new DataTable(tableName);

                string sql   = String.Format("select rowid, * from {0} ;", tableName);
                int    sqErr = vm.Execute(sql);
                int    nCols = vm.NumCols();

                for (int i = 0; i < nCols; i++)
                {
                    string colName = vm.ColumnName(i);
                    string colType = vm.ColumnType(colName);
                    DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();
                    colStyle.MappingName = colName;
                    colStyle.HeaderText  = colName;

                    switch (colType.ToUpper())
                    {
                    case "INTEGER":
                        dataTable.Columns.Add(colName, typeof(int));
                        colStyle.Width = 60;
                        break;

                    case "REAL":
                        dataTable.Columns.Add(colName, typeof(double));
                        colStyle.Width = 60;
                        break;

                    case "TEXT":
                        dataTable.Columns.Add(colName, typeof(string));
                        colStyle.TextBox.Multiline = true;
                        if (colName == "ParamName" || colName == "ContentType" || colName == "TestType" || colName == "Prerequisite")
                        {
                            colStyle.Width = 100;
                        }
                        else if (colName == "ParamValue")
                        {
                            colStyle.Width = 275;
                        }
                        else
                        {
                            colStyle.Width = 200;
                        }
                        break;

                    case "BLOB":
                        DataColumn col = new DataColumn(colName, typeof(SqlBinary));
                        col.ReadOnly = true;
                        dataTable.Columns.Add(col);
                        colStyle.Width = 150;
                        break;

                    default:
                        colStyle = null;
                        break;
                    }

                    if (colStyle != null)
                    {
                        tableStyle.GridColumnStyles.Add(colStyle);
                        gridWidth += colStyle.Width;
                    }
                }


                //rowid should be readonly
                dataTable.Columns[0].ReadOnly = true;

                // update grid with new cols and width
                dataGrid1.TableStyles.Add(tableStyle);
                dataGrid1.Width = gridWidth;

                while (sqErr == Sqlite.Row)
                {
                    object [] cols = new object[nCols];

                    for (int i = 0; i < nCols; i++)
                    {
                        string colName = vm.ColumnName(i);
                        string colType = vm.ColumnType(colName);
                        switch (colType.ToUpper())
                        {
                        case "INTEGER":
                            int colInt = vm.GetInt(colName);
                            cols[i] = colInt;
                            break;

                        case "REAL":
                            double colDbl = vm.GetDouble(colName);
                            cols[i] = colDbl;
                            break;

                        case "TEXT":
                            string colText = vm.GetString(colName);
                            cols[i] = colText;
                            break;

                        case "BLOB":
                            byte[] bytes = vm.GetBlob(colName).Read();
                            if (bytes != null)
                            {
                                SqlBinary binBlob = new SqlBinary(bytes);
                                cols[i] = binBlob;
                            }
                            else
                            {
                                cols[i] = null;
                            }
                            break;

                        default:
                            break;
                        }
                    }

                    dataTable.Rows.Add(cols);
                    sqErr = vm.NextRow();
                }
            }

            vm.SqlFinalize();
            vm = null;
            dataTable.AcceptChanges();

            dataGrid1.DataSource = dataTable;
            this.Width           = gridWidth + 75;
        }
Example #28
0
        public void WriteBack()
        {
            this.vm = new SqliteVm(this.database, true);
            for (int i=0; i<dataTable.Rows.Count; i++)
            {
                int sqErr = Sqlite.Ok;

                DataRow row = dataTable.Rows[i];

                switch (row.RowState)
                {
                    case DataRowState.Added:
                    {
                        string insertStmt = String.Format("INSERT INTO {0} (",dataTable.TableName);
                        for (int j=1; j<dataTable.Columns.Count; j++)
                        {
                            // ignore binary for now...
                            if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                            {
                                continue;
                            }

                            if (j > 1) { insertStmt += ",";}

                            insertStmt += dataTable.Columns[j].ColumnName;
                        }

                        insertStmt += ") VALUES (";

                        for (int j=1; j<dataTable.Columns.Count; j++)
                        {
                            // ignore binary for now...
                            if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                            {
                                continue;
                            }

                            if (j > 1) { insertStmt += ",";}

                            insertStmt += String.Format("\"{0}\"", row[j].ToString());
                        }

                        insertStmt += ");";
                        sqErr = vm.Execute(insertStmt);
                    }
                    break;

                    case DataRowState.Deleted:
                    {
                        // hack... rejectchanges to get the row back...
                        row.RejectChanges();
                        object obj = row[0];
                        if (obj != null)
                        {
                            string rowId = obj.ToString();
                            if (rowId != null && rowId.Length > 0)
                            {
                                string deleteStmt =
                                    String.Format("DELETE FROM {0} WHERE rowid = {1};",
                                    dataTable.TableName, rowId);
                                sqErr = vm.Execute(deleteStmt);
                            }
                        }
                        row.Delete();
                    }
                    break;

                    case DataRowState.Modified:
                    {
                        int nCols = dataTable.Columns.Count;

                        string updateStmt = String.Format("UPDATE {0} SET ", dataTable.TableName);

                        for (int j=1; j<nCols; j++)
                        {
                            // ignore binary for now...
                            if (dataTable.Columns[j].DataType == typeof(SqlBinary))
                            {
                                continue;
                            }

                            if (j > 1) { updateStmt += ",";}

                            updateStmt += String.Format("{0}=\"{1}\"",
                                dataTable.Columns[j].ColumnName,
                                row[j].ToString());
                        }

                        updateStmt += String.Format(" WHERE rowid = {0};", row[0].ToString());
                        sqErr = vm.Execute(updateStmt);
                    }
                    break;

                    case DataRowState.Unchanged:
                    {
                        // do nothing
                        ;
                        ;
                    }
                    break;

                    default:
                    break;
                }
            }

            vm.SqlFinalize();
            vm = null;
        }
Example #29
0
        private void NewDatabase(bool template)
        {
            saveFileDialog1.Filter = "db files (*.db)|*.db|All files (*.*)|*.*";
            saveFileDialog1.RestoreDirectory = true;
            SqliteDb db = null;

            string dbName = "";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                dbName = saveFileDialog1.FileName;
                if (!IsReadOnly(dbName))
                {

                    //Close the existing database
                    if (null != this.database)
                    {
                        this.CloseDatabase();
                    }

                    //Delete the file
                    this.DeleteFile(dbName);

                    //Open the database. SQLite will create the file for us
                    db = new SqliteDb();
                    db.Open(dbName);

                    //Create a template database for the unit test infrastructure
                    if (template && (db != null))
                    {
                        SqliteVm templateVm = new SqliteVm(db, true);
                        templateVm.Execute("CREATE TABLE TestCase (ExecuteSequence INTEGER, TestName TEXT, ParamSets TEXT, Description TEXT, TestType TEXT, Prerequisite TEXT)");
                        templateVm.Execute("CREATE TABLE Params (ParamSet INTEGER, ParamName TEXT, ParamValue TEXT)");
                        templateVm.Execute("CREATE TABLE CommonParams (ParamName text, ParamValue text)");
                        templateVm.Execute("CREATE TABLE ApiTestResults (Description TEXT, ParamSet INTEGER, ContentType TEXT, Result TEXT)");
                        templateVm.Execute("CREATE TABLE HttpTestResults (Description TEXT, ParamSet INTEGER, ContentType TEXT, Result BLOB)");
                        templateVm.SqlFinalize();
                        templateVm = null;
                    }
                    db.Close();

                    //Open the new database
                    this.OpenDatabase(dbName);
                }
                else
                {
                    string errMsg;
                    errMsg = String.Format("File {0} is readOnly and cannot be replaced", dbName);
                    System.Windows.Forms.MessageBox.Show(errMsg,
                        "Error",
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Hand);
                    this.NewDatabase(template);
                }
            }
        }
Example #30
0
        public void LoadTable(SqliteDb newDatabase, string tableName)
        {
            this.Name = tableName;
            this.SetDatabase(newDatabase);

            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = tableName;
            if (tableName == "TestCase")
            {
                tableStyle.PreferredRowHeight *= 3;
            }
            else if (tableName == "ApiTestResults" || tableName == "HttpTestResults")
            {
                tableStyle.PreferredRowHeight *= 2;
            }

            int gridWidth = 0;

            vm = new SqliteVm(database, true);
            if (vm != null)
            {
                dataTable = new DataTable(tableName);

                string sql = String.Format("select rowid, * from {0} ;", tableName);
                int sqErr = vm.Execute(sql);
                int nCols = vm.NumCols();

                for (int i = 0; i<nCols; i++)
                {
                    string colName = vm.ColumnName(i);
                    string colType = vm.ColumnType(colName);
                    DataGridTextBoxColumn colStyle = new DataGridTextBoxColumn();
                    colStyle.MappingName = colName;
                    colStyle.HeaderText = colName;

                    switch (colType.ToUpper())
                    {
                        case "INTEGER":
                            dataTable.Columns.Add(colName, typeof(int));
                            colStyle.Width = 60;
                            break;
                        case "REAL":
                            dataTable.Columns.Add(colName, typeof(double));
                            colStyle.Width = 60;
                            break;
                        case "TEXT":
                            dataTable.Columns.Add(colName, typeof(string));
                            colStyle.TextBox.Multiline = true;
                            if (colName == "ParamName" || colName == "ContentType" || colName == "TestType" || colName == "Prerequisite")
                            {
                                colStyle.Width = 100;
                            }
                            else if (colName == "ParamValue")
                            {
                                colStyle.Width = 275;
                            }
                            else
                            {
                                colStyle.Width = 200;
                            }
                            break;

                        case "BLOB":
                            DataColumn col = new DataColumn(colName, typeof(SqlBinary));
                            col.ReadOnly = true;
                            dataTable.Columns.Add(col);
                            colStyle.Width = 150;
                            break;
                        default:
                            colStyle = null;
                            break;
                    }

                    if (colStyle != null)
                    {
                        tableStyle.GridColumnStyles.Add(colStyle);
                        gridWidth += colStyle.Width;
                    }
                }

                //rowid should be readonly
                dataTable.Columns[0].ReadOnly = true;

                // update grid with new cols and width
                dataGrid1.TableStyles.Add(tableStyle);
                dataGrid1.Width = gridWidth;

                while (sqErr == Sqlite.Row)
                {
                    object [] cols = new object[nCols];

                    for (int i=0; i<nCols; i++)
                    {
                        string colName = vm.ColumnName(i);
                        string colType = vm.ColumnType(colName);
                        switch (colType.ToUpper())
                        {
                            case "INTEGER":
                                int colInt = vm.GetInt(colName);
                                cols[i] = colInt;
                                break;
                            case "REAL":
                                double colDbl = vm.GetDouble(colName);
                                cols[i] = colDbl;
                                break;
                            case "TEXT":
                                string colText = vm.GetString(colName);
                                cols[i] = colText;
                                break;
                            case "BLOB":
                                byte[] bytes = vm.GetBlob(colName).Read();
                                if (bytes != null)
                                {
                                    SqlBinary binBlob = new SqlBinary(bytes);
                                    cols[i] = binBlob;
                                }
                                else
                                {
                                    cols[i] = null;
                                }
                                break;
                            default:
                                break;
                        }
                    }

                    dataTable.Rows.Add(cols);
                    sqErr = vm.NextRow();
                }
            }

            vm.SqlFinalize();
            vm = null;
            dataTable.AcceptChanges();

            dataGrid1.DataSource = dataTable;
            this.Width = gridWidth+75;
        }
Example #31
0
        private void SqlExecute_Click(object sender, System.EventArgs e)
        {
            if (richTextBox1.Text.Length > 0 && database != null)
            {
                SqliteVm vm = new SqliteVm(database, true);
                string sql = richTextBox1.Text;
                int sqlErr = Sqlite.Error;
                try
                {
                    sqlErr = vm.Execute(sql);
                }
                catch (SEHException)
                {
                    // Do nothing.  Execution failed...
                }

                if (sqlErr != Sqlite.Done && sqlErr != Sqlite.Ok)
                {
                    string errMsg = vm.GetErrMsg();
                    errMsg += "\n";
                    errMsg += vm.GetQueryTail();
                    MessageBox.Show(errMsg, "Sqlite Execution Error");

                    vm.SqlFinalize();
                    vm = null;
                }
                else
                {
                    vm.SqlFinalize();
                    vm = null;

                    // reload database
                    OpenDatabase(databaseName);
                }
            }
        }
Example #32
0
        //Create a FileInfo object and overwrite the ReadOnly attribute
        //Create a new SqliteDb object as the old will not be aware of the change
        //Send the new object to the parent form
        private void MakeWriteable(string dbName)
        {
            FileInfo dbFile = new FileInfo(dbName);
            dbFile.Attributes = FileAttributes.Archive;

            if (null != this.vm)
            {
                this.vm.SqlFinalize();
            }
            this.database.Close();
            this.database = new SqliteDb();
            this.database.Open(dbName);

            Form1 form = this.ParentForm as Form1;
            if (form != null)
            {
                form.SetDatabase(database);
            }

            this.vm = new SqliteVm(this.database, true);
        }