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 "ado/AConnection.hxx"
13 #include "ado/ADatabaseMetaData.hxx"
14 #include "ado/ADriver.hxx"
15 #include "ado/AStatement.hxx"
16 #include "ado/ACallableStatement.hxx"
17 #include "ado/APreparedStatement.hxx"
18 #include "ado/ACatalog.hxx"
19 #include <com/sun/star/sdbc/ColumnValue.hpp>
20 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
21 #include <com/sun/star/sdbc/XRow.hpp>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <cppuhelper/typeprovider.hxx>
24 #include <connectivity/dbexception.hxx>
25 #include <osl/file.hxx>
26 #include "strings.hrc"
27 
28 
29 using namespace ::com::sun::star::beans;
30 using namespace ::com::sun::star::sdbc;
31 using namespace ::com::sun::star::uno;
32 
33 namespace connectivity { namespace ado {
34 
35 
36 class AdoDriverTest: public test::BootstrapFixture
37 {
38 public:
AdoDriverTest()39     AdoDriverTest() : test::BootstrapFixture(false, false) {};
40 
41     void test_metadata();
42     void test_select_default_all();
43 
44     virtual void setUp();
45     virtual void tearDown();
46 
47     CPPUNIT_TEST_SUITE(AdoDriverTest);
48 
49     CPPUNIT_TEST(test_metadata);
50     CPPUNIT_TEST(test_select_default_all);
51     CPPUNIT_TEST_SUITE_END();
52 
53 private:
54     Reference<XInterface> m_xAdoComponent;
55     Reference<XConnection> m_xConnection;
56 };
57 
setUp()58 void AdoDriverTest::setUp()
59 {
60     test::BootstrapFixture::setUp();
61     m_xAdoComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.sdbc.ado.ODriver");
62     CPPUNIT_ASSERT_MESSAGE("no ado component!", m_xAdoComponent.is());
63 
64     OUString url = "sdbc:ado:access:PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" +
65         m_directories.getPathFromWorkdir("/CppunitTest/TS001018407.mdb");
66 
67     Sequence< PropertyValue > info;
68     Reference< XDriver> xDriver(m_xAdoComponent, UNO_QUERY);
69     if (!xDriver.is())
70     {
71         CPPUNIT_ASSERT_MESSAGE("cannot connect to ado driver!", xDriver.is());
72     }
73 
74     m_xConnection = xDriver->connect(url, info);
75     if (!m_xConnection.is())
76     {
77         CPPUNIT_ASSERT_MESSAGE("cannot connect to students data source!", m_xConnection.is());
78     }
79 }
80 
tearDown()81 void AdoDriverTest::tearDown()
82 {
83     m_xAdoComponent = 0;
84     test::BootstrapFixture::tearDown();
85 }
86 
test_metadata()87 void AdoDriverTest::test_metadata()
88 {
89     Reference< XDatabaseMetaData > xDatabaseMetaData = m_xConnection->getMetaData();
90     if (!xDatabaseMetaData.is())
91     {
92         CPPUNIT_ASSERT_MESSAGE("cannot retrieve meta data!", xDatabaseMetaData.is());
93     }
94 
95     const Any catalog;
96     const OUString schemaPattern = "%";
97     const OUString tableNamePattern = "%";
98     const Sequence< OUString > types;
99 
100     Reference< XResultSet > xResultSet =
101         xDatabaseMetaData->getTables(catalog, schemaPattern, tableNamePattern, types);
102     if (!xResultSet.is())
103     {
104         CPPUNIT_ASSERT_MESSAGE("cannot retrieve tables!", xResultSet.is());
105     }
106 }
107 
test_select_default_all()108 void AdoDriverTest::test_select_default_all()
109 {
110     const OUString sql = "select \"FirstName\" from \"Students\" ORDER BY \"FirstName\"";
111     Reference< XPreparedStatement > xStatement = m_xConnection->prepareStatement(sql);
112     if (!xStatement.is())
113     {
114         CPPUNIT_ASSERT_MESSAGE("cannot create prepared statement!", xStatement.is());
115     }
116 
117     Reference< XResultSet > xResultSet = xStatement->executeQuery();
118     if (!xResultSet.is())
119     {
120         CPPUNIT_ASSERT_MESSAGE("cannot execute sql statement!", xResultSet.is());
121     }
122 
123     Reference< XRow > xDelegatorRow(xResultSet, UNO_QUERY);
124     if (!xDelegatorRow.is())
125     {
126         CPPUNIT_ASSERT_MESSAGE("cannot extract row from result set!", xDelegatorRow.is());
127     }
128 
129     sal_Bool result = xResultSet->first();
130     CPPUNIT_ASSERT_MESSAGE("fetch first row failed!", result);
131 /*
132     OUString mail = xDelegatorRow->getString(1);
133     CPPUNIT_ASSERT_MESSAGE("first row is not john@doe.org!", mail.equalsAscii("john@doe.org"));
134 */
135 }
136 
137 CPPUNIT_TEST_SUITE_REGISTRATION(AdoDriverTest);
138 
139 }}
140 
141 CPPUNIT_PLUGIN_IMPLEMENT();
142