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