From 3c8f9d02e8d9238f6e3dd6a4c769bb26fe8e9093 Mon Sep 17 00:00:00 2001 From: Adam Seskunas Date: Thu, 13 Jun 2024 13:44:30 -0700 Subject: [PATCH] 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 --- dbaccess/JunitTest_dbaccess_complex.mk | 1 - dbaccess/qa/complex/dbaccess/DataSource.java | 68 -------------------- dbaccess/qa/unit/CRMDatabase_test.cxx | 54 ++++++++++++++++ 3 files changed, 54 insertions(+), 69 deletions(-) delete mode 100644 dbaccess/qa/complex/dbaccess/DataSource.java diff --git a/dbaccess/JunitTest_dbaccess_complex.mk b/dbaccess/JunitTest_dbaccess_complex.mk index ba333cbf3b27..99a3cd9f5cbc 100644 --- a/dbaccess/JunitTest_dbaccess_complex.mk +++ b/dbaccess/JunitTest_dbaccess_complex.mk @@ -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 \ diff --git a/dbaccess/qa/complex/dbaccess/DataSource.java b/dbaccess/qa/complex/dbaccess/DataSource.java deleted file mode 100644 index 0413a6e07b69..000000000000 --- a/dbaccess/qa/complex/dbaccess/DataSource.java +++ /dev/null @@ -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()); - } -} diff --git a/dbaccess/qa/unit/CRMDatabase_test.cxx b/dbaccess/qa/unit/CRMDatabase_test.cxx index 0f0cae5e67ef..634bd17b0b0d 100644 --- a/dbaccess/qa/unit/CRMDatabase_test.cxx +++ b/dbaccess/qa/unit/CRMDatabase_test.cxx @@ -9,13 +9,19 @@ #include "dbtest_base.cxx" +#include #include +#include +#include +#include +#include #include #include #include #include #include #include +#include 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 xNameAccess( + getMultiServiceFactory()->createInstance(u"com.sun.star.sdb.DatabaseContext"_ustr), + UNO_QUERY); + Reference xBiblioDataSource(xNameAccess->getByName(u"Bibliography"_ustr), + UNO_QUERY); + CPPUNIT_ASSERT(xBiblioDataSource.is()); + + Reference 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 xDocument(mxComponent, UNO_QUERY_THROW); + uno::Reference xConnection = getConnectionForDocument(xDocument); + createTables(xConnection); + + uno::Reference xDataSource = xDocument->getDataSource(); + CPPUNIT_ASSERT(xDataSource.is()); + + // Get a XDatabaseContext from XSingleService factory in order to register the DataSource + Reference 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 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();