Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / dbaccess / qa / extras / rowsetclones.cxx
blob0afcd5220c21ad093e5c51862f0186a7d674c4e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
11 #include <test/unoapi_test.hxx>
12 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
13 #include <com/sun/star/sdb/CommandType.hpp>
14 #include <com/sun/star/sdbc/XConnection.hpp>
15 #include <com/sun/star/sdbc/XResultSet.hpp>
16 #include <com/sun/star/sdbc/XRow.hpp>
17 #include <com/sun/star/sdbcx/XRowLocate.hpp>
18 #include <com/sun/star/sdbc/XRowSet.hpp>
19 #include <com/sun/star/sdb/XResultSetAccess.hpp>
20 #include <com/sun/star/beans/XPropertySet.hpp>
22 using namespace ::com::sun::star;
23 using namespace ::com::sun::star::uno;
24 using namespace ::com::sun::star::beans;
25 using namespace ::com::sun::star::sdb;
26 using namespace ::com::sun::star::sdbc;
27 using namespace ::com::sun::star::sdbcx;
29 class RowSetClones : public UnoApiTest
31 public:
32 RowSetClones();
34 void test();
36 CPPUNIT_TEST_SUITE(RowSetClones);
37 CPPUNIT_TEST(test);
38 CPPUNIT_TEST_SUITE_END();
43 RowSetClones::RowSetClones()
44 : UnoApiTest("")
48 void RowSetClones::test()
50 const OUString sFilePath(m_directories.getURLFromWorkdir(u"CppunitTest/RowSetClones.odb"));
52 mxComponent = loadFromDesktop(sFilePath);
53 uno::Reference< XOfficeDatabaseDocument > xDocument(mxComponent, UNO_QUERY_THROW);
55 uno::Reference< XDataSource > xDataSource = xDocument->getDataSource();
56 CPPUNIT_ASSERT(xDataSource.is());
58 uno::Reference< XConnection > xConnection = xDataSource->getConnection("","");
59 CPPUNIT_ASSERT(xConnection.is());
61 uno::Reference< XRowSet > xRowSet (getMultiServiceFactory()->createInstance("com.sun.star.sdb.RowSet" ), UNO_QUERY);
62 CPPUNIT_ASSERT(xRowSet.is());
63 uno::Reference< XPropertySet > rowSetProperties ( xRowSet, UNO_QUERY );
64 CPPUNIT_ASSERT(rowSetProperties.is());
65 rowSetProperties->setPropertyValue("Command", Any(OUString("SELECT * FROM Assets ORDER BY AssetID")));
66 rowSetProperties->setPropertyValue("CommandType", Any(CommandType::COMMAND));
67 rowSetProperties->setPropertyValue("ActiveConnection", Any(xConnection));
69 xRowSet->execute();
70 uno::Reference< XResultSet > xResultSet = xRowSet;
71 CPPUNIT_ASSERT(xResultSet.is());
72 // always starts at BeforeFirst position
73 CPPUNIT_ASSERT(xResultSet->isBeforeFirst());
74 CPPUNIT_ASSERT(xResultSet->next());
75 CPPUNIT_ASSERT(xResultSet->isFirst());
76 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSet->getRow());
78 uno::Reference< XRow > xRow(xResultSet, UNO_QUERY);
79 CPPUNIT_ASSERT(xRow.is());
80 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));
82 uno::Reference< XResultSetAccess > xResultSetAccess(xResultSet, UNO_QUERY);
83 CPPUNIT_ASSERT(xResultSetAccess.is());
84 uno::Reference< XResultSet > xResultSetClone = xResultSetAccess->createResultSet();
85 CPPUNIT_ASSERT(xResultSetClone.is());
87 uno::Reference< XRow > xRowClone(xResultSetClone, UNO_QUERY);
88 CPPUNIT_ASSERT(xRowClone.is());
90 // the clone starts at same position as what it is cloned from,
91 // and does not move its source.
92 CPPUNIT_ASSERT(xResultSetClone->isFirst());
93 CPPUNIT_ASSERT(xResultSet->isFirst());
94 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSet->getRow());
95 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSetClone->getRow());
96 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));
97 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));
99 // if we move the source, the clone does not move
100 CPPUNIT_ASSERT(xResultSet->next());
101 CPPUNIT_ASSERT(xResultSetClone->isFirst());
102 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xResultSet->getRow());
103 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xResultSetClone->getRow());
104 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xRow->getInt(1));
105 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));
107 CPPUNIT_ASSERT(xResultSet->last());
108 CPPUNIT_ASSERT(xResultSet->isLast());
109 CPPUNIT_ASSERT(xResultSetClone->isFirst());
110 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRowClone->getInt(1));
112 // and the other way round
113 CPPUNIT_ASSERT(xResultSet->first());
114 CPPUNIT_ASSERT(xResultSetClone->next());
115 CPPUNIT_ASSERT(xResultSet->isFirst());
116 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xResultSetClone->getRow());
117 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xRowClone->getInt(1));
118 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));
120 CPPUNIT_ASSERT(xResultSetClone->last());
121 CPPUNIT_ASSERT(xResultSetClone->isLast());
122 CPPUNIT_ASSERT(xResultSet->isFirst());
123 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xRow->getInt(1));
126 CPPUNIT_TEST_SUITE_REGISTRATION(RowSetClones);
128 CPPUNIT_PLUGIN_IMPLEMENT();
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */