Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / io / test / testconnection.cxx
blob64979e07b76a0aa5968d6518374c911ba42396c2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <stdio.h>
30 #include <osl/time.h>
32 #include <osl/diagnose.h>
33 #include <osl/thread.hxx>
35 #include <cppuhelper/servicefactory.hxx>
37 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/registry/XImplementationRegistration.hpp>
41 #include <com/sun/star/connection/XConnector.hpp>
42 #include <com/sun/star/connection/XAcceptor.hpp>
44 using namespace ::osl;
45 using namespace ::rtl;
46 using namespace ::cppu;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::io;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::registry;
51 using namespace ::com::sun::star::connection;
54 class MyThread :
55 public Thread
57 public:
58 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
59 m_rAcceptor( r ),
60 m_sConnectionDescription( sConnectionDescription )
62 virtual void SAL_CALL run();
64 Reference < XAcceptor > m_rAcceptor;
65 private:
66 Reference < XConnection > m_rConnection;
67 OUString m_sConnectionDescription;
70 void doWrite( const Reference < XConnection > &r )
72 Sequence < sal_Int8 > seq(10);
73 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
75 seq.getArray()[i] = i;
78 r->write( seq );
81 void doRead( const Reference < XConnection > &r )
83 Sequence < sal_Int8 > seq(10);
85 OSL_ASSERT( 10 == r->read( seq , 10 ) );
87 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
89 OSL_ASSERT( seq.getConstArray()[i] == i );
94 void MyThread::run()
96 try
98 m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
100 catch ( const Exception &e)
102 OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
103 printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
106 if( m_rConnection.is() )
108 Sequence < sal_Int8 > seq(12);
111 doWrite( m_rConnection );
112 doRead( m_rConnection );
114 catch (... )
116 printf( "unknown exception was thrown\n" );
117 throw;
127 void testConnection( const OUString &sConnectionDescription ,
128 const Reference < XAcceptor > &rAcceptor,
129 const Reference < XConnector > &rConnector )
132 MyThread thread( rAcceptor , sConnectionDescription );
133 thread.create();
135 sal_Bool bGotit = sal_False;
136 Reference < XConnection > r;
138 while( ! bGotit )
142 // Why is this wait necessary ????
143 TimeValue value = {1,0};
144 osl_waitThread( &value );
145 r = rConnector->connect( sConnectionDescription );
146 OSL_ASSERT( r.is() );
147 doWrite( r );
148 doRead( r );
149 bGotit = sal_True;
151 catch( ... )
153 printf( "Couldn't connect, retrying ...\n" );
158 r->close();
162 Sequence < sal_Int8 > seq(10);
163 r->write( seq );
164 OSL_FAIL( "expected exception not thrown" );
166 catch ( IOException & )
168 // everything is ok
170 catch ( ... )
172 OSL_FAIL( "wrong exception was thrown" );
175 thread.join();
180 int SAL_CALL main( int argc, char * argv[] )
182 Reference< XMultiServiceFactory > xMgr(
183 createRegistryServiceFactory( OUString( "applicat.rdb") ) );
185 Reference< XImplementationRegistration > xImplReg(
186 xMgr->createInstance( OUString("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY );
187 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
189 OUString aLibName =
190 OUString( "connector.uno" SAL_DLLEXTENSION );
191 xImplReg->registerImplementation(
192 OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
194 aLibName = OUString( "acceptor.uno" SAL_DLLEXTENSION );
195 xImplReg->registerImplementation(
196 OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
198 Reference < XAcceptor > rAcceptor(
199 xMgr->createInstance(
200 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY );
202 Reference < XAcceptor > rAcceptorPipe(
203 xMgr->createInstance(
204 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY );
206 Reference < XConnector > rConnector(
207 xMgr->createInstance( OUString("com.sun.star.connection.Connector") ) , UNO_QUERY );
210 printf( "Testing sockets" );
211 fflush( stdout );
212 testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor , rConnector );
213 printf( " Done\n" );
215 printf( "Testing pipe" );
216 fflush( stdout );
217 testConnection( OUString("pipe,name=bla") , rAcceptorPipe , rConnector );
218 printf( " Done\n" );
220 // check, if errornous strings make any problem
221 rAcceptor = Reference< XAcceptor > (
222 xMgr->createInstance( OUString("com.sun.star.connection.Acceptor") ),
223 UNO_QUERY );
227 rAcceptor->accept( OUString() );
228 OSL_FAIL( "empty connection string" );
230 catch( IllegalArgumentException & )
232 // everything is fine
234 catch( ... )
236 OSL_FAIL( "unexpected akexception with empty connection string" );
241 rConnector->connect( OUString() );
242 OSL_FAIL( "empty connection string" );
244 catch( ConnectionSetupException & )
246 // everything is fine
248 catch( ... )
250 OSL_FAIL( "unexpected exception with empty connection string" );
254 MyThread thread( rAcceptor , OUString("socket,host=localhost,port=2001") );
255 thread.create();
257 TimeValue value = {0,1};
258 osl_waitThread( &value );
261 rAcceptor->accept( OUString("socket,host=localhost,port=2001") );
262 OSL_FAIL( "already existing exception expected" );
264 catch( AlreadyAcceptingException & )
266 // everything is fine
268 catch( ... )
270 OSL_FAIL( "unknown exception, already existing existing expected" );
273 rAcceptor->stopAccepting();
274 thread.join();
276 Reference < XComponent > rComp( xMgr , UNO_QUERY );
277 if( rComp.is() )
279 rComp->dispose();
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */