update dev300-m58
[ooovba.git] / stoc / test / testproxyfac.cxx
blob93874e3fb3cc137f91f13c93e36506675ce42013
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testproxyfac.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
34 #include "sal/main.h"
35 #include "osl/diagnose.h"
36 #include "rtl/alloc.h"
37 #include "uno/environment.hxx"
38 #include "cppuhelper/servicefactory.hxx"
39 #include "cppuhelper/implbase1.hxx"
40 #include "cppuhelper/implbase3.hxx"
41 #include "com/sun/star/uno/XCurrentContext.hpp"
42 #include "com/sun/star/lang/DisposedException.hpp"
43 #include "com/sun/star/lang/XComponent.hpp"
44 #include "com/sun/star/lang/XServiceInfo.hpp"
45 #include "com/sun/star/registry/XSimpleRegistry.hpp"
46 #include "com/sun/star/registry/XImplementationRegistration.hpp"
47 #include "com/sun/star/beans/XPropertySet.hpp"
48 #include "com/sun/star/reflection/XProxyFactory.hpp"
50 #include <stdio.h>
53 using namespace ::rtl;
54 using namespace ::osl;
55 using namespace ::cppu;
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
60 typedef WeakImplHelper3< lang::XServiceInfo,
61 XCurrentContext,
62 reflection::XProxyFactory > t_impl;
64 //==============================================================================
65 class TargetObject : public t_impl
67 public:
68 static int s_obj;
70 virtual ~TargetObject() {
71 --s_obj;
72 OSL_TRACE( "~TargetObject()" );
74 TargetObject()
75 { ++s_obj; }
77 Any SAL_CALL queryInterface( Type const & type )
78 throw (RuntimeException);
80 // XServiceInfo
81 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
82 { return OUString::createFromAscii( "target" ); }
83 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
84 throw (RuntimeException)
85 { return sal_False; }
86 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
87 throw (RuntimeException)
88 { return Sequence< OUString >(); }
89 // XProxyFactory
90 virtual Reference< XAggregation > SAL_CALL createProxy(
91 const Reference< XInterface > & xTarget ) throw (RuntimeException)
92 { return Reference< XAggregation >( xTarget, UNO_QUERY ); }
93 // XCurrentContext
94 virtual Any SAL_CALL getValueByName( OUString const & name )
95 throw (RuntimeException)
96 { return makeAny( name ); }
99 //______________________________________________________________________________
100 Any TargetObject::queryInterface( Type const & type )
101 throw (RuntimeException)
103 Any ret( t_impl::queryInterface( type ) );
104 if (ret.hasValue())
105 return ret;
106 throw lang::DisposedException(
107 OUString( RTL_CONSTASCII_USTRINGPARAM("my test exception") ),
108 static_cast< OWeakObject * >(this) );
111 int TargetObject::s_obj = 0;
114 //==============================================================================
115 class TestMaster : public WeakImplHelper1< lang::XServiceInfo >
117 Reference< XAggregation > m_xProxyTarget;
118 Reference<lang::XServiceInfo> m_xOtherProxyTargetBeforeSetDelegator;
120 inline TestMaster() { ++s_obj; }
121 public:
122 static int s_obj;
123 static Reference< XInterface > create(
124 Reference< reflection::XProxyFactory > const & xProxyFac );
125 static Reference< XInterface > create(
126 Reference< XInterface > const & xTarget,
127 Reference< reflection::XProxyFactory > const & xProxyFac );
129 virtual ~TestMaster() {
130 --s_obj;
131 OSL_TRACE( "~TestMaster()" );
134 virtual Any SAL_CALL queryInterface( const Type & rType )
135 throw (RuntimeException)
137 Any aRet(
138 WeakImplHelper1< lang::XServiceInfo >::queryInterface( rType ) );
139 if (aRet.hasValue())
140 return aRet;
141 return m_xProxyTarget->queryAggregation( rType );
144 // XServiceInfo
145 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
146 { return OUString::createFromAscii( "master" ); }
147 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
148 throw (RuntimeException)
149 { return sal_False; }
150 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
151 throw (RuntimeException)
152 { return Sequence< OUString >(); }
155 int TestMaster::s_obj = 0;
158 Reference< XInterface > TestMaster::create(
159 Reference< XInterface > const & xTarget,
160 Reference< reflection::XProxyFactory > const & xProxyFac )
162 TestMaster * that = new TestMaster;
163 Reference< XInterface > xRet( static_cast< OWeakObject * >( that ) );
165 Reference< XAggregation > xAgg( xProxyFac->createProxy( xTarget ) );
166 // ownership take over
167 that->m_xProxyTarget.set( xAgg, UNO_QUERY_THROW );
168 that->m_xOtherProxyTargetBeforeSetDelegator.set(
169 that->m_xProxyTarget, UNO_QUERY );
171 that->m_xProxyTarget->setDelegator( xRet );
172 return xRet;
175 Reference< XInterface > TestMaster::create(
176 Reference< reflection::XProxyFactory > const & xProxyFac )
178 return create(
179 static_cast< OWeakObject * >( new TargetObject ), xProxyFac );
183 static void test_proxyfac_(
184 Reference< XInterface > const & xMaster, OUString const & test,
185 Reference< reflection::XProxyFactory > const & /*xProxyFac*/ )
187 (void)test;
188 Reference< lang::XServiceInfo > xMaster_XServiceInfo(
189 xMaster, UNO_QUERY_THROW );
190 OSL_ASSERT( xMaster_XServiceInfo->getImplementationName().equals( test ) );
192 Reference< reflection::XProxyFactory > xTarget( xMaster, UNO_QUERY_THROW );
193 Reference< XCurrentContext > xTarget_XCurrentContext(
194 xTarget, UNO_QUERY_THROW );
195 Reference< XCurrentContext > xMaster_XCurrentContext(
196 xMaster, UNO_QUERY_THROW );
198 OSL_ASSERT(
199 xTarget_XCurrentContext->getValueByName( test ) == makeAny( test ) );
200 OSL_ASSERT(
201 xMaster_XCurrentContext->getValueByName( test ) == makeAny( test ) );
203 Reference< XAggregation > xFakeAgg( xTarget->createProxy( xTarget ) );
204 if (xFakeAgg.is())
206 OSL_ASSERT( xTarget == xFakeAgg );
207 OSL_ASSERT( xMaster == xFakeAgg );
210 Reference< lang::XServiceInfo > xTarget_XServiceInfo(
211 xTarget, UNO_QUERY_THROW );
212 OSL_ASSERT( xTarget_XServiceInfo->getImplementationName().equals( test ) );
213 Reference< lang::XServiceInfo > xTarget_XServiceInfo2(
214 xTarget, UNO_QUERY_THROW );
215 OSL_ASSERT( xTarget_XServiceInfo2.get() == xTarget_XServiceInfo.get() );
217 OSL_ASSERT( xTarget == xTarget_XCurrentContext );
218 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
219 OSL_ASSERT(
220 xTarget_XCurrentContext.get() == xMaster_XCurrentContext.get() );
221 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
222 OSL_ASSERT( xTarget == xMaster );
223 OSL_ASSERT( xTarget_XServiceInfo.get() == xMaster_XServiceInfo.get() );
224 OSL_ASSERT( xTarget_XServiceInfo == xMaster );
225 OSL_ASSERT( xMaster_XServiceInfo == xMaster );
229 Reference< registry::XRegistryKey >(
230 xMaster, UNO_QUERY_THROW );
232 catch (lang::DisposedException & exc)
234 if (! exc.Message.equalsAsciiL(
235 RTL_CONSTASCII_STRINGPARAM("my test exception") ))
236 throw;
240 static void test_proxyfac(
241 Reference< XInterface > const & xMaster, OUString const & test,
242 Reference< reflection::XProxyFactory > const & xProxyFac )
244 test_proxyfac_( xMaster, test, xProxyFac );
245 // proxy the proxy...
246 Reference< XInterface > xNew( TestMaster::create( xMaster, xProxyFac ) );
247 test_proxyfac_(
248 xNew, OUString( RTL_CONSTASCII_USTRINGPARAM("master") ), xProxyFac );
251 SAL_IMPLEMENT_MAIN()
253 bool success = true;
255 Environment cpp_env;
256 OUString cpp( RTL_CONSTASCII_USTRINGPARAM(
257 CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
258 uno_getEnvironment(
259 reinterpret_cast< uno_Environment ** >( &cpp_env ),
260 cpp.pData, 0 );
261 OSL_ENSURE( cpp_env.is(), "### cannot get C++ uno env!" );
264 Reference< lang::XMultiServiceFactory > xMgr(
265 createRegistryServiceFactory(
266 OUString( RTL_CONSTASCII_USTRINGPARAM("stoctest.rdb") ) ) );
270 Reference< registry::XImplementationRegistration > xImplReg(
271 xMgr->createInstance(
272 OUString(
273 RTL_CONSTASCII_USTRINGPARAM(
274 "com.sun.star.registry.ImplementationRegistration")
275 ) ),
276 UNO_QUERY );
277 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
279 OUString aLibName(
280 RTL_CONSTASCII_USTRINGPARAM("proxyfac.uno" SAL_DLLEXTENSION) );
281 xImplReg->registerImplementation(
282 OUString(
283 RTL_CONSTASCII_USTRINGPARAM(
284 "com.sun.star.loader.SharedLibrary") ),
285 aLibName, Reference< registry::XSimpleRegistry >() );
287 Reference< reflection::XProxyFactory > xProxyFac(
288 xMgr->createInstance(
289 OUString::createFromAscii(
290 "com.sun.star.reflection.ProxyFactory") ),
291 UNO_QUERY_THROW );
293 Reference< XAggregation > x(
294 xProxyFac->createProxy(
295 static_cast< OWeakObject * >( new TargetObject ) ) );
296 // no call
299 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
300 test_proxyfac(
301 xMaster,
302 OUString( RTL_CONSTASCII_USTRINGPARAM("master") ),
303 xProxyFac );
306 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
307 // no call
311 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
312 Reference< reflection::XProxyFactory > xSlave_lives_alone(
313 xMaster, UNO_QUERY_THROW );
314 xMaster.clear();
315 test_proxyfac(
316 xSlave_lives_alone,
317 OUString( RTL_CONSTASCII_USTRINGPARAM("master") ),
318 xProxyFac );
319 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
322 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
323 Reference< reflection::XProxyFactory > xSlave_lives_alone(
324 xMaster, UNO_QUERY_THROW );
325 // no call
328 test_proxyfac(
329 xProxyFac->createProxy(
330 static_cast< OWeakObject * >( new TargetObject ) ),
331 OUString( RTL_CONSTASCII_USTRINGPARAM("target") ),
332 xProxyFac );
333 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
335 catch (Exception & rExc)
337 (void)rExc;
338 OSL_ENSURE(
339 ! __FILE__,
340 OUStringToOString(
341 rExc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
342 success = false;
346 Reference< lang::XComponent > xComp;
347 Reference< beans::XPropertySet >(
348 xMgr, UNO_QUERY_THROW )->getPropertyValue(
349 OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) )
350 >>= xComp;
351 xComp->dispose();
354 if (TestMaster::s_obj != 0)
355 fprintf( stderr, "TestMaster objects: %d\n", TestMaster::s_obj );
356 if (TargetObject::s_obj != 0)
357 fprintf( stderr, "TargetObject objects: %d\n", TargetObject::s_obj );
359 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
360 void ** ppInterfaces;
361 sal_Int32 len;
362 uno_ExtEnvironment * env = cpp_env.get()->pExtEnv;
363 (*env->getRegisteredInterfaces)(
364 env, &ppInterfaces, &len, rtl_allocateMemory );
365 if (len != 0)
366 fprintf( stderr, "%d registered C++ interfaces left!\n", len );
368 success &= (TestMaster::s_obj == 0 &&
369 TargetObject::s_obj == 0 &&
370 len == 0);
371 if (success)
373 printf( "testproxyfac succeeded.\n" );
374 return 0;
376 else
378 fprintf( stderr, "testproxyfac failed!\n" );
379 return 1;