xref: /trunk/main/extensions/workben/testcomponent.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 
27cdf0e10cSrcweir //------------------------------------------------------
28cdf0e10cSrcweir // testcomponent - Loads a service and its testcomponent from dlls performs a test.
29cdf0e10cSrcweir // Expands the dll-names depending on the actual environment.
30cdf0e10cSrcweir // Example : testcomponent stardiv.uno.io.Pipe stm
31cdf0e10cSrcweir //
32cdf0e10cSrcweir // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
33cdf0e10cSrcweir //
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <stdio.h>
36cdf0e10cSrcweir #include <smart/com/sun/star/registry/XImplementationRegistration.hxx>
37cdf0e10cSrcweir #include <smart/com/sun/star/lang/XComponent.hxx>
38cdf0e10cSrcweir //#include <com/sun/star/registry/ stardiv/uno/repos/simplreg.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <vos/dynload.hxx>
41cdf0e10cSrcweir #include <vos/diagnose.hxx>
42cdf0e10cSrcweir #include <usr/services.hxx>
43cdf0e10cSrcweir #include <vcl/svapp.hxx>
44cdf0e10cSrcweir #include <usr/ustring.hxx>
45cdf0e10cSrcweir #include <tools/string.hxx>
46cdf0e10cSrcweir #include <vos/conditn.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <smart/com/sun/star/test/XSimpleTest.hxx>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace rtl;
51cdf0e10cSrcweir using namespace vos;
52cdf0e10cSrcweir using namespace usr;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // Needed to switch on solaris threads
56cdf0e10cSrcweir #ifdef SOLARIS
57cdf0e10cSrcweir extern "C" void ChangeGlobalInit();
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir 
main(int argc,char ** argv)60cdf0e10cSrcweir int __LOADONCALLAPI main (int argc, char **argv)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     if( argc < 3) {
64cdf0e10cSrcweir         printf( "usage : testcomponent service dll [additional dlls]\n" );
65cdf0e10cSrcweir         exit( 0 );
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir #ifdef SOLARIS
68cdf0e10cSrcweir     // switch on threads in solaris
69cdf0e10cSrcweir     ChangeGlobalInit();
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     // create service manager
73cdf0e10cSrcweir //  XMultiServiceFactoryRef xSMgr = getProcessServiceManager();
74cdf0e10cSrcweir     XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
75cdf0e10cSrcweir     OSL_ASSERT( xSMgr.is() );
76cdf0e10cSrcweir     registerUsrServices( xSMgr );
77cdf0e10cSrcweir     setProcessServiceManager( xSMgr );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     XImplementationRegistrationRef xReg;
80cdf0e10cSrcweir     XSimpleRegistryRef xSimpleReg;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     try {
83cdf0e10cSrcweir         // Create registration service
84cdf0e10cSrcweir         XInterfaceRef x = xSMgr->createInstance(
85cdf0e10cSrcweir             UString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ) );
86cdf0e10cSrcweir         x->queryInterface( XImplementationRegistration::getSmartUik() , xReg );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir /*      x = xSMgr->createInstance( L"stardiv.uno.repos.SimpleRegistry" );
89cdf0e10cSrcweir         OSL_ASSERT( x.is() );
90cdf0e10cSrcweir         x->queryInterface( XSimpleRegistry::getSmartUik() , xSimpleReg );
91cdf0e10cSrcweir         OSL_ASSERT( xSimpleReg.is() );
92cdf0e10cSrcweir         xSimpleReg->open( L"testcomp.rdb" , FALSE , TRUE );
93cdf0e10cSrcweir */  }
94cdf0e10cSrcweir     catch( Exception& e ) {
95cdf0e10cSrcweir         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
96cdf0e10cSrcweir         exit(1);
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     sal_Char szBuf[1024];
100cdf0e10cSrcweir     OString sTestName;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     try {
103cdf0e10cSrcweir         // Load dll for the tested component
104cdf0e10cSrcweir         for( int n = 2 ; n <argc ; n ++ ) {
105cdf0e10cSrcweir             ORealDynamicLoader::computeModuleName( argv[n] , szBuf, 1024 );
106cdf0e10cSrcweir             UString aDllName( OStringToOWString( szBuf, CHARSET_SYSTEM ) );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             xReg->registerImplementation(
109cdf0e10cSrcweir                 UString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
110cdf0e10cSrcweir                 aDllName,
111cdf0e10cSrcweir                 xSimpleReg );
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir     catch( Exception& e ) {
115cdf0e10cSrcweir         printf( "Couldn't reach dll %s\n" , szBuf );
116cdf0e10cSrcweir         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         exit(1);
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     try {
123cdf0e10cSrcweir         // Load dll for the test component
124cdf0e10cSrcweir         sTestName = "test";
125cdf0e10cSrcweir         sTestName += argv[2];
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         ORealDynamicLoader::computeModuleName( sTestName.getStr() , szBuf, 1024 );
128cdf0e10cSrcweir         UString aDllName = OStringToOWString( szBuf, CHARSET_SYSTEM );
129cdf0e10cSrcweir         xReg->registerImplementation(
130cdf0e10cSrcweir             UString::createFromAscii( "com.sun.star.loader.SharedLibrary" ) ,
131cdf0e10cSrcweir             aDllName,
132cdf0e10cSrcweir             xSimpleReg );
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir     catch( Exception& e ) {
135cdf0e10cSrcweir         printf( "Couldn't reach dll %s\n" , szBuf );
136cdf0e10cSrcweir         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
137cdf0e10cSrcweir         exit(1);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     // Instantiate test service
142cdf0e10cSrcweir     sTestName = "test.";
143cdf0e10cSrcweir     sTestName += argv[1];
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     XInterfaceRef xIntTest = xSMgr->createInstance( OStringToOWString( sTestName , CHARSET_SYSTEM ) );
146cdf0e10cSrcweir     XSimpleTestRef xTest( xIntTest , USR_QUERY );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if( ! xTest.is() ) {
149cdf0e10cSrcweir         printf( "Couldn't instantiate test service \n" );
150cdf0e10cSrcweir         exit( 1 );
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     INT32 nHandle = 0;
155cdf0e10cSrcweir     INT32 nNewHandle;
156cdf0e10cSrcweir     INT32 nErrorCount = 0;
157cdf0e10cSrcweir     INT32 nWarningCount = 0;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     // loop until all test are performed
160cdf0e10cSrcweir     while( nHandle != -1 ) {
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         // Instantiate serivce
163cdf0e10cSrcweir         XInterfaceRef x = xSMgr->createInstance( OStringToOWString( argv[1] , CHARSET_SYSTEM ) );
164cdf0e10cSrcweir         if( ! x.is() ) {
165cdf0e10cSrcweir             printf( "Couldn't instantiate service !\n" );
166cdf0e10cSrcweir             exit( 1 );
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         // do the test
170cdf0e10cSrcweir         try {
171cdf0e10cSrcweir             nNewHandle = xTest->test( OStringToOWString( argv[1] , CHARSET_SYSTEM ) , x , nHandle );
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir         catch ( Exception& e ) {
174cdf0e10cSrcweir             printf( "testcomponent : uncaught exception %s\n" ,
175cdf0e10cSrcweir                         OWStringToOString( e.getName(), CHARSET_SYSTEM ).getStr() );
176cdf0e10cSrcweir             exit(1);
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         catch(...) {
179cdf0e10cSrcweir             printf( "testcomponent : uncaught unknown exception\n"  );
180cdf0e10cSrcweir             exit(1);
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
184cdf0e10cSrcweir         // print errors and warning
185cdf0e10cSrcweir         Sequence<UString> seqErrors = xTest->getErrors();
186cdf0e10cSrcweir         Sequence<UString> seqWarnings = xTest->getWarnings();
187cdf0e10cSrcweir         if( seqWarnings.getLen() > nWarningCount ) {
188cdf0e10cSrcweir             printf( "Warnings during test %d!\n" , nHandle );
189cdf0e10cSrcweir             for( ; nWarningCount < seqWarnings.getLen() ; nWarningCount ++ ) {
190cdf0e10cSrcweir                 printf( "Warning\n%s\n---------\n" ,
191cdf0e10cSrcweir                     OWStringToOString( seqWarnings.getArray()[nWarningCount], CHARSET_SYSTEM ).getStr() );
192cdf0e10cSrcweir             }
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         if( seqErrors.getLen() > nErrorCount ) {
197cdf0e10cSrcweir             printf( "Errors during test %d!\n" , nHandle );
198cdf0e10cSrcweir             for( ; nErrorCount < seqErrors.getLen() ; nErrorCount ++ ) {
199cdf0e10cSrcweir                 printf( "%s\n" ,
200cdf0e10cSrcweir                             OWStringToOString(
201cdf0e10cSrcweir                                     seqErrors.getArray()[nErrorCount], CHARSET_SYSTEM ).getStr() );
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         nHandle = nNewHandle;
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     if( xTest->testPassed() ) {
209cdf0e10cSrcweir         printf( "Test passed !\n" );
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir     else {
212cdf0e10cSrcweir         printf( "Test failed !\n" );
213cdf0e10cSrcweir     }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     XComponentRef rComp( xSMgr , USR_QUERY );
216cdf0e10cSrcweir     rComp->dispose();
217cdf0e10cSrcweir     return 0;
218cdf0e10cSrcweir }
219