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