Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / sax / test / testcomponent.cxx
blob9b6091fb9cae3f4e6e384dd857f9b621c746af50
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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
25 // Therefore the testcode must exist in teststm and the testservice must be named com.sun.star.io.Pipe
28 #include <stdio.h>
29 #include <com/sun/star/registry/XImplementationRegistration.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/test/XSimpleTest.hpp>
34 #include <cppuhelper/servicefactory.hxx>
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;
43 // Needed to switch on solaris threads
44 #ifdef __sun
45 extern "C" void ChangeGlobalInit();
46 #endif
48 int main (int argc, char **argv)
51 if( argc < 3) {
52 printf( "usage : testcomponent service dll [additional dlls]\n" );
53 exit( 0 );
55 #ifdef __sun
56 // switch on threads in solaris
57 ChangeGlobalInit();
58 #endif
60 // create service manager
61 Reference< XMultiServiceFactory > xSMgr =
62 createRegistryServiceFactory( OUString( "applicat.rdb") );
64 Reference < XImplementationRegistration > xReg;
65 Reference < XSimpleRegistry > xSimpleReg;
67 try
69 // Create registration service
70 Reference < XInterface > x = xSMgr->createInstance( "com.sun.star.registry.ImplementationRegistration" );
71 xReg.set( x , UNO_QUERY );
73 catch (const Exception&)
75 printf( "Couldn't create ImplementationRegistration service\n" );
76 exit(1);
79 sal_Char szBuf[1024];
80 OString sTestName;
82 try
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 );
99 catch (const Exception &e)
101 printf( "Couldn't reach dll %s\n" , szBuf );
102 printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
104 exit(1);
110 // Load dll for the test component
111 sTestName = "test";
112 sTestName += argv[2];
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
122 xReg->registerImplementation(
123 OUString("com.sun.star.loader.SharedLibrary") ,
124 aDllName,
125 xSimpleReg );
127 catch (const Exception&)
129 printf( "Couldn't reach dll %s\n" , szBuf );
130 exit(1);
134 // Instantiate test service
135 sTestName = "test.";
136 sTestName += argv[1];
138 Reference < XInterface > xIntTest =
139 xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
140 Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
142 if( ! xTest.is() ) {
143 printf( "Couldn't instantiate test service \n" );
144 exit( 1 );
148 sal_Int32 nHandle = 0;
149 sal_Int32 nNewHandle;
150 sal_Int32 nErrorCount = 0;
151 sal_Int32 nWarningCount = 0;
153 // loop until all test are performed
154 while( nHandle != -1 )
156 // Instantiate service
157 Reference< XInterface > x =
158 xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
159 if( ! x.is() )
161 printf( "Couldn't instantiate service !\n" );
162 exit( 1 );
165 // do the test
168 nNewHandle = xTest->test(
169 OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
171 catch (const Exception &e)
173 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
174 printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
175 exit(1);
177 catch (...)
179 printf( "testcomponent : uncaught unknown exception\n" );
180 exit(1);
184 // print errors and warning
185 Sequence<OUString> seqErrors = xTest->getErrors();
186 Sequence<OUString> seqWarnings = xTest->getWarnings();
187 if( seqWarnings.getLength() > nWarningCount )
189 printf( "Warnings during test %" SAL_PRIxUINT32 "!\n" , nHandle );
190 for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
192 OString o = OUStringToOString(
193 seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
194 printf( "Warning\n%s\n---------\n" , o.getStr() );
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() );
208 nHandle = nNewHandle;
211 if( xTest->testPassed() ) {
212 printf( "Test passed !\n" );
214 else {
215 printf( "Test failed !\n" );
218 Reference <XComponent > rComp( xSMgr , UNO_QUERY );
219 rComp->dispose();
220 return 0;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */