summaryrefslogtreecommitdiff
path: root/dbaccess/qa/unit/CRMDatabase_test.cxx
blob: 9b61b85f8a12a4e09dbbb49e746bb870cb02aeb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* -*- 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 <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/sdb/DatabaseContext.hpp>
#include <com/sun/star/sdb/XDocumentDataSource.hpp>
#include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/uno/XNamingService.hpp>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

class CRMDBTest : public DBTestBase
{
public:
    void testCRMDatabase();
    void testRegistrationName();
    uno::Reference<XConnection> setUpDBConnection();

    CPPUNIT_TEST_SUITE(CRMDBTest);
    CPPUNIT_TEST(testCRMDatabase);
    CPPUNIT_TEST(testRegistrationName);
    CPPUNIT_TEST_SUITE_END();
};

uno::Reference<XConnection> CRMDBTest::setUpDBConnection()
{
    createDBDocument(u"sdbc:embedded:hsqldb"_ustr);
    uno::Reference<sdb::XOfficeDatabaseDocument> xDocument(mxComponent, UNO_QUERY_THROW);
    uno::Reference<XDataSource> xDataSource = xDocument->getDataSource();
    CPPUNIT_ASSERT(xDataSource.is());

    // create queries before establishing connection to database
    createQueries(xDataSource);

    uno::Reference<XConnection> xConnection = getConnectionForDocument(xDocument);
    return xConnection;
}

void CRMDBTest::testCRMDatabase()
{
    uno::Reference<XConnection> xConnection = setUpDBConnection();
    createTables(xConnection);

    // test selection
    Reference<XStatement> xStatement = xConnection->createStatement();
    Reference<XResultSet> xResults
        = xStatement->executeQuery(u"SELECT \"NAME\" FROM \"CATEGORIES\" ORDER BY \"ID\""_ustr);
    CPPUNIT_ASSERT(xResults.is());

    Reference<XRow> 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<XQueriesSupplier> xQuerySupplier(xConnection, UNO_QUERY_THROW);
    uno::Reference<container::XNameAccess> 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<sdbcx::XColumnsSupplier> xColumns(
        xQueryAccess->getByName(u"unshipped orders"_ustr), UNO_QUERY);
    uno::Reference<container::XNameAccess> xColumnAccess(xColumns->getColumns());
    CPPUNIT_ASSERT(xColumnAccess->hasElements());

    Sequence<OUString> 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<container::XNameAccess> xNameAccess(
        getMultiServiceFactory()->createInstance(u"com.sun.star.sdb.DatabaseContext"_ustr),
        UNO_QUERY);
    Reference<sdbc::XDataSource> xBiblioDataSource(xNameAccess->getByName(u"Bibliography"_ustr),
                                                   UNO_QUERY);
    CPPUNIT_ASSERT(xBiblioDataSource.is());

    Reference<beans::XPropertySet> 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<sdb::XOfficeDatabaseDocument> xDocument(mxComponent, UNO_QUERY_THROW);
    uno::Reference<XConnection> xConnection = getConnectionForDocument(xDocument);
    createTables(xConnection);

    uno::Reference<XDataSource> xDataSource = xDocument->getDataSource();
    CPPUNIT_ASSERT(xDataSource.is());

    // Get a XDatabaseContext from XSingleService factory in order to register the DataSource
    Reference<sdb::XDatabaseContext> 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<beans::XPropertySet> 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: */