update dev300-m58
[ooovba.git] / io / test / testconnection.cxx
blob2fe9a83529af34a0e685b05ea55f7e78a930c17f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testconnection.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_io.hxx"
33 #include <stdio.h>
34 #include <osl/time.h>
36 #include <osl/diagnose.h>
37 #include <osl/thread.hxx>
39 #include <cppuhelper/servicefactory.hxx>
41 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/registry/XImplementationRegistration.hpp>
45 #include <com/sun/star/connection/XConnector.hpp>
46 #include <com/sun/star/connection/XAcceptor.hpp>
48 using namespace ::osl;
49 using namespace ::rtl;
50 using namespace ::cppu;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::io;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::registry;
55 using namespace ::com::sun::star::connection;
58 class MyThread :
59 public Thread
61 public:
62 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
63 m_rAcceptor( r ),
64 m_sConnectionDescription( sConnectionDescription )
66 virtual void SAL_CALL run();
68 Reference < XAcceptor > m_rAcceptor;
69 private:
70 Reference < XConnection > m_rConnection;
71 OUString m_sConnectionDescription;
74 void doWrite( const Reference < XConnection > &r )
76 Sequence < sal_Int8 > seq(10);
77 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
79 seq.getArray()[i] = i;
82 r->write( seq );
85 void doRead( const Reference < XConnection > &r )
87 Sequence < sal_Int8 > seq(10);
89 OSL_ASSERT( 10 == r->read( seq , 10 ) );
91 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
93 OSL_ASSERT( seq.getConstArray()[i] == i );
98 void MyThread::run()
102 m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
104 catch ( Exception &e)
106 OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
107 printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
110 if( m_rConnection.is() )
112 Sequence < sal_Int8 > seq(12);
115 doWrite( m_rConnection );
116 doRead( m_rConnection );
118 catch (... )
120 printf( "unknown exception was thrown\n" );
121 throw;
131 void testConnection( const OUString &sConnectionDescription ,
132 const Reference < XAcceptor > &rAcceptor,
133 const Reference < XConnector > &rConnector )
136 MyThread thread( rAcceptor , sConnectionDescription );
137 thread.create();
139 sal_Bool bGotit = sal_False;
140 Reference < XConnection > r;
142 while( ! bGotit )
146 // Why is this wait necessary ????
147 TimeValue value = {1,0};
148 osl_waitThread( &value );
149 r = rConnector->connect( sConnectionDescription );
150 OSL_ASSERT( r.is() );
151 doWrite( r );
152 doRead( r );
153 bGotit = sal_True;
155 catch( ... )
157 printf( "Couldn't connect, retrying ...\n" );
162 r->close();
166 Sequence < sal_Int8 > seq(10);
167 r->write( seq );
168 OSL_ENSURE( 0 , "expected exception not thrown" );
170 catch ( IOException & )
172 // everything is ok
174 catch ( ... )
176 OSL_ENSURE( 0 , "wrong exception was thrown" );
179 thread.join();
184 #if (defined UNX) || (defined OS2)
185 int main( int argc, char * argv[] )
186 #else
187 int __cdecl main( int argc, char * argv[] )
188 #endif
190 Reference< XMultiServiceFactory > xMgr(
191 createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) ) );
193 Reference< XImplementationRegistration > xImplReg(
194 xMgr->createInstance( OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY );
195 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
197 OUString aLibName =
198 OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION );
199 xImplReg->registerImplementation(
200 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
202 aLibName = OUString::createFromAscii( "acceptor.uno" SAL_DLLEXTENSION );
203 xImplReg->registerImplementation(
204 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
206 Reference < XAcceptor > rAcceptor(
207 xMgr->createInstance(
208 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY );
210 Reference < XAcceptor > rAcceptorPipe(
211 xMgr->createInstance(
212 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY );
214 Reference < XConnector > rConnector(
215 xMgr->createInstance( OUString::createFromAscii("com.sun.star.connection.Connector") ) , UNO_QUERY );
218 printf( "Testing sockets" );
219 fflush( stdout );
220 testConnection( OUString::createFromAscii("socket,host=localhost,port=2001"), rAcceptor , rConnector );
221 printf( " Done\n" );
223 printf( "Testing pipe" );
224 fflush( stdout );
225 testConnection( OUString::createFromAscii("pipe,name=bla") , rAcceptorPipe , rConnector );
226 printf( " Done\n" );
228 // check, if errornous strings make any problem
229 rAcceptor = Reference< XAcceptor > (
230 xMgr->createInstance( OUString::createFromAscii( "com.sun.star.connection.Acceptor" ) ),
231 UNO_QUERY );
235 rAcceptor->accept( OUString() );
236 OSL_ENSURE( 0 , "empty connection string" );
238 catch( IllegalArgumentException & )
240 // everything is fine
242 catch( ... )
244 OSL_ENSURE( 0, "unexpected akexception with empty connection string" );
249 rConnector->connect( OUString() );
250 OSL_ENSURE( 0 , "empty connection string" );
252 catch( ConnectionSetupException & )
254 // everything is fine
256 catch( ... )
258 OSL_ENSURE( 0, "unexpected exception with empty connection string" );
262 MyThread thread( rAcceptor , OUString::createFromAscii("socket,host=localhost,port=2001") );
263 thread.create();
265 TimeValue value = {0,1};
266 osl_waitThread( &value );
269 rAcceptor->accept( OUString::createFromAscii("socket,host=localhost,port=2001") );
270 OSL_ENSURE( 0 , "already existing exception expected" );
272 catch( AlreadyAcceptingException & e)
274 // everything is fine
276 catch( ... )
278 OSL_ENSURE( 0, "unknown exception, already existing existing expected" );
281 rAcceptor->stopAccepting();
282 thread.join();
284 Reference < XComponent > rComp( xMgr , UNO_QUERY );
285 if( rComp.is() )
287 rComp->dispose();