/* -*- 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 "dbtest_base.cxx" #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace ::com::sun::star; using namespace ::com::sun::star::uno; class CRMDBTest : public DBTestBase { public: void testCRMDatabase(); void testRegistrationName(); uno::Reference setUpDBConnection(); CPPUNIT_TEST_SUITE(CRMDBTest); CPPUNIT_TEST(testCRMDatabase); CPPUNIT_TEST(testRegistrationName); CPPUNIT_TEST_SUITE_END(); }; uno::Reference CRMDBTest::setUpDBConnection() { createDBDocument(u"sdbc:embedded:hsqldb"_ustr); uno::Reference xDocument(mxComponent, UNO_QUERY_THROW); uno::Reference xDataSource = xDocument->getDataSource(); CPPUNIT_ASSERT(xDataSource.is()); // create queries before establishing connection to database createQueries(xDataSource); uno::Reference xConnection = getConnectionForDocument(xDocument); return xConnection; } void CRMDBTest::testCRMDatabase() { uno::Reference xConnection = setUpDBConnection(); createTables(xConnection); // test selection Reference xStatement = xConnection->createStatement(); Reference xResults = xStatement->executeQuery(u"SELECT \"NAME\" FROM \"CATEGORIES\" ORDER BY \"ID\""_ustr); CPPUNIT_ASSERT(xResults.is()); Reference xRow(xResults, UNO_QUERY_THROW); CPPUNIT_ASSERT(xResults->next()); CPPUNIT_ASSERT_EQUAL(u"Food"_ustr, xRow->getString(1)); CPPUNIT_ASSERT(xResults->next()); CPPUNIT_ASSERT_EQUAL(u"Furniture"_ustr, xRow->getString(1)); // test if the queries have been created and can be used uno::Reference xQuerySupplier(xConnection, UNO_QUERY_THROW); uno::Reference xQueryAccess = xQuerySupplier->getQueries(); CPPUNIT_ASSERT(xQueryAccess->hasElements()); // the unshipped orders query depends on the all orders query, so we'll test // to see if both work uno::Reference xColumns( xQueryAccess->getByName(u"unshipped orders"_ustr), UNO_QUERY); uno::Reference xColumnAccess(xColumns->getColumns()); CPPUNIT_ASSERT(xColumnAccess->hasElements()); Sequence ColumnNames = xColumnAccess->getElementNames(); CPPUNIT_ASSERT(ColumnNames.hasElements()); // first column returned should be from the all orders query CPPUNIT_ASSERT_EQUAL(ColumnNames[0], u"Order No."_ustr); } void CRMDBTest::testRegistrationName() { // 1. check the existing "Bibliography" data source whether it has the proper name Reference xNameAccess( getMultiServiceFactory()->createInstance(u"com.sun.star.sdb.DatabaseContext"_ustr), UNO_QUERY); Reference xBiblioDataSource(xNameAccess->getByName(u"Bibliography"_ustr), UNO_QUERY); CPPUNIT_ASSERT(xBiblioDataSource.is()); Reference xBiblioProp(xBiblioDataSource, UNO_QUERY); OUString sBiblioDataSourceName; xBiblioProp->getPropertyValue(u"Name"_ustr) >>= sBiblioDataSourceName; CPPUNIT_ASSERT_EQUAL(u"Bibliography"_ustr, sBiblioDataSourceName); // 2. register a newly created data source, and verify it has the proper name // Setup the CRMDatabase createDBDocument(u"sdbc:embedded:hsqldb"_ustr); uno::Reference xDocument(mxComponent, UNO_QUERY_THROW); uno::Reference xConnection = getConnectionForDocument(xDocument); createTables(xConnection); uno::Reference xDataSource = xDocument->getDataSource(); CPPUNIT_ASSERT(xDataSource.is()); // Get a XDatabaseContext from XSingleService factory in order to register the DataSource Reference xDatabaseContext(xNameAccess, UNO_QUERY_THROW); CPPUNIT_ASSERT(xDatabaseContext.is()); // Register the datasource xDatabaseContext->registerObject(u"SomeNewHsqlDataSource"_ustr, xDataSource); // Check the newly created data source to see if it has the proper name Reference xProp(xDataSource, UNO_QUERY); OUString sDataSourceName; xProp->getPropertyValue(u"Name"_ustr) >>= sDataSourceName; CPPUNIT_ASSERT_EQUAL(u"SomeNewHsqlDataSource"_ustr, sDataSourceName); CPPUNIT_ASSERT_THROW_MESSAGE( "Bibliography already exists", xDatabaseContext->registerObject(u"Bibliography"_ustr, xDataSource), container::ElementExistException); } CPPUNIT_TEST_SUITE_REGISTRATION(CRMDBTest); CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */