xref: /trunk/main/extensions/workben/pythonautotest.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir #include <stardiv/uno/repos/implementationregistration.hxx>
28cdf0e10cSrcweir #include <stardiv/uno/script/script.hxx>
29cdf0e10cSrcweir #include <stardiv/uno/beans/exactname.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ustring.hxx>
32cdf0e10cSrcweir #include <vos/dynload.hxx>
33cdf0e10cSrcweir #include <vos/diagnose.hxx>
34cdf0e10cSrcweir #include <usr/services.hxx>
35cdf0e10cSrcweir #include <vcl/svapp.hxx>
36cdf0e10cSrcweir #include <usr/ustring.hxx>
37cdf0e10cSrcweir #include <usr/weak.hxx>
38cdf0e10cSrcweir #include <tools/string.hxx>
39cdf0e10cSrcweir #include <vos/conditn.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace rtl;
42cdf0e10cSrcweir using namespace vos;
43cdf0e10cSrcweir using namespace usr;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
46cdf0e10cSrcweir #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /*****
53cdf0e10cSrcweir *
54cdf0e10cSrcweir * A Test root object !
55cdf0e10cSrcweir *
56cdf0e10cSrcweir *****/
57cdf0e10cSrcweir class MyPythonRoot :
58cdf0e10cSrcweir         public XInvokation,
59cdf0e10cSrcweir         public OWeakObject
60cdf0e10cSrcweir {
61cdf0e10cSrcweir public:
MyPythonRoot()62cdf0e10cSrcweir     MyPythonRoot() { m_iTestValue = 15; }
63cdf0e10cSrcweir     BOOL                queryInterface( Uik aUik, XInterfaceRef & rOut );
acquire()64cdf0e10cSrcweir     void                acquire()                        { OWeakObject::acquire(); }
release()65cdf0e10cSrcweir     void                release()                        { OWeakObject::release(); }
getImplementation(Reflection * p)66cdf0e10cSrcweir     void*               getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir public:
getIntrospection(void) const69cdf0e10cSrcweir     XIntrospectionAccessRef getIntrospection(void) const        THROWS( (UsrSystemException) )
70cdf0e10cSrcweir                 { return XIntrospectionAccessRef();  }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     UsrAny                  invoke( const UString& FunctionName,
73cdf0e10cSrcweir                                     const Sequence< UsrAny >& Params,
74cdf0e10cSrcweir                                     Sequence< INT16 >& OutParamIndex,
75cdf0e10cSrcweir                                     Sequence< UsrAny >& OutParam)
76cdf0e10cSrcweir                                                                 THROWS( (   IllegalArgumentException,
77cdf0e10cSrcweir                                                                             CannotConvertException,
78cdf0e10cSrcweir                                                                             InvocationTargetException,
79cdf0e10cSrcweir                                                                             UsrSystemException) );
80cdf0e10cSrcweir     void                    setValue(const UString& PropertyName, const UsrAny& Value)
81cdf0e10cSrcweir                                                                 THROWS( (   UnknownPropertyException,
82cdf0e10cSrcweir                                                                             CannotConvertException,
83cdf0e10cSrcweir                                                                             InvocationTargetException,
84cdf0e10cSrcweir                                                                             UsrSystemException) );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     UsrAny                  getValue(const UString& PropertyName)
87cdf0e10cSrcweir                                                                 THROWS( (   UnknownPropertyException,
88cdf0e10cSrcweir                                                                             UsrSystemException) );
89cdf0e10cSrcweir     BOOL                    hasMethod(const UString& Name) const THROWS( (UsrSystemException) );
90cdf0e10cSrcweir     BOOL                    hasProperty(const UString& Name) const THROWS( (UsrSystemException) );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
getTestValueViaInout(int & inout)93cdf0e10cSrcweir     void getTestValueViaInout( int &inout )
94cdf0e10cSrcweir         { inout = m_iTestValue; }
95cdf0e10cSrcweir 
getTestValue() const96cdf0e10cSrcweir     INT32 getTestValue() const
97cdf0e10cSrcweir         { return m_iTestValue; }
98cdf0e10cSrcweir 
setTestValue(INT32 i)99cdf0e10cSrcweir     void setTestValue( INT32 i )
100cdf0e10cSrcweir         { m_iTestValue = i; }
101cdf0e10cSrcweir 
printTestValue()102cdf0e10cSrcweir     void printTestValue()
103cdf0e10cSrcweir         { fprintf( stderr, "TestValue : %d\n" , getTestValue() ); }
104cdf0e10cSrcweir 
addTestValue(INT32 i)105cdf0e10cSrcweir     void addTestValue( INT32 i )
106cdf0e10cSrcweir         { m_iTestValue += i; }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir private:
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     INT32 m_iTestValue;
111cdf0e10cSrcweir };
112cdf0e10cSrcweir 
queryInterface(Uik aUik,XInterfaceRef & rOut)113cdf0e10cSrcweir BOOL MyPythonRoot::queryInterface( Uik aUik, XInterfaceRef &rOut )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     if( aUik == XInvokation::getSmartUik() ) {
116cdf0e10cSrcweir         rOut = ( XInvokation * ) this;
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     else {
119cdf0e10cSrcweir         return OWeakObject::queryInterface( aUik , rOut );
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir     return TRUE;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
invoke(const UString & FunctionName,const Sequence<UsrAny> & Params,Sequence<INT16> & OutParamIndex,Sequence<UsrAny> & OutParam)125cdf0e10cSrcweir UsrAny  MyPythonRoot::invoke(   const UString& FunctionName,
126cdf0e10cSrcweir                                 const Sequence< UsrAny >& Params,
127cdf0e10cSrcweir                                 Sequence< INT16 >& OutParamIndex,
128cdf0e10cSrcweir                                 Sequence< UsrAny >& OutParam)
129cdf0e10cSrcweir                                                                 THROWS( (   IllegalArgumentException,
130cdf0e10cSrcweir                                                                             CannotConvertException,
131cdf0e10cSrcweir                                                                             InvocationTargetException,
132cdf0e10cSrcweir                                                                             UsrSystemException) )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     if( L"printTestValue" == FunctionName ) {
135cdf0e10cSrcweir         printTestValue();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir     else if( L"addTestValue" == FunctionName ) {
138cdf0e10cSrcweir         addTestValue( Params.getConstArray()[0].getINT32() );
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir     else if( L"getTestValueViaInout" == FunctionName ) {
141cdf0e10cSrcweir         int i = Params.getConstArray()[0].getINT32();
142cdf0e10cSrcweir         getTestValueViaInout( i );
143cdf0e10cSrcweir         OutParam.getArray()[0].setINT32( i );
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir     else {
146cdf0e10cSrcweir         THROW( InvocationTargetException() );
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     return UsrAny();
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
setValue(const UString & PropertyName,const UsrAny & Value)152cdf0e10cSrcweir void    MyPythonRoot::setValue(const UString& PropertyName, const UsrAny& Value)
153cdf0e10cSrcweir                                                                 THROWS( (   UnknownPropertyException,
154cdf0e10cSrcweir                                                                             CannotConvertException,
155cdf0e10cSrcweir                                                                             InvocationTargetException,
156cdf0e10cSrcweir                                                                             UsrSystemException) )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     if( L"TestValue" == PropertyName ) {
159cdf0e10cSrcweir         setTestValue( Value.getINT32() );
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir     else {
162cdf0e10cSrcweir         THROW( UnknownPropertyException() );
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
getValue(const UString & PropertyName)166cdf0e10cSrcweir UsrAny  MyPythonRoot::getValue(const UString& PropertyName)
167cdf0e10cSrcweir                                                                 THROWS( (   UnknownPropertyException,
168cdf0e10cSrcweir                                                                             UsrSystemException) )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     UsrAny aRet;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     if( L"TestValue" == PropertyName ) {
173cdf0e10cSrcweir         aRet.setINT32( getTestValue() );
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     else {
176cdf0e10cSrcweir         THROW( UnknownPropertyException() );
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     return aRet;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
hasMethod(const UString & Name) const183cdf0e10cSrcweir BOOL    MyPythonRoot::hasMethod(const UString& Name) const      THROWS( (UsrSystemException) )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     if( L"printTestValue" == Name ) {
186cdf0e10cSrcweir         return TRUE;
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir     else if( L"addTestValue" == Name ) {
189cdf0e10cSrcweir         return TRUE;
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir     else if( L"getTestValueViaInout" == Name ) {
192cdf0e10cSrcweir         return TRUE;
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir     return FALSE;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
hasProperty(const UString & Name) const198cdf0e10cSrcweir BOOL    MyPythonRoot::hasProperty(const UString& Name) const THROWS( (UsrSystemException) )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     if( L"TestValue" == Name ) {
201cdf0e10cSrcweir         return TRUE;
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     return FALSE;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
208cdf0e10cSrcweir /*****
209cdf0e10cSrcweir *
210cdf0e10cSrcweir * A test engine listener to check the debug interface
211cdf0e10cSrcweir *
212cdf0e10cSrcweir *****/
213cdf0e10cSrcweir class TestListener :
214cdf0e10cSrcweir     public XEngineListener,
215cdf0e10cSrcweir     public OWeakObject
216cdf0e10cSrcweir {
217cdf0e10cSrcweir public:
218cdf0e10cSrcweir 
TestListener()219cdf0e10cSrcweir     TestListener()
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         m_pDebuggingRef = 0;
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
TestListener(XDebuggingRef * p)225cdf0e10cSrcweir     TestListener( XDebuggingRef *p )
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         attach( p  );
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
~TestListener()230cdf0e10cSrcweir     ~TestListener()
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         if( m_pDebuggingRef ) {
233cdf0e10cSrcweir             detach();
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     BOOL                queryInterface( Uik aUik, XInterfaceRef & rOut );
acquire()238cdf0e10cSrcweir     void                acquire()                        { OWeakObject::acquire(); }
release()239cdf0e10cSrcweir     void                release()                        { OWeakObject::release(); }
getImplementation(Reflection * p)240cdf0e10cSrcweir     void*               getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 
attach(XDebuggingRef * p)243cdf0e10cSrcweir     void attach( XDebuggingRef *p )
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         m_pDebuggingRef = p;
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     void detach( );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 
disposing(const EventObject & o)251cdf0e10cSrcweir     virtual void disposing( const EventObject &o )
252cdf0e10cSrcweir     {
253cdf0e10cSrcweir         if( m_pDebuggingRef ) {
254cdf0e10cSrcweir             detach();
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir     }
interrupt(const InterruptEngineEvent & Evt)257cdf0e10cSrcweir     virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) )
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir 
running(const EventObject & Evt)261cdf0e10cSrcweir     virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) )
262cdf0e10cSrcweir     {
263cdf0e10cSrcweir         (*m_pDebuggingRef)->stop();
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         m_aDebugCondition.set();
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
finished(const FinishEngineEvent & Evt)268cdf0e10cSrcweir     virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) )
269cdf0e10cSrcweir     {
270cdf0e10cSrcweir         m_aDebugCondition.set();
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     void cmdLine();
275cdf0e10cSrcweir protected:
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     OCondition m_aDebugCondition;
278cdf0e10cSrcweir     XDebuggingRef *m_pDebuggingRef;
279cdf0e10cSrcweir };
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 
cmdLine()283cdf0e10cSrcweir void TestListener::cmdLine()
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     // Condition is set by running listener
286cdf0e10cSrcweir     m_aDebugCondition.wait();
287cdf0e10cSrcweir     m_aDebugCondition.reset();
288cdf0e10cSrcweir     (*m_pDebuggingRef)->doContinue();
289cdf0e10cSrcweir     m_aDebugCondition.wait();
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
detach()292cdf0e10cSrcweir void TestListener::detach()
293cdf0e10cSrcweir {
294cdf0e10cSrcweir     OSL_ASSERT( m_pDebuggingRef );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     m_pDebuggingRef = 0;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
queryInterface(Uik aUik,XInterfaceRef & rOut)299cdf0e10cSrcweir BOOL TestListener::queryInterface( Uik aUik, XInterfaceRef & rOut )
300cdf0e10cSrcweir {
301cdf0e10cSrcweir     if( aUik == XEngineListener::getSmartUik() )
302cdf0e10cSrcweir         rOut = (XEngineListener*)this;
303cdf0e10cSrcweir     else
304cdf0e10cSrcweir         return OWeakObject::queryInterface( aUik, rOut );
305cdf0e10cSrcweir     return TRUE;
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 
checkInvokation(const XInvokationRef & xInvoke)309cdf0e10cSrcweir void checkInvokation( const XInvokationRef &xInvoke )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir     UsrAny anyList;
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     // check exporting an object as an invokation
314cdf0e10cSrcweir     OSL_ASSERT( xInvoke->hasProperty( L"list" ) );
315cdf0e10cSrcweir     anyList = xInvoke->getValue( L"list" );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     OSL_ASSERT( anyList.getReflection() == XInvokation_getReflection() );
318cdf0e10cSrcweir     XInvokationRef *pRef = ( XInvokationRef * ) anyList.get();
319cdf0e10cSrcweir     OSL_ASSERT( (*pRef).is() );
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     OSL_ASSERT( (*pRef)->hasMethod( L"append"  ) );
322cdf0e10cSrcweir     OSL_ASSERT( (*pRef)->hasMethod( L"count" ) );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     Sequence<UsrAny> seq(1);
325cdf0e10cSrcweir     UsrAny any( (INT32) 1);
326cdf0e10cSrcweir     (seq.getArray())[0] = any;
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() );
329cdf0e10cSrcweir     int nOldSize = any.getINT32();
330cdf0e10cSrcweir 
331cdf0e10cSrcweir     any = (*pRef)->invoke( L"append" , seq  , Sequence<INT16>(), Sequence<UsrAny>() );
332cdf0e10cSrcweir     any = (*pRef)->invoke( L"count" , seq , Sequence<INT16>(), Sequence<UsrAny>() );
333cdf0e10cSrcweir 
334cdf0e10cSrcweir     OSL_ASSERT( nOldSize + 1 == any.getINT32() );
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir // just for testing !
338cdf0e10cSrcweir class PythonCodeLibrary :
339cdf0e10cSrcweir         public XLibraryAccess,
340cdf0e10cSrcweir         public OWeakObject
341cdf0e10cSrcweir {
342cdf0e10cSrcweir 
343cdf0e10cSrcweir     BOOL                queryInterface( Uik aUik, XInterfaceRef & rOut );
acquire()344cdf0e10cSrcweir     void                acquire()                        { OWeakObject::acquire(); }
release()345cdf0e10cSrcweir     void                release()                        { OWeakObject::release(); }
getImplementation(Reflection * p)346cdf0e10cSrcweir     void*               getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 
isFunction(const UString & FunctionName)349cdf0e10cSrcweir     virtual BOOL isFunction(const UString& FunctionName)            THROWS( (UsrSystemException) )
350cdf0e10cSrcweir     {
351cdf0e10cSrcweir         return FALSE;
352cdf0e10cSrcweir     }
353cdf0e10cSrcweir 
isValidPath(const UString & PathName)354cdf0e10cSrcweir     virtual BOOL isValidPath(const UString& PathName)               THROWS( (UsrSystemException) )
355cdf0e10cSrcweir     {
356cdf0e10cSrcweir         return FALSE;
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir 
getModuleNames(void)359cdf0e10cSrcweir     virtual Sequence< UString > getModuleNames(void)                THROWS( (UsrSystemException) )
360cdf0e10cSrcweir     {
361cdf0e10cSrcweir         return Sequence<UString> ();
362cdf0e10cSrcweir     }
363cdf0e10cSrcweir 
getModuleSource(const UString & ModulName)364cdf0e10cSrcweir     virtual UString getModuleSource(const UString& ModulName)       THROWS( (UsrSystemException) )
365cdf0e10cSrcweir     {
366cdf0e10cSrcweir         if( ModulName == L"testmodul" ) {
367cdf0e10cSrcweir             return UString( L"def testmethod():\n"
368cdf0e10cSrcweir                             L"  return 42\n");
369cdf0e10cSrcweir         }
370cdf0e10cSrcweir         return UString();
371cdf0e10cSrcweir     }
372cdf0e10cSrcweir 
getModuleCode(const UString & ModuleName)373cdf0e10cSrcweir     virtual Sequence< BYTE > getModuleCode(const UString& ModuleName) THROWS( (UsrSystemException) )
374cdf0e10cSrcweir     {
375cdf0e10cSrcweir         return Sequence< BYTE > ();
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir 
getFunctionSource(const UString & FunctionName)378cdf0e10cSrcweir     virtual UString getFunctionSource(const UString& FunctionName) THROWS( (UsrSystemException) )
379cdf0e10cSrcweir     {
380cdf0e10cSrcweir         return UString();
381cdf0e10cSrcweir     }
getFunctionCode(const UString & FunctionName)382cdf0e10cSrcweir     virtual Sequence< BYTE > getFunctionCode(const UString& FunctionName) THROWS( (UsrSystemException) )
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir         return Sequence< BYTE > ();
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir };
387cdf0e10cSrcweir 
queryInterface(Uik aUik,XInterfaceRef & rOut)388cdf0e10cSrcweir BOOL PythonCodeLibrary::queryInterface( Uik aUik, XInterfaceRef & rOut )
389cdf0e10cSrcweir {
390cdf0e10cSrcweir     if( XLibraryAccess::getSmartUik() == aUik ) {
391cdf0e10cSrcweir         rOut = (XLibraryAccess* ) this;
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir     else {
394cdf0e10cSrcweir         return OWeakObject::queryInterface( aUik , rOut );
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir     return TRUE;
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 
402cdf0e10cSrcweir /*
403cdf0e10cSrcweir  * main.
404cdf0e10cSrcweir  */
main(int argc,char ** argv)405cdf0e10cSrcweir int __LOADONCALLAPI main (int argc, char **argv)
406cdf0e10cSrcweir {
407cdf0e10cSrcweir     // necessary startup code
408cdf0e10cSrcweir     XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
409cdf0e10cSrcweir     registerUsrServices( xSMgr );
410cdf0e10cSrcweir     setProcessServiceManager( xSMgr );
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" );
413cdf0e10cSrcweir     XImplementationRegistrationRef xReg( x, USR_QUERY );
414cdf0e10cSrcweir     sal_Char szBuf[1024];
415cdf0e10cSrcweir 
416cdf0e10cSrcweir     ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 );
417cdf0e10cSrcweir     UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) );
418cdf0e10cSrcweir     xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
419cdf0e10cSrcweir 
420cdf0e10cSrcweir     x = xSMgr->createInstance( L"stardiv.script.Python" );
421cdf0e10cSrcweir     XEngineRef xEngine( x, USR_QUERY );
422cdf0e10cSrcweir     XInvokationRef xInvoke(  x, USR_QUERY );
423cdf0e10cSrcweir     XDebuggingRef xDebug( x , USR_QUERY );
424cdf0e10cSrcweir 
425cdf0e10cSrcweir     XInterfaceRef rRoot( (XInvokation * )new MyPythonRoot , USR_QUERY );
426cdf0e10cSrcweir     xEngine->setRoot( rRoot );
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     // execute a simple script
430cdf0e10cSrcweir     xEngine->run(   L"nIntTest = 5\n"
431cdf0e10cSrcweir                     L"list = [2,3,4]\n" , XInterfaceRef(), Sequence<UsrAny> () );
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     /****
434cdf0e10cSrcweir     *
435cdf0e10cSrcweir     * Xinvokation - Test
436cdf0e10cSrcweir     *
437cdf0e10cSrcweir     *****/
438cdf0e10cSrcweir     // get/set an int !
439cdf0e10cSrcweir     {
440cdf0e10cSrcweir         OSL_ASSERT( xInvoke->hasProperty( L"nIntTest" ) );
441cdf0e10cSrcweir         UsrAny any = xInvoke->getValue( L"nIntTest" );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG );
444cdf0e10cSrcweir         OSL_ASSERT( any.getINT32() == 5 );
445cdf0e10cSrcweir 
446cdf0e10cSrcweir         // simple test: set an int !
447cdf0e10cSrcweir         xInvoke->setValue( L"nIntTest" , UsrAny( (INT32) 10 ) );
448cdf0e10cSrcweir         any = xInvoke->getValue( L"nIntTest" );
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG );
451cdf0e10cSrcweir         OSL_ASSERT( any.getINT32() == 10 );
452cdf0e10cSrcweir     }
453cdf0e10cSrcweir 
454cdf0e10cSrcweir     //  call a python method !
455cdf0e10cSrcweir     {
456cdf0e10cSrcweir         xEngine->run( L"def foo():\n"
457cdf0e10cSrcweir                       L"    return 'this is foo'\n" , XInterfaceRef() , Sequence<UsrAny> () );
458cdf0e10cSrcweir         OSL_ASSERT( xInvoke->hasMethod( L"foo" ) );
459cdf0e10cSrcweir         UsrAny any = xInvoke->invoke(   L"foo" ,
460cdf0e10cSrcweir                                         Sequence<UsrAny>(),
461cdf0e10cSrcweir                                         Sequence<INT16>() ,
462cdf0e10cSrcweir                                         Sequence<UsrAny> () );
463cdf0e10cSrcweir         OSL_ASSERT( any.getString() == L"this is foo" );
464cdf0e10cSrcweir     }
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 
467cdf0e10cSrcweir     // check exception handling !
468cdf0e10cSrcweir     {
469cdf0e10cSrcweir         try {
470cdf0e10cSrcweir             xInvoke->invoke( L"foo" , Sequence<UsrAny>(1) , Sequence<INT16>(), Sequence<UsrAny> () );
471cdf0e10cSrcweir             // wrong number of arguments
472cdf0e10cSrcweir             OSL_ASSERT( 0 );
473cdf0e10cSrcweir         }
474cdf0e10cSrcweir         catch ( IllegalArgumentException& e ) {
475cdf0e10cSrcweir         }
476cdf0e10cSrcweir         catch ( InvocationTargetException& e ) {
477cdf0e10cSrcweir         }
478cdf0e10cSrcweir         catch ( CannotConvertException& e ) {
479cdf0e10cSrcweir             // empty any cannot be converted
480cdf0e10cSrcweir         }
481cdf0e10cSrcweir     }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir     // check InOut-Parameter
484cdf0e10cSrcweir     checkInvokation( xInvoke );
485cdf0e10cSrcweir 
486cdf0e10cSrcweir     /*******
487cdf0e10cSrcweir     *
488cdf0e10cSrcweir     * Check Introspection Access
489cdf0e10cSrcweir     *
490cdf0e10cSrcweir     *******/
491cdf0e10cSrcweir     {
492cdf0e10cSrcweir         XIntrospectionAccessRef xIntrospection = xInvoke->getIntrospection();
493cdf0e10cSrcweir         OSL_ASSERT( xIntrospection.is() );
494cdf0e10cSrcweir 
495cdf0e10cSrcweir         // no further test, simply call them
496cdf0e10cSrcweir         xIntrospection->getMethods(0);
497cdf0e10cSrcweir         xIntrospection->getProperties(0);
498cdf0e10cSrcweir 
499cdf0e10cSrcweir         OSL_ASSERT( xIntrospection->getSuppliedMethodConcepts() == 0 );
500cdf0e10cSrcweir         OSL_ASSERT( xIntrospection->getSuppliedPropertyConcepts() == 0 );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir         Property prop = xIntrospection->getProperty( L"nIntTest" ,0 );
503cdf0e10cSrcweir         OSL_ASSERT( prop.Name == L"nIntTest" );
504cdf0e10cSrcweir         OSL_ASSERT( prop.Type->getTypeClass() == TypeClass_LONG );
505cdf0e10cSrcweir 
506cdf0e10cSrcweir         XIdlMethodRef method = xIntrospection->getMethod( L"foo" , 0 );
507cdf0e10cSrcweir         OSL_ASSERT( method->getName() == L"foo" );
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     /******
512cdf0e10cSrcweir     *
513cdf0e10cSrcweir     * Multithreading test
514cdf0e10cSrcweir     *
515cdf0e10cSrcweir     *******/
516cdf0e10cSrcweir 
517cdf0e10cSrcweir     /******
518cdf0e10cSrcweir     *
519cdf0e10cSrcweir     * XDebuggingTest
520cdf0e10cSrcweir     *
521cdf0e10cSrcweir     ******/
522cdf0e10cSrcweir     // stop/doContinue + runAsync listener
523cdf0e10cSrcweir     {
524cdf0e10cSrcweir         // test hangs, if something is wrong
525cdf0e10cSrcweir 
526cdf0e10cSrcweir         TestListener *pListener = new TestListener( &xDebug );
527cdf0e10cSrcweir         XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY );
528cdf0e10cSrcweir 
529cdf0e10cSrcweir         // single listener check !
530cdf0e10cSrcweir         xEngine->runAsync( L"pass\n"
531cdf0e10cSrcweir                             , XInterfaceRef() , Sequence<UsrAny> () , ref );
532cdf0e10cSrcweir         pListener->cmdLine();
533cdf0e10cSrcweir     }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir     // ListenerAdministration check !
536cdf0e10cSrcweir     {
537cdf0e10cSrcweir         // test hangs, if something is wrong
538cdf0e10cSrcweir 
539cdf0e10cSrcweir         TestListener *pListener = new TestListener( &xDebug );
540cdf0e10cSrcweir         XEngineListenerRef ref( (XEngineListener * ) pListener , USR_QUERY );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir         // engine listener check !
543cdf0e10cSrcweir         xEngine->addEngineListener( ref );
544cdf0e10cSrcweir         xEngine->runAsync( L"pass\n"
545cdf0e10cSrcweir                             , XInterfaceRef() , Sequence<UsrAny> () , XEngineListenerRef() );
546cdf0e10cSrcweir         pListener->cmdLine();
547cdf0e10cSrcweir         xEngine->removeEngineListener( ref);
548cdf0e10cSrcweir 
549cdf0e10cSrcweir     }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir     // check the import mechanism
552cdf0e10cSrcweir     {
553cdf0e10cSrcweir         XLibraryAccessRef xLibrary( ( XLibraryAccess * ) new PythonCodeLibrary , USR_QUERY );
554cdf0e10cSrcweir         xEngine->setLibraryAccess( xLibrary );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir         xEngine->run(   L"import testmodul\n"
557cdf0e10cSrcweir                         L"x = testmodul.testmethod()\n" , XInterfaceRef() , Sequence<UsrAny>() );
558cdf0e10cSrcweir         UsrAny any = xInvoke->getValue( L"x" );
559cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG );
560cdf0e10cSrcweir         OSL_ASSERT( any.getINT32() == 42 );
561cdf0e10cSrcweir     }
562cdf0e10cSrcweir 
563cdf0e10cSrcweir     // check other imports
564cdf0e10cSrcweir     {
565cdf0e10cSrcweir         // Check, if the libraries are available at run time
566cdf0e10cSrcweir         xEngine->run(   L"import math\n"
567cdf0e10cSrcweir                         L"dMathTest = math.exp(0)\n"  , XInterfaceRef() , Sequence<UsrAny> () );
568cdf0e10cSrcweir 
569cdf0e10cSrcweir         OSL_ASSERT( xInvoke->hasProperty( L"dMathTest" ) );
570cdf0e10cSrcweir         UsrAny any = xInvoke->getValue( L"dMathTest" );
571cdf0e10cSrcweir 
572cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_DOUBLE );
573cdf0e10cSrcweir         OSL_ASSERT( any.getDouble() == 1. );
574cdf0e10cSrcweir     }
575cdf0e10cSrcweir 
576cdf0e10cSrcweir     // Test connection to root object !
577cdf0e10cSrcweir     {
578cdf0e10cSrcweir         xEngine->run(   L"x = stardiv.root.TestValue\n"
579cdf0e10cSrcweir                         L"y = stardiv.inout(5)\n"
580cdf0e10cSrcweir                         L"stardiv.root.getTestValueViaInout(y)\n"
581cdf0e10cSrcweir                         L"z = y.value\n" , XInterfaceRef() , Sequence<UsrAny> () );
582cdf0e10cSrcweir 
583cdf0e10cSrcweir         UsrAny any = xInvoke->getValue( L"x" );
584cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG );
585cdf0e10cSrcweir         OSL_ASSERT( any.getINT32() == 15 );
586cdf0e10cSrcweir 
587cdf0e10cSrcweir         any = xInvoke->getValue( L"z" );
588cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_LONG );
589cdf0e10cSrcweir         OSL_ASSERT( any.getINT32() == 15 );
590cdf0e10cSrcweir     }
591cdf0e10cSrcweir 
592cdf0e10cSrcweir     // Test exactName interface
593cdf0e10cSrcweir     {
594cdf0e10cSrcweir         UsrAny any = xInvoke->getValue( L"__builtins__" );
595cdf0e10cSrcweir         OSL_ASSERT( any.getReflection()->getTypeClass() == TypeClass_INTERFACE );
596cdf0e10cSrcweir 
597cdf0e10cSrcweir         XInvokationRef rInv( *((XInterfaceRef *) any.get() ), USR_QUERY );
598cdf0e10cSrcweir         OSL_ASSERT( rInv.is() );
599cdf0e10cSrcweir 
600cdf0e10cSrcweir         XExactNameRef rName( *((XInterfaceRef*) any.get() ), USR_QUERY );
601cdf0e10cSrcweir         OSL_ASSERT( rName.is() );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir         UString str = rName->getExactName( L"SYNTAXERROR" );
604cdf0e10cSrcweir         OSL_ASSERT( str.len() );
605cdf0e10cSrcweir     }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir 
608cdf0e10cSrcweir     // Test exactName interface of the engine itself
609cdf0e10cSrcweir     {
610cdf0e10cSrcweir         XExactNameRef rName( xInvoke , USR_QUERY );
611cdf0e10cSrcweir         OSL_ASSERT( rName.is() );
612cdf0e10cSrcweir         UString str = rName->getExactName( L"STARDIV" );
613cdf0e10cSrcweir         OSL_ASSERT( str.len() );
614cdf0e10cSrcweir     }
615cdf0e10cSrcweir 
616cdf0e10cSrcweir 
617cdf0e10cSrcweir     return 0;
618cdf0e10cSrcweir }
619