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