Bump for 4.0-15
[LibreOffice.git] / io / test / testcomponent.cxx
blob05be118b8b8444dd6fc6d1f21c962c33ff2716fc
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 //------------------------------------------------------
22 // testcomponent - Loads a service and its testcomponent from dlls performs a test.
23 // Expands the dll-names depending on the actual environment.
24 // Example : testcomponent stardiv.uno.io.Pipe stm
26 // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
29 #include <stdio.h>
30 #include <com/sun/star/registry/XImplementationRegistration.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/test/XSimpleTest.hpp>
35 #include <cppuhelper/servicefactory.hxx>
37 using namespace ::rtl;
38 using namespace ::cppu;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::test;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::registry;
44 // Needed to switch on solaris threads
46 int main (int argc, char **argv)
49 if( argc < 3) {
50 printf( "usage : testcomponent service dll [additional dlls]\n" );
51 exit( 0 );
54 // create service manager
55 Reference< XMultiServiceFactory > xSMgr = createRegistryServiceFactory(
56 OUString( "applicat.rdb" ) );
58 Reference < XImplementationRegistration > xReg;
59 Reference < XSimpleRegistry > xSimpleReg;
61 try
63 // Create registration service
64 Reference < XInterface > x = xSMgr->createInstance(
65 OUString("com.sun.star.registry.ImplementationRegistration") );
66 xReg = Reference< XImplementationRegistration > ( x , UNO_QUERY );
68 catch( Exception & ) {
69 printf( "Couldn't create ImplementationRegistration service\n" );
70 exit(1);
73 sal_Char szBuf[1024];
74 OString sTestName;
76 try
78 // Load dll for the tested component
79 for( int n = 2 ; n <argc ; n ++ ) {
80 OUString aDllName = OStringToOUString( argv[n] , RTL_TEXTENCODING_ASCII_US );
81 xReg->registerImplementation(
82 OUString("com.sun.star.loader.SharedLibrary"),
83 aDllName,
84 xSimpleReg );
87 catch( const Exception &e ) {
88 printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
90 exit(1);
94 try
96 // Load dll for the test component
97 sTestName = "test";
98 sTestName += argv[2];
100 #if defined(SAL_W32)
101 OUString aDllName = OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
102 #else
103 OUString aDllName("lib");
104 aDllName += OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US );
105 aDllName += OUString(".so");
106 #endif
108 xReg->registerImplementation(
109 OUString("com.sun.star.loader.SharedLibrary") ,
110 aDllName,
111 xSimpleReg );
113 catch( Exception & )
115 printf( "Couldn't reach dll %s\n" , szBuf );
116 exit(1);
120 // Instantiate test service
121 sTestName = "test.";
122 sTestName += argv[1];
124 Reference < XInterface > xIntTest =
125 xSMgr->createInstance( OStringToOUString( sTestName , RTL_TEXTENCODING_ASCII_US ) );
126 Reference< XSimpleTest > xTest( xIntTest , UNO_QUERY );
128 if( ! xTest.is() ) {
129 printf( "Couldn't instantiate test service \n" );
130 exit( 1 );
134 sal_Int32 nHandle = 0;
135 sal_Int32 nNewHandle;
136 sal_Int32 nErrorCount = 0;
137 sal_Int32 nWarningCount = 0;
139 // loop until all test are performed
140 while( nHandle != -1 )
142 // Instantiate serivce
143 Reference< XInterface > x =
144 xSMgr->createInstance( OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) );
145 if( ! x.is() )
147 printf( "Couldn't instantiate service !\n" );
148 exit( 1 );
151 // do the test
154 nNewHandle = xTest->test(
155 OStringToOUString( argv[1] , RTL_TEXTENCODING_ASCII_US ) , x , nHandle );
157 catch( const Exception & e ) {
158 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
159 printf( "testcomponent : uncaught exception %s\n" , o.getStr() );
160 exit(1);
162 catch( ... )
164 printf( "testcomponent : uncaught unknown exception\n" );
165 exit(1);
169 // print errors and warning
170 Sequence<OUString> seqErrors = xTest->getErrors();
171 Sequence<OUString> seqWarnings = xTest->getWarnings();
172 if( seqWarnings.getLength() > nWarningCount )
174 printf( "Warnings during test %d!\n" , nHandle );
175 for( ; nWarningCount < seqWarnings.getLength() ; nWarningCount ++ )
177 OString o = OUStringToOString(
178 seqWarnings.getArray()[nWarningCount], RTL_TEXTENCODING_ASCII_US );
179 printf( "Warning\n%s\n---------\n" , o.getStr() );
184 if( seqErrors.getLength() > nErrorCount ) {
185 printf( "Errors during test %d!\n" , nHandle );
186 for( ; nErrorCount < seqErrors.getLength() ; nErrorCount ++ )
188 OString o = OUStringToOString(
189 seqErrors.getArray()[nErrorCount], RTL_TEXTENCODING_ASCII_US );
190 printf( "%s\n" , o.getStr() );
194 nHandle = nNewHandle;
197 if( xTest->testPassed() ) {
198 printf( "Test passed !\n" );
200 else {
201 printf( "Test failed !\n" );
204 Reference <XComponent > rComp( xSMgr , UNO_QUERY );
205 rComp->dispose();
206 return 0;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */