Branch libreoffice-5-0-4
[LibreOffice.git] / extensions / workben / testcomponent.cxx
blob721d2b05990bf7cc66efd717dfa6b76a583e3b6e
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 .
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 com.sun.star.io.Pipe stm
26 // Therefore the testcode must exist in teststm and the testservice must be named test.com.sun.star.io.Pipe
29 #include <stdio.h>
30 #include <smart/com/sun/star/registry/XImplementationRegistration.hxx>
31 #include <smart/com/sun/star/lang/XComponent.hxx>
33 #include <osl/diagnose.h>
34 #include <usr/services.hxx>
35 #include <vcl/svapp.hxx>
36 #include <usr/ustring.hxx>
37 #include <osl/conditn.hxx>
39 #include <smart/com/sun/star/test/XSimpleTest.hxx>
41 using namespace usr;
43 using ::rtl::OWStringToOString;
46 // Needed to switch on solaris threads
47 #ifdef SOLARIS
48 extern "C" void ChangeGlobalInit();
49 #endif
51 int SAL_CALL main (int argc, char **argv)
54 if( argc < 3) {
55 printf( "usage : testcomponent service dll [additional dlls]\n" );
56 exit( 0 );
58 #ifdef SOLARIS
59 // switch on threads in solaris
60 ChangeGlobalInit();
61 #endif
63 // create service manager
64 // XMultiServiceFactoryRef xSMgr = getProcessServiceManager();
65 XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
66 OSL_ASSERT( xSMgr.is() );
67 registerUsrServices( xSMgr );
68 setProcessServiceManager( xSMgr );
70 XImplementationRegistrationRef xReg;
71 XSimpleRegistryRef xSimpleReg;
73 try {
74 // Create registration service
75 XInterfaceRef x = xSMgr->createInstance(
76 UString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.registry.ImplementationRegistration" )) );
77 x->queryInterface( XImplementationRegistration::getSmartUik() , xReg );
80 catch( Exception& e ) {
81 printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
82 exit(1);
85 sal_Char szBuf[1024];
86 OString sTestName;
88 try {
89 // Load dll for the tested component
90 for( int n = 2 ; n <argc ; n ++ ) {
91 ORealDynamicLoader::computeModuleName( argv[n] , szBuf, 1024 );
92 UString aDllName( OStringToOWString( szBuf, CHARSET_SYSTEM ) );
94 xReg->registerImplementation(
95 UString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.SharedLibrary" )),
96 aDllName,
97 xSimpleReg );
100 catch( Exception& e ) {
101 printf( "Couldn't reach dll %s\n" , szBuf );
102 printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
104 exit(1);
108 try {
109 // Load dll for the test component
110 sTestName = "test";
111 sTestName += argv[2];
113 ORealDynamicLoader::computeModuleName( sTestName.getStr() , szBuf, 1024 );
114 UString aDllName = OStringToOWString( szBuf, CHARSET_SYSTEM );
115 xReg->registerImplementation(
116 UString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.SharedLibrary" )) ,
117 aDllName,
118 xSimpleReg );
120 catch( Exception& e ) {
121 printf( "Couldn't reach dll %s\n" , szBuf );
122 printf( "%s\n" , OWStringToOString( e.getName() , CHARSET_SYSTEM ).getStr() );
123 exit(1);
127 // Instantiate test service
128 sTestName = "test.";
129 sTestName += argv[1];
131 XInterfaceRef xIntTest = xSMgr->createInstance( OStringToOWString( sTestName , CHARSET_SYSTEM ) );
132 XSimpleTestRef xTest( xIntTest , USR_QUERY );
134 if( ! xTest.is() ) {
135 printf( "Couldn't instantiate test service \n" );
136 exit( 1 );
140 INT32 nHandle = 0;
141 INT32 nNewHandle;
142 INT32 nErrorCount = 0;
143 INT32 nWarningCount = 0;
145 // loop until all test are performed
146 while( nHandle != -1 ) {
148 // Instantiate serivce
149 XInterfaceRef x = xSMgr->createInstance( OStringToOWString( argv[1] , CHARSET_SYSTEM ) );
150 if( ! x.is() ) {
151 printf( "Couldn't instantiate service !\n" );
152 exit( 1 );
155 // do the test
156 try {
157 nNewHandle = xTest->test( OStringToOWString( argv[1] , CHARSET_SYSTEM ) , x , nHandle );
159 catch ( Exception& e ) {
160 printf( "testcomponent : uncaught exception %s\n" ,
161 OWStringToOString( e.getName(), CHARSET_SYSTEM ).getStr() );
162 exit(1);
164 catch(...) {
165 printf( "testcomponent : uncaught unknown exception\n" );
166 exit(1);
170 // print errors and warning
171 Sequence<UString> seqErrors = xTest->getErrors();
172 Sequence<UString> seqWarnings = xTest->getWarnings();
173 if( seqWarnings.getLen() > nWarningCount ) {
174 printf( "Warnings during test %d!\n" , nHandle );
175 for( ; nWarningCount < seqWarnings.getLen() ; nWarningCount ++ ) {
176 printf( "Warning\n%s\n---------\n" ,
177 OWStringToOString( seqWarnings.getArray()[nWarningCount], CHARSET_SYSTEM ).getStr() );
182 if( seqErrors.getLen() > nErrorCount ) {
183 printf( "Errors during test %d!\n" , nHandle );
184 for( ; nErrorCount < seqErrors.getLen() ; nErrorCount ++ ) {
185 printf( "%s\n" ,
186 OWStringToOString(
187 seqErrors.getArray()[nErrorCount], CHARSET_SYSTEM ).getStr() );
191 nHandle = nNewHandle;
194 if( xTest->testPassed() ) {
195 printf( "Test passed !\n" );
197 else {
198 printf( "Test failed !\n" );
201 XComponentRef rComp( xSMgr , USR_QUERY );
202 rComp->dispose();
203 return 0;
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */