merge the formfield patch from ooo-build
[ooovba.git] / io / test / stm / pumptest.cxx
blob3c786b9092b3459cb18173d0e90e63ead0290557
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: pumptest.cxx,v $
10 * $Revision: 1.9 $
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 <com/sun/star/test/XSimpleTest.hpp>
39 #include <com/sun/star/io/XActiveDataSource.hpp>
40 #include <com/sun/star/io/XActiveDataSink.hpp>
41 #include <com/sun/star/io/XActiveDataControl.hpp>
42 #include <com/sun/star/io/XConnectable.hpp>
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/registry/XRegistryKey.hpp>
48 #include <uno/dispatcher.h>
49 #include <uno/mapping.hxx>
50 #include <cppuhelper/implbase1.hxx>
51 #include <cppuhelper/factory.hxx>
52 #include <osl/mutex.hxx>
53 #include <osl/thread.h>
54 #include <list>
59 using namespace ::rtl;
60 using namespace ::osl;
61 using namespace ::cppu;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::io;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::test;
67 #include "testfactreg.hxx"
69 static void mywait()
71 TimeValue a = { 0, 10000 };
72 osl_waitThread( &a );
73 osl_yieldThread();
74 osl_yieldThread();
77 class OPumpTest : public WeakImplHelper1 < XSimpleTest >
79 public:
80 OPumpTest( const Reference< XMultiServiceFactory > & rFactory );
81 ~OPumpTest();
83 public: // implementation names
84 static Sequence< OUString > getSupportedServiceNames_Static(void) throw();
85 static OUString getImplementationName_Static() throw();
87 public:
88 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
89 throw ( IllegalArgumentException, RuntimeException) ;
91 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
92 const Reference < XInterface >& TestObject,
93 sal_Int32 hTestHandle)
94 throw ( IllegalArgumentException,
95 RuntimeException);
97 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException) ;
98 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException) ;
99 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException);
100 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException);
102 private:
103 void testSimple( const Reference < XInterface > & );
104 void testWrongUsage( const Reference < XInterface > & );
105 void testClose( const Reference< XInterface >& );
106 void testTerminate( const Reference< XInterface >& );
107 void testFunction( const Reference< XInterface >& );
108 private:
109 Sequence<Any> m_seqExceptions;
110 Sequence<OUString> m_seqErrors;
111 Sequence<OUString> m_seqWarnings;
112 Reference< XMultiServiceFactory > m_rSmgr;
116 OPumpTest::OPumpTest( const Reference< XMultiServiceFactory > &rFactory ) :
117 m_rSmgr( rFactory )
122 OPumpTest::~OPumpTest()
129 void OPumpTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject )
130 throw ( IllegalArgumentException,
131 RuntimeException)
133 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
134 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
135 if( info.is() )
137 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
138 ERROR_ASSERT( ! info->supportsService(
139 OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb") ) ), "XServiceInfo test failed" );
142 Reference < XActiveDataSource > xActiveDataSource( TestObject, UNO_QUERY );
143 Reference < XActiveDataSink > xActiveDataSink( TestObject, UNO_QUERY );
144 Reference < XActiveDataControl > xActiveDataControl( TestObject , UNO_QUERY );
145 Reference < XConnectable > xConnectable( TestObject , UNO_QUERY );
147 ERROR_ASSERT( xActiveDataSource.is() && xActiveDataSink.is() && xActiveDataControl.is () &&
148 xConnectable.is(), "specified interface not supported" );
152 sal_Int32 OPumpTest::test(
153 const OUString& TestName,
154 const Reference < XInterface >& TestObject,
155 sal_Int32 hTestHandle)
156 throw ( IllegalArgumentException, RuntimeException)
158 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pump") ) == TestName ) {
161 if( 0 == hTestHandle ) {
162 testInvariant( TestName , TestObject );
164 else if ( 1 == hTestHandle )
166 testWrongUsage( TestObject);
168 else if ( 2 == hTestHandle )
170 testClose( TestObject);
172 else if ( 3 == hTestHandle )
174 testTerminate( TestObject );
176 else if ( 4 == hTestHandle )
178 testFunction( TestObject );
181 catch( Exception & e )
183 OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
184 BUILD_ERROR( 0 , s.getStr() );
186 catch( ... )
188 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
191 hTestHandle ++;
193 if( 5 == hTestHandle )
195 // all tests finished.
196 hTestHandle = -1;
199 else {
200 throw IllegalArgumentException();
202 return hTestHandle;
207 sal_Bool OPumpTest::testPassed(void) throw (RuntimeException)
209 return m_seqErrors.getLength() == 0;
213 Sequence< OUString > OPumpTest::getErrors(void) throw (RuntimeException)
215 return m_seqErrors;
219 Sequence< Any > OPumpTest::getErrorExceptions(void) throw (RuntimeException)
221 return m_seqExceptions;
225 Sequence< OUString > OPumpTest::getWarnings(void) throw (RuntimeException)
227 return m_seqWarnings;
231 /***
232 * the test methods
234 ****/
237 void OPumpTest::testSimple( const Reference < XInterface > &r )
239 // jbu todo: add sensible test
243 class TestListener: public WeakImplHelper1< XStreamListener >
245 public:
246 sal_Bool m_bStarted;
247 sal_Bool m_bClosed;
248 sal_Bool m_bTerminated;
249 sal_Bool m_bError;
250 sal_Bool m_bDisposed;
251 TestListener() : m_bStarted (sal_False),
252 m_bClosed (sal_False),
253 m_bTerminated ( sal_False ),
254 m_bError( sal_False ),
255 m_bDisposed( sal_False )
258 virtual void SAL_CALL disposing( const EventObject &obj ) throw (::com::sun::star::uno::RuntimeException)
260 m_bDisposed = sal_True;
261 // printf( "disposing called\n");
264 virtual void SAL_CALL started( ) throw (::com::sun::star::uno::RuntimeException)
266 m_bStarted = sal_True;
267 // printf( "started called\n");
269 virtual void SAL_CALL closed( ) throw (::com::sun::star::uno::RuntimeException)
271 m_bClosed = sal_True;
272 // printf( "closed called\n");
274 virtual void SAL_CALL terminated( ) throw (::com::sun::star::uno::RuntimeException)
276 m_bTerminated = sal_True;
277 // printf( "terminated called\n");
279 virtual void SAL_CALL error( const ::com::sun::star::uno::Any& aException )
280 throw (::com::sun::star::uno::RuntimeException)
282 m_bError = sal_True;
283 Exception e;
284 aException >>= e;
285 // printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
289 class TestCase
291 public:
292 TestCase( const Reference< XMultiServiceFactory > & rSMgr,
293 const Reference< XInterface > &r ) : m_rSmgr( rSMgr ), m_pTestListener( 0 )
295 m_rControl = Reference<XActiveDataControl>( r, UNO_QUERY );
297 Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
298 Reference< XActiveDataSink > rSink( r , UNO_QUERY );
300 m_rOutSource = Reference< XOutputStream > ( createPipe() );
301 rSink->setInputStream(Reference< XInputStream> (m_rOutSource,UNO_QUERY));
303 Reference< XOutputStream > rOutSink( createPipe() );
304 m_rInSink = Reference< XInputStream > ( rOutSink, UNO_QUERY );
305 rSource->setOutputStream( rOutSink );
307 m_pTestListener = new TestListener();
308 m_pTestListener->acquire();
309 m_rControl->addListener( m_pTestListener );
312 ~TestCase()
314 if( m_pTestListener )
315 m_pTestListener->release();
318 TestListener *m_pTestListener;
319 Reference< XActiveDataControl > m_rControl;
320 Reference< XOutputStream > m_rOutSource;
321 Reference< XInputStream > m_rInSink;
322 Reference< XMultiServiceFactory > m_rSmgr;
324 private:
325 Reference< XOutputStream > createPipe()
327 Reference< XOutputStream > rOut( m_rSmgr->createInstance(
328 OUString::createFromAscii( "com.sun.star.io.Pipe" )),UNO_QUERY);
329 return rOut;
335 void OPumpTest::testClose( const Reference< XInterface > &r )
337 TestCase t( m_rSmgr, r );
339 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
340 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
341 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
342 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
344 t.m_rControl->start();
345 mywait();
347 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
348 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
349 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
350 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
352 Reference< XStreamListener > rListener( new TestListener() );
353 t.m_rControl->addListener( rListener );
354 t.m_rControl->removeListener( rListener );
356 t.m_rOutSource->closeOutput();
357 mywait();
358 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
359 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" );
360 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
361 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
364 void OPumpTest::testTerminate( const Reference< XInterface > &r )
366 TestCase t( m_rSmgr, r );
368 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
369 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
370 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
371 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
373 t.m_rControl->start();
374 mywait();
376 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
377 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" );
378 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
379 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
381 t.m_rControl->terminate();
383 mywait();
384 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
385 ERROR_ASSERT( t.m_pTestListener->m_bTerminated , "should be terminiated already" );
386 // terminte leads to an error, that is no surprise, in fact
387 // one can't tell wether the error occurs because of the terminate
388 // call or for some other reason !
389 // ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
390 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
393 void OPumpTest::testFunction( const Reference< XInterface > &r )
395 TestCase t( m_rSmgr, r );
397 t.m_rControl->start();
399 t.m_rOutSource->writeBytes( Sequence< sal_Int8 > ( 5 ) );
401 Sequence< sal_Int8 > dummy;
402 ERROR_ASSERT( 5 == t.m_rInSink->readBytes( dummy , 5 ), "couldn't read the expected number of bytes" );
404 t.m_rOutSource->closeOutput();
405 mywait();
407 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
408 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" );
409 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
410 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
413 void OPumpTest::testWrongUsage( const Reference< XInterface > &r )
415 Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
416 Reference< XActiveDataSink > rSink( r , UNO_QUERY );
417 Reference< XActiveDataControl > rControl( r, UNO_QUERY );
419 Reference< XInputStream > rIn( m_rSmgr->createInstance(
420 OUString::createFromAscii( "com.sun.star.io.DataInputStream" )),UNO_QUERY);
421 Reference< XOutputStream > rOut( m_rSmgr->createInstance(
422 OUString::createFromAscii( "com.sun.star.io.DataOutputStream" )),UNO_QUERY);
424 rSink->setInputStream( rIn );
425 rSource->setOutputStream( rOut );
427 rControl->start();
429 mywait();
432 Reference< XInterface > SAL_CALL OPumpTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw( Exception )
434 return *new OPumpTest( rSMgr );
437 Sequence<OUString> OPumpTest_getSupportedServiceNames(void) throw()
439 OUString s = OPumpTest_getServiceName();
440 Sequence< OUString > seq( &s , 1 );
441 return seq;
444 OUString OPumpTest_getServiceName() throw()
446 return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.io.Pump" ) );
449 OUString OPumpTest_getImplementationName() throw()
451 return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.comp.io.Pump") );