Kaydet (Commit) e04e7a72 authored tarafından Tamas Bunth's avatar Tamas Bunth Kaydeden (comit) Tamás Bunth

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: 's avatarTamás Bunth <btomi96@gmail.com>
üst 63476c34
# -*- 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:
......@@ -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 \
......
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]
/* -*- 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@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: */
......@@ -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,\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment