LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/connectivity/qa/connectivity/mork - DriverTest.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 82 93 88.2 %
Date: 2013-07-09 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           6 : class MorkDriverTest: public test::BootstrapFixture
      26             : {
      27             : public:
      28           3 :     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();
      36             :     virtual void tearDown();
      37             : 
      38           2 :     CPPUNIT_TEST_SUITE(MorkDriverTest);
      39             : 
      40           1 :     CPPUNIT_TEST(test_metadata);
      41           1 :     CPPUNIT_TEST(test_select_default_all);
      42           1 :     CPPUNIT_TEST(test_select_list_table_joe_doe_5);
      43           2 :     CPPUNIT_TEST_SUITE_END();
      44             : 
      45             : private:
      46             :     Reference<XInterface> m_xMorkComponent;
      47             :     Reference<XConnection> m_xConnection;
      48             : };
      49             : 
      50          24 : void MorkDriverTest::checkAcceptsURL(Reference< XDriver> xDriver, const char* url, bool expected)
      51             : {
      52          24 :     sal_Bool res = xDriver->acceptsURL(OUString::createFromAscii(url));
      53          24 :     if (res != expected)
      54             :     {
      55           0 :         CPPUNIT_ASSERT_MESSAGE("wrong URL outcome!", true);
      56             :     }
      57          24 : }
      58             : 
      59           3 : void MorkDriverTest::setUp()
      60             : {
      61           3 :     test::BootstrapFixture::setUp();
      62           3 :     m_xMorkComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.sdbc.MorkDriver");
      63           3 :     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           6 :     OUString url = OUString("sdbc:address:thunderbird:unittest:") +
      68           9 :         getPathFromSrc("/connectivity/qa/connectivity/mork/abook_10_john_does.mab");
      69             : 
      70           6 :     Sequence< PropertyValue > info;
      71           6 :     Reference< XDriver> xDriver(m_xMorkComponent, UNO_QUERY);
      72           3 :     if (!xDriver.is())
      73             :     {
      74           0 :         CPPUNIT_ASSERT_MESSAGE("cannot connect to mork driver!", xDriver.is());
      75             :     }
      76             : 
      77             :     // bad
      78           3 :     checkAcceptsURL(xDriver, "sdbc:address:macab",        false);
      79           3 :     checkAcceptsURL(xDriver, "sdbc:mozab:ldap:",          false);
      80           3 :     checkAcceptsURL(xDriver, "sdbc:mozab:outlook:",       false);
      81           3 :     checkAcceptsURL(xDriver, "sdbc:mozab:outlookexp:",    false);
      82             : 
      83             :     // good
      84           3 :     checkAcceptsURL(xDriver, "sdbc:mozab:mozilla:",       true);
      85           3 :     checkAcceptsURL(xDriver, "sdbc:mozab:thunderbird:",   true);
      86           3 :     checkAcceptsURL(xDriver, "sdbc:address:mozilla:",     true);
      87           3 :     checkAcceptsURL(xDriver, "sdbc:address:thunderbird:", true);
      88             : 
      89           3 :     m_xConnection = xDriver->connect(url, info);
      90           3 :     if (!m_xConnection.is())
      91             :     {
      92           0 :         CPPUNIT_ASSERT_MESSAGE("cannot connect to address book data soure!", m_xConnection.is());
      93           3 :     }
      94           3 : }
      95             : 
      96           3 : void MorkDriverTest::tearDown()
      97             : {
      98             : // how to make dispose() work?
      99             : // Reference< com::sun::star::lang::XComponent >( m_xMorkComponent, UNO_QUERY_THROW )->dispose();
     100           3 :     test::BootstrapFixture::tearDown();
     101           3 : }
     102             : 
     103           1 : void MorkDriverTest::test_metadata()
     104             : {
     105           1 :     Reference< XDatabaseMetaData > xDatabaseMetaData = m_xConnection->getMetaData();
     106           1 :     if (!xDatabaseMetaData.is())
     107             :     {
     108           0 :         CPPUNIT_ASSERT_MESSAGE("cannot retrieve meta data!", xDatabaseMetaData.is());
     109             :     }
     110             : 
     111           2 :     const Any catalog;
     112           2 :     const OUString schemaPattern = "%";
     113           2 :     const OUString tableNamePattern = "%";
     114           2 :     const Sequence< OUString > types;
     115             : 
     116             :     Reference< XResultSet > xResultSet =
     117           2 :         xDatabaseMetaData->getTables(catalog, schemaPattern, tableNamePattern, types);
     118           1 :     if (!xResultSet.is())
     119             :     {
     120           0 :         CPPUNIT_ASSERT_MESSAGE("cannot retrieve tables!", xResultSet.is());
     121           1 :     }
     122             : 
     123             :     // TODO: how to access that result set and check the tables?
     124             :     // it should be 3 tables inside: AddressBook, does_5 and does_10
     125           1 : }
     126             : 
     127           1 : void MorkDriverTest::test_select_default_all()
     128             : {
     129           1 :     const OUString sql = "select \"E-mail\" from \"AddressBook\" ORDER BY \"E-mail\"";
     130           2 :     Reference< XPreparedStatement > xStatement = m_xConnection->prepareStatement(sql);
     131           1 :     if (!xStatement.is())
     132             :     {
     133           0 :         CPPUNIT_ASSERT_MESSAGE("cannot create prepared statement!", xStatement.is());
     134             :     }
     135             : 
     136           2 :     Reference< XResultSet > xResultSet = xStatement->executeQuery();
     137           1 :     if (!xResultSet.is())
     138             :     {
     139           0 :         CPPUNIT_ASSERT_MESSAGE("cannot execure sql statement!", xResultSet.is());
     140             :     }
     141             : 
     142           2 :     Reference< XRow > xDelegatorRow(xResultSet, UNO_QUERY);
     143           1 :     if (!xDelegatorRow.is())
     144             :     {
     145           0 :         CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xDelegatorRow.is());
     146             :     }
     147             : 
     148           1 :     sal_Bool result = xResultSet->first();
     149           1 :     CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
     150           2 :     OUString mail = xDelegatorRow->getString(1);
     151           1 :     CPPUNIT_ASSERT_MESSAGE("first row is not john@doe.org!", mail.equalsAscii("john@doe.org"));
     152             : 
     153           1 :     result = xResultSet->next();
     154           1 :     CPPUNIT_ASSERT_MESSAGE("fetch second row failed!", result);
     155           1 :     mail = xDelegatorRow->getString(1);
     156           1 :     CPPUNIT_ASSERT_MESSAGE("second row is not john@doe10.org!", mail.equalsAscii("john@doe10.org"));
     157             : 
     158           1 :     result = xResultSet->last();
     159           1 :     CPPUNIT_ASSERT_MESSAGE("fetch last row failed!", result);
     160           1 :     mail = xDelegatorRow->getString(1);
     161           2 :     CPPUNIT_ASSERT_MESSAGE("last row is not john@doe9.org!", mail.equalsAscii("john@doe9.org"));
     162           1 : }
     163             : 
     164           1 : void MorkDriverTest::test_select_list_table_joe_doe_5()
     165             : {
     166           1 :     const OUString sql = "select \"E-mail\" from \"does_5\" where \"E-mail\" LIKE '%doe5.org' ";
     167           2 :     Reference< XPreparedStatement > xStatement = m_xConnection->prepareStatement(sql);
     168           1 :     if (!xStatement.is())
     169             :     {
     170           0 :         CPPUNIT_ASSERT_MESSAGE("cannot create prepared statement!", xStatement.is());
     171             :     }
     172             : 
     173           2 :     Reference< XResultSet > xResultSet = xStatement->executeQuery();
     174           1 :     if (!xResultSet.is())
     175             :     {
     176           0 :         CPPUNIT_ASSERT_MESSAGE("cannot execure sql statement!", xResultSet.is());
     177             :     }
     178             : 
     179           2 :     Reference< XRow > xDelegatorRow(xResultSet, UNO_QUERY);
     180           1 :     if (!xDelegatorRow.is())
     181             :     {
     182           0 :         CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xDelegatorRow.is());
     183             :     }
     184             : 
     185           1 :     sal_Bool result = xResultSet->first();
     186           1 :     CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
     187           2 :     OUString mail = xDelegatorRow->getString(1);
     188           2 :     CPPUNIT_ASSERT_MESSAGE("last row is not john@doe5.org!", mail.equalsAscii("john@doe5.org"));
     189           1 : }
     190             : 
     191           1 : CPPUNIT_TEST_SUITE_REGISTRATION(MorkDriverTest);
     192             : 
     193             : }}
     194             : 
     195           4 : CPPUNIT_PLUGIN_IMPLEMENT();

Generated by: LCOV version 1.10