1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21 // testcomponent - Loads a service and its testcomponent from dlls performs a test.
22 // Expands the dll-names depending on the actual environment.
23 // Example : testcomponent com.sun.star.io.Pipe stm
24
25 // Therefore the testcode must exist in teststm and the testservice must be named com.sun.star.io.Pipe
26
27
28 #include <stdio.h>
29 #include <com/sun/star/registry/XImplementationRegistration.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31
32 #include <com/sun/star/test/XSimpleTest.hpp>
33
34 #include <cppuhelper/servicefactory.hxx>
35
36
37 using namespace ::cppu;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::test;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::registry;
42
43 // Needed to switch on solaris threads
44 #ifdef __sun
45 extern "C" void ChangeGlobalInit();
46 #endif
47
main(int argc,char ** argv)48 int main (int argc, char **argv)
49 {
50
51 if( argc < 3) {
52 printf( "usage : testcomponent service dll [additional dlls]\n" );
53 exit( 0 );
54 }
55 #ifdef __sun
56 // switch on threads in solaris
57 ChangeGlobalInit();
58 #endif
59
60 // create service manager
61 Reference< XMultiServiceFactory > xSMgr =
62 createRegistryServiceFactory( OUString( "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( "com.sun.star.registry.ImplementationRegistration" );
71 xReg.set( x , UNO_QUERY );
72 }
73 catch (const Exception&)
74 {
75 printf( "Couldn't create ImplementationRegistration service\n" );
76 exit(1);
77 }
78
79 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 #ifdef _WIN32
87 OUString aDllName = OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
88 #else
89 OUString aDllName = "lib";
90 aDllName += OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
91 aDllName += ".so";
92 #endif
93 xReg->registerImplementation(
94 OUString("com.sun.star.loader.SharedLibrary"),
95 aDllName,
96 xSimpleReg );
97 }
98 }
99 catch (const Exception &e)
100 {
101 printf( "Couldn't reach dll %s\n" , szBuf );
102 printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
103
104 exit(1);
105 }
106
107
108 try
109 {
110 // Load dll for the test component
111 sTestName = "test";
112 sTestName += argv[2];
113
114 #ifdef _WIN32
115 OUString aDllName = OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
116 #else
117 OUString aDllName = "lib";
118 aDllName += OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
119 aDllName += ".so";
120 #endif
121
122 xReg->registerImplementation(
123 OUString("com.sun.star.loader.SharedLibrary") ,
124 aDllName,
125 xSimpleReg );
126 }
127 catch (const Exception&)
128 {
129 printf( "Couldn't reach dll %s\n" , szBuf );
130 exit(1);
131 }
132
133
134 // Instantiate test service
135 sTestName = "test.";
136 sTestName += argv[1];
137
138 Reference < XInterface > xIntTest =
139 xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
140 Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
141
142 if( ! xTest.is() ) {
143 printf( "Couldn't instantiate test service \n" );
144 exit( 1 );
145 }
146
147
148 sal_Int32 nHandle = 0;
149 sal_Int32 nNewHandle;
150 sal_Int32 nErrorCount = 0;
151 sal_Int32 nWarningCount = 0;
152
153 // loop until all test are performed
154 while( nHandle != -1 )
155 {
156 // Instantiate service
157 Reference< XInterface > x =
158 xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
159 if( ! x.is() )
160 {
161 printf( "Couldn't instantiate service !\n" );
162 exit( 1 );
163 }
164
165 // do the test
166 try
167 {
168 nNewHandle = xTest->test(
169 OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
170 }
171 catch (const Exception &e)
172 {
173 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
174 printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
175 exit(1);
176 }
177 catch (...)
178 {
179 printf( "testcomponent : uncaught unknown exception\n" );
180 exit(1);
181 }
182
183
184 // print errors and warning
185 Sequence<OUString> seqErrors = xTest->getErrors();
186 Sequence<OUString> seqWarnings = xTest->getWarnings();
187 if( seqWarnings.getLength() > nWarningCount )
188 {
189 printf( "Warnings during test %" SAL_PRIxUINT32 "!\n" , nHandle );
190 for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
191 {
192 OString o = OUStringToOString(
193 seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
194 printf( "Warning\n%s\n---------\n" , o.getStr() );
195 }
196 }
197
198
199 if( seqErrors.getLength() > nErrorCount ) {
200 printf( "Errors during test %" SAL_PRIxUINT32 "!\n" , nHandle );
201 for( ; nErrorCount < seqErrors.getLength() ; nErrorCount ++ ) {
202 OString o = OUStringToOString(
203 seqErrors.getArray()[nErrorCount], RTL_TEXTENCODING_ASCII_US );
204 printf( "%s\n" , o.getStr() );
205 }
206 }
207
208 nHandle = nNewHandle;
209 }
210
211 if( xTest->testPassed() ) {
212 printf( "Test passed !\n" );
213 }
214 else {
215 printf( "Test failed !\n" );
216 }
217
218 Reference <XComponent > rComp( xSMgr , UNO_QUERY );
219 rComp->dispose();
220 return 0;
221 }
222
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
224