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