Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / stoc / test / testproxyfac.cxx
blob769e93ee8c12555f2c502694585f55aeb0690d5c
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 "cppuhelper/servicefactory.hxx"
26 #include "cppuhelper/implbase1.hxx"
27 #include "cppuhelper/implbase3.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 ::rtl;
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
47 typedef WeakImplHelper3< lang::XServiceInfo,
48 XCurrentContext,
49 reflection::XProxyFactory > t_impl;
51 //==============================================================================
52 class TargetObject : public t_impl
54 public:
55 static int s_obj;
57 virtual ~TargetObject() {
58 --s_obj;
59 OSL_TRACE( "~TargetObject()" );
61 TargetObject()
62 { ++s_obj; }
64 Any SAL_CALL queryInterface( Type const & type )
65 throw (RuntimeException);
67 // XServiceInfo
68 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
69 { return OUString("target"); }
70 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
71 throw (RuntimeException)
72 { return sal_False; }
73 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
74 throw (RuntimeException)
75 { return Sequence< OUString >(); }
76 // XProxyFactory
77 virtual Reference< XAggregation > SAL_CALL createProxy(
78 const Reference< XInterface > & xTarget ) throw (RuntimeException)
79 { return Reference< XAggregation >( xTarget, UNO_QUERY ); }
80 // XCurrentContext
81 virtual Any SAL_CALL getValueByName( OUString const & name )
82 throw (RuntimeException)
83 { return makeAny( name ); }
86 //______________________________________________________________________________
87 Any TargetObject::queryInterface( Type const & type )
88 throw (RuntimeException)
90 Any ret( t_impl::queryInterface( type ) );
91 if (ret.hasValue())
92 return ret;
93 throw lang::DisposedException(
94 OUString( "my test exception" ),
95 static_cast< OWeakObject * >(this) );
98 int TargetObject::s_obj = 0;
101 //==============================================================================
102 class TestMaster : public WeakImplHelper1< lang::XServiceInfo >
104 Reference< XAggregation > m_xProxyTarget;
105 Reference<lang::XServiceInfo> m_xOtherProxyTargetBeforeSetDelegator;
107 inline TestMaster() { ++s_obj; }
108 public:
109 static int s_obj;
110 static Reference< XInterface > create(
111 Reference< reflection::XProxyFactory > const & xProxyFac );
112 static Reference< XInterface > create(
113 Reference< XInterface > const & xTarget,
114 Reference< reflection::XProxyFactory > const & xProxyFac );
116 virtual ~TestMaster() {
117 --s_obj;
118 OSL_TRACE( "~TestMaster()" );
121 virtual Any SAL_CALL queryInterface( const Type & rType )
122 throw (RuntimeException)
124 Any aRet(
125 WeakImplHelper1< lang::XServiceInfo >::queryInterface( rType ) );
126 if (aRet.hasValue())
127 return aRet;
128 return m_xProxyTarget->queryAggregation( rType );
131 // XServiceInfo
132 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
133 { return OUString("master"); }
134 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
135 throw (RuntimeException)
136 { return sal_False; }
137 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
138 throw (RuntimeException)
139 { return Sequence< OUString >(); }
142 int TestMaster::s_obj = 0;
145 Reference< XInterface > TestMaster::create(
146 Reference< XInterface > const & xTarget,
147 Reference< reflection::XProxyFactory > const & xProxyFac )
149 TestMaster * that = new TestMaster;
150 Reference< XInterface > xRet( static_cast< OWeakObject * >( that ) );
152 Reference< XAggregation > xAgg( xProxyFac->createProxy( xTarget ) );
153 // ownership take over
154 that->m_xProxyTarget.set( xAgg, UNO_QUERY_THROW );
155 that->m_xOtherProxyTargetBeforeSetDelegator.set(
156 that->m_xProxyTarget, UNO_QUERY );
158 that->m_xProxyTarget->setDelegator( xRet );
159 return xRet;
162 Reference< XInterface > TestMaster::create(
163 Reference< reflection::XProxyFactory > const & xProxyFac )
165 return create(
166 static_cast< OWeakObject * >( new TargetObject ), xProxyFac );
170 static void test_proxyfac_(
171 Reference< XInterface > const & xMaster, OUString const & test,
172 Reference< reflection::XProxyFactory > const & /*xProxyFac*/ )
174 (void)test;
175 Reference< lang::XServiceInfo > xMaster_XServiceInfo(
176 xMaster, UNO_QUERY_THROW );
177 OSL_ASSERT( xMaster_XServiceInfo->getImplementationName().equals( test ) );
179 Reference< reflection::XProxyFactory > xTarget( xMaster, UNO_QUERY_THROW );
180 Reference< XCurrentContext > xTarget_XCurrentContext(
181 xTarget, UNO_QUERY_THROW );
182 Reference< XCurrentContext > xMaster_XCurrentContext(
183 xMaster, UNO_QUERY_THROW );
185 OSL_ASSERT(
186 xTarget_XCurrentContext->getValueByName( test ) == makeAny( test ) );
187 OSL_ASSERT(
188 xMaster_XCurrentContext->getValueByName( test ) == makeAny( test ) );
190 Reference< XAggregation > xFakeAgg( xTarget->createProxy( xTarget ) );
191 if (xFakeAgg.is())
193 OSL_ASSERT( xTarget == xFakeAgg );
194 OSL_ASSERT( xMaster == xFakeAgg );
197 Reference< lang::XServiceInfo > xTarget_XServiceInfo(
198 xTarget, UNO_QUERY_THROW );
199 OSL_ASSERT( xTarget_XServiceInfo->getImplementationName().equals( test ) );
200 Reference< lang::XServiceInfo > xTarget_XServiceInfo2(
201 xTarget, UNO_QUERY_THROW );
202 OSL_ASSERT( xTarget_XServiceInfo2.get() == xTarget_XServiceInfo.get() );
204 OSL_ASSERT( xTarget == xTarget_XCurrentContext );
205 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
206 OSL_ASSERT(
207 xTarget_XCurrentContext.get() == xMaster_XCurrentContext.get() );
208 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
209 OSL_ASSERT( xTarget == xMaster );
210 OSL_ASSERT( xTarget_XServiceInfo.get() == xMaster_XServiceInfo.get() );
211 OSL_ASSERT( xTarget_XServiceInfo == xMaster );
212 OSL_ASSERT( xMaster_XServiceInfo == xMaster );
216 Reference< registry::XRegistryKey >(
217 xMaster, UNO_QUERY_THROW );
219 catch (const lang::DisposedException & exc)
221 if ( exc.Message != "my test exception" )
222 throw;
226 static void test_proxyfac(
227 Reference< XInterface > const & xMaster, OUString const & test,
228 Reference< reflection::XProxyFactory > const & xProxyFac )
230 test_proxyfac_( xMaster, test, xProxyFac );
231 // proxy the proxy...
232 Reference< XInterface > xNew( TestMaster::create( xMaster, xProxyFac ) );
233 test_proxyfac_(
234 xNew, OUString( "master" ), xProxyFac );
237 SAL_IMPLEMENT_MAIN()
239 bool success = true;
241 Environment cpp_env;
242 OUString cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
243 uno_getEnvironment(
244 reinterpret_cast< uno_Environment ** >( &cpp_env ),
245 cpp.pData, 0 );
246 OSL_ENSURE( cpp_env.is(), "### cannot get C++ uno env!" );
249 Reference< lang::XMultiServiceFactory > xMgr(
250 createRegistryServiceFactory(
251 OUString( "stoctest.rdb" ) ) );
255 Reference< registry::XImplementationRegistration > xImplReg(
256 xMgr->createInstance(
257 OUString( "com.sun.star.registry.ImplementationRegistration"
258 ) ),
259 UNO_QUERY );
260 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
262 OUString aLibName(
263 "proxyfac.uno" SAL_DLLEXTENSION );
264 xImplReg->registerImplementation(
265 OUString( "com.sun.star.loader.SharedLibrary" ),
266 aLibName, Reference< registry::XSimpleRegistry >() );
268 Reference< reflection::XProxyFactory > xProxyFac(
269 xMgr->createInstance(
270 OUString(
271 "com.sun.star.reflection.ProxyFactory") ),
272 UNO_QUERY_THROW );
274 Reference< XAggregation > x(
275 xProxyFac->createProxy(
276 static_cast< OWeakObject * >( new TargetObject ) ) );
277 // no call
280 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
281 test_proxyfac(
282 xMaster,
283 OUString( "master" ),
284 xProxyFac );
287 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
288 // no call
292 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
293 Reference< reflection::XProxyFactory > xSlave_lives_alone(
294 xMaster, UNO_QUERY_THROW );
295 xMaster.clear();
296 test_proxyfac(
297 xSlave_lives_alone,
298 OUString( "master" ),
299 xProxyFac );
300 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
303 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
304 Reference< reflection::XProxyFactory > xSlave_lives_alone(
305 xMaster, UNO_QUERY_THROW );
306 // no call
309 test_proxyfac(
310 xProxyFac->createProxy(
311 static_cast< OWeakObject * >( new TargetObject ) ),
312 OUString( "target" ),
313 xProxyFac );
314 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
316 catch (const Exception & rExc)
318 (void)rExc;
319 OSL_ENSURE(
320 ! __FILE__,
321 OUStringToOString(
322 rExc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
323 success = false;
327 Reference< lang::XComponent > xComp;
328 Reference< beans::XPropertySet >(
329 xMgr, UNO_QUERY_THROW )->getPropertyValue(
330 OUString( "DefaultContext" ) )
331 >>= xComp;
332 xComp->dispose();
335 if (TestMaster::s_obj != 0)
336 fprintf( stderr, "TestMaster objects: %d\n", TestMaster::s_obj );
337 if (TargetObject::s_obj != 0)
338 fprintf( stderr, "TargetObject objects: %d\n", TargetObject::s_obj );
340 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
341 void ** ppInterfaces;
342 sal_Int32 len;
343 uno_ExtEnvironment * env = cpp_env.get()->pExtEnv;
344 (*env->getRegisteredInterfaces)(
345 env, &ppInterfaces, &len, rtl_allocateMemory );
346 if (len != 0)
347 fprintf( stderr, "%d registered C++ interfaces left!\n", len );
349 success &= (TestMaster::s_obj == 0 &&
350 TargetObject::s_obj == 0 &&
351 len == 0);
352 if (success)
354 printf( "testproxyfac succeeded.\n" );
355 return 0;
357 else
359 fprintf( stderr, "testproxyfac failed!\n" );
360 return 1;
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */