summaryrefslogtreecommitdiffstats
path: root/dbaccess/qa/extras/rowsetclones.cxx
blob: f62314205be3887de8c3c189c8403e4a870df030 (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
/* -*- 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 <sal/config.h>
#include <test/unoapi_test.hxx>
#include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/sdb/CommandType.hpp>
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbc/XResultSet.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbcx/XRowLocate.hpp>
#include <com/sun/star/sdbc/XRowSet.hpp>
#include <com/sun/star/sdb/XResultSetAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;

class RowSetClones : public UnoApiTest
{
public:
    RowSetClones();

    void test();

    CPPUNIT_TEST_SUITE(RowSetClones);
    CPPUNIT_TEST(test);
    CPPUNIT_TEST_SUITE_END();

};


RowSetClones::RowSetClones()
    : UnoApiTest("")
{
}

void RowSetClones::test()
{
    const OUString sFilePath(m_directories.getURLFromWorkdir("CppunitTest/RowSetClones.odb"));

    uno::Reference< lang::XComponent > xComponent (loadFromDesktop(sFilePath));
    CPPUNIT_ASSERT(xComponent.is());

    uno::Reference< XOfficeDatabaseDocument > xDocument(xComponent, UNO_QUERY);
    CPPUNIT_ASSERT(xDocument.is());

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

    uno::Reference< XConnection > xConnection = xDataSource->getConnection("","");
    CPPUNIT_ASSERT(xConnection.is());

    uno::Reference< XRowSet > xRowSet (getMultiServiceFactory()->createInstance("com.sun.star.sdb.RowSet" ), UNO_QUERY);
    CPPUNIT_ASSERT(xRowSet.is());
    uno::Reference< XPropertySet > rowSetProperties ( xRowSet, UNO_QUERY );
    CPPUNIT_ASSERT(rowSetProperties.is());
    rowSetProperties->setPropertyValue("Command", Any(OUString("SELECT * FROM Assets ORDER BY AssetID")));
    rowSetProperties->setPropertyValue("CommandType", Any(CommandType::COMMAND));
    rowSetProperties->setPropertyValue("ActiveConnection", Any(xConnection));

    xRowSet->execute();
    uno::Reference< XResultSet > xResultSet = xRowSet;
    CPPUNIT_ASSERT(xResultSet.is());
    // always starts at BeforeFirst position
    CPPUNIT_ASSERT(xResultSet->isBeforeFirst());
    CPPUNIT_ASSERT(xResultSet->next());
    CPPUNIT_ASSERT(xResultSet->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSet->getRow());

    uno::Reference< XRow > xRow(xResultSet, UNO_QUERY);
    CPPUNIT_ASSERT(xRow.is());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));

    uno::Reference< XResultSetAccess > xResultSetAccess(xResultSet, UNO_QUERY);
    CPPUNIT_ASSERT(xResultSetAccess.is());
    uno::Reference< XResultSet > xResultSetClone = xResultSetAccess->createResultSet();
    CPPUNIT_ASSERT(xResultSetClone.is());

    uno::Reference< XRow > xRowClone(xResultSetClone, UNO_QUERY);
    CPPUNIT_ASSERT(xRowClone.is());

    // the clone starts at same position as what it is cloned from,
    // and does not move its source.
    CPPUNIT_ASSERT(xResultSetClone->isFirst());
    CPPUNIT_ASSERT(xResultSet->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSet->getRow());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSetClone->getRow());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));

    // if we move the source, the clone does not move
    CPPUNIT_ASSERT(xResultSet->next());
    CPPUNIT_ASSERT(xResultSetClone->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xResultSet->getRow());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSetClone->getRow());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xRow->getInt(1));
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));

    CPPUNIT_ASSERT(xResultSet->last());
    CPPUNIT_ASSERT(xResultSet->isLast());
    CPPUNIT_ASSERT(xResultSetClone->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));

    // and the other way round
    CPPUNIT_ASSERT(xResultSet->first());
    CPPUNIT_ASSERT(xResultSetClone->next());
    CPPUNIT_ASSERT(xResultSet->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xResultSetClone->getRow());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xRowClone->getInt(1));
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));

    CPPUNIT_ASSERT(xResultSetClone->last());
    CPPUNIT_ASSERT(xResultSetClone->isLast());
    CPPUNIT_ASSERT(xResultSet->isFirst());
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));

    closeDocument(uno::Reference<lang::XComponent>(xDocument, uno::UNO_QUERY));
}

CPPUNIT_TEST_SUITE_REGISTRATION(RowSetClones);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */