LCOV - code coverage report
Current view: top level - connectivity/qa/connectivity/mork - DriverTest.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 85 96 88.5 %
Date: 2014-11-03 Functions: 15 16 93.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <test/bootstrapfixture.hxx>
      11             : 
      12             : #include "MColumnAlias.hxx"
      13             : #include "MQueryHelper.hxx"
      14             : #include "MConnection.hxx"
      15             : 
      16             : #include <com/sun/star/sdbc/XDriver.hpp>
      17             : 
      18             : using namespace ::com::sun::star::beans;
      19             : using namespace ::com::sun::star::sdbc;
      20             : using namespace ::com::sun::star::uno;
      21             : 
      22             : namespace connectivity { namespace mork {
      23             : 
      24             : 
      25          12 : class MorkDriverTest: public test::BootstrapFixture
      26             : {
      27             : public:
      28           6 :     MorkDriverTest() : test::BootstrapFixture(false, false) {};
      29             : 
      30             :     void checkAcceptsURL(Reference< XDriver> xDriver, const char* url, bool expected);
      31             :     void test_metadata();
      32             :     void test_select_default_all();
      33             :     void test_select_list_table_joe_doe_5();
      34             : 
      35             :     virtual void setUp() SAL_OVERRIDE;
      36             :     virtual void tearDown() SAL_OVERRIDE;
      37             : 
      38           4 :     CPPUNIT_TEST_SUITE(MorkDriverTest);
      39             : 
      40           2 :     CPPUNIT_TEST(test_metadata);
      41           2 :     CPPUNIT_TEST(test_select_default_all);
      42           2 :     CPPUNIT_TEST(test_select_list_table_joe_doe_5);
      43           4 :     CPPUNIT_TEST_SUITE_END();
      44             : 
      45             : private:
      46             :     Reference<XInterface> m_xMorkComponent;
      47             :     Reference<XConnection> m_xConnection;
      48             : };
      49             : 
      50          48 : void MorkDriverTest::checkAcceptsURL(Reference< XDriver> xDriver, const char* url, bool expected)
      51             : {
      52          48 :     bool res = xDriver->acceptsURL(OUString::createFromAscii(url));
      53          48 :     if (res != expected)
      54             :     {
      55           0 :         CPPUNIT_ASSERT_MESSAGE("wrong URL outcome!", true);
      56             :     }
      57          48 : }
      58             : 
      59           6 : void MorkDriverTest::setUp()
      60             : {
      61           6 :     test::BootstrapFixture::setUp();
      62           6 :     m_xMorkComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.sdbc.MorkDriver");
      63           6 :     CPPUNIT_ASSERT_MESSAGE("no mork component!", m_xMorkComponent.is());
      64             : 
      65             :     // is this the best way to pass test file through URL?
      66             :     // may be take a custom Sequence< PropertyValue > route?
      67          12 :     OUString url = "sdbc:address:thunderbird:unittest:" +
      68          18 :         getPathFromSrc("/connectivity/qa/connectivity/mork/abook_10_john_does.mab");
      69             : 
      70          12 :     Sequence< PropertyValue > info;
      71          12 :     Reference< XDriver> xDriver(m_xMorkComponent, UNO_QUERY);
      72           6 :     if (!xDriver.is())
      73             :     {
      74           0 :         CPPUNIT_ASSERT_MESSAGE("cannot connect to mork driver!", xDriver.is());
      75             :     }
      76             : 
      77             :     // bad
      78           6 :     checkAcceptsURL(xDriver, "sdbc:address:macab",        false);
      79           6 :     checkAcceptsURL(xDriver, "sdbc:mozab:ldap:",          false);
      80           6 :     checkAcceptsURL(xDriver, "sdbc:mozab:outlook:",       false);
      81           6 :     checkAcceptsURL(xDriver, "sdbc:mozab:outlookexp:",    false);
      82             : 
      83             :     // good
      84           6 :     checkAcceptsURL(xDriver, "sdbc:mozab:mozilla:",       true);
      85           6 :     checkAcceptsURL(xDriver, "sdbc:mozab:thunderbird:",   true);
      86           6 :     checkAcceptsURL(xDriver, "sdbc:address:mozilla:",     true);
      87           6 :     checkAcceptsURL(xDriver, "sdbc:address:thunderbird:", true);
      88             : 
      89           6 :     m_xConnection = xDriver->connect(url, info);
      90           6 :     if (!m_xConnection.is())
      91             :     {
      92           0 :         CPPUNIT_ASSERT_MESSAGE("cannot connect to address book data soure!", m_xConnection.is());
      93           6 :     }
      94           6 : }
      95             : 
      96           6 : void MorkDriverTest::tearDown()
      97             : {
      98             : // how to make dispose() work?
      99             : // Reference< com::sun::star::lang::XComponent >( m_xMorkComponent, UNO_QUERY_THROW )->dispose();
     100           6 :     m_xConnection->close();
     101           6 :     test::BootstrapFixture::tearDown();
     102           6 : }
     103             : 
     104           2 : void MorkDriverTest::test_metadata()
     105             : {
     106           2 :     Reference< XDatabaseMetaData > xDatabaseMetaData = m_xConnection->getMetaData();
     107           2 :     if (!xDatabaseMetaData.is())
     108             :     {
     109           0 :         CPPUNIT_ASSERT_MESSAGE("cannot retrieve meta data!", xDatabaseMetaData.is());
     110             :     }
     111             : 
     112           4 :     const Any catalog;
     113           4 :     const OUString schemaPattern = "%";
     114           4 :     const OUString tableNamePattern = "%";
     115           4 :     const Sequence< OUString > types;
     116             : 
     117             :     Reference< XResultSet > xResultSet =
     118           4 :         xDatabaseMetaData->getTables(catalog, schemaPattern, tableNamePattern, types);
     119           2 :     if (!xResultSet.is())
     120             :     {
     121           0 :         CPPUNIT_ASSERT_MESSAGE("cannot retrieve tables!", xResultSet.is());
     122           2 :     }
     123             : 
     124             :     // TODO: how to access that result set and check the tables?
     125             :     // it should be 3 tables inside: AddressBook, does_5 and does_10
     126           2 : }
     127             : 
     128           2 : void MorkDriverTest::test_select_default_all()
     129             : {
     130           2 :     const OUString sql = "select \"E-mail\" from \"AddressBook\" ORDER BY \"E-mail\"";
     131           4 :     Reference< XPreparedStatement > xStatement = m_xConnection->prepareStatement(sql);
     132           2 :     if (!xStatement.is())
     133             :     {
     134           0 :         CPPUNIT_ASSERT_MESSAGE("cannot create prepared statement!", xStatement.is());
     135             :     }
     136             : 
     137           4 :     Reference< XResultSet > xResultSet = xStatement->executeQuery();
     138           2 :     if (!xResultSet.is())
     139             :     {
     140           0 :         CPPUNIT_ASSERT_MESSAGE("cannot execute sql statement!", xResultSet.is());
     141             :     }
     142             : 
     143           4 :     Reference< XRow > xDelegatorRow(xResultSet, UNO_QUERY);
     144           2 :     if (!xDelegatorRow.is())
     145             :     {
     146           0 :         CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xDelegatorRow.is());
     147             :     }
     148             : 
     149           2 :     bool result = xResultSet->first();
     150           2 :     CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
     151           4 :     OUString mail = xDelegatorRow->getString(1);
     152           2 :     CPPUNIT_ASSERT_MESSAGE("first row is not john@doe.org!", mail.equalsAscii("john@doe.org"));
     153             : 
     154           2 :     result = xResultSet->next();
     155           2 :     CPPUNIT_ASSERT_MESSAGE("fetch second row failed!", result);
     156           2 :     mail = xDelegatorRow->getString(1);
     157           2 :     CPPUNIT_ASSERT_MESSAGE("second row is not john@doe10.org!", mail.equalsAscii("john@doe10.org"));
     158             : 
     159           2 :     result = xResultSet->last();
     160           2 :     CPPUNIT_ASSERT_MESSAGE("fetch last row failed!", result);
     161           2 :     mail = xDelegatorRow->getString(1);
     162           2 :     CPPUNIT_ASSERT_MESSAGE("last row is not john@doe9.org!", mail.equalsAscii("john@doe9.org"));
     163             : 
     164             :     css::uno::Reference<css::sdbc::XCloseable>(
     165           4 :         xStatement, css::uno::UNO_QUERY_THROW)->close();
     166           2 : }
     167             : 
     168           2 : void MorkDriverTest::test_select_list_table_joe_doe_5()
     169             : {
     170           2 :     const OUString sql = "select \"E-mail\" from \"does_5\" where \"E-mail\" LIKE '%doe5.org' ";
     171           4 :     Reference< XPreparedStatement > xStatement = m_xConnection->prepareStatement(sql);
     172           2 :     if (!xStatement.is())
     173             :     {
     174           0 :         CPPUNIT_ASSERT_MESSAGE("cannot create prepared statement!", xStatement.is());
     175             :     }
     176             : 
     177           4 :     Reference< XResultSet > xResultSet = xStatement->executeQuery();
     178           2 :     if (!xResultSet.is())
     179             :     {
     180           0 :         CPPUNIT_ASSERT_MESSAGE("cannot execute sql statement!", xResultSet.is());
     181             :     }
     182             : 
     183           4 :     Reference< XRow > xDelegatorRow(xResultSet, UNO_QUERY);
     184           2 :     if (!xDelegatorRow.is())
     185             :     {
     186           0 :         CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xDelegatorRow.is());
     187             :     }
     188             : 
     189           2 :     bool result = xResultSet->first();
     190           2 :     CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
     191           4 :     OUString mail = xDelegatorRow->getString(1);
     192           2 :     CPPUNIT_ASSERT_MESSAGE("last row is not john@doe5.org!", mail.equalsAscii("john@doe5.org"));
     193             : 
     194             :     css::uno::Reference<css::sdbc::XCloseable>(
     195           4 :         xStatement, css::uno::UNO_QUERY_THROW)->close();
     196           2 : }
     197             : 
     198           2 : CPPUNIT_TEST_SUITE_REGISTRATION(MorkDriverTest);
     199             : 
     200             : }}
     201             : 
     202           8 : CPPUNIT_PLUGIN_IMPLEMENT();

Generated by: LCOV version 1.10