1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
12 #include <test/unoapi_test.hxx>
13 #include <rtl/strbuf.hxx>
14 #include <osl/file.hxx>
15 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
16 #include <com/sun/star/frame/Desktop.hpp>
17 #include <com/sun/star/lang/XComponent.hpp>
18 #include <com/sun/star/sdb/CommandType.hpp>
19 #include <com/sun/star/sdbc/XConnection.hpp>
20 #include <com/sun/star/sdbc/XResultSet.hpp>
21 #include <com/sun/star/sdbc/XRow.hpp>
22 #include <com/sun/star/sdbcx/XRowLocate.hpp>
23 #include <com/sun/star/sdbc/XRowSet.hpp>
24 #include <com/sun/star/sdb/XResultSetAccess.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <sfx2/app.hxx>
28 #include <sfx2/docfilt.hxx>
29 #include <sfx2/docfile.hxx>
30 #include <sfx2/objsh.hxx>
31 #include <sfx2/sfxmodelfactory.hxx>
32 #include <svl/intitem.hxx>
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::sdb
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::com::sun::star::sdbcx
;
41 class RowSetClones
: public UnoApiTest
48 CPPUNIT_TEST_SUITE(RowSetClones
);
50 CPPUNIT_TEST_SUITE_END();
55 RowSetClones::RowSetClones()
60 void RowSetClones::test()
62 const OUString
sFilePath(m_directories
.getURLFromWorkdir("CppunitTest/RowSetClones.odb"));
64 uno::Reference
< lang::XComponent
> xComponent (loadFromDesktop(sFilePath
));
65 CPPUNIT_ASSERT(xComponent
.is());
67 uno::Reference
< XOfficeDatabaseDocument
> xDocument(xComponent
, UNO_QUERY
);
68 CPPUNIT_ASSERT(xDocument
.is());
70 uno::Reference
< XDataSource
> xDataSource
= xDocument
->getDataSource();
71 CPPUNIT_ASSERT(xDataSource
.is());
73 uno::Reference
< XConnection
> xConnection
= xDataSource
->getConnection("","");
74 CPPUNIT_ASSERT(xConnection
.is());
76 uno::Reference
< XRowSet
> xRowSet (getMultiServiceFactory()->createInstance("com.sun.star.sdb.RowSet" ), UNO_QUERY
);
77 CPPUNIT_ASSERT(xRowSet
.is());
78 uno::Reference
< XPropertySet
> rowSetProperties ( xRowSet
, UNO_QUERY
);
79 CPPUNIT_ASSERT(rowSetProperties
.is());
80 rowSetProperties
->setPropertyValue("Command", Any(OUString("SELECT * FROM Assets ORDER BY AssetID")));
81 rowSetProperties
->setPropertyValue("CommandType", Any(CommandType::COMMAND
));
82 rowSetProperties
->setPropertyValue("ActiveConnection", Any(xConnection
));
85 uno::Reference
< XResultSet
> xResultSet(xRowSet
, UNO_QUERY
);
86 CPPUNIT_ASSERT(xResultSet
.is());
87 // always starts at BeforeFirst position
88 CPPUNIT_ASSERT(xResultSet
->isBeforeFirst());
89 CPPUNIT_ASSERT(xResultSet
->next());
90 CPPUNIT_ASSERT(xResultSet
->isFirst());
91 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xResultSet
->getRow());
93 uno::Reference
< XRow
> xRow(xResultSet
, UNO_QUERY
);
94 CPPUNIT_ASSERT(xRow
.is());
95 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRow
->getInt(1));
97 uno::Reference
< XResultSetAccess
> xResultSetAccess(xResultSet
, UNO_QUERY
);
98 CPPUNIT_ASSERT(xResultSetAccess
.is());
99 uno::Reference
< XResultSet
> xResultSetClone
= xResultSetAccess
->createResultSet();
100 CPPUNIT_ASSERT(xResultSetClone
.is());
102 uno::Reference
< XRow
> xRowClone(xResultSetClone
, UNO_QUERY
);
103 CPPUNIT_ASSERT(xRowClone
.is());
105 // the clone starts at same position as what it is cloned from,
106 // and does not move its source.
107 CPPUNIT_ASSERT(xResultSetClone
->isFirst());
108 CPPUNIT_ASSERT(xResultSet
->isFirst());
109 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xResultSet
->getRow());
110 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xResultSetClone
->getRow());
111 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRow
->getInt(1));
112 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRowClone
->getInt(1));
114 // if we move the source, the clone does not move
115 CPPUNIT_ASSERT(xResultSet
->next());
116 CPPUNIT_ASSERT(xResultSetClone
->isFirst());
117 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), xResultSet
->getRow());
118 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xResultSetClone
->getRow());
119 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), xRow
->getInt(1));
120 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRowClone
->getInt(1));
122 CPPUNIT_ASSERT(xResultSet
->last());
123 CPPUNIT_ASSERT(xResultSet
->isLast());
124 CPPUNIT_ASSERT(xResultSetClone
->isFirst());
125 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRowClone
->getInt(1));
127 // and the other way round
128 CPPUNIT_ASSERT(xResultSet
->first());
129 CPPUNIT_ASSERT(xResultSetClone
->next());
130 CPPUNIT_ASSERT(xResultSet
->isFirst());
131 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), xResultSetClone
->getRow());
132 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), xRowClone
->getInt(1));
133 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRow
->getInt(1));
135 CPPUNIT_ASSERT(xResultSetClone
->last());
136 CPPUNIT_ASSERT(xResultSetClone
->isLast());
137 CPPUNIT_ASSERT(xResultSet
->isFirst());
138 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), xRow
->getInt(1));
140 closeDocument(uno::Reference
<lang::XComponent
>(xDocument
, uno::UNO_QUERY
));
143 CPPUNIT_TEST_SUITE_REGISTRATION(RowSetClones
);
145 CPPUNIT_PLUGIN_IMPLEMENT();
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */