[Libreoffice-commits] core.git: connectivity/CppunitTest_connectivity_mysql_test.mk connectivity/Module_connectivity.mk connectivity/qa connectivity/README dbaccess/Module_dbaccess.mk

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Mon Oct 22 13:43:50 UTC 2018


 connectivity/CppunitTest_connectivity_mysql_test.mk |   66 +++++++
 connectivity/Module_connectivity.mk                 |    6 
 connectivity/README                                 |   12 +
 connectivity/qa/connectivity/mysql/mysql.cxx        |  182 ++++++++++++++++++++
 dbaccess/Module_dbaccess.mk                         |    1 
 5 files changed, 267 insertions(+)

New commits:
commit e04e7a7214450fc6dff2571ecda65f7b7ea0d168
Author:     Tamas Bunth <tamas.bunth at collabora.co.uk>
AuthorDate: Mon Oct 22 11:27:55 2018 +0200
Commit:     Tamás Bunth <btomi96 at gmail.com>
CommitDate: Mon Oct 22 15:43:29 2018 +0200

    Add unit test for mysqlc connector
    
    It can be used to test the mysqlc connector whenever there is an
    external mysql or mariadb server running.
    
    The test runs only when CONNECTIVITY_TEST_MYSQL_DRIVER environment variable
    is set. This variable should contain the URL of the database, including
    the port number and the host name.
    
    The URL should also contain a user name password pair which can be used
    to connect to the external database. The URL format is the following:
    
    [user]/[passwd]@sdbc:mysql:mysqlc:[host]:[port]/[db_name]
    
    README is updated and contains detailed information about the test.
    
    Change-Id: I1bbc9369ff193a29c06de63a0f6cc975877c8da3
    Reviewed-on: https://gerrit.libreoffice.org/62171
    Tested-by: Jenkins
    Reviewed-by: Tamás Bunth <btomi96 at gmail.com>

diff --git a/connectivity/CppunitTest_connectivity_mysql_test.mk b/connectivity/CppunitTest_connectivity_mysql_test.mk
new file mode 100644
index 000000000000..5d9a8bb05d58
--- /dev/null
+++ b/connectivity/CppunitTest_connectivity_mysql_test.mk
@@ -0,0 +1,66 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+$(eval $(call gb_CppunitTest_CppunitTest,mysql_test))
+
+$(eval $(call gb_CppunitTest_use_external,mysql_test,boost_headers))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,mysql_test, \
+    connectivity/qa/connectivity/mysql/mysql \
+))
+
+$(eval $(call gb_CppunitTest_use_libraries,mysql_test, \
+    comphelper \
+    cppu \
+    dbaxml \
+    sal \
+    subsequenttest \
+    svt \
+    test \
+    unotest \
+    utl \
+    xo \
+))
+
+$(eval $(call gb_CppunitTest_use_api,mysql_test,\
+    offapi \
+    oovbaapi \
+    udkapi \
+))
+
+$(eval $(call gb_CppunitTest_use_ure,mysql_test))
+$(eval $(call gb_CppunitTest_use_vcl,mysql_test))
+
+$(eval $(call gb_CppunitTest_use_components,mysql_test,\
+    basic/util/sb \
+    comphelper/util/comphelp \
+    configmgr/source/configmgr \
+    connectivity/source/drivers/mysqlc/mysqlc \
+    connectivity/source/manager/sdbc2 \
+    filter/source/config/cache/filterconfig1 \
+    framework/util/fwk \
+    i18npool/util/i18npool \
+    linguistic/source/lng \
+    package/source/xstor/xstor \
+    package/util/package2 \
+    sax/source/expatwrap/expwrap \
+    sfx2/util/sfx \
+    svl/source/fsstor/fsstorage \
+    svl/util/svl \
+    toolkit/util/tk \
+    ucb/source/core/ucb1 \
+    ucb/source/ucp/file/ucpfile1 \
+    unotools/util/utl \
+    uui/util/uui \
+    xmloff/util/xo \
+))
+
+$(eval $(call gb_CppunitTest_use_configuration,mysql_test))
+
+# vim: set noet sw=4 ts=4:
diff --git a/connectivity/Module_connectivity.mk b/connectivity/Module_connectivity.mk
index dec95bd4133b..d946eb274a0d 100644
--- a/connectivity/Module_connectivity.mk
+++ b/connectivity/Module_connectivity.mk
@@ -131,6 +131,12 @@ $(eval $(call gb_Module_add_subsequentcheck_targets,connectivity,\
 
 endif
 
+ifneq ($(CONNECTIVITY_TEST_MYSQL_DRIVER),)
+$(eval $(call gb_Module_add_check_targets,connectivity,\
+    CppunitTest_connectivity_mysql_test \
+))
+endif
+
 # general tests
 $(eval $(call gb_Module_add_check_targets,connectivity,\
 	CppunitTest_connectivity_commontools \
diff --git a/connectivity/README b/connectivity/README
index 852d4f8c4fd6..7dfc9d758937 100644
--- a/connectivity/README
+++ b/connectivity/README
@@ -1 +1,13 @@
 Contains database pieces, drivers, etc.
+
+
+=== mysql_test ===
+
+- The CppunitTest_mysql_test unit test can be used to test the mysqlc
+  library with any versions of mysql or mariadb server of your choice.
+  
+- This test does not run automatically. It can be triggered with setting
+  the environment variable "CONNECTIVITY_TEST_MYSQL_DRIVER".
+  
+- The environment variable should contain an URL of the following format:
+  [user]/[passwd]@sdbc:mysql:mysqlc:[host]:[port]/[db_name]
diff --git a/connectivity/qa/connectivity/mysql/mysql.cxx b/connectivity/qa/connectivity/mysql/mysql.cxx
new file mode 100644
index 000000000000..f5057dce1519
--- /dev/null
+++ b/connectivity/qa/connectivity/mysql/mysql.cxx
@@ -0,0 +1,182 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <test/bootstrapfixture.hxx>
+
+#include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
+#include <com/sun/star/sdbc/XColumnLocate.hpp>
+#include <com/sun/star/sdbc/XConnection.hpp>
+#include <com/sun/star/sdbc/XResultSet.hpp>
+#include <com/sun/star/sdbc/XRow.hpp>
+#include <com/sun/star/sdbc/XParameters.hpp>
+#include <com/sun/star/sdbc/XStatement.hpp>
+#include <com/sun/star/sdbc/XDriver.hpp>
+#include <svtools/miscopt.hxx>
+#include <osl/process.h>
+
+using namespace ::com::sun::star;
+using namespace ::com::sun::star::sdb;
+using namespace ::com::sun::star::sdbc;
+using namespace ::com::sun::star::uno;
+using namespace ::com::sun::star::beans;
+
+class MysqlTestDriver : public test::BootstrapFixture
+{
+private:
+    OUString m_sUrl;
+    Reference<XInterface> m_xMysqlcComponent;
+    Reference<XDriver> m_xDriver;
+    Sequence<PropertyValue> m_infos;
+
+public:
+    MysqlTestDriver()
+        : test::BootstrapFixture(false, false)
+    {
+    }
+    virtual void setUp() override;
+    void testDBConnection();
+    void testCreateAndDropTable();
+    void testIntegerInsertAndQuery();
+
+    CPPUNIT_TEST_SUITE(MysqlTestDriver);
+    CPPUNIT_TEST(testDBConnection);
+    CPPUNIT_TEST(testCreateAndDropTable);
+    CPPUNIT_TEST(testIntegerInsertAndQuery);
+    CPPUNIT_TEST_SUITE_END();
+};
+
+void MysqlTestDriver::setUp()
+{
+    test::BootstrapFixture::setUp();
+
+    /* Get URL from environment variable. This test suite should run only when
+     * there is an URL given. This is because it can be used for testing connection to
+     * external databases as well.
+     *
+     * Example URL:
+     * username/password at sdbc:mysql:mysqlc:localhost:3306/testdatabase
+     */
+    osl_getEnvironment(OUString("CONNECTIVITY_TEST_MYSQL_DRIVER").pData, &m_sUrl.pData);
+    m_xMysqlcComponent
+        = getMultiServiceFactory()->createInstance("com.sun.star.comp.sdbc.mysqlc.MysqlCDriver");
+    CPPUNIT_ASSERT_MESSAGE("no mysqlc component!", m_xMysqlcComponent.is());
+
+    // set user name and password
+    m_infos = Sequence<PropertyValue>{ 2 };
+    m_infos[0].Name = OUString{ "user" };
+    sal_Int32 nPer = m_sUrl.indexOf("/");
+    m_infos[0].Value = makeAny(m_sUrl.copy(0, nPer));
+    m_sUrl = m_sUrl.copy(nPer + 1);
+    m_infos[1].Name = OUString{ "password" };
+    sal_Int32 nAt = m_sUrl.indexOf("@");
+    m_infos[1].Value = makeAny(m_sUrl.copy(0, nAt));
+    m_sUrl = m_sUrl.copy(nAt + 1);
+
+    m_xDriver.set(m_xMysqlcComponent, UNO_QUERY);
+    if (!m_xDriver.is())
+    {
+        CPPUNIT_ASSERT_MESSAGE("cannot connect to mysqlc driver!", m_xDriver.is());
+    }
+}
+
+/**
+ * Test database connection. It is assumed that the given URL is correct and
+ * there is a server running at the location.
+ */
+void MysqlTestDriver::testDBConnection()
+{
+    Reference<XConnection> xConnection = m_xDriver->connect(m_sUrl, m_infos);
+    if (!xConnection.is())
+    {
+        CPPUNIT_ASSERT_MESSAGE("cannot connect to data source!", xConnection.is());
+    }
+
+    uno::Reference<XStatement> xStatement = xConnection->createStatement();
+    CPPUNIT_ASSERT(xStatement.is());
+
+    Reference<XResultSet> xResultSet = xStatement->executeQuery("SELECT 1");
+    CPPUNIT_ASSERT(xResultSet.is());
+    Reference<XRow> xRow(xResultSet, UNO_QUERY);
+    CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xRow.is());
+
+    sal_Bool result = xResultSet->first();
+    CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
+}
+
+/**
+ * Test creation and removal of a table
+ */
+void MysqlTestDriver::testCreateAndDropTable()
+{
+    Reference<XConnection> xConnection = m_xDriver->connect(m_sUrl, m_infos);
+    if (!xConnection.is())
+    {
+        CPPUNIT_ASSERT_MESSAGE("cannot connect to data source!", xConnection.is());
+    }
+
+    uno::Reference<XStatement> xStatement = xConnection->createStatement();
+    CPPUNIT_ASSERT(xStatement.is());
+
+    auto nUpdateCount
+        = xStatement->executeUpdate("CREATE TABLE myTestTable (id INTEGER PRIMARY KEY)");
+    CPPUNIT_ASSERT_EQUAL(0, nUpdateCount); // it's a DDL statement
+
+    // we can use the same xStatement instance here
+    nUpdateCount = xStatement->executeUpdate("DROP TABLE myTestTable");
+    CPPUNIT_ASSERT_EQUAL(0, nUpdateCount); // it's a DDL statement
+}
+
+void MysqlTestDriver::testIntegerInsertAndQuery()
+{
+    Reference<XConnection> xConnection = m_xDriver->connect(m_sUrl, m_infos);
+    if (!xConnection.is())
+    {
+        CPPUNIT_ASSERT_MESSAGE("cannot connect to data source!", xConnection.is());
+    }
+
+    Reference<XStatement> xStatement = xConnection->createStatement();
+    CPPUNIT_ASSERT(xStatement.is());
+
+    auto nUpdateCount
+        = xStatement->executeUpdate("CREATE TABLE myTestTable (id INTEGER PRIMARY KEY)");
+    CPPUNIT_ASSERT_EQUAL(0, nUpdateCount); // it's a DDL statement
+
+    Reference<XPreparedStatement> xPrepared
+        = xConnection->prepareStatement(OUString{ "INSERT INTO myTestTable VALUES (?)" });
+    Reference<XParameters> xParams(xPrepared, UNO_QUERY);
+    constexpr int ROW_COUNT = 3;
+    for (int i = 0; i < ROW_COUNT; ++i)
+    {
+        xParams->setLong(1, i); // first and only column
+        nUpdateCount = xPrepared->executeUpdate();
+        CPPUNIT_ASSERT_EQUAL(1, nUpdateCount); // one row is inserted at a time
+    }
+
+    // now let's query the existing data
+    Reference<XResultSet> xResultSet = xStatement->executeQuery("SELECT id from myTestTable");
+    CPPUNIT_ASSERT_MESSAGE("result set cannot be instantiated after query", xResultSet.is());
+    Reference<XRow> xRow(xResultSet, UNO_QUERY);
+    CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xRow.is());
+
+    for (long i = 0; i < ROW_COUNT; ++i)
+    {
+        bool hasRow = xResultSet->next();
+        CPPUNIT_ASSERT_MESSAGE("not enough result after query", hasRow);
+        CPPUNIT_ASSERT_EQUAL(i, xRow->getLong(1)); // first and only column
+    }
+
+    nUpdateCount = xStatement->executeUpdate("DROP TABLE myTestTable");
+    CPPUNIT_ASSERT_EQUAL(0, nUpdateCount); // it's a DDL statement
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(MysqlTestDriver);
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/dbaccess/Module_dbaccess.mk b/dbaccess/Module_dbaccess.mk
index ed5f4b3d9fb4..9c834b0d7e7c 100644
--- a/dbaccess/Module_dbaccess.mk
+++ b/dbaccess/Module_dbaccess.mk
@@ -37,6 +37,7 @@ ifeq ($(ENABLE_FIREBIRD_SDBC),TRUE)
 $(eval $(call gb_Module_add_check_targets,dbaccess,\
     $(if,$(ENABLE_JAVA),CppunitTest_dbaccess_hsqlbinary_import) \
 ))
+
 # remove if we have a be file for this
 ifeq ($(ENDIANNESS),little)
 $(eval $(call gb_Module_add_check_targets,dbaccess,\


More information about the Libreoffice-commits mailing list