Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / stoc / test / testproxyfac.cxx
blobebc8786963161a119d5423c38d49ace1b5e7d1a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/main.h>
22 #include <osl/diagnose.h>
23 #include <rtl/alloc.h>
24 #include <uno/environment.hxx>
25 #include <uno/lbnames.h>
26 #include <cppuhelper/servicefactory.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <com/sun/star/uno/XCurrentContext.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/registry/XSimpleRegistry.hpp>
33 #include <com/sun/star/registry/XImplementationRegistration.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/reflection/XProxyFactory.hpp>
37 #include <stdio.h>
40 using namespace ::osl;
41 using namespace ::cppu;
42 using namespace ::com::sun::star;
43 using namespace css::uno;
46 typedef WeakImplHelper< lang::XServiceInfo,
47 XCurrentContext,
48 reflection::XProxyFactory > t_impl;
51 class TargetObject : public t_impl
53 public:
54 static int s_obj;
56 virtual ~TargetObject() {
57 --s_obj;
58 SAL_INFO("stoc", "~TargetObject()" );
60 TargetObject()
61 { ++s_obj; }
63 Any SAL_CALL queryInterface( Type const & type )
64 throw (RuntimeException);
66 // XServiceInfo
67 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
68 { return OUString("target"); }
69 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
70 throw (RuntimeException)
71 { return sal_False; }
72 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
73 throw (RuntimeException)
74 { return Sequence< OUString >(); }
75 // XProxyFactory
76 virtual Reference< XAggregation > SAL_CALL createProxy(
77 const Reference< XInterface > & xTarget ) throw (RuntimeException)
78 { return Reference< XAggregation >( xTarget, UNO_QUERY ); }
79 // XCurrentContext
80 virtual Any SAL_CALL getValueByName( OUString const & name )
81 throw (RuntimeException)
82 { return makeAny( name ); }
86 Any TargetObject::queryInterface( Type const & type )
87 throw (RuntimeException)
89 Any ret( t_impl::queryInterface( type ) );
90 if (ret.hasValue())
91 return ret;
92 throw lang::DisposedException(
93 OUString( "my test exception" ),
94 static_cast< OWeakObject * >(this) );
97 int TargetObject::s_obj = 0;
100 class TestMaster : public WeakImplHelper< lang::XServiceInfo >
102 Reference< XAggregation > m_xProxyTarget;
103 Reference<lang::XServiceInfo> m_xOtherProxyTargetBeforeSetDelegator;
105 inline TestMaster() { ++s_obj; }
106 public:
107 static int s_obj;
108 static Reference< XInterface > create(
109 Reference< reflection::XProxyFactory > const & xProxyFac );
110 static Reference< XInterface > create(
111 Reference< XInterface > const & xTarget,
112 Reference< reflection::XProxyFactory > const & xProxyFac );
114 virtual ~TestMaster() {
115 --s_obj;
116 SAL_INFO("stoc", "~TestMaster()" );
119 virtual Any SAL_CALL queryInterface( const Type & rType )
120 throw (RuntimeException)
122 Any aRet(
123 WeakImplHelper< lang::XServiceInfo >::queryInterface( rType ) );
124 if (aRet.hasValue())
125 return aRet;
126 return m_xProxyTarget->queryAggregation( rType );
129 // XServiceInfo
130 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
131 { return OUString("master"); }
132 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
133 throw (RuntimeException)
134 { return sal_False; }
135 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
136 throw (RuntimeException)
137 { return Sequence< OUString >(); }
140 int TestMaster::s_obj = 0;
143 Reference< XInterface > TestMaster::create(
144 Reference< XInterface > const & xTarget,
145 Reference< reflection::XProxyFactory > const & xProxyFac )
147 TestMaster * that = new TestMaster;
148 Reference< XInterface > xRet( static_cast< OWeakObject * >( that ) );
150 Reference< XAggregation > xAgg( xProxyFac->createProxy( xTarget ) );
151 // ownership take over
152 that->m_xProxyTarget.set( xAgg, UNO_QUERY_THROW );
153 that->m_xOtherProxyTargetBeforeSetDelegator.set(
154 that->m_xProxyTarget, UNO_QUERY );
156 that->m_xProxyTarget->setDelegator( xRet );
157 return xRet;
160 Reference< XInterface > TestMaster::create(
161 Reference< reflection::XProxyFactory > const & xProxyFac )
163 return create(
164 static_cast< OWeakObject * >( new TargetObject ), xProxyFac );
168 static void test_proxyfac_(
169 Reference< XInterface > const & xMaster, OUString const & test,
170 Reference< reflection::XProxyFactory > const & /*xProxyFac*/ )
172 (void)test;
173 Reference< lang::XServiceInfo > xMaster_XServiceInfo(
174 xMaster, UNO_QUERY_THROW );
175 OSL_ASSERT( xMaster_XServiceInfo->getImplementationName().equals( test ) );
177 Reference< reflection::XProxyFactory > xTarget( xMaster, UNO_QUERY_THROW );
178 Reference< XCurrentContext > xTarget_XCurrentContext(
179 xTarget, UNO_QUERY_THROW );
180 Reference< XCurrentContext > xMaster_XCurrentContext(
181 xMaster, UNO_QUERY_THROW );
183 OSL_ASSERT(
184 xTarget_XCurrentContext->getValueByName( test ) == makeAny( test ) );
185 OSL_ASSERT(
186 xMaster_XCurrentContext->getValueByName( test ) == makeAny( test ) );
188 Reference< XAggregation > xFakeAgg( xTarget->createProxy( xTarget ) );
189 if (xFakeAgg.is())
191 OSL_ASSERT( xTarget == xFakeAgg );
192 OSL_ASSERT( xMaster == xFakeAgg );
195 Reference< lang::XServiceInfo > xTarget_XServiceInfo(
196 xTarget, UNO_QUERY_THROW );
197 OSL_ASSERT( xTarget_XServiceInfo->getImplementationName().equals( test ) );
198 Reference< lang::XServiceInfo > xTarget_XServiceInfo2(
199 xTarget, UNO_QUERY_THROW );
200 OSL_ASSERT( xTarget_XServiceInfo2.get() == xTarget_XServiceInfo.get() );
202 OSL_ASSERT( xTarget == xTarget_XCurrentContext );
203 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
204 OSL_ASSERT(
205 xTarget_XCurrentContext.get() == xMaster_XCurrentContext.get() );
206 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
207 OSL_ASSERT( xTarget == xMaster );
208 OSL_ASSERT( xTarget_XServiceInfo.get() == xMaster_XServiceInfo.get() );
209 OSL_ASSERT( xTarget_XServiceInfo == xMaster );
210 OSL_ASSERT( xMaster_XServiceInfo == xMaster );
214 Reference< registry::XRegistryKey >(
215 xMaster, UNO_QUERY_THROW );
217 catch (const lang::DisposedException & exc)
219 if ( exc.Message != "my test exception" )
220 throw;
224 static void test_proxyfac(
225 Reference< XInterface > const & xMaster, OUString const & test,
226 Reference< reflection::XProxyFactory > const & xProxyFac )
228 test_proxyfac_( xMaster, test, xProxyFac );
229 // proxy the proxy...
230 Reference< XInterface > xNew( TestMaster::create( xMaster, xProxyFac ) );
231 test_proxyfac_(
232 xNew, OUString( "master" ), xProxyFac );
235 SAL_IMPLEMENT_MAIN()
237 bool success = true;
239 Environment cpp_env;
240 OUString cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
241 uno_getEnvironment(
242 reinterpret_cast< uno_Environment ** >( &cpp_env ),
243 cpp.pData, 0 );
244 OSL_ENSURE( cpp_env.is(), "### cannot get C++ uno env!" );
247 Reference< lang::XMultiServiceFactory > xMgr(
248 createRegistryServiceFactory(
249 OUString( "stoctest.rdb" ) ) );
253 Reference< registry::XImplementationRegistration > xImplReg(
254 xMgr->createInstance( "com.sun.star.registry.ImplementationRegistration" ),
255 UNO_QUERY );
256 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
258 OUString aLibName(
259 "proxyfac.uno" SAL_DLLEXTENSION );
260 xImplReg->registerImplementation(
261 OUString( "com.sun.star.loader.SharedLibrary" ),
262 aLibName, Reference< registry::XSimpleRegistry >() );
264 Reference< reflection::XProxyFactory > xProxyFac(
265 xMgr->createInstance("com.sun.star.reflection.ProxyFactory"),
266 UNO_QUERY_THROW );
268 Reference< XAggregation > x(
269 xProxyFac->createProxy(
270 static_cast< OWeakObject * >( new TargetObject ) ) );
271 // no call
274 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
275 test_proxyfac(
276 xMaster,
277 OUString( "master" ),
278 xProxyFac );
281 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
282 // no call
286 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
287 Reference< reflection::XProxyFactory > xSlave_lives_alone(
288 xMaster, UNO_QUERY_THROW );
289 xMaster.clear();
290 test_proxyfac(
291 xSlave_lives_alone,
292 OUString( "master" ),
293 xProxyFac );
294 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
297 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
298 Reference< reflection::XProxyFactory > xSlave_lives_alone(
299 xMaster, UNO_QUERY_THROW );
300 // no call
303 test_proxyfac(
304 xProxyFac->createProxy(
305 static_cast< OWeakObject * >( new TargetObject ) ),
306 OUString( "target" ),
307 xProxyFac );
308 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
310 catch (const Exception & rExc)
312 (void)rExc;
313 OSL_ENSURE(
314 ! __FILE__,
315 OUStringToOString(
316 rExc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
317 success = false;
321 Reference< lang::XComponent > xComp;
322 Reference< beans::XPropertySet >(
323 xMgr, UNO_QUERY_THROW )->getPropertyValue(
324 OUString( "DefaultContext" ) )
325 >>= xComp;
326 xComp->dispose();
329 if (TestMaster::s_obj != 0)
330 fprintf( stderr, "TestMaster objects: %d\n", TestMaster::s_obj );
331 if (TargetObject::s_obj != 0)
332 fprintf( stderr, "TargetObject objects: %d\n", TargetObject::s_obj );
334 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
335 void ** ppInterfaces;
336 sal_Int32 len;
337 uno_ExtEnvironment * env = cpp_env.get()->pExtEnv;
338 (*env->getRegisteredInterfaces)(
339 env, &ppInterfaces, &len, rtl_allocateMemory );
340 if (len != 0)
341 fprintf( stderr, "%d registered C++ interfaces left!\n", len );
343 success &= (TestMaster::s_obj == 0 &&
344 TargetObject::s_obj == 0 &&
345 len == 0);
346 if (success)
348 printf( "testproxyfac succeeded.\n" );
349 return 0;
351 else
353 fprintf( stderr, "testproxyfac failed!\n" );
354 return 1;
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */