update dev300-m58
[ooovba.git] / extensions / workben / pythonautotest.cxx
blob16010f893eb36fa674ebbc72f6ed6e9f0fc10f4f
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: pythonautotest.cxx,v $
10 * $Revision: 1.7 $
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_extensions.hxx"
33 #include <stdio.h>
34 #include <stardiv/uno/repos/implementationregistration.hxx>
35 #include <stardiv/uno/script/script.hxx>
36 #include <stardiv/uno/beans/exactname.hxx>
38 #include <rtl/ustring.hxx>
39 #include <vos/dynload.hxx>
40 #include <vos/diagnose.hxx>
41 #include <usr/services.hxx>
42 #include <vcl/svapp.hxx>
43 #include <usr/ustring.hxx>
44 #include <usr/weak.hxx>
45 #include <tools/string.hxx>
46 #include <vos/conditn.hxx>
47 #if OSL_DEBUG_LEVEL == 0
48 #define NDEBUG
49 #endif
50 #include <assert.h>
52 using namespace rtl;
53 using namespace vos;
54 using namespace usr;
56 #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
57 #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
63 /*****
65 * A Test root object !
67 *****/
68 class MyPythonRoot :
69 public XInvokation,
70 public OWeakObject
72 public:
73 MyPythonRoot() { m_iTestValue = 15; }
74 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
75 void acquire() { OWeakObject::acquire(); }
76 void release() { OWeakObject::release(); }
77 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
79 public:
80 XIntrospectionAccessRef getIntrospection(void) const THROWS( (UsrSystemException) )
81 { return XIntrospectionAccessRef(); }
83 UsrAny invoke( const UString& FunctionName,
84 const Sequence< UsrAny >& Params,
85 Sequence< INT16 >& OutParamIndex,
86 Sequence< UsrAny >& OutParam)
87 THROWS( ( IllegalArgumentException,
88 CannotConvertException,
89 InvocationTargetException,
90 UsrSystemException) );
91 void setValue(const UString& PropertyName, const UsrAny& Value)
92 THROWS( ( UnknownPropertyException,
93 CannotConvertException,
94 InvocationTargetException,
95 UsrSystemException) );
97 UsrAny getValue(const UString& PropertyName)
98 THROWS( ( UnknownPropertyException,
99 UsrSystemException) );
100 BOOL hasMethod(const UString& Name) const THROWS( (UsrSystemException) );
101 BOOL hasProperty(const UString& Name) const THROWS( (UsrSystemException) );
104 void getTestValueViaInout( int &inout )
105 { inout = m_iTestValue; }
107 INT32 getTestValue() const
108 { return m_iTestValue; }
110 void setTestValue( INT32 i )
111 { m_iTestValue = i; }
113 void printTestValue()
114 { fprintf( stderr, "TestValue : %d\n" , getTestValue() ); }
116 void addTestValue( INT32 i )
117 { m_iTestValue += i; }
119 private:
121 INT32 m_iTestValue;
124 BOOL MyPythonRoot::queryInterface( Uik aUik, XInterfaceRef &rOut )
126 if( aUik == XInvokation::getSmartUik() ) {
127 rOut = ( XInvokation * ) this;
129 else {
130 return OWeakObject::queryInterface( aUik , rOut );
132 return TRUE;
136 UsrAny MyPythonRoot::invoke( const UString& FunctionName,
137 const Sequence< UsrAny >& Params,
138 Sequence< INT16 >& OutParamIndex,
139 Sequence< UsrAny >& OutParam)
140 THROWS( ( IllegalArgumentException,
141 CannotConvertException,
142 InvocationTargetException,
143 UsrSystemException) )
145 if( L"printTestValue" == FunctionName ) {
146 printTestValue();
148 else if( L"addTestValue" == FunctionName ) {
149 addTestValue( Params.getConstArray()[0].getINT32() );
151 else if( L"getTestValueViaInout" == FunctionName ) {
152 int i = Params.getConstArray()[0].getINT32();
153 getTestValueViaInout( i );
154 OutParam.getArray()[0].setINT32( i );
156 else {
157 THROW( InvocationTargetException() );
160 return UsrAny();
163 void MyPythonRoot::setValue(const UString& PropertyName, const UsrAny& Value)
164 THROWS( ( UnknownPropertyException,
165 CannotConvertException,
166 InvocationTargetException,
167 UsrSystemException) )
169 if( L"TestValue" == PropertyName ) {
170 setTestValue( Value.getINT32() );
172 else {
173 THROW( UnknownPropertyException() );
177 UsrAny MyPythonRoot::getValue(const UString& PropertyName)
178 THROWS( ( UnknownPropertyException,
179 UsrSystemException) )
181 UsrAny aRet;
183 if( L"TestValue" == PropertyName ) {
184 aRet.setINT32( getTestValue() );
186 else {
187 THROW( UnknownPropertyException() );
190 return aRet;
194 BOOL MyPythonRoot::hasMethod(const UString& Name) const THROWS( (UsrSystemException) )
196 if( L"printTestValue" == Name ) {
197 return TRUE;
199 else if( L"addTestValue" == Name ) {
200 return TRUE;
202 else if( L"getTestValueViaInout" == Name ) {
203 return TRUE;
205 return FALSE;
209 BOOL MyPythonRoot::hasProperty(const UString& Name) const THROWS( (UsrSystemException) )
211 if( L"TestValue" == Name ) {
212 return TRUE;
215 return FALSE;
219 /*****
221 * A test engine listener to check the debug interface
223 *****/
224 class TestListener :
225 public XEngineListener,
226 public OWeakObject
228 public:
230 TestListener()
232 m_pDebuggingRef = 0;
236 TestListener( XDebuggingRef *p )
238 attach( p );
241 ~TestListener()
243 if( m_pDebuggingRef ) {
244 detach();
248 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
249 void acquire() { OWeakObject::acquire(); }
250 void release() { OWeakObject::release(); }
251 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
254 void attach( XDebuggingRef *p )
256 m_pDebuggingRef = p;
259 void detach( );
262 virtual void disposing( const EventObject &o )
264 if( m_pDebuggingRef ) {
265 detach();
268 virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) )
272 virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) )
274 (*m_pDebuggingRef)->stop();
276 m_aDebugCondition.set();
279 virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) )
281 m_aDebugCondition.set();
285 void cmdLine();
286 protected:
288 OCondition m_aDebugCondition;
289 XDebuggingRef *m_pDebuggingRef;
294 void TestListener::cmdLine()
296 // Condition is set by running listener
297 m_aDebugCondition.wait();
298 m_aDebugCondition.reset();
299 (*m_pDebuggingRef)->doContinue();
300 m_aDebugCondition.wait();
303 void TestListener::detach()
305 assert( m_pDebuggingRef );
307 m_pDebuggingRef = 0;
310 BOOL TestListener::queryInterface( Uik aUik, XInterfaceRef & rOut )
312 if( aUik == XEngineListener::getSmartUik() )
313 rOut = (XEngineListener*)this;
314 else
315 return OWeakObject::queryInterface( aUik, rOut );
316 return TRUE;
320 void checkInvokation( const XInvokationRef &xInvoke )
322 UsrAny anyList;
324 // check exporting an object as an invokation
325 assert( xInvoke->hasProperty( L"list" ) );
326 anyList = xInvoke->getValue( L"list" );
328 assert( anyList.getReflection() == XInvokation_getReflection() );
329 XInvokationRef *pRef = ( XInvokationRef * ) anyList.get();
330 assert( (*pRef).is() );
332 assert( (*pRef)->hasMethod( L"append" ) );
333 assert( (*pRef)->hasMethod( L"count" ) );
335 Sequence<UsrAny> seq(1);
336 UsrAny any( (INT32) 1);
337 (seq.getArray())[0] = any;
339 any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() );
340 int nOldSize = any.getINT32();
342 any = (*pRef)->invoke( L"append" , seq , Sequence<INT16>(), Sequence<UsrAny>() );
343 any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() );
345 assert( nOldSize + 1 == any.getINT32() );
348 // just for testing !
349 class PythonCodeLibrary :
350 public XLibraryAccess,
351 public OWeakObject
354 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
355 void acquire() { OWeakObject::acquire(); }
356 void release() { OWeakObject::release(); }
357 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
360 virtual BOOL isFunction(const UString& FunctionName) THROWS( (UsrSystemException) )
362 return FALSE;
365 virtual BOOL isValidPath(const UString& PathName) THROWS( (UsrSystemException) )
367 return FALSE;
370 virtual Sequence< UString > getModuleNames(void) THROWS( (UsrSystemException) )
372 return Sequence<UString> ();
375 virtual UString getModuleSource(const UString& ModulName) THROWS( (UsrSystemException) )
377 if( ModulName == L"testmodul" ) {
378 return UString( L"def testmethod():\n"
379 L" return 42\n");
381 return UString();
384 virtual Sequence< BYTE > getModuleCode(const UString& ModuleName) THROWS( (UsrSystemException) )
386 return Sequence< BYTE > ();
389 virtual UString getFunctionSource(const UString& FunctionName) THROWS( (UsrSystemException) )
391 return UString();
393 virtual Sequence< BYTE > getFunctionCode(const UString& FunctionName) THROWS( (UsrSystemException) )
395 return Sequence< BYTE > ();
399 BOOL PythonCodeLibrary::queryInterface( Uik aUik, XInterfaceRef & rOut )
401 if( XLibraryAccess::getSmartUik() == aUik ) {
402 rOut = (XLibraryAccess* ) this;
404 else {
405 return OWeakObject::queryInterface( aUik , rOut );
408 return TRUE;
414 * main.
416 int __LOADONCALLAPI main (int argc, char **argv)
418 // necessary startup code
419 XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
420 registerUsrServices( xSMgr );
421 setProcessServiceManager( xSMgr );
423 XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" );
424 XImplementationRegistrationRef xReg( x, USR_QUERY );
425 sal_Char szBuf[1024];
427 ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 );
428 UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) );
429 xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
431 x = xSMgr->createInstance( L"stardiv.script.Python" );
432 XEngineRef xEngine( x, USR_QUERY );
433 XInvokationRef xInvoke( x, USR_QUERY );
434 XDebuggingRef xDebug( x , USR_QUERY );
436 XInterfaceRef rRoot( (XInvokation * )new MyPythonRoot , USR_QUERY );
437 xEngine->setRoot( rRoot );
440 // execute a simple script
441 xEngine->run( L"nIntTest = 5\n"
442 L"list = [2,3,4]\n" , XInterfaceRef(), Sequence<UsrAny> () );
444 /****
446 * Xinvokation - Test
448 *****/
449 // get/set an int !
451 assert( xInvoke->hasProperty( L"nIntTest" ) );
452 UsrAny any = xInvoke->getValue( L"nIntTest" );
454 assert( any.getReflection()->getTypeClass() == TypeClass_LONG );
455 assert( any.getINT32() == 5 );
457 // simple test: set an int !
458 xInvoke->setValue( L"nIntTest" , UsrAny( (INT32) 10 ) );
459 any = xInvoke->getValue( L"nIntTest" );
461 assert( any.getReflection()->getTypeClass() == TypeClass_LONG );
462 assert( any.getINT32() == 10 );
465 // call a python method !
467 xEngine->run( L"def foo():\n"
468 L" return 'this is foo'\n" , XInterfaceRef() , Sequence<UsrAny> () );
469 assert( xInvoke->hasMethod( L"foo" ) );
470 UsrAny any = xInvoke->invoke( L"foo" ,
471 Sequence<UsrAny>(),
472 Sequence<INT16>() ,
473 Sequence<UsrAny> () );
474 assert( any.getString() == L"this is foo" );
478 // check exception handling !
480 try {
481 xInvoke->invoke( L"foo" , Sequence<UsrAny>(1) , Sequence<INT16>(), Sequence<UsrAny> () );
482 // wrong number of arguments
483 assert( 0 );
485 catch ( IllegalArgumentException& e ) {
487 catch ( InvocationTargetException& e ) {
489 catch ( CannotConvertException& e ) {
490 // empty any cannot be converted
494 // check InOut-Parameter
495 checkInvokation( xInvoke );
497 /*******
499 * Check Introspection Access
501 *******/
503 XIntrospectionAccessRef xIntrospection = xInvoke->getIntrospection();
504 assert( xIntrospection.is() );
506 // no further test, simply call them
507 xIntrospection->getMethods(0);
508 xIntrospection->getProperties(0);
510 assert( xIntrospection->getSuppliedMethodConcepts() == 0 );
511 assert( xIntrospection->getSuppliedPropertyConcepts() == 0 );
513 Property prop = xIntrospection->getProperty( L"nIntTest" ,0 );
514 assert( prop.Name == L"nIntTest" );
515 assert( prop.Type->getTypeClass() == TypeClass_LONG );
517 XIdlMethodRef method = xIntrospection->getMethod( L"foo" , 0 );
518 assert( method->getName() == L"foo" );
522 /******
524 * Multithreading test
526 *******/
528 /******
530 * XDebuggingTest
532 ******/
533 // stop/doContinue + runAsync listener
535 // test hangs, if something is wrong
537 TestListener *pListener = new TestListener( &xDebug );
538 XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY );
540 // single listener check !
541 xEngine->runAsync( L"pass\n"
542 , XInterfaceRef() , Sequence<UsrAny> () , ref );
543 pListener->cmdLine();
546 // ListenerAdministration check !
548 // test hangs, if something is wrong
550 TestListener *pListener = new TestListener( &xDebug );
551 XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY );
553 // engine listener check !
554 xEngine->addEngineListener( ref );
555 xEngine->runAsync( L"pass\n"
556 , XInterfaceRef() , Sequence<UsrAny> () , XEngineListenerRef() );
557 pListener->cmdLine();
558 xEngine->removeEngineListener( ref);
562 // check the import mechanism
564 XLibraryAccessRef xLibrary( ( XLibraryAccess * ) new PythonCodeLibrary , USR_QUERY );
565 xEngine->setLibraryAccess( xLibrary );
567 xEngine->run( L"import testmodul\n"
568 L"x = testmodul.testmethod()\n" , XInterfaceRef() , Sequence<UsrAny>() );
569 UsrAny any = xInvoke->getValue( L"x" );
570 assert( any.getReflection()->getTypeClass() == TypeClass_LONG );
571 assert( any.getINT32() == 42 );
574 // check other imports
576 // Check, if the libraries are available at run time
577 xEngine->run( L"import math\n"
578 L"dMathTest = math.exp(0)\n" , XInterfaceRef() , Sequence<UsrAny> () );
580 assert( xInvoke->hasProperty( L"dMathTest" ) );
581 UsrAny any = xInvoke->getValue( L"dMathTest" );
583 assert( any.getReflection()->getTypeClass() == TypeClass_DOUBLE );
584 assert( any.getDouble() == 1. );
587 // Test connection to root object !
589 xEngine->run( L"x = stardiv.root.TestValue\n"
590 L"y = stardiv.inout(5)\n"
591 L"stardiv.root.getTestValueViaInout(y)\n"
592 L"z = y.value\n" , XInterfaceRef() , Sequence<UsrAny> () );
594 UsrAny any = xInvoke->getValue( L"x" );
595 assert( any.getReflection()->getTypeClass() == TypeClass_LONG );
596 assert( any.getINT32() == 15 );
598 any = xInvoke->getValue( L"z" );
599 assert( any.getReflection()->getTypeClass() == TypeClass_LONG );
600 assert( any.getINT32() == 15 );
603 // Test exactName interface
605 UsrAny any = xInvoke->getValue( L"__builtins__" );
606 assert( any.getReflection()->getTypeClass() == TypeClass_INTERFACE );
608 XInvokationRef rInv( *((XInterfaceRef *) any.get() ), USR_QUERY );
609 assert( rInv.is() );
611 XExactNameRef rName( *((XInterfaceRef*) any.get() ), USR_QUERY );
612 assert( rName.is() );
614 UString str = rName->getExactName( L"SYNTAXERROR" );
615 assert( str.len() );
619 // Test exactName interface of the engine itself
621 XExactNameRef rName( xInvoke , USR_QUERY );
622 assert( rName.is() );
623 UString str = rName->getExactName( L"STARDIV" );
624 assert( str.len() );
628 return 0;