xref: /aoo42x/main/configmgr/qa/unit/test.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir *
7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir *
9*cdf0e10cSrcweir * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir *
15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir *
26*cdf0e10cSrcweir ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_configmgr.hxx"
29*cdf0e10cSrcweir #include "sal/config.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cstddef>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "com/sun/star/beans/NamedValue.hpp"
34*cdf0e10cSrcweir #include "com/sun/star/beans/PropertyChangeEvent.hpp"
35*cdf0e10cSrcweir #include "com/sun/star/beans/XPropertyChangeListener.hpp"
36*cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/beans/XPropertyState.hpp"
38*cdf0e10cSrcweir #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
39*cdf0e10cSrcweir #include "com/sun/star/container/XNameReplace.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/container/XNamed.hpp"
41*cdf0e10cSrcweir #include "com/sun/star/lang/EventObject.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/lang/XComponent.hpp"
43*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
44*cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
45*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
46*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
47*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
48*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
49*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
50*cdf0e10cSrcweir #include "com/sun/star/util/XChangesBatch.hpp"
51*cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
52*cdf0e10cSrcweir #include "cppuhelper/servicefactory.hxx"
53*cdf0e10cSrcweir #include "osl/conditn.hxx"
54*cdf0e10cSrcweir #include "osl/thread.h"
55*cdf0e10cSrcweir #include "osl/thread.hxx"
56*cdf0e10cSrcweir #include "osl/time.h"
57*cdf0e10cSrcweir #include "rtl/ref.hxx"
58*cdf0e10cSrcweir #include "rtl/string.h"
59*cdf0e10cSrcweir #include "rtl/textcvt.h"
60*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
61*cdf0e10cSrcweir #include "rtl/ustring.h"
62*cdf0e10cSrcweir #include "rtl/ustring.hxx"
63*cdf0e10cSrcweir #include "sal/types.h"
64*cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir namespace {
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir namespace css = com::sun::star;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir void normalize(
71*cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & relative,
72*cdf0e10cSrcweir     rtl::OUString * normalizedPath, rtl::OUString * name)
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     sal_Int32 i = relative.lastIndexOf('/');
75*cdf0e10cSrcweir     if (i == -1) {
76*cdf0e10cSrcweir         *normalizedPath = path;
77*cdf0e10cSrcweir         *name = relative;
78*cdf0e10cSrcweir     } else {
79*cdf0e10cSrcweir         rtl::OUStringBuffer buf(path);
80*cdf0e10cSrcweir         buf.append(sal_Unicode('/'));
81*cdf0e10cSrcweir         buf.append(relative.copy(0, i));
82*cdf0e10cSrcweir         *normalizedPath = buf.makeStringAndClear();
83*cdf0e10cSrcweir         *name = relative.copy(i + 1);
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir class Test: public CppUnit::TestFixture {
88*cdf0e10cSrcweir public:
89*cdf0e10cSrcweir     virtual void setUp();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     virtual void tearDown();
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     void testKeyFetch();
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     void testKeySet();
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     void testKeyReset();
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     void testSetSetMemberName();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     void testReadCommands();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     void testThreads();
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     void testRecursive();
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     void testCrossThreads();
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     css::uno::Any getKey(
110*cdf0e10cSrcweir         rtl::OUString const & path, rtl::OUString const & relative) const;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     void setKey(
113*cdf0e10cSrcweir         rtl::OUString const & path, rtl::OUString const & name,
114*cdf0e10cSrcweir         css::uno::Any const & value) const;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     bool resetKey(rtl::OUString const & path, rtl::OUString const & name) const;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > createViewAccess(
119*cdf0e10cSrcweir         rtl::OUString const & path) const;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > createUpdateAccess(
122*cdf0e10cSrcweir         rtl::OUString const & path) const;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
125*cdf0e10cSrcweir     CPPUNIT_TEST(testKeyFetch);
126*cdf0e10cSrcweir     CPPUNIT_TEST(testKeySet);
127*cdf0e10cSrcweir     CPPUNIT_TEST(testKeyReset);
128*cdf0e10cSrcweir     CPPUNIT_TEST(testSetSetMemberName);
129*cdf0e10cSrcweir     CPPUNIT_TEST(testReadCommands);
130*cdf0e10cSrcweir     CPPUNIT_TEST(testThreads);
131*cdf0e10cSrcweir     CPPUNIT_TEST(testRecursive);
132*cdf0e10cSrcweir     CPPUNIT_TEST(testCrossThreads);
133*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir private:
136*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > context_;
137*cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
138*cdf0e10cSrcweir };
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir class TestThread: public osl::Thread {
141*cdf0e10cSrcweir public:
142*cdf0e10cSrcweir     TestThread(osl::Condition & stop);
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     bool getSuccess() const;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir protected:
147*cdf0e10cSrcweir     virtual bool iteration() = 0;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir private:
150*cdf0e10cSrcweir     virtual void SAL_CALL run();
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     osl::Condition & stop_;
153*cdf0e10cSrcweir     bool success_;
154*cdf0e10cSrcweir };
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir TestThread::TestThread(
157*cdf0e10cSrcweir     osl::Condition & stop):
158*cdf0e10cSrcweir     stop_(stop), success_(true)
159*cdf0e10cSrcweir {}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir bool TestThread::getSuccess() const {
162*cdf0e10cSrcweir     return success_;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir void TestThread::run() {
166*cdf0e10cSrcweir     try {
167*cdf0e10cSrcweir         while (!stop_.check()) {
168*cdf0e10cSrcweir             if (!iteration()) {
169*cdf0e10cSrcweir                 success_ = false;
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir     } catch (...) {
173*cdf0e10cSrcweir         success_ = false;
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir class ReaderThread: public TestThread {
178*cdf0e10cSrcweir public:
179*cdf0e10cSrcweir     ReaderThread(
180*cdf0e10cSrcweir         osl::Condition & stop, Test const & test, rtl::OUString const & path,
181*cdf0e10cSrcweir         rtl::OUString const & relative);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir private:
184*cdf0e10cSrcweir     virtual bool iteration();
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     Test const & test_;
187*cdf0e10cSrcweir     rtl::OUString path_;
188*cdf0e10cSrcweir     rtl::OUString relative_;
189*cdf0e10cSrcweir };
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir ReaderThread::ReaderThread(
192*cdf0e10cSrcweir     osl::Condition & stop, Test const & test, rtl::OUString const & path,
193*cdf0e10cSrcweir     rtl::OUString const & relative):
194*cdf0e10cSrcweir     TestThread(stop), test_(test), path_(path), relative_(relative)
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     create();
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir bool ReaderThread::iteration() {
200*cdf0e10cSrcweir     return test_.getKey(path_, relative_).hasValue();
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir class WriterThread: public TestThread {
204*cdf0e10cSrcweir public:
205*cdf0e10cSrcweir     WriterThread(
206*cdf0e10cSrcweir         osl::Condition & stop, Test const & test, rtl::OUString const & path,
207*cdf0e10cSrcweir         rtl::OUString const & relative);
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir private:
210*cdf0e10cSrcweir     virtual bool iteration();
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     Test const & test_;
213*cdf0e10cSrcweir     rtl::OUString path_;
214*cdf0e10cSrcweir     rtl::OUString name_;
215*cdf0e10cSrcweir     std::size_t index_;
216*cdf0e10cSrcweir };
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir WriterThread::WriterThread(
219*cdf0e10cSrcweir     osl::Condition & stop, Test const & test, rtl::OUString const & path,
220*cdf0e10cSrcweir     rtl::OUString const & relative):
221*cdf0e10cSrcweir     TestThread(stop), test_(test), index_(0)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     normalize(path, relative, &path_, &name_);
224*cdf0e10cSrcweir     create();
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir bool WriterThread::iteration() {
228*cdf0e10cSrcweir     rtl::OUString options[] = {
229*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("fish")),
230*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("chips")),
231*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("kippers")),
232*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bloaters")) };
233*cdf0e10cSrcweir     test_.setKey(path_, name_, css::uno::makeAny(options[index_]));
234*cdf0e10cSrcweir     index_ = (index_ + 1) % (sizeof options / sizeof (rtl::OUString));
235*cdf0e10cSrcweir     return true;
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir class RecursiveTest:
239*cdf0e10cSrcweir     public cppu::WeakImplHelper1< css::beans::XPropertyChangeListener >
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir public:
242*cdf0e10cSrcweir     RecursiveTest(Test const & theTest, int count, bool * destroyed);
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     void test();
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir protected:
247*cdf0e10cSrcweir     virtual ~RecursiveTest();
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     virtual void step() const = 0;
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     Test const & test_;
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir private:
254*cdf0e10cSrcweir     virtual void SAL_CALL disposing(css::lang::EventObject const &)
255*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     virtual void SAL_CALL propertyChange(
258*cdf0e10cSrcweir         css::beans::PropertyChangeEvent const &)
259*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     int count_;
262*cdf0e10cSrcweir     bool * destroyed_;
263*cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > properties_;
264*cdf0e10cSrcweir };
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir RecursiveTest::RecursiveTest(
267*cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
268*cdf0e10cSrcweir     test_(theTest), count_(count), destroyed_(destroyed)
269*cdf0e10cSrcweir {}
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir void RecursiveTest::test() {
272*cdf0e10cSrcweir     properties_ = css::uno::Reference< css::beans::XPropertySet >(
273*cdf0e10cSrcweir         test_.createUpdateAccess(
274*cdf0e10cSrcweir             rtl::OUString(
275*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
276*cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
277*cdf0e10cSrcweir                     "dotuno:WebHtml"))),
278*cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
279*cdf0e10cSrcweir     properties_->addPropertyChangeListener(
280*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")), this);
281*cdf0e10cSrcweir     step();
282*cdf0e10cSrcweir     CPPUNIT_ASSERT(count_ == 0);
283*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
284*cdf0e10cSrcweir         properties_, css::uno::UNO_QUERY_THROW)->dispose();
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir RecursiveTest::~RecursiveTest() {
288*cdf0e10cSrcweir     *destroyed_ = true;
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir void RecursiveTest::disposing(css::lang::EventObject const & Source)
292*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir     CPPUNIT_ASSERT(properties_.is() && Source.Source == properties_);
295*cdf0e10cSrcweir     properties_.clear();
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir void RecursiveTest::propertyChange(css::beans::PropertyChangeEvent const & evt)
299*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     CPPUNIT_ASSERT(
302*cdf0e10cSrcweir         evt.Source == properties_ &&
303*cdf0e10cSrcweir         evt.PropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Label")));
304*cdf0e10cSrcweir     if (count_ > 0) {
305*cdf0e10cSrcweir         --count_;
306*cdf0e10cSrcweir         step();
307*cdf0e10cSrcweir     }
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir class SimpleRecursiveTest: public RecursiveTest {
311*cdf0e10cSrcweir public:
312*cdf0e10cSrcweir     SimpleRecursiveTest(Test const & theTest, int count, bool * destroyed);
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir private:
315*cdf0e10cSrcweir     virtual void step() const;
316*cdf0e10cSrcweir };
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir SimpleRecursiveTest::SimpleRecursiveTest(
319*cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
320*cdf0e10cSrcweir     RecursiveTest(theTest, count, destroyed)
321*cdf0e10cSrcweir {}
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir void SimpleRecursiveTest::step() const {
324*cdf0e10cSrcweir     test_.setKey(
325*cdf0e10cSrcweir         rtl::OUString(
326*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
327*cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
328*cdf0e10cSrcweir                 "dotuno:WebHtml")),
329*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")),
330*cdf0e10cSrcweir         css::uno::makeAny(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("step"))));
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir class CrossThreadTest: public RecursiveTest {
334*cdf0e10cSrcweir public:
335*cdf0e10cSrcweir     CrossThreadTest(Test const & theTest, int count, bool * destroyed);
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir private:
338*cdf0e10cSrcweir     virtual void step() const;
339*cdf0e10cSrcweir };
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir CrossThreadTest::CrossThreadTest(
342*cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
343*cdf0e10cSrcweir     RecursiveTest(theTest, count, destroyed)
344*cdf0e10cSrcweir {}
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir void CrossThreadTest::step() const {
347*cdf0e10cSrcweir     osl::Condition stop;
348*cdf0e10cSrcweir     stop.set();
349*cdf0e10cSrcweir     WriterThread(
350*cdf0e10cSrcweir         stop, test_,
351*cdf0e10cSrcweir         rtl::OUString(
352*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
353*cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
354*cdf0e10cSrcweir                 "dotuno:WebHtml")),
355*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))).join();
356*cdf0e10cSrcweir     test_.resetKey(
357*cdf0e10cSrcweir         rtl::OUString(
358*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
359*cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
360*cdf0e10cSrcweir                 "dotuno:WebHtml")),
361*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")));
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir void Test::setUp() {
365*cdf0e10cSrcweir     char const * forward = getForwardString();
366*cdf0e10cSrcweir     rtl_uString * registry = 0;
367*cdf0e10cSrcweir     CPPUNIT_ASSERT(
368*cdf0e10cSrcweir         rtl_convertStringToUString(
369*cdf0e10cSrcweir             &registry, forward, rtl_str_getLength(forward),
370*cdf0e10cSrcweir             osl_getThreadTextEncoding(),
371*cdf0e10cSrcweir             (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
372*cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
373*cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
374*cdf0e10cSrcweir     context_ = css::uno::Reference< css::uno::XComponentContext >(
375*cdf0e10cSrcweir         css::uno::Reference< css::beans::XPropertySet >(
376*cdf0e10cSrcweir             cppu::createRegistryServiceFactory(
377*cdf0e10cSrcweir                 rtl::OUString(registry, SAL_NO_ACQUIRE)),
378*cdf0e10cSrcweir             css::uno::UNO_QUERY_THROW)->getPropertyValue(
379*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))),
380*cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
381*cdf0e10cSrcweir     CPPUNIT_ASSERT(
382*cdf0e10cSrcweir         context_->getValueByName(
383*cdf0e10cSrcweir             rtl::OUString(
384*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
385*cdf0e10cSrcweir                     "/singletons/"
386*cdf0e10cSrcweir                     "com.sun.star.configuration.theDefaultProvider"))) >>=
387*cdf0e10cSrcweir         provider_);
388*cdf0e10cSrcweir }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir void Test::tearDown() {
391*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
392*cdf0e10cSrcweir         context_, css::uno::UNO_QUERY_THROW)->dispose();
393*cdf0e10cSrcweir }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir void Test::testKeyFetch() {
396*cdf0e10cSrcweir     rtl::OUString s;
397*cdf0e10cSrcweir     CPPUNIT_ASSERT(
398*cdf0e10cSrcweir         getKey(
399*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
400*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("L10N/ooLocale"))) >>=
401*cdf0e10cSrcweir         s);
402*cdf0e10cSrcweir     CPPUNIT_ASSERT(
403*cdf0e10cSrcweir         getKey(
404*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
405*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString"))) >>=
406*cdf0e10cSrcweir         s);
407*cdf0e10cSrcweir }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir void Test::testKeySet() {
410*cdf0e10cSrcweir     setKey(
411*cdf0e10cSrcweir         rtl::OUString(
412*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
413*cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString")),
414*cdf0e10cSrcweir         css::uno::makeAny(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baa"))));
415*cdf0e10cSrcweir     rtl::OUString s;
416*cdf0e10cSrcweir     CPPUNIT_ASSERT(
417*cdf0e10cSrcweir         getKey(
418*cdf0e10cSrcweir             rtl::OUString(
419*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
420*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))) >>=
421*cdf0e10cSrcweir         s);
422*cdf0e10cSrcweir     CPPUNIT_ASSERT(s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("baa")));
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir void Test::testKeyReset() {
426*cdf0e10cSrcweir     if (resetKey(
427*cdf0e10cSrcweir             rtl::OUString(
428*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
429*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))))
430*cdf0e10cSrcweir     {
431*cdf0e10cSrcweir         rtl::OUString s;
432*cdf0e10cSrcweir         CPPUNIT_ASSERT(
433*cdf0e10cSrcweir             getKey(
434*cdf0e10cSrcweir                 rtl::OUString(
435*cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
436*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))) >>=
437*cdf0e10cSrcweir             s);
438*cdf0e10cSrcweir         CPPUNIT_ASSERT(s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Foo")));
439*cdf0e10cSrcweir     }
440*cdf0e10cSrcweir }
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir void Test::testSetSetMemberName() {
443*cdf0e10cSrcweir     rtl::OUString s;
444*cdf0e10cSrcweir     CPPUNIT_ASSERT(
445*cdf0e10cSrcweir         getKey(
446*cdf0e10cSrcweir             rtl::OUString(
447*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
448*cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
449*cdf0e10cSrcweir                     ".uno:FontworkShapeType")),
450*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))) >>=
451*cdf0e10cSrcweir         s);
452*cdf0e10cSrcweir     CPPUNIT_ASSERT(
453*cdf0e10cSrcweir         s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Fontwork Shape")));
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > access(
456*cdf0e10cSrcweir         createUpdateAccess(
457*cdf0e10cSrcweir             rtl::OUString(
458*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
459*cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/"
460*cdf0e10cSrcweir                     "Commands"))),
461*cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
462*cdf0e10cSrcweir     css::uno::Reference< css::container::XNamed > member;
463*cdf0e10cSrcweir     access->getByName(
464*cdf0e10cSrcweir         rtl::OUString(
465*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(".uno:FontworkGalleryFloater"))) >>=
466*cdf0e10cSrcweir         member;
467*cdf0e10cSrcweir     CPPUNIT_ASSERT(member.is());
468*cdf0e10cSrcweir     member->setName(
469*cdf0e10cSrcweir         rtl::OUString(
470*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(".uno:FontworkShapeType")));
471*cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch >(
472*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->commitChanges();
473*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
474*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir     CPPUNIT_ASSERT(
477*cdf0e10cSrcweir         getKey(
478*cdf0e10cSrcweir             rtl::OUString(
479*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
480*cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
481*cdf0e10cSrcweir                     ".uno:FontworkShapeType")),
482*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))) >>=
483*cdf0e10cSrcweir         s);
484*cdf0e10cSrcweir     CPPUNIT_ASSERT(
485*cdf0e10cSrcweir         s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Fontwork Gallery")));
486*cdf0e10cSrcweir }
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir void Test::testReadCommands() {
489*cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > access(
490*cdf0e10cSrcweir         createViewAccess(
491*cdf0e10cSrcweir             rtl::OUString(
492*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
493*cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/"
494*cdf0e10cSrcweir                     "Commands"))),
495*cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
496*cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > names(access->getElementNames());
497*cdf0e10cSrcweir     CPPUNIT_ASSERT(names.getLength() == 695);
498*cdf0e10cSrcweir         // testSetSetMemberName() already removed ".uno:FontworkGalleryFloater"
499*cdf0e10cSrcweir     sal_uInt32 n = osl_getGlobalTimer();
500*cdf0e10cSrcweir     for (int i = 0; i < 8; ++i) {
501*cdf0e10cSrcweir         for (sal_Int32 j = 0; j < names.getLength(); ++j) {
502*cdf0e10cSrcweir             css::uno::Reference< css::container::XNameAccess > child;
503*cdf0e10cSrcweir             if (access->getByName(names[j]) >>= child) {
504*cdf0e10cSrcweir                 CPPUNIT_ASSERT(child.is());
505*cdf0e10cSrcweir                 child->getByName(
506*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")));
507*cdf0e10cSrcweir                 child->getByName(
508*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ContextLabel")));
509*cdf0e10cSrcweir                 child->getByName(
510*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Properties")));
511*cdf0e10cSrcweir             }
512*cdf0e10cSrcweir         }
513*cdf0e10cSrcweir     }
514*cdf0e10cSrcweir     n = osl_getGlobalTimer() - n;
515*cdf0e10cSrcweir     t_print("Reading elements took %" SAL_PRIuUINT32 " ms\n", n);
516*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
517*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
518*cdf0e10cSrcweir }
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir void Test::testThreads() {
521*cdf0e10cSrcweir     struct Entry { rtl::OUString path; rtl::OUString relative; };
522*cdf0e10cSrcweir     Entry list[] = {
523*cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
524*cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString")) },
525*cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
526*cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString")) },
527*cdf0e10cSrcweir         { rtl::OUString(
528*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
529*cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
530*cdf0e10cSrcweir           rtl::OUString(
531*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
532*cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:WebHtml/Label")) },
533*cdf0e10cSrcweir         { rtl::OUString(
534*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
535*cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
536*cdf0e10cSrcweir           rtl::OUString(
537*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
538*cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:NewPresentation/Label")) },
539*cdf0e10cSrcweir         { rtl::OUString(
540*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
541*cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
542*cdf0e10cSrcweir           rtl::OUString(
543*cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
544*cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:RecentFileList/Label")) },
545*cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
546*cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("L10N/ooLocale")) },
547*cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
548*cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/ABoolean")) }
549*cdf0e10cSrcweir     };
550*cdf0e10cSrcweir     std::size_t const numReaders = sizeof list / sizeof (Entry);
551*cdf0e10cSrcweir     std::size_t const numWriters = numReaders - 2;
552*cdf0e10cSrcweir     ReaderThread * readers[numReaders];
553*cdf0e10cSrcweir     WriterThread * writers[numWriters];
554*cdf0e10cSrcweir     osl::Condition stop;
555*cdf0e10cSrcweir     for (std::size_t i = 0; i < numReaders; ++i) {
556*cdf0e10cSrcweir         CPPUNIT_ASSERT(getKey(list[i].path, list[i].relative).hasValue());
557*cdf0e10cSrcweir         readers[i] = new ReaderThread(
558*cdf0e10cSrcweir             stop, *this, list[i].path, list[i].relative);
559*cdf0e10cSrcweir     }
560*cdf0e10cSrcweir     for (std::size_t i = 0; i < numWriters; ++i) {
561*cdf0e10cSrcweir         writers[i] = new WriterThread(
562*cdf0e10cSrcweir             stop, *this, list[i].path, list[i].relative);
563*cdf0e10cSrcweir     }
564*cdf0e10cSrcweir     for (int i = 0; i < 5; ++i) {
565*cdf0e10cSrcweir         for (std::size_t j = 0; j < numReaders; ++j) {
566*cdf0e10cSrcweir             rtl::OUString path;
567*cdf0e10cSrcweir             rtl::OUString name;
568*cdf0e10cSrcweir             normalize(list[j].path, list[j].relative, &path, &name);
569*cdf0e10cSrcweir             resetKey(path, name);
570*cdf0e10cSrcweir             osl::Thread::yield();
571*cdf0e10cSrcweir         }
572*cdf0e10cSrcweir     }
573*cdf0e10cSrcweir     stop.set();
574*cdf0e10cSrcweir     bool success = true;
575*cdf0e10cSrcweir     for (std::size_t i = 0; i < numReaders; ++i) {
576*cdf0e10cSrcweir         readers[i]->join();
577*cdf0e10cSrcweir         success = success && readers[i]->getSuccess();
578*cdf0e10cSrcweir         delete readers[i];
579*cdf0e10cSrcweir     }
580*cdf0e10cSrcweir     for (std::size_t i = 0; i < numWriters; ++i) {
581*cdf0e10cSrcweir         writers[i]->join();
582*cdf0e10cSrcweir         success = success && writers[i]->getSuccess();
583*cdf0e10cSrcweir         delete writers[i];
584*cdf0e10cSrcweir     }
585*cdf0e10cSrcweir     CPPUNIT_ASSERT(success);
586*cdf0e10cSrcweir }
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir void Test::testRecursive() {
589*cdf0e10cSrcweir     bool destroyed = false;
590*cdf0e10cSrcweir     rtl::Reference< RecursiveTest >(
591*cdf0e10cSrcweir         new SimpleRecursiveTest(*this, 100, &destroyed))->test();
592*cdf0e10cSrcweir     CPPUNIT_ASSERT(destroyed);
593*cdf0e10cSrcweir }
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir void Test::testCrossThreads() {
596*cdf0e10cSrcweir     bool destroyed = false;
597*cdf0e10cSrcweir     rtl::Reference< RecursiveTest >(
598*cdf0e10cSrcweir         new SimpleRecursiveTest(*this, 10, &destroyed))->test();
599*cdf0e10cSrcweir     CPPUNIT_ASSERT(destroyed);
600*cdf0e10cSrcweir }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir css::uno::Any Test::getKey(
603*cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & relative) const
604*cdf0e10cSrcweir {
605*cdf0e10cSrcweir     css::uno::Reference< css::container::XHierarchicalNameAccess > access(
606*cdf0e10cSrcweir         createViewAccess(path), css::uno::UNO_QUERY_THROW);
607*cdf0e10cSrcweir     css::uno::Any value(access->getByHierarchicalName(relative));
608*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
609*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
610*cdf0e10cSrcweir     return value;
611*cdf0e10cSrcweir }
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir void Test::setKey(
614*cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & name,
615*cdf0e10cSrcweir     css::uno::Any const & value) const
616*cdf0e10cSrcweir {
617*cdf0e10cSrcweir     css::uno::Reference< css::container::XNameReplace > access(
618*cdf0e10cSrcweir         createUpdateAccess(path), css::uno::UNO_QUERY_THROW);
619*cdf0e10cSrcweir     access->replaceByName(name, value);
620*cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch >(
621*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->commitChanges();
622*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
623*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
624*cdf0e10cSrcweir }
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir bool Test::resetKey(rtl::OUString const & path, rtl::OUString const & name)
627*cdf0e10cSrcweir     const
628*cdf0e10cSrcweir {
629*cdf0e10cSrcweir     //TODO: support setPropertyToDefault
630*cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch > access(
631*cdf0e10cSrcweir         createUpdateAccess(path), css::uno::UNO_QUERY_THROW);
632*cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertyState > state(
633*cdf0e10cSrcweir         access, css::uno::UNO_QUERY);
634*cdf0e10cSrcweir     if (!state.is()) {
635*cdf0e10cSrcweir         return false;
636*cdf0e10cSrcweir     }
637*cdf0e10cSrcweir     state->setPropertyToDefault(name);
638*cdf0e10cSrcweir     access->commitChanges();
639*cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
640*cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
641*cdf0e10cSrcweir     return true;
642*cdf0e10cSrcweir }
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > Test::createViewAccess(
645*cdf0e10cSrcweir     rtl::OUString const & path) const
646*cdf0e10cSrcweir {
647*cdf0e10cSrcweir     css::uno::Any arg(
648*cdf0e10cSrcweir         css::uno::makeAny(
649*cdf0e10cSrcweir             css::beans::NamedValue(
650*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
651*cdf0e10cSrcweir                 css::uno::makeAny(path))));
652*cdf0e10cSrcweir     return provider_->createInstanceWithArguments(
653*cdf0e10cSrcweir         rtl::OUString(
654*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
655*cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationAccess")),
656*cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any >(&arg, 1));
657*cdf0e10cSrcweir }
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > Test::createUpdateAccess(
660*cdf0e10cSrcweir     rtl::OUString const & path) const
661*cdf0e10cSrcweir {
662*cdf0e10cSrcweir     css::uno::Any arg(
663*cdf0e10cSrcweir         css::uno::makeAny(
664*cdf0e10cSrcweir             css::beans::NamedValue(
665*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
666*cdf0e10cSrcweir                 css::uno::makeAny(path))));
667*cdf0e10cSrcweir     return provider_->createInstanceWithArguments(
668*cdf0e10cSrcweir         rtl::OUString(
669*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
670*cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationUpdateAccess")),
671*cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any >(&arg, 1));
672*cdf0e10cSrcweir }
673*cdf0e10cSrcweir 
674*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltest");
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir }
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir NOADDITIONAL;
679