tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / io / test / testconnection.cxx
blob4131bcf74dd316dc2389302f50fca08a5120a969
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 #include <stdio.h>
21 #include <osl/time.h>
23 #include <osl/diagnose.h>
24 #include <osl/thread.hxx>
26 #include <com/sun/star/io/IOException.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <com/sun/star/connection/AlreadyAcceptingException.hpp>
33 #include <com/sun/star/connection/ConnectionSetupException.hpp>
34 #include <com/sun/star/connection/XConnector.hpp>
35 #include <com/sun/star/connection/XAcceptor.hpp>
37 #include <cppuhelper/bootstrap.hxx>
39 using namespace ::osl;
40 using namespace ::cppu;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::connection;
47 namespace {
49 class MyThread :
50 public Thread
52 public:
53 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
54 m_rAcceptor( r ),
55 m_sConnectionDescription( sConnectionDescription )
57 virtual void SAL_CALL run();
59 Reference < XAcceptor > m_rAcceptor;
60 private:
61 Reference < XConnection > m_rConnection;
62 OUString m_sConnectionDescription;
65 void doWrite( const Reference < XConnection > &r )
67 Sequence < sal_Int8 > seq(10);
68 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
70 seq.getArray()[i] = i;
73 r->write( seq );
76 void doRead( const Reference < XConnection > &r )
78 Sequence < sal_Int8 > seq(10);
80 OSL_ASSERT( 10 == r->read( seq , 10 ) );
82 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
84 OSL_ASSERT( seq.getConstArray()[i] == i );
89 void MyThread::run()
91 try
93 m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
95 catch ( const Exception &e)
97 OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
98 printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
101 if( m_rConnection.is() )
105 doWrite( m_rConnection );
106 doRead( m_rConnection );
108 catch (... )
110 printf( "unknown exception was thrown\n" );
111 throw;
118 void testConnection( const OUString &sConnectionDescription ,
119 const Reference < XAcceptor > &rAcceptor,
120 const Reference < XConnector > &rConnector )
122 MyThread thread( rAcceptor , sConnectionDescription );
123 thread.create();
125 bool bGotit = false;
126 Reference < XConnection > r;
128 while( ! bGotit )
132 // Why is this wait necessary ????
133 osl::Thread::wait(std::chrono::seconds(1));
134 r = rConnector->connect( sConnectionDescription );
135 OSL_ASSERT( r.is() );
136 doWrite( r );
137 doRead( r );
138 bGotit = true;
140 catch( ... )
142 printf( "Couldn't connect, retrying ...\n" );
147 r->close();
151 Sequence < sal_Int8 > seq(10);
152 r->write( seq );
153 OSL_FAIL( "expected exception not thrown" );
155 catch ( IOException & )
157 // everything is ok
159 catch ( ... )
161 OSL_FAIL( "wrong exception was thrown" );
164 thread.join();
170 int main()
172 Reference< XMultiServiceFactory > xMgr(
173 defaultBootstrap_InitialComponentContext()->getServiceManager(), UNO_QUERY );
175 Reference < XAcceptor > rAcceptor(
176 xMgr->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY );
178 Reference < XAcceptor > rAcceptorPipe(
179 xMgr->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY );
181 Reference < XConnector > rConnector(
182 xMgr->createInstance("com.sun.star.connection.Connector") , UNO_QUERY );
185 printf( "Testing sockets" );
186 fflush( stdout );
187 testConnection( "socket,host=localhost,port=2001", rAcceptor , rConnector );
188 printf( " Done\n" );
190 printf( "Testing pipe" );
191 fflush( stdout );
192 testConnection( "pipe,name=bla" , rAcceptorPipe , rConnector );
193 printf( " Done\n" );
195 // check, if erroneous strings make any problem
196 rAcceptor.set(
197 xMgr->createInstance("com.sun.star.connection.Acceptor"),
198 UNO_QUERY );
202 rAcceptor->accept( OUString() );
203 OSL_FAIL( "empty connection string" );
205 catch( IllegalArgumentException & )
207 // everything is fine
209 catch( ... )
211 OSL_FAIL( "unexpected akexception with empty connection string" );
216 rConnector->connect( OUString() );
217 OSL_FAIL( "empty connection string" );
219 catch( ConnectionSetupException & )
221 // everything is fine
223 catch( ... )
225 OSL_FAIL( "unexpected exception with empty connection string" );
229 MyThread thread( rAcceptor , "socket,host=localhost,port=2001" );
230 thread.create();
232 osl::Thread::wait(std::chrono::seconds(1));
235 rAcceptor->accept( "socket,host=localhost,port=2001" );
236 OSL_FAIL( "already existing exception expected" );
238 catch( AlreadyAcceptingException & )
240 // everything is fine
242 catch( ... )
244 OSL_FAIL( "unknown exception, already existing exception expected" );
247 rAcceptor->stopAccepting();
248 thread.join();
250 Reference < XComponent > rComp( xMgr , UNO_QUERY );
251 if( rComp.is() )
253 rComp->dispose();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */