xref: /AOO41X/main/sax/test/testcomponent.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir //------------------------------------------------------
29*cdf0e10cSrcweir // testcomponent - Loads a service and its testcomponent from dlls performs a test.
30*cdf0e10cSrcweir // Expands the dll-names depending on the actual environment.
31*cdf0e10cSrcweir // Example : testcomponent stardiv.uno.io.Pipe stm
32*cdf0e10cSrcweir //
33*cdf0e10cSrcweir // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
34*cdf0e10cSrcweir //
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <stdio.h>
37*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <com/sun/star/test/XSimpleTest.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <vos/dynload.hxx>
45*cdf0e10cSrcweir #include <vos/diagnose.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::rtl;
48*cdf0e10cSrcweir using namespace ::cppu;
49*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50*cdf0e10cSrcweir using namespace ::com::sun::star::test;
51*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
52*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir // Needed to switch on solaris threads
55*cdf0e10cSrcweir #ifdef SOLARIS
56*cdf0e10cSrcweir extern "C" void ChangeGlobalInit();
57*cdf0e10cSrcweir #endif
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir int main (int argc, char **argv)
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	if( argc < 3) {
63*cdf0e10cSrcweir 		printf( "usage : testcomponent service dll [additional dlls]\n" );
64*cdf0e10cSrcweir 		exit( 0 );
65*cdf0e10cSrcweir 	}
66*cdf0e10cSrcweir #ifdef SOLARIS
67*cdf0e10cSrcweir 	// switch on threads in solaris
68*cdf0e10cSrcweir 	ChangeGlobalInit();
69*cdf0e10cSrcweir #endif
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	// create service manager
72*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xSMgr =
73*cdf0e10cSrcweir 		createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	Reference < XImplementationRegistration > xReg;
76*cdf0e10cSrcweir 	Reference < XSimpleRegistry > xSimpleReg;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	try
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		// Create registration service
81*cdf0e10cSrcweir 		Reference < XInterface > x = xSMgr->createInstance(
82*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ) );
83*cdf0e10cSrcweir 		xReg = Reference<  XImplementationRegistration > ( x , UNO_QUERY );
84*cdf0e10cSrcweir 	}
85*cdf0e10cSrcweir 	catch( Exception & ) {
86*cdf0e10cSrcweir 		printf( "Couldn't create ImplementationRegistration service\n" );
87*cdf0e10cSrcweir 		exit(1);
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	sal_Char szBuf[1024];
91*cdf0e10cSrcweir 	OString sTestName;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	try
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		// Load dll for the tested component
96*cdf0e10cSrcweir 		for( int n = 2 ; n <argc ; n ++ ) {
97*cdf0e10cSrcweir #ifdef SAL_W32
98*cdf0e10cSrcweir 			OUString aDllName = OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
99*cdf0e10cSrcweir #else
100*cdf0e10cSrcweir 			OUString aDllName = OUString( RTL_CONSTASCII_USTRINGPARAM("lib"));
101*cdf0e10cSrcweir 			aDllName += OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
102*cdf0e10cSrcweir 			aDllName += OUString( RTL_CONSTASCII_USTRINGPARAM(".so"));
103*cdf0e10cSrcweir #endif
104*cdf0e10cSrcweir 			xReg->registerImplementation(
105*cdf0e10cSrcweir 				OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
106*cdf0e10cSrcweir 				aDllName,
107*cdf0e10cSrcweir 				xSimpleReg );
108*cdf0e10cSrcweir 		}
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 	catch( Exception &e ) {
111*cdf0e10cSrcweir 		printf( "Couldn't reach dll %s\n" , szBuf );
112*cdf0e10cSrcweir 		printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 		exit(1);
115*cdf0e10cSrcweir 	}
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	try
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		// Load dll for the test component
121*cdf0e10cSrcweir 		sTestName = "test";
122*cdf0e10cSrcweir 		sTestName += argv[2];
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir #ifdef SAL_W32
125*cdf0e10cSrcweir 		OUString aDllName = OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
126*cdf0e10cSrcweir #else
127*cdf0e10cSrcweir 		OUString aDllName = OUString( RTL_CONSTASCII_USTRINGPARAM("lib"));
128*cdf0e10cSrcweir 		aDllName += OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
129*cdf0e10cSrcweir 		aDllName += OUString( RTL_CONSTASCII_USTRINGPARAM(".so"));
130*cdf0e10cSrcweir #endif
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		xReg->registerImplementation(
133*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ) ,
134*cdf0e10cSrcweir 			aDllName,
135*cdf0e10cSrcweir 			xSimpleReg );
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 	catch( Exception & e )
138*cdf0e10cSrcweir 	{
139*cdf0e10cSrcweir 		printf( "Couldn't reach dll %s\n" , szBuf );
140*cdf0e10cSrcweir 		exit(1);
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	// Instantiate test service
145*cdf0e10cSrcweir 	sTestName = "test.";
146*cdf0e10cSrcweir 	sTestName += argv[1];
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	Reference < XInterface > xIntTest =
149*cdf0e10cSrcweir 		xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
150*cdf0e10cSrcweir 	Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	if( ! xTest.is() ) {
153*cdf0e10cSrcweir 		printf( "Couldn't instantiate test service \n" );
154*cdf0e10cSrcweir 		exit( 1 );
155*cdf0e10cSrcweir 	}
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	sal_Int32 nHandle = 0;
159*cdf0e10cSrcweir 	sal_Int32 nNewHandle;
160*cdf0e10cSrcweir 	sal_Int32 nErrorCount = 0;
161*cdf0e10cSrcweir 	sal_Int32 nWarningCount = 0;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	// loop until all test are performed
164*cdf0e10cSrcweir 	while( nHandle != -1 )
165*cdf0e10cSrcweir 	{
166*cdf0e10cSrcweir 		// Instantiate serivce
167*cdf0e10cSrcweir 		Reference< XInterface > x =
168*cdf0e10cSrcweir 			xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
169*cdf0e10cSrcweir 		if( ! x.is() )
170*cdf0e10cSrcweir 		{
171*cdf0e10cSrcweir 			printf( "Couldn't instantiate service !\n" );
172*cdf0e10cSrcweir 			exit( 1 );
173*cdf0e10cSrcweir 		}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 		// do the test
176*cdf0e10cSrcweir 		try
177*cdf0e10cSrcweir 		{
178*cdf0e10cSrcweir 			nNewHandle = xTest->test(
179*cdf0e10cSrcweir 				OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
180*cdf0e10cSrcweir 		}
181*cdf0e10cSrcweir 		catch( Exception & e ) {
182*cdf0e10cSrcweir 			OString o  = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
183*cdf0e10cSrcweir 			printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
184*cdf0e10cSrcweir 			exit(1);
185*cdf0e10cSrcweir 		}
186*cdf0e10cSrcweir 		catch( ... )
187*cdf0e10cSrcweir 		{
188*cdf0e10cSrcweir 			printf( "testcomponent : uncaught unknown exception\n"  );
189*cdf0e10cSrcweir 			exit(1);
190*cdf0e10cSrcweir 		}
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		// print errors and warning
194*cdf0e10cSrcweir 		Sequence<OUString> seqErrors = xTest->getErrors();
195*cdf0e10cSrcweir 		Sequence<OUString> seqWarnings = xTest->getWarnings();
196*cdf0e10cSrcweir 		if( seqWarnings.getLength() > nWarningCount )
197*cdf0e10cSrcweir 		{
198*cdf0e10cSrcweir 			printf( "Warnings during test %d!\n" , nHandle );
199*cdf0e10cSrcweir 			for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
200*cdf0e10cSrcweir 			{
201*cdf0e10cSrcweir 				OString o = OUStringToOString(
202*cdf0e10cSrcweir 					seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
203*cdf0e10cSrcweir 				printf( "Warning\n%s\n---------\n" , o.getStr() );
204*cdf0e10cSrcweir 			}
205*cdf0e10cSrcweir 		}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		if( seqErrors.getLength() > nErrorCount ) {
209*cdf0e10cSrcweir 			printf( "Errors during test %d!\n" , nHandle );
210*cdf0e10cSrcweir 			for( ; nErrorCount < seqErrors.getLength() ; nErrorCount ++ ) {
211*cdf0e10cSrcweir 				OString o = OUStringToOString(
212*cdf0e10cSrcweir 					seqErrors.getArray()[nErrorCount], RTL_TEXTENCODING_ASCII_US );
213*cdf0e10cSrcweir 				printf( "%s\n" , o.getStr() );
214*cdf0e10cSrcweir 			}
215*cdf0e10cSrcweir 		}
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		nHandle = nNewHandle;
218*cdf0e10cSrcweir 	}
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	if( xTest->testPassed() ) {
221*cdf0e10cSrcweir 		printf( "Test passed !\n" );
222*cdf0e10cSrcweir 	}
223*cdf0e10cSrcweir 	else {
224*cdf0e10cSrcweir 		printf( "Test failed !\n" );
225*cdf0e10cSrcweir 	}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 	Reference <XComponent >  rComp( xSMgr , UNO_QUERY );
228*cdf0e10cSrcweir 	rComp->dispose();
229*cdf0e10cSrcweir 	return 0;
230*cdf0e10cSrcweir }
231