Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sax / test / testcomponent.cxx
blob0413af256c85dc1fa5c9c63f067efd5fb8fb5e1d
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 .
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
25 // Therefor 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>
36 #include <osl/diagnose.h>
38 using namespace ::rtl;
39 using namespace ::cppu;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::test;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::registry;
45 // Needed to switch on solaris threads
46 #ifdef SOLARIS
47 extern "C" void ChangeGlobalInit();
48 #endif
50 int main (int argc, char **argv)
53 if( argc < 3) {
54 printf( "usage : testcomponent service dll [additional dlls]\n" );
55 exit( 0 );
57 #ifdef SOLARIS
58 // switch on threads in solaris
59 ChangeGlobalInit();
60 #endif
62 // create service manager
63 Reference< XMultiServiceFactory > xSMgr =
64 createRegistryServiceFactory( OUString( "applicat.rdb") );
66 Reference < XImplementationRegistration > xReg;
67 Reference < XSimpleRegistry > xSimpleReg;
69 try
71 // Create registration service
72 Reference < XInterface > x = xSMgr->createInstance(
73 OUString("com.sun.star.registry.ImplementationRegistration") );
74 xReg = Reference< XImplementationRegistration > ( x , UNO_QUERY );
76 catch (const Exception&)
78 printf( "Couldn't create ImplementationRegistration service\n" );
79 exit(1);
82 sal_Char szBuf[1024];
83 OString sTestName;
85 try
87 // Load dll for the tested component
88 for( int n = 2 ; n <argc ; n ++ ) {
89 #ifdef SAL_W32
90 OUString aDllName = OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
91 #else
92 OUString aDllName = "lib";
93 aDllName += OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
94 aDllName += ".so";
95 #endif
96 xReg->registerImplementation(
97 OUString("com.sun.star.loader.SharedLibrary"),
98 aDllName,
99 xSimpleReg );
102 catch (const Exception &e)
104 printf( "Couldn't reach dll %s\n" , szBuf );
105 printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
107 exit(1);
113 // Load dll for the test component
114 sTestName = "test";
115 sTestName += argv[2];
117 #ifdef SAL_W32
118 OUString aDllName = OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
119 #else
120 OUString aDllName = "lib";
121 aDllName += OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
122 aDllName += ".so";
123 #endif
125 xReg->registerImplementation(
126 OUString("com.sun.star.loader.SharedLibrary") ,
127 aDllName,
128 xSimpleReg );
130 catch (const Exception&)
132 printf( "Couldn't reach dll %s\n" , szBuf );
133 exit(1);
137 // Instantiate test service
138 sTestName = "test.";
139 sTestName += argv[1];
141 Reference < XInterface > xIntTest =
142 xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
143 Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
145 if( ! xTest.is() ) {
146 printf( "Couldn't instantiate test service \n" );
147 exit( 1 );
151 sal_Int32 nHandle = 0;
152 sal_Int32 nNewHandle;
153 sal_Int32 nErrorCount = 0;
154 sal_Int32 nWarningCount = 0;
156 // loop until all test are performed
157 while( nHandle != -1 )
159 // Instantiate serivce
160 Reference< XInterface > x =
161 xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
162 if( ! x.is() )
164 printf( "Couldn't instantiate service !\n" );
165 exit( 1 );
168 // do the test
171 nNewHandle = xTest->test(
172 OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
174 catch (const Exception &e)
176 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
177 printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
178 exit(1);
180 catch (...)
182 printf( "testcomponent : uncaught unknown exception\n" );
183 exit(1);
187 // print errors and warning
188 Sequence<OUString> seqErrors = xTest->getErrors();
189 Sequence<OUString> seqWarnings = xTest->getWarnings();
190 if( seqWarnings.getLength() > nWarningCount )
192 printf( "Warnings during test %" SAL_PRIxUINT32 "!\n" , nHandle );
193 for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
195 OString o = OUStringToOString(
196 seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
197 printf( "Warning\n%s\n---------\n" , o.getStr() );
202 if( seqErrors.getLength() > nErrorCount ) {
203 printf( "Errors during test %" SAL_PRIxUINT32 "!\n" , nHandle );
204 for( ; nErrorCount < seqErrors.getLength() ; nErrorCount ++ ) {
205 OString o = OUStringToOString(
206 seqErrors.getArray()[nErrorCount], RTL_TEXTENCODING_ASCII_US );
207 printf( "%s\n" , o.getStr() );
211 nHandle = nNewHandle;
214 if( xTest->testPassed() ) {
215 printf( "Test passed !\n" );
217 else {
218 printf( "Test failed !\n" );
221 Reference <XComponent > rComp( xSMgr , UNO_QUERY );
222 rComp->dispose();
223 return 0;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */