xref: /AOO41X/main/io/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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_io.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //------------------------------------------------------
32*cdf0e10cSrcweir // testcomponent - Loads a service and its testcomponent from dlls performs a test.
33*cdf0e10cSrcweir // Expands the dll-names depending on the actual environment.
34*cdf0e10cSrcweir // Example : testcomponent stardiv.uno.io.Pipe stm
35*cdf0e10cSrcweir //
36*cdf0e10cSrcweir // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
37*cdf0e10cSrcweir //
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <stdio.h>
40*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <com/sun/star/test/XSimpleTest.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <cppuhelper/servicefactory.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 
56*cdf0e10cSrcweir int main (int argc, char **argv)
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	if( argc < 3) {
60*cdf0e10cSrcweir 		printf( "usage : testcomponent service dll [additional dlls]\n" );
61*cdf0e10cSrcweir 		exit( 0 );
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	// create service manager
65*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xSMgr = createRegistryServiceFactory(
66*cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( "applicat.rdb" ) ) );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	Reference < XImplementationRegistration > xReg;
69*cdf0e10cSrcweir 	Reference < XSimpleRegistry > xSimpleReg;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	try
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		// Create registration service
74*cdf0e10cSrcweir 		Reference < XInterface > x = xSMgr->createInstance(
75*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ) );
76*cdf0e10cSrcweir 		xReg = Reference<  XImplementationRegistration > ( x , UNO_QUERY );
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir 	catch( Exception & ) {
79*cdf0e10cSrcweir 		printf( "Couldn't create ImplementationRegistration service\n" );
80*cdf0e10cSrcweir 		exit(1);
81*cdf0e10cSrcweir 	}
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	sal_Char szBuf[1024];
84*cdf0e10cSrcweir 	OString sTestName;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	try
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		// Load dll for the tested component
89*cdf0e10cSrcweir 		for( int n = 2 ; n <argc ; n ++ ) {
90*cdf0e10cSrcweir 			OUString aDllName = OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
91*cdf0e10cSrcweir 			xReg->registerImplementation(
92*cdf0e10cSrcweir 				OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
93*cdf0e10cSrcweir 				aDllName,
94*cdf0e10cSrcweir 				xSimpleReg );
95*cdf0e10cSrcweir 		}
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 	catch( Exception &e ) {
98*cdf0e10cSrcweir 		printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 		exit(1);
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	try
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		// Load dll for the test component
107*cdf0e10cSrcweir 		sTestName = "test";
108*cdf0e10cSrcweir 		sTestName += argv[2];
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir #if defined(SAL_W32) || defined(SAL_OS2)
111*cdf0e10cSrcweir 		OUString aDllName = OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
112*cdf0e10cSrcweir #else
113*cdf0e10cSrcweir 		OUString aDllName = OUString::createFromAscii("lib");
114*cdf0e10cSrcweir 		aDllName += OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
115*cdf0e10cSrcweir 		aDllName += OUString::createFromAscii(".so");
116*cdf0e10cSrcweir #endif
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		xReg->registerImplementation(
119*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ) ,
120*cdf0e10cSrcweir 			aDllName,
121*cdf0e10cSrcweir 			xSimpleReg );
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 	catch( Exception & e )
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		printf( "Couldn't reach dll %s\n" , szBuf );
126*cdf0e10cSrcweir 		exit(1);
127*cdf0e10cSrcweir 	}
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	// Instantiate test service
131*cdf0e10cSrcweir 	sTestName = "test.";
132*cdf0e10cSrcweir 	sTestName += argv[1];
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	Reference < XInterface > xIntTest =
135*cdf0e10cSrcweir 		xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
136*cdf0e10cSrcweir 	Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	if( ! xTest.is() ) {
139*cdf0e10cSrcweir 		printf( "Couldn't instantiate test service \n" );
140*cdf0e10cSrcweir 		exit( 1 );
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	sal_Int32 nHandle = 0;
145*cdf0e10cSrcweir 	sal_Int32 nNewHandle;
146*cdf0e10cSrcweir 	sal_Int32 nErrorCount = 0;
147*cdf0e10cSrcweir 	sal_Int32 nWarningCount = 0;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 	// loop until all test are performed
150*cdf0e10cSrcweir 	while( nHandle != -1 )
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		// Instantiate serivce
153*cdf0e10cSrcweir 		Reference< XInterface > x =
154*cdf0e10cSrcweir 			xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
155*cdf0e10cSrcweir 		if( ! x.is() )
156*cdf0e10cSrcweir 		{
157*cdf0e10cSrcweir 			printf( "Couldn't instantiate service !\n" );
158*cdf0e10cSrcweir 			exit( 1 );
159*cdf0e10cSrcweir 		}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		// do the test
162*cdf0e10cSrcweir 		try
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir 			nNewHandle = xTest->test(
165*cdf0e10cSrcweir 				OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
166*cdf0e10cSrcweir 		}
167*cdf0e10cSrcweir 		catch( Exception & e ) {
168*cdf0e10cSrcweir 			OString o  = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
169*cdf0e10cSrcweir 			printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
170*cdf0e10cSrcweir 			exit(1);
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 		catch( ... )
173*cdf0e10cSrcweir 		{
174*cdf0e10cSrcweir 			printf( "testcomponent : uncaught unknown exception\n"  );
175*cdf0e10cSrcweir 			exit(1);
176*cdf0e10cSrcweir 		}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 		// print errors and warning
180*cdf0e10cSrcweir 		Sequence<OUString> seqErrors = xTest->getErrors();
181*cdf0e10cSrcweir 		Sequence<OUString> seqWarnings = xTest->getWarnings();
182*cdf0e10cSrcweir 		if( seqWarnings.getLength() > nWarningCount )
183*cdf0e10cSrcweir 		{
184*cdf0e10cSrcweir 			printf( "Warnings during test %d!\n" , nHandle );
185*cdf0e10cSrcweir 			for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
186*cdf0e10cSrcweir 			{
187*cdf0e10cSrcweir 				OString o = OUStringToOString(
188*cdf0e10cSrcweir 					seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
189*cdf0e10cSrcweir 				printf( "Warning\n%s\n---------\n" , o.getStr() );
190*cdf0e10cSrcweir 			}
191*cdf0e10cSrcweir 		}
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 		if( seqErrors.getLength() > nErrorCount ) {
195*cdf0e10cSrcweir 			printf( "Errors during test %d!\n" , nHandle );
196*cdf0e10cSrcweir 			for( ; nErrorCount < seqErrors.getLength() ; nErrorCount ++ )
197*cdf0e10cSrcweir 			{
198*cdf0e10cSrcweir 				OString o = OUStringToOString(
199*cdf0e10cSrcweir 					seqErrors.getArray()[nErrorCount], RTL_TEXTENCODING_ASCII_US );
200*cdf0e10cSrcweir 				printf( "%s\n" , o.getStr() );
201*cdf0e10cSrcweir 			}
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		nHandle = nNewHandle;
205*cdf0e10cSrcweir 	}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	if( xTest->testPassed() ) {
208*cdf0e10cSrcweir 		printf( "Test passed !\n" );
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 	else {
211*cdf0e10cSrcweir 		printf( "Test failed !\n" );
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 	Reference <XComponent >  rComp( xSMgr , UNO_QUERY );
215*cdf0e10cSrcweir 	rComp->dispose();
216*cdf0e10cSrcweir 	return 0;
217*cdf0e10cSrcweir }
218