xref: /AOO41X/main/extensions/workben/testcomponent.cxx (revision 2a97ec55f1442d65917e8c8b82a55ab76c9ff676)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 //------------------------------------------------------
28 // testcomponent - Loads a service and its testcomponent from dlls performs a test.
29 // Expands the dll-names depending on the actual environment.
30 // Example : testcomponent stardiv.uno.io.Pipe stm
31 //
32 // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
33 //
34 
35 #include <stdio.h>
36 #include <smart/com/sun/star/registry/XImplementationRegistration.hxx>
37 #include <smart/com/sun/star/lang/XComponent.hxx>
38 //#include <com/sun/star/registry/ stardiv/uno/repos/simplreg.hxx>
39 
40 #include <vos/dynload.hxx>
41 #include <vos/diagnose.hxx>
42 #include <usr/services.hxx>
43 #include <vcl/svapp.hxx>
44 #include <usr/ustring.hxx>
45 #include <tools/string.hxx>
46 #include <vos/conditn.hxx>
47 
48 #include <smart/com/sun/star/test/XSimpleTest.hxx>
49 
50 using namespace rtl;
51 using namespace vos;
52 using namespace usr;
53 
54 
55 // Needed to switch on solaris threads
56 #ifdef SOLARIS
57 extern "C" void ChangeGlobalInit();
58 #endif
59 
main(int argc,char ** argv)60 int __LOADONCALLAPI main (int argc, char **argv)
61 {
62 
63     if( argc < 3) {
64         printf( "usage : testcomponent service dll [additional dlls]\n" );
65         exit( 0 );
66     }
67 #ifdef SOLARIS
68     // switch on threads in solaris
69     ChangeGlobalInit();
70 #endif
71 
72     // create service manager
73 //  XMultiServiceFactoryRef xSMgr = getProcessServiceManager();
74     XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
75     OSL_ASSERT( xSMgr.is() );
76     registerUsrServices( xSMgr );
77     setProcessServiceManager( xSMgr );
78 
79     XImplementationRegistrationRef xReg;
80     XSimpleRegistryRef xSimpleReg;
81 
82     try {
83         // Create registration service
84         XInterfaceRef x = xSMgr->createInstance(
85             UString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ) );
86         x->queryInterface( XImplementationRegistration::getSmartUik() , xReg );
87 
88 /*      x = xSMgr->createInstance( L"stardiv.uno.repos.SimpleRegistry" );
89         OSL_ASSERT( x.is() );
90         x->queryInterface( XSimpleRegistry::getSmartUik() , xSimpleReg );
91         OSL_ASSERT( xSimpleReg.is() );
92         xSimpleReg->open( L"testcomp.rdb" , FALSE , TRUE );
93 */  }
94     catch( Exception& e ) {
95         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
96         exit(1);
97     }
98 
99     sal_Char szBuf[1024];
100     OString sTestName;
101 
102     try {
103         // Load dll for the tested component
104         for( int n = 2 ; n <argc ; n ++ ) {
105             ORealDynamicLoader::computeModuleName( argv[n] , szBuf, 1024 );
106             UString aDllName( OStringToOWString( szBuf, CHARSET_SYSTEM ) );
107 
108             xReg->registerImplementation(
109                 UString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
110                 aDllName,
111                 xSimpleReg );
112         }
113     }
114     catch( Exception& e ) {
115         printf( "Couldn't reach dll %s\n" , szBuf );
116         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
117 
118         exit(1);
119     }
120 
121 
122     try {
123         // Load dll for the test component
124         sTestName = "test";
125         sTestName += argv[2];
126 
127         ORealDynamicLoader::computeModuleName( sTestName.getStr() , szBuf, 1024 );
128         UString aDllName = OStringToOWString( szBuf, CHARSET_SYSTEM );
129         xReg->registerImplementation(
130             UString::createFromAscii( "com.sun.star.loader.SharedLibrary" ) ,
131             aDllName,
132             xSimpleReg );
133     }
134     catch( Exception& e ) {
135         printf( "Couldn't reach dll %s\n" , szBuf );
136         printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
137         exit(1);
138     }
139 
140 
141     // Instantiate test service
142     sTestName = "test.";
143     sTestName += argv[1];
144 
145     XInterfaceRef xIntTest = xSMgr->createInstance( OStringToOWString( sTestName , CHARSET_SYSTEM ) );
146     XSimpleTestRef xTest( xIntTest , USR_QUERY );
147 
148     if( ! xTest.is() ) {
149         printf( "Couldn't instantiate test service \n" );
150         exit( 1 );
151     }
152 
153 
154     INT32 nHandle = 0;
155     INT32 nNewHandle;
156     INT32 nErrorCount = 0;
157     INT32 nWarningCount = 0;
158 
159     // loop until all test are performed
160     while( nHandle != -1 ) {
161 
162         // Instantiate serivce
163         XInterfaceRef x = xSMgr->createInstance( OStringToOWString( argv[1] , CHARSET_SYSTEM ) );
164         if( ! x.is() ) {
165             printf( "Couldn't instantiate service !\n" );
166             exit( 1 );
167         }
168 
169         // do the test
170         try {
171             nNewHandle = xTest->test( OStringToOWString( argv[1] , CHARSET_SYSTEM ) , x , nHandle );
172         }
173         catch ( Exception& e ) {
174             printf( "testcomponent : uncaught exception %s\n" ,
175                         OWStringToOString( e.getName(), CHARSET_SYSTEM ).getStr() );
176             exit(1);
177         }
178         catch(...) {
179             printf( "testcomponent : uncaught unknown exception\n"  );
180             exit(1);
181         }
182 
183 
184         // print errors and warning
185         Sequence<UString> seqErrors = xTest->getErrors();
186         Sequence<UString> seqWarnings = xTest->getWarnings();
187         if( seqWarnings.getLen() > nWarningCount ) {
188             printf( "Warnings during test %d!\n" , nHandle );
189             for( ; nWarningCount < seqWarnings.getLen() ; nWarningCount ++ ) {
190                 printf( "Warning\n%s\n---------\n" ,
191                     OWStringToOString( seqWarnings.getArray()[nWarningCount], CHARSET_SYSTEM ).getStr() );
192             }
193         }
194 
195 
196         if( seqErrors.getLen() > nErrorCount ) {
197             printf( "Errors during test %d!\n" , nHandle );
198             for( ; nErrorCount < seqErrors.getLen() ; nErrorCount ++ ) {
199                 printf( "%s\n" ,
200                             OWStringToOString(
201                                     seqErrors.getArray()[nErrorCount], CHARSET_SYSTEM ).getStr() );
202             }
203         }
204 
205         nHandle = nNewHandle;
206     }
207 
208     if( xTest->testPassed() ) {
209         printf( "Test passed !\n" );
210     }
211     else {
212         printf( "Test failed !\n" );
213     }
214 
215     XComponentRef rComp( xSMgr , USR_QUERY );
216     rComp->dispose();
217     return 0;
218 }
219