crashtesting: assert on reimport of docx export of ooo102874-2.doc
[LibreOffice.git] / stoc / test / testproxyfac.cxx
blobbefc67ae8ee31a413d5c04f93ad9ec7f57f52465
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 <sal/log.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/alloc.h>
25 #include <uno/environment.hxx>
26 #include <uno/lbnames.h>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <com/sun/star/uno/XCurrentContext.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 #include <com/sun/star/registry/XImplementationRegistration.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/reflection/XProxyFactory.hpp>
38 #include <stdio.h>
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star;
44 using namespace css::uno;
47 typedef WeakImplHelper< lang::XServiceInfo,
48 XCurrentContext,
49 reflection::XProxyFactory > t_impl;
52 class TargetObject : public t_impl
54 public:
55 static int s_obj;
57 virtual ~TargetObject() {
58 --s_obj;
59 SAL_INFO("stoc", "~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 ); }
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 getXWeak() );
98 int TargetObject::s_obj = 0;
101 class TestMaster : public WeakImplHelper< lang::XServiceInfo >
103 Reference< XAggregation > m_xProxyTarget;
104 Reference<lang::XServiceInfo> m_xOtherProxyTargetBeforeSetDelegator;
106 inline TestMaster() { ++s_obj; }
107 public:
108 static int s_obj;
109 static Reference< XInterface > create(
110 Reference< reflection::XProxyFactory > const & xProxyFac );
111 static Reference< XInterface > create(
112 Reference< XInterface > const & xTarget,
113 Reference< reflection::XProxyFactory > const & xProxyFac );
115 virtual ~TestMaster() {
116 --s_obj;
117 SAL_INFO("stoc", "~TestMaster()" );
120 virtual Any SAL_CALL queryInterface( const Type & rType )
121 throw (RuntimeException)
123 Any aRet(
124 WeakImplHelper< lang::XServiceInfo >::queryInterface( rType ) );
125 if (aRet.hasValue())
126 return aRet;
127 return m_xProxyTarget->queryAggregation( rType );
130 // XServiceInfo
131 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
132 { return OUString("master"); }
133 virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
134 throw (RuntimeException)
135 { return sal_False; }
136 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
137 throw (RuntimeException)
138 { return Sequence< OUString >(); }
141 int TestMaster::s_obj = 0;
144 Reference< XInterface > TestMaster::create(
145 Reference< XInterface > const & xTarget,
146 Reference< reflection::XProxyFactory > const & xProxyFac )
148 TestMaster * that = new TestMaster;
149 Reference< XInterface > xRet( getXWeak( that ) );
151 Reference< XAggregation > xAgg( xProxyFac->createProxy( xTarget ) );
152 // ownership take over
153 that->m_xProxyTarget.set( xAgg, UNO_QUERY_THROW );
154 that->m_xOtherProxyTargetBeforeSetDelegator.set(
155 that->m_xProxyTarget, UNO_QUERY );
157 that->m_xProxyTarget->setDelegator( xRet );
158 return xRet;
161 Reference< XInterface > TestMaster::create(
162 Reference< reflection::XProxyFactory > const & xProxyFac )
164 return create(
165 getXWeak( new TargetObject ), xProxyFac );
169 static void test_proxyfac_(
170 Reference< XInterface > const & xMaster, OUString const & test,
171 Reference< reflection::XProxyFactory > const & /*xProxyFac*/ )
173 (void)test;
174 Reference< lang::XServiceInfo > xMaster_XServiceInfo(
175 xMaster, UNO_QUERY_THROW );
176 OSL_ASSERT( xMaster_XServiceInfo->getImplementationName().equals( test ) );
178 Reference< reflection::XProxyFactory > xTarget( xMaster, UNO_QUERY_THROW );
179 Reference< XCurrentContext > xTarget_XCurrentContext(
180 xTarget, UNO_QUERY_THROW );
181 Reference< XCurrentContext > xMaster_XCurrentContext(
182 xMaster, UNO_QUERY_THROW );
184 OSL_ASSERT(
185 xTarget_XCurrentContext->getValueByName( test ) == makeAny( test ) );
186 OSL_ASSERT(
187 xMaster_XCurrentContext->getValueByName( test ) == makeAny( test ) );
189 Reference< XAggregation > xFakeAgg( xTarget->createProxy( xTarget ) );
190 if (xFakeAgg.is())
192 OSL_ASSERT( xTarget == xFakeAgg );
193 OSL_ASSERT( xMaster == xFakeAgg );
196 Reference< lang::XServiceInfo > xTarget_XServiceInfo(
197 xTarget, UNO_QUERY_THROW );
198 OSL_ASSERT( xTarget_XServiceInfo->getImplementationName().equals( test ) );
199 Reference< lang::XServiceInfo > xTarget_XServiceInfo2(
200 xTarget, UNO_QUERY_THROW );
201 OSL_ASSERT( xTarget_XServiceInfo2.get() == xTarget_XServiceInfo.get() );
203 OSL_ASSERT( xTarget == xTarget_XCurrentContext );
204 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
205 OSL_ASSERT(
206 xTarget_XCurrentContext.get() == xMaster_XCurrentContext.get() );
207 OSL_ASSERT( xTarget_XCurrentContext == xMaster );
208 OSL_ASSERT( xTarget == xMaster );
209 OSL_ASSERT( xTarget_XServiceInfo.get() == xMaster_XServiceInfo.get() );
210 OSL_ASSERT( xTarget_XServiceInfo == xMaster );
211 OSL_ASSERT( xMaster_XServiceInfo == xMaster );
215 Reference< registry::XRegistryKey >(
216 xMaster, UNO_QUERY_THROW );
218 catch (const lang::DisposedException & exc)
220 if ( exc.Message != "my test exception" )
221 throw;
225 static void test_proxyfac(
226 Reference< XInterface > const & xMaster, OUString const & test,
227 Reference< reflection::XProxyFactory > const & xProxyFac )
229 test_proxyfac_( xMaster, test, xProxyFac );
230 // proxy the proxy...
231 Reference< XInterface > xNew( TestMaster::create( xMaster, xProxyFac ) );
232 test_proxyfac_(
233 xNew, OUString( "master" ), xProxyFac );
236 SAL_IMPLEMENT_MAIN()
238 bool success = true;
240 Environment cpp_env;
241 OUString cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
242 uno_getEnvironment(
243 reinterpret_cast< uno_Environment ** >( &cpp_env ),
244 cpp.pData, 0 );
245 OSL_ENSURE( cpp_env.is(), "### cannot get C++ uno env!" );
248 Reference< lang::XMultiServiceFactory > xMgr(
249 createRegistryServiceFactory(
250 OUString( "stoctest.rdb" ) ) );
254 Reference< registry::XImplementationRegistration > xImplReg(
255 xMgr->createInstance( "com.sun.star.registry.ImplementationRegistration" ),
256 UNO_QUERY );
257 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
259 OUString aLibName(
260 "proxyfac.uno" SAL_DLLEXTENSION );
261 xImplReg->registerImplementation(
262 OUString( "com.sun.star.loader.SharedLibrary" ),
263 aLibName, Reference< registry::XSimpleRegistry >() );
265 Reference< reflection::XProxyFactory > xProxyFac(
266 xMgr->createInstance("com.sun.star.reflection.ProxyFactory"),
267 UNO_QUERY_THROW );
269 Reference< XAggregation > x(
270 xProxyFac->createProxy(
271 getXWeak( new TargetObject ) ) );
272 // no call
275 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
276 test_proxyfac(
277 xMaster,
278 OUString( "master" ),
279 xProxyFac );
282 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
283 // no call
287 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
288 Reference< reflection::XProxyFactory > xSlave_lives_alone(
289 xMaster, UNO_QUERY_THROW );
290 xMaster.clear();
291 test_proxyfac(
292 xSlave_lives_alone,
293 OUString( "master" ),
294 xProxyFac );
295 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
298 Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
299 Reference< reflection::XProxyFactory > xSlave_lives_alone(
300 xMaster, UNO_QUERY_THROW );
301 // no call
304 test_proxyfac(
305 xProxyFac->createProxy(
306 getXWeak( new TargetObject ) ),
307 OUString( "target" ),
308 xProxyFac );
309 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
311 catch (const Exception & rExc)
313 (void)rExc;
314 OSL_ENSURE(
315 ! __FILE__,
316 OUStringToOString(
317 rExc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
318 success = false;
322 Reference< lang::XComponent > xComp;
323 Reference< beans::XPropertySet >(
324 xMgr, UNO_QUERY_THROW )->getPropertyValue(
325 OUString( "DefaultContext" ) )
326 >>= xComp;
327 xComp->dispose();
330 if (TestMaster::s_obj != 0)
331 fprintf( stderr, "TestMaster objects: %d\n", TestMaster::s_obj );
332 if (TargetObject::s_obj != 0)
333 fprintf( stderr, "TargetObject objects: %d\n", TargetObject::s_obj );
335 uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
336 void ** ppInterfaces;
337 sal_Int32 len;
338 uno_ExtEnvironment * env = cpp_env.get()->pExtEnv;
339 (*env->getRegisteredInterfaces)(
340 env, &ppInterfaces, &len, rtl_allocateMemory );
341 if (len != 0)
342 fprintf( stderr, "%d registered C++ interfaces left!\n", len );
344 success &= (TestMaster::s_obj == 0 &&
345 TargetObject::s_obj == 0 &&
346 len == 0);
347 if (success)
349 printf( "testproxyfac succeeded.\n" );
350 return 0;
352 else
354 fprintf( stderr, "testproxyfac failed!\n" );
355 return 1;
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */