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