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