dbaccess complex/DataSource.java to extras/dataSource.cxx

Port the Junit test in DataSource.java over to CppUnit.
In order to port the test as written in Java the
CRM Database was ported, so add a test for that here as
well.

Change-Id: Ib9ac369e3faaa30207b83368889763f0eb063876
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/169071
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
This commit is contained in:
Adam Seskunas 2024-06-13 13:44:30 -07:00 committed by Xisco Fauli
parent c8bec7d4b3
commit 3c8f9d02e8
3 changed files with 54 additions and 69 deletions

View file

@ -33,7 +33,6 @@ $(eval $(call gb_JunitTest_add_sourcefiles,dbaccess_complex,\
dbaccess/qa/complex/dbaccess/CRMBasedTestCase \
dbaccess/qa/complex/dbaccess/CopyTableInterActionHandler \
dbaccess/qa/complex/dbaccess/CopyTableWizard \
dbaccess/qa/complex/dbaccess/DataSource \
dbaccess/qa/complex/dbaccess/DatabaseApplication \
dbaccess/qa/complex/dbaccess/DatabaseDocument \
dbaccess/qa/complex/dbaccess/FileHelper \

View file

@ -1,68 +0,0 @@
/*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package complex.dbaccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XNamingService;
import connectivity.tools.CRMDatabase;
import connectivity.tools.HsqlDatabase;
// ---------- junit imports -----------------
import org.junit.Test;
import static org.junit.Assert.*;
public class DataSource extends TestCase
{
HsqlDatabase m_database;
connectivity.tools.DataSource m_dataSource;
private void createTestCase() throws Exception
{
if (m_database == null)
{
final CRMDatabase database = new CRMDatabase( getMSF(), false );
m_database = database.getDatabase();
m_dataSource = m_database.getDataSource();
}
}
@Test
public void testRegistrationName() throws Exception
{
createTestCase();
// 1. check the existing "Bibliography" data source whether it has the proper name
String dataSourceName = "Bibliography";
final connectivity.tools.DataSource bibliography = new connectivity.tools.DataSource(getMSF(), dataSourceName);
assertEquals("pre-registered database has a wrong name!", dataSourceName, bibliography.getName());
// 2. register a newly created data source, and verify it has the proper name
dataSourceName = "someDataSource";
final XNamingService dataSourceRegistrations = UnoRuntime.queryInterface(
XNamingService.class, getMSF().createInstance( "com.sun.star.sdb.DatabaseContext" ) );
final XNameAccess existenceCheck = UnoRuntime.queryInterface( XNameAccess.class, dataSourceRegistrations );
if ( existenceCheck.hasByName( "someDataSource" ) )
dataSourceRegistrations.revokeObject( "someDataSource" );
dataSourceRegistrations.registerObject("someDataSource", m_dataSource.getXDataSource());
assertEquals("registration name of a newly registered data source is wrong", dataSourceName, m_dataSource.getName());
}
}

View file

@ -9,13 +9,19 @@
#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;
@ -24,9 +30,11 @@ class CRMDBTest : public DBTestBase
{
public:
void testCRMDatabase();
void testRegistrationName();
CPPUNIT_TEST_SUITE(CRMDBTest);
CPPUNIT_TEST(testCRMDatabase);
CPPUNIT_TEST(testRegistrationName);
CPPUNIT_TEST_SUITE_END();
};
@ -73,6 +81,52 @@ void CRMDBTest::testCRMDatabase()
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();