LibreOffice Module unotest (master) 1
unobootstrapprotector.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/uno/Exception.hpp>
21
24
25#include <com/sun/star/lang/XComponent.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27#include <com/sun/star/uno/XComponentContext.hpp>
28
29#include <sal/types.h>
30
31#include <cppunit/Protector.h>
32
33namespace {
34
35using namespace com::sun::star;
36
37//cppunit calls instantiates a new TextFixture for each test and calls setUp
38//and tearDown on that for every test in a fixture
39
40//We basically need to call dispose on our root component context to
41//shut down cleanly in the right order.
42
43//But we can't setup and tear down the root component context for
44//every test because all the uno singletons will be invalid after
45//the first dispose. So lets setup the default context once before
46//all tests are run, and tear it down once after all have finished
47
48class Prot : public CppUnit::Protector
49{
50public:
51 Prot();
52
53 virtual ~Prot() override;
54
55 Prot(const Prot&) = delete;
56 Prot& operator=(const Prot&) = delete;
57
58 virtual bool protect(
59 CppUnit::Functor const & functor,
60 CppUnit::ProtectorContext const & context) override;
61private:
62 uno::Reference<uno::XComponentContext> m_xContext;
63};
64
65
66Prot::Prot()
68{
69 uno::Reference<lang::XMultiComponentFactory> xFactory = m_xContext->getServiceManager();
70 uno::Reference<lang::XMultiServiceFactory> xSFactory(xFactory, uno::UNO_QUERY_THROW);
71
73}
74
75bool Prot::protect(
76 CppUnit::Functor const & functor, CppUnit::ProtectorContext const &)
77{
78 return functor();
79}
80
81Prot::~Prot()
82{
83 uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose();
84}
85
86}
87
88extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * unobootstrapprotector()
89{
90 return new Prot;
91}
92
93/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
Reference< XSingleServiceFactory > xFactory
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
SAL_DLLPUBLIC_EXPORT CppUnit::Protector * unobootstrapprotector()