From 8d7e8a0a3b9a4a4f7b805e91674b79a4cca7cf72 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Fri, 13 Apr 2012 11:13:19 +0100 Subject: setup/teardown default component context once before/after all tests avoids the problems of dangling uno singletons invalidated after the first dispose and the chain of other singletons that don't expect to need to re-initialize, etc. reenable editeng cppunit test inherit i18npool cppunit test from unotest base drop LibreOfficeProtector, do "throwable" work in setUp/tearDown not in ctors/dtors --- Repository.mk | 1 + RepositoryFixes.mk | 1 + editeng/qa/unit/core-test.cxx | 2 +- .../CppunitTest_i18npool_test_breakiterator.mk | 1 + i18npool/qa/cppunit/test_breakiterator.cxx | 34 ++---- sal/cppunittester/cppunittester.cxx | 10 +- sal/inc/cppunittester/protectorfactory.hxx | 9 +- solenv/gbuild/CppunitTest.mk | 5 +- test/source/bootstrapfixture.cxx | 13 +-- tools/prj/build.lst | 2 +- unotest/Library_unobootstrapprotector.mk | 53 ++++++++++ unotest/Module_unotest.mk | 1 + unotest/source/cpp/bootstrapfixturebase.cxx | 26 ++--- .../unobootstrapprotector.cxx | 114 +++++++++++++++++++++ .../unoexceptionprotector.cxx | 25 +---- 15 files changed, 203 insertions(+), 94 deletions(-) create mode 100644 unotest/Library_unobootstrapprotector.mk create mode 100644 unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx diff --git a/Repository.mk b/Repository.mk index 2bf19b6e0e4e..52dee490f0ce 100644 --- a/Repository.mk +++ b/Repository.mk @@ -406,6 +406,7 @@ $(eval $(call gb_Helper_register_libraries,PLAINLIBS_OOO, \ ucpchelp1 \ ucphier1 \ ucppkg1 \ + unobootstrapprotector \ unoexceptionprotector \ unopkgapp \ unotest \ diff --git a/RepositoryFixes.mk b/RepositoryFixes.mk index 76516ebdeb20..d07d791b8ba0 100644 --- a/RepositoryFixes.mk +++ b/RepositoryFixes.mk @@ -41,6 +41,7 @@ gb_Library_FILENAMES := $(patsubst purpenvhelper:libpurpen%,purpenvhelper:libuno gb_Library_FILENAMES := $(patsubst salhelper:libsalhelper%,salhelper:libuno_salhelper%,$(gb_Library_FILENAMES)) gb_Library_FILENAMES := $(patsubst ucbhelper:libucbhelper%,ucbhelper:libucbhelper4%,$(gb_Library_FILENAMES)) ifneq ($(OS),ANDROID) +gb_Library_FILENAMES := $(patsubst unobootstrapprotector:libuno%,unobootstrapprotector:uno%,$(gb_Library_FILENAMES)) gb_Library_FILENAMES := $(patsubst unoexceptionprotector:libuno%,unoexceptionprotector:uno%,$(gb_Library_FILENAMES)) endif gb_Library_FILENAMES := $(patsubst unsafe_uno:libunsafe_uno%,unsafe_uno:libunsafe_uno_uno%,$(gb_Library_FILENAMES)) diff --git a/editeng/qa/unit/core-test.cxx b/editeng/qa/unit/core-test.cxx index 61db59f9308e..fc5828f4a307 100644 --- a/editeng/qa/unit/core-test.cxx +++ b/editeng/qa/unit/core-test.cxx @@ -55,7 +55,7 @@ public: void testConstruction(); CPPUNIT_TEST_SUITE(Test); -// CPPUNIT_TEST(testConstruction); + CPPUNIT_TEST(testConstruction); CPPUNIT_TEST_SUITE_END(); private: diff --git a/i18npool/CppunitTest_i18npool_test_breakiterator.mk b/i18npool/CppunitTest_i18npool_test_breakiterator.mk index b9e972c34b06..62100a017244 100644 --- a/i18npool/CppunitTest_i18npool_test_breakiterator.mk +++ b/i18npool/CppunitTest_i18npool_test_breakiterator.mk @@ -37,6 +37,7 @@ $(eval $(call gb_CppunitTest_use_libraries,i18npool_test_breakiterator,\ cppu \ cppuhelper \ sal \ + unotest \ $(gb_STDLIBS) \ )) diff --git a/i18npool/qa/cppunit/test_breakiterator.cxx b/i18npool/qa/cppunit/test_breakiterator.cxx index 7a70f010210e..14051d4ee358 100644 --- a/i18npool/qa/cppunit/test_breakiterator.cxx +++ b/i18npool/qa/cppunit/test_breakiterator.cxx @@ -35,14 +35,11 @@ #include #include #include -#include "cppunit/TestAssert.h" -#include "cppunit/TestFixture.h" -#include "cppunit/extensions/HelperMacros.h" -#include "cppunit/plugin/TestPlugIn.h" #include #include #include #include +#include #include @@ -50,12 +47,9 @@ using namespace ::com::sun::star; -class TestBreakIterator : public CppUnit::TestFixture +class TestBreakIterator : public test::BootstrapFixtureBase { public: - TestBreakIterator(); - ~TestBreakIterator(); - virtual void setUp(); virtual void tearDown(); @@ -72,11 +66,7 @@ public: CPPUNIT_TEST(testAsian); CPPUNIT_TEST(testThai); CPPUNIT_TEST_SUITE_END(); - private: - uno::Reference m_xContext; - uno::Reference m_xFactory; - uno::Reference m_xMSF; uno::Reference m_xBreak; }; @@ -327,27 +317,17 @@ void TestBreakIterator::testThai() #endif } -TestBreakIterator::TestBreakIterator() -{ - m_xContext = cppu::defaultBootstrap_InitialComponentContext(); - m_xFactory = m_xContext->getServiceManager(); - m_xMSF = uno::Reference(m_xFactory, uno::UNO_QUERY_THROW); - m_xBreak = uno::Reference< i18n::XBreakIterator >(m_xMSF->createInstance( - ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.i18n.BreakIterator"))), - uno::UNO_QUERY_THROW); -} - void TestBreakIterator::setUp() { -} - -TestBreakIterator::~TestBreakIterator() -{ - uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose(); + BootstrapFixtureBase::setUp(); + m_xBreak = uno::Reference< i18n::XBreakIterator >(m_xSFactory->createInstance( + "com.sun.star.i18n.BreakIterator"), uno::UNO_QUERY_THROW); } void TestBreakIterator::tearDown() { + BootstrapFixtureBase::tearDown(); + m_xBreak.clear(); } CPPUNIT_TEST_SUITE_REGISTRATION(TestBreakIterator); diff --git a/sal/cppunittester/cppunittester.cxx b/sal/cppunittester/cppunittester.cxx index ba21dacef325..22ac029c4240 100644 --- a/sal/cppunittester/cppunittester.cxx +++ b/sal/cppunittester/cppunittester.cxx @@ -180,7 +180,7 @@ SAL_IMPLEMENT_MAIN() { #endif boost::ptr_vector modules; - cppunittester::LibreOfficeProtector *throw_protector = 0; + CppUnit::Protector *throw_protector = 0; CppUnit::TestResult result; std::string args; std::string testlib; @@ -231,14 +231,8 @@ SAL_IMPLEMENT_MAIN() { index+=3; } - bool ok = false; ProtectedFixtureFunctor tests(testlib, args, result); - //if the unoprotector was given on the command line, use it to catch - //and report the error message of exceptions - if (throw_protector) - ok = throw_protector->protect(tests); - else - ok = tests.run(); + bool ok = tests.run(); return ok ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/sal/inc/cppunittester/protectorfactory.hxx b/sal/inc/cppunittester/protectorfactory.hxx index 093eb5f5d588..b666412cb51d 100644 --- a/sal/inc/cppunittester/protectorfactory.hxx +++ b/sal/inc/cppunittester/protectorfactory.hxx @@ -38,16 +38,9 @@ namespace cppunittester { - class LibreOfficeProtector : public CppUnit::Protector - { - public: - virtual bool protect(CppUnit::Functor const & functor) = 0; - using CppUnit::Protector::protect; - }; - // The type of CppUnit::Protector factory functions that can be plugged into // cppunittester: - extern "C" typedef LibreOfficeProtector * SAL_CALL ProtectorFactory(); + extern "C" typedef CppUnit::Protector * SAL_CALL ProtectorFactory(); } #ifdef DISABLE_DYNLOADING diff --git a/solenv/gbuild/CppunitTest.mk b/solenv/gbuild/CppunitTest.mk index 6f6b32522d4a..258fc52da6c4 100644 --- a/solenv/gbuild/CppunitTest.mk +++ b/solenv/gbuild/CppunitTest.mk @@ -68,8 +68,9 @@ $(if $(URE),\ "-env:UNO_SERVICES=$(foreach item,$(UNO_SERVICES),$(call gb_Helper_make_url,$(item)))") \ $(foreach dir,URE_INTERNAL_LIB_DIR LO_LIB_DIR,\ -env:$(dir)=$(call gb_Helper_make_url,$(gb_CppunitTest_LIBDIR))) \ - --protector unoexceptionprotector$(gb_Library_DLLEXT) unoexceptionprotector) \ -$(ARGS) + --protector unoexceptionprotector$(gb_Library_DLLEXT) unoexceptionprotector \ + --protector unobootstrapprotector$(gb_Library_DLLEXT) unobootstrapprotector \ + ) $(ARGS) endef .PHONY : $(call gb_CppunitTest_get_clean_target,%) diff --git a/test/source/bootstrapfixture.cxx b/test/source/bootstrapfixture.cxx index 284dbf7f3210..edb7b63b30a0 100644 --- a/test/source/bootstrapfixture.cxx +++ b/test/source/bootstrapfixture.cxx @@ -62,6 +62,12 @@ test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog, bool bNeedUCB ) : m_bNeedUCB( bNeedUCB ) , m_bAssertOnDialog( bAssertOnDialog ) { +} + +void test::BootstrapFixture::setUp() +{ + test::BootstrapFixtureBase::setUp(); + // force locale (and resource files loaded) to en-US const LanguageType eLang=LANGUAGE_ENGLISH_US; @@ -69,11 +75,7 @@ test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog, bool bNeedUCB ) MsLangId::convertLanguageToIsoNames(eLang, aLang, aCountry); lang::Locale aLocale(aLang, aCountry, rtl::OUString()); ResMgr::SetDefaultLocale( aLocale ); -} -void test::BootstrapFixture::setUp() -{ - test::BootstrapFixtureBase::setUp(); if (m_bNeedUCB) { // initialise UCB-Broker @@ -96,9 +98,8 @@ void test::BootstrapFixture::setUp() aLocalOptions.SetUILocaleConfigString( aLangISO ); InitVCL(m_xSFactory); - if (Application::IsHeadlessModeRequested()) { + if (Application::IsHeadlessModeRequested()) Application::EnableHeadlessMode(true); - } if( m_bAssertOnDialog ) ErrorHandler::RegisterDisplay( aBasicErrorFunc ); diff --git a/tools/prj/build.lst b/tools/prj/build.lst index fb086f248e2a..ee6803567843 100644 --- a/tools/prj/build.lst +++ b/tools/prj/build.lst @@ -1,3 +1,3 @@ -tl tools : cppu external offapi ZLIB:zlib EXPAT:expat basegfx comphelper i18npool unotest NULL +tl tools : cppu external offapi ZLIB:zlib EXPAT:expat basegfx comphelper i18npool NULL tl tools\prj nmake - all tl_prj NULL # tl tools\qa nmake - all tl_qa tl_utl NULL diff --git a/unotest/Library_unobootstrapprotector.mk b/unotest/Library_unobootstrapprotector.mk new file mode 100644 index 000000000000..26b7a2cbf732 --- /dev/null +++ b/unotest/Library_unobootstrapprotector.mk @@ -0,0 +1,53 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# Version: MPL 1.1 / GPLv3+ / LGPLv3+ +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (the "License"); you may not use this file except in compliance with +# the License or as specified alternatively below. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# Major Contributor(s): +# Copyright (C) 2012 Matúš Kukan (initial developer) +# +# All Rights Reserved. +# +# For minor contributions see the git repository. +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 3 or later (the "GPLv3+"), or +# the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), +# in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable +# instead of those above. + +$(eval $(call gb_Library_Library,unobootstrapprotector)) + +$(eval $(call gb_Library_use_package,unobootstrapprotector,unotest_inc)) + +$(eval $(call gb_Library_use_api,unobootstrapprotector,\ + udkapi \ + offapi \ +)) + +$(eval $(call gb_Library_use_libraries,unobootstrapprotector,\ + comphelper \ + cppu \ + cppuhelper \ + sal \ + $(gb_STDLIBS) \ +)) + +$(eval $(call gb_Library_use_externals,unobootstrapprotector,\ + cppunit \ +)) + +$(eval $(call gb_Library_add_exception_objects,unobootstrapprotector,\ + unotest/source/cpp/unobootstrapprotector/unobootstrapprotector \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/unotest/Module_unotest.mk b/unotest/Module_unotest.mk index fc9f18cc48f8..6980b6e70a82 100644 --- a/unotest/Module_unotest.mk +++ b/unotest/Module_unotest.mk @@ -28,6 +28,7 @@ $(eval $(call gb_Module_Module,unotest)) $(eval $(call gb_Module_add_targets,unotest,\ + Library_unobootstrapprotector \ Library_unoexceptionprotector \ Library_unotest \ Package_inc \ diff --git a/unotest/source/cpp/bootstrapfixturebase.cxx b/unotest/source/cpp/bootstrapfixturebase.cxx index 0c7c841f1d99..5ce1a0611604 100644 --- a/unotest/source/cpp/bootstrapfixturebase.cxx +++ b/unotest/source/cpp/bootstrapfixturebase.cxx @@ -44,7 +44,7 @@ using namespace ::com::sun::star; // heavy lifting is deferred until setUp. setUp and tearDown are interleaved // between the tests as you might expect. test::BootstrapFixtureBase::BootstrapFixtureBase() - : m_aSrcRootURL(RTL_CONSTASCII_USTRINGPARAM("file://")), m_aSolverRootURL( m_aSrcRootURL ) + : m_aSrcRootURL("file://"), m_aSolverRootURL( m_aSrcRootURL ) { #ifndef ANDROID const char* pSrcRoot = getenv( "SRC_ROOT" ); @@ -74,46 +74,38 @@ test::BootstrapFixtureBase::~BootstrapFixtureBase() ::rtl::OUString test::BootstrapFixtureBase::getURLFromSrc( const char *pPath ) { - return m_aSrcRootURL + rtl::OUString::createFromAscii( pPath ); + return m_aSrcRootURL + rtl::OUString::createFromAscii( pPath ); } ::rtl::OUString test::BootstrapFixtureBase::getPathFromSrc( const char *pPath ) { - return m_aSrcRootPath + rtl::OUString::createFromAscii( pPath ); + return m_aSrcRootPath + rtl::OUString::createFromAscii( pPath ); } ::rtl::OUString test::BootstrapFixtureBase::getURLFromSolver( const char *pPath ) { - return m_aSolverRootURL + rtl::OUString::createFromAscii( pPath ); + return m_aSolverRootURL + rtl::OUString::createFromAscii( pPath ); } ::rtl::OUString test::BootstrapFixtureBase::getPathFromSolver( const char *pPath ) { - return m_aSolverRootPath + rtl::OUString::createFromAscii( pPath ); + return m_aSolverRootPath + rtl::OUString::createFromAscii( pPath ); } void test::BootstrapFixtureBase::setUp() { // set UserInstallation to user profile dir in test/user-template rtl::Bootstrap aDefaultVars; - rtl::OUString sUserInstallURL = m_aSolverRootURL + rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/unittest" ) ); - aDefaultVars.set( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UserInstallation") ), - sUserInstallURL); + rtl::OUString sUserInstallURL = m_aSolverRootURL + rtl::OUString("/unittest"); + aDefaultVars.set(rtl::OUString("UserInstallation"), sUserInstallURL); - m_xContext = cppu::defaultBootstrap_InitialComponentContext(); + m_xContext = comphelper::getProcessComponentContext(); m_xFactory = m_xContext->getServiceManager(); - m_xSFactory = uno::Reference (m_xFactory, uno::UNO_QUERY_THROW); - - // Without this we're crashing because callees are using - // getProcessServiceFactory. In general those should be removed in favour - // of retaining references to the root ServiceFactory as its passed around - comphelper::setProcessServiceFactory(m_xSFactory); + m_xSFactory = uno::Reference(m_xFactory, uno::UNO_QUERY_THROW); } void test::BootstrapFixtureBase::tearDown() { - // uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose(); } - /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx b/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx new file mode 100644 index 000000000000..cb0983a08b40 --- /dev/null +++ b/unotest/source/cpp/unobootstrapprotector/unobootstrapprotector.cxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2000, 2011 Oracle and/or its affiliates. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "sal/config.h" +#include "sal/precppunit.hxx" + +#include +#include +#include + +#include "boost/noncopyable.hpp" +#include "com/sun/star/uno/Any.hxx" +#include "com/sun/star/uno/Exception.hpp" + +#include +#include +#include + +#include +#include +#include + +#include "cppuhelper/exc_hlp.hxx" +#include "cppunit/Message.h" +#include "osl/thread.h" +#include "rtl/string.hxx" +#include "rtl/ustring.h" +#include "rtl/ustring.hxx" +#include "sal/types.h" + +#include "protectorfactory.hxx" + +namespace { + +using namespace com::sun::star; + +//cppunit calls instantiates a new TextFixture for each test and calls setUp +//and tearDown on that for every test in a fixture +// +//We basically need to call dispose on our root component context context to +//shut down cleanly in the right order. +// +//But we can't setup and tear down the root component context for +//every test because all the uno singletons will be invalid after +//the first dispose. So lets setup the default context once before +//all tests are run, and tear it down once after all have finished + +class Prot : public CppUnit::Protector, private boost::noncopyable +{ +public: + Prot(); + + virtual ~Prot(); + + virtual bool protect( + CppUnit::Functor const & functor, + CppUnit::ProtectorContext const & context); +private: + uno::Reference m_xContext; +}; + + +Prot::Prot() +{ + m_xContext = cppu::defaultBootstrap_InitialComponentContext(); + + uno::Reference xFactory = m_xContext->getServiceManager(); + uno::Reference xSFactory(xFactory, uno::UNO_QUERY_THROW); + + comphelper::setProcessServiceFactory(xSFactory); +} + +bool Prot::protect( + CppUnit::Functor const & functor, CppUnit::ProtectorContext const &) +{ + return functor(); +} + +Prot::~Prot() +{ + uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose(); +} + +} + +extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL unobootstrapprotector() +{ + return new Prot; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx b/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx index 3829bd1b3b6e..6f1f6795edcf 100644 --- a/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx +++ b/unotest/source/cpp/unoexceptionprotector/unoexceptionprotector.cxx @@ -59,7 +59,7 @@ std::string convert(rtl::OUString const & s16) { : static_cast< std::string::size_type >(s8.getLength()))); } -class Prot : public cppunittester::LibreOfficeProtector, private boost::noncopyable +class Prot : public CppUnit::Protector, private boost::noncopyable { public: Prot() {} @@ -69,8 +69,6 @@ public: virtual bool protect( CppUnit::Functor const & functor, CppUnit::ProtectorContext const & context); - - virtual bool protect(CppUnit::Functor const & functor); }; bool Prot::protect( @@ -93,27 +91,6 @@ bool Prot::protect( return false; } -bool Prot::protect(CppUnit::Functor const & functor) -{ - bool bRet = false; - try - { - bRet = functor(); - } catch (const css::uno::Exception &e) - { - css::uno::Any a(cppu::getCaughtException()); - std::cerr - << convert(rtl::OUString( - RTL_CONSTASCII_USTRINGPARAM( - "An uncaught exception of type ")) - + a.getValueTypeName()) - << std::endl << "Exception Message was: " << convert(e.Message) - << std::endl; - throw; - } - return bRet; -} - } extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL -- cgit