Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / io / test / stm / pumptest.cxx
blobd8b3fe631ec83f0465b67b4e049380ddb2a2cc08
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 <com/sun/star/test/XSimpleTest.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/io/XActiveDataSink.hpp>
37 #include <com/sun/star/io/XActiveDataControl.hpp>
38 #include <com/sun/star/io/XConnectable.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/registry/XRegistryKey.hpp>
44 #include <uno/dispatcher.h>
45 #include <uno/mapping.hxx>
46 #include <cppuhelper/implbase1.hxx>
47 #include <cppuhelper/factory.hxx>
48 #include <osl/mutex.hxx>
49 #include <osl/thread.h>
50 #include <list>
55 using namespace ::rtl;
56 using namespace ::osl;
57 using namespace ::cppu;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::io;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::test;
63 #include "testfactreg.hxx"
65 static void mywait()
67 TimeValue a = { 0, 10000 };
68 osl_waitThread( &a );
69 osl_yieldThread();
70 osl_yieldThread();
73 class OPumpTest : public WeakImplHelper1 < XSimpleTest >
75 public:
76 OPumpTest( const Reference< XMultiServiceFactory > & rFactory );
77 ~OPumpTest();
79 public: // implementation names
80 static Sequence< OUString > getSupportedServiceNames_Static(void) throw();
81 static OUString getImplementationName_Static() throw();
83 public:
84 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
85 throw ( IllegalArgumentException, RuntimeException) ;
87 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
88 const Reference < XInterface >& TestObject,
89 sal_Int32 hTestHandle)
90 throw ( IllegalArgumentException,
91 RuntimeException);
93 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException) ;
94 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException) ;
95 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException);
96 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException);
98 private:
99 void testSimple( const Reference < XInterface > & );
100 void testWrongUsage( const Reference < XInterface > & );
101 void testClose( const Reference< XInterface >& );
102 void testTerminate( const Reference< XInterface >& );
103 void testFunction( const Reference< XInterface >& );
104 private:
105 Sequence<Any> m_seqExceptions;
106 Sequence<OUString> m_seqErrors;
107 Sequence<OUString> m_seqWarnings;
108 Reference< XMultiServiceFactory > m_rSmgr;
112 OPumpTest::OPumpTest( const Reference< XMultiServiceFactory > &rFactory ) :
113 m_rSmgr( rFactory )
118 OPumpTest::~OPumpTest()
125 void OPumpTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject )
126 throw ( IllegalArgumentException,
127 RuntimeException)
129 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
130 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
131 if( info.is() )
133 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
134 ERROR_ASSERT( ! info->supportsService(
135 OUString( "bla bluzb" ) ), "XServiceInfo test failed" );
138 Reference < XActiveDataSource > xActiveDataSource( TestObject, UNO_QUERY );
139 Reference < XActiveDataSink > xActiveDataSink( TestObject, UNO_QUERY );
140 Reference < XActiveDataControl > xActiveDataControl( TestObject , UNO_QUERY );
141 Reference < XConnectable > xConnectable( TestObject , UNO_QUERY );
143 ERROR_ASSERT( xActiveDataSource.is() && xActiveDataSink.is() && xActiveDataControl.is () &&
144 xConnectable.is(), "specified interface not supported" );
148 sal_Int32 OPumpTest::test(
149 const OUString& TestName,
150 const Reference < XInterface >& TestObject,
151 sal_Int32 hTestHandle)
152 throw ( IllegalArgumentException, RuntimeException)
154 if( OUString( "com.sun.star.io.Pump" ) == TestName ) {
157 if( 0 == hTestHandle ) {
158 testInvariant( TestName , TestObject );
160 else if ( 1 == hTestHandle )
162 testWrongUsage( TestObject);
164 else if ( 2 == hTestHandle )
166 testClose( TestObject);
168 else if ( 3 == hTestHandle )
170 testTerminate( TestObject );
172 else if ( 4 == hTestHandle )
174 testFunction( TestObject );
177 catch( const Exception & e )
179 OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
180 BUILD_ERROR( 0 , s.getStr() );
182 catch( ... )
184 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
187 hTestHandle ++;
189 if( 5 == hTestHandle )
191 // all tests finished.
192 hTestHandle = -1;
195 else {
196 throw IllegalArgumentException();
198 return hTestHandle;
203 sal_Bool OPumpTest::testPassed(void) throw (RuntimeException)
205 return m_seqErrors.getLength() == 0;
209 Sequence< OUString > OPumpTest::getErrors(void) throw (RuntimeException)
211 return m_seqErrors;
215 Sequence< Any > OPumpTest::getErrorExceptions(void) throw (RuntimeException)
217 return m_seqExceptions;
221 Sequence< OUString > OPumpTest::getWarnings(void) throw (RuntimeException)
223 return m_seqWarnings;
227 /***
228 * the test methods
230 ****/
233 void OPumpTest::testSimple( const Reference < XInterface > &r )
235 // jbu todo: add sensible test
239 class TestListener: public WeakImplHelper1< XStreamListener >
241 public:
242 sal_Bool m_bStarted;
243 sal_Bool m_bClosed;
244 sal_Bool m_bTerminated;
245 sal_Bool m_bError;
246 sal_Bool m_bDisposed;
247 TestListener() : m_bStarted (sal_False),
248 m_bClosed (sal_False),
249 m_bTerminated ( sal_False ),
250 m_bError( sal_False ),
251 m_bDisposed( sal_False )
254 virtual void SAL_CALL disposing( const EventObject &obj ) throw (::com::sun::star::uno::RuntimeException)
256 m_bDisposed = sal_True;
257 // printf( "disposing called\n");
260 virtual void SAL_CALL started( ) throw (::com::sun::star::uno::RuntimeException)
262 m_bStarted = sal_True;
263 // printf( "started called\n");
265 virtual void SAL_CALL closed( ) throw (::com::sun::star::uno::RuntimeException)
267 m_bClosed = sal_True;
268 // printf( "closed called\n");
270 virtual void SAL_CALL terminated( ) throw (::com::sun::star::uno::RuntimeException)
272 m_bTerminated = sal_True;
273 // printf( "terminated called\n");
275 virtual void SAL_CALL error( const ::com::sun::star::uno::Any& aException )
276 throw (::com::sun::star::uno::RuntimeException)
278 m_bError = sal_True;
279 Exception e;
280 aException >>= e;
281 // printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
285 class TestCase
287 public:
288 TestCase( const Reference< XMultiServiceFactory > & rSMgr,
289 const Reference< XInterface > &r ) : m_rSmgr( rSMgr ), m_pTestListener( 0 )
291 m_rControl = Reference<XActiveDataControl>( r, UNO_QUERY );
293 Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
294 Reference< XActiveDataSink > rSink( r , UNO_QUERY );
296 m_rOutSource = Reference< XOutputStream > ( createPipe() );
297 rSink->setInputStream(Reference< XInputStream> (m_rOutSource,UNO_QUERY));
299 Reference< XOutputStream > rOutSink( createPipe() );
300 m_rInSink = Reference< XInputStream > ( rOutSink, UNO_QUERY );
301 rSource->setOutputStream( rOutSink );
303 m_pTestListener = new TestListener();
304 m_pTestListener->acquire();
305 m_rControl->addListener( m_pTestListener );
308 ~TestCase()
310 if( m_pTestListener )
311 m_pTestListener->release();
314 TestListener *m_pTestListener;
315 Reference< XActiveDataControl > m_rControl;
316 Reference< XOutputStream > m_rOutSource;
317 Reference< XInputStream > m_rInSink;
318 Reference< XMultiServiceFactory > m_rSmgr;
320 private:
321 Reference< XOutputStream > createPipe()
323 Reference< XOutputStream > rOut( m_rSmgr->createInstance(
324 OUString("com.sun.star.io.Pipe")),UNO_QUERY);
325 return rOut;
331 void OPumpTest::testClose( const Reference< XInterface > &r )
333 TestCase t( m_rSmgr, r );
335 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
336 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
337 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
338 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
340 t.m_rControl->start();
341 mywait();
343 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
344 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
345 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
346 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
348 Reference< XStreamListener > rListener( new TestListener() );
349 t.m_rControl->addListener( rListener );
350 t.m_rControl->removeListener( rListener );
352 t.m_rOutSource->closeOutput();
353 mywait();
354 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
355 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" );
356 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
357 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
360 void OPumpTest::testTerminate( const Reference< XInterface > &r )
362 TestCase t( m_rSmgr, r );
364 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
365 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
366 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
367 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
369 t.m_rControl->start();
370 mywait();
372 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
373 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
374 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
375 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
377 t.m_rControl->terminate();
379 mywait();
380 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
381 ERROR_ASSERT( t.m_pTestListener->m_bTerminated , "should be terminiated already" );
382 // terminte leads to an error, that is no surprise, in fact
383 // one can't tell whether the error occurs because of the terminate
384 // call or for some other reason !
385 // ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
386 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
389 void OPumpTest::testFunction( const Reference< XInterface > &r )
391 TestCase t( m_rSmgr, r );
393 t.m_rControl->start();
395 t.m_rOutSource->writeBytes( Sequence< sal_Int8 > ( 5 ) );
397 Sequence< sal_Int8 > dummy;
398 ERROR_ASSERT( 5 == t.m_rInSink->readBytes( dummy , 5 ), "couldn't read the expected number of bytes" );
400 t.m_rOutSource->closeOutput();
401 mywait();
403 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
404 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" );
405 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
406 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
409 void OPumpTest::testWrongUsage( const Reference< XInterface > &r )
411 Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
412 Reference< XActiveDataSink > rSink( r , UNO_QUERY );
413 Reference< XActiveDataControl > rControl( r, UNO_QUERY );
415 Reference< XInputStream > rIn( m_rSmgr->createInstance(
416 OUString("com.sun.star.io.DataInputStream")),UNO_QUERY);
417 Reference< XOutputStream > rOut( m_rSmgr->createInstance(
418 OUString("com.sun.star.io.DataOutputStream")),UNO_QUERY);
420 rSink->setInputStream( rIn );
421 rSource->setOutputStream( rOut );
423 rControl->start();
425 mywait();
428 Reference< XInterface > SAL_CALL OPumpTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw( Exception )
430 return *new OPumpTest( rSMgr );
433 Sequence<OUString> OPumpTest_getSupportedServiceNames(void) throw()
435 OUString s = OPumpTest_getServiceName();
436 Sequence< OUString > seq( &s , 1 );
437 return seq;
440 OUString OPumpTest_getServiceName() throw()
442 return OUString( "test.com.sun.star.io.Pump" );
445 OUString OPumpTest_getImplementationName() throw()
447 return OUString( "test.com.sun.star.comp.io.Pump" );
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */