1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_io.hxx" 30 #include <stdio.h> 31 #include <osl/time.h> 32 33 #include <osl/diagnose.h> 34 #include <com/sun/star/test/XSimpleTest.hpp> 35 36 #include <com/sun/star/io/XActiveDataSource.hpp> 37 #include <com/sun/star/io/XActiveDataSink.hpp> 38 #include <com/sun/star/io/XActiveDataControl.hpp> 39 #include <com/sun/star/io/XConnectable.hpp> 40 #include <com/sun/star/lang/XServiceInfo.hpp> 41 #include <com/sun/star/lang/XSingleServiceFactory.hpp> 42 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 43 #include <com/sun/star/registry/XRegistryKey.hpp> 44 45 #include <uno/dispatcher.h> 46 #include <uno/mapping.hxx> 47 #include <cppuhelper/implbase1.hxx> 48 #include <cppuhelper/factory.hxx> 49 #include <osl/mutex.hxx> 50 #include <osl/thread.h> 51 #include <list> 52 53 54 55 56 using namespace ::rtl; 57 using namespace ::osl; 58 using namespace ::cppu; 59 using namespace ::com::sun::star::uno; 60 using namespace ::com::sun::star::io; 61 using namespace ::com::sun::star::lang; 62 using namespace ::com::sun::star::test; 63 64 #include "testfactreg.hxx" 65 66 static void mywait() 67 { 68 TimeValue a = { 0, 10000 }; 69 osl_waitThread( &a ); 70 osl_yieldThread(); 71 osl_yieldThread(); 72 } 73 74 class OPumpTest : public WeakImplHelper1 < XSimpleTest > 75 { 76 public: 77 OPumpTest( const Reference< XMultiServiceFactory > & rFactory ); 78 ~OPumpTest(); 79 80 public: // implementation names 81 static Sequence< OUString > getSupportedServiceNames_Static(void) throw(); 82 static OUString getImplementationName_Static() throw(); 83 84 public: 85 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject) 86 throw ( IllegalArgumentException, RuntimeException) ; 87 88 virtual sal_Int32 SAL_CALL test( const OUString& TestName, 89 const Reference < XInterface >& TestObject, 90 sal_Int32 hTestHandle) 91 throw ( IllegalArgumentException, 92 RuntimeException); 93 94 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException) ; 95 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException) ; 96 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException); 97 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException); 98 99 private: 100 void testSimple( const Reference < XInterface > & ); 101 void testWrongUsage( const Reference < XInterface > & ); 102 void testClose( const Reference< XInterface >& ); 103 void testTerminate( const Reference< XInterface >& ); 104 void testFunction( const Reference< XInterface >& ); 105 private: 106 Sequence<Any> m_seqExceptions; 107 Sequence<OUString> m_seqErrors; 108 Sequence<OUString> m_seqWarnings; 109 Reference< XMultiServiceFactory > m_rSmgr; 110 111 }; 112 113 OPumpTest::OPumpTest( const Reference< XMultiServiceFactory > &rFactory ) : 114 m_rSmgr( rFactory ) 115 { 116 117 } 118 119 OPumpTest::~OPumpTest() 120 { 121 122 } 123 124 125 126 void OPumpTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject ) 127 throw ( IllegalArgumentException, 128 RuntimeException) 129 { 130 Reference< XServiceInfo > info( TestObject, UNO_QUERY ); 131 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" ); 132 if( info.is() ) 133 { 134 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" ); 135 ERROR_ASSERT( ! info->supportsService( 136 OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb") ) ), "XServiceInfo test failed" ); 137 } 138 139 Reference < XActiveDataSource > xActiveDataSource( TestObject, UNO_QUERY ); 140 Reference < XActiveDataSink > xActiveDataSink( TestObject, UNO_QUERY ); 141 Reference < XActiveDataControl > xActiveDataControl( TestObject , UNO_QUERY ); 142 Reference < XConnectable > xConnectable( TestObject , UNO_QUERY ); 143 144 ERROR_ASSERT( xActiveDataSource.is() && xActiveDataSink.is() && xActiveDataControl.is () && 145 xConnectable.is(), "specified interface not supported" ); 146 } 147 148 149 sal_Int32 OPumpTest::test( 150 const OUString& TestName, 151 const Reference < XInterface >& TestObject, 152 sal_Int32 hTestHandle) 153 throw ( IllegalArgumentException, RuntimeException) 154 { 155 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pump") ) == TestName ) { 156 try 157 { 158 if( 0 == hTestHandle ) { 159 testInvariant( TestName , TestObject ); 160 } 161 else if ( 1 == hTestHandle ) 162 { 163 testWrongUsage( TestObject); 164 } 165 else if ( 2 == hTestHandle ) 166 { 167 testClose( TestObject); 168 } 169 else if ( 3 == hTestHandle ) 170 { 171 testTerminate( TestObject ); 172 } 173 else if ( 4 == hTestHandle ) 174 { 175 testFunction( TestObject ); 176 } 177 } 178 catch( Exception & e ) 179 { 180 OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ); 181 BUILD_ERROR( 0 , s.getStr() ); 182 } 183 catch( ... ) 184 { 185 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" ); 186 } 187 188 hTestHandle ++; 189 190 if( 5 == hTestHandle ) 191 { 192 // all tests finished. 193 hTestHandle = -1; 194 } 195 } 196 else { 197 throw IllegalArgumentException(); 198 } 199 return hTestHandle; 200 } 201 202 203 204 sal_Bool OPumpTest::testPassed(void) throw (RuntimeException) 205 { 206 return m_seqErrors.getLength() == 0; 207 } 208 209 210 Sequence< OUString > OPumpTest::getErrors(void) throw (RuntimeException) 211 { 212 return m_seqErrors; 213 } 214 215 216 Sequence< Any > OPumpTest::getErrorExceptions(void) throw (RuntimeException) 217 { 218 return m_seqExceptions; 219 } 220 221 222 Sequence< OUString > OPumpTest::getWarnings(void) throw (RuntimeException) 223 { 224 return m_seqWarnings; 225 } 226 227 228 /*** 229 * the test methods 230 * 231 ****/ 232 233 234 void OPumpTest::testSimple( const Reference < XInterface > &r ) 235 { 236 // jbu todo: add sensible test 237 238 } 239 240 class TestListener: public WeakImplHelper1< XStreamListener > 241 { 242 public: 243 sal_Bool m_bStarted; 244 sal_Bool m_bClosed; 245 sal_Bool m_bTerminated; 246 sal_Bool m_bError; 247 sal_Bool m_bDisposed; 248 TestListener() : m_bStarted (sal_False), 249 m_bClosed (sal_False), 250 m_bTerminated ( sal_False ), 251 m_bError( sal_False ), 252 m_bDisposed( sal_False ) 253 {} 254 255 virtual void SAL_CALL disposing( const EventObject &obj ) throw (::com::sun::star::uno::RuntimeException) 256 { 257 m_bDisposed = sal_True; 258 // printf( "disposing called\n"); 259 } 260 261 virtual void SAL_CALL started( ) throw (::com::sun::star::uno::RuntimeException) 262 { 263 m_bStarted = sal_True; 264 // printf( "started called\n"); 265 } 266 virtual void SAL_CALL closed( ) throw (::com::sun::star::uno::RuntimeException) 267 { 268 m_bClosed = sal_True; 269 // printf( "closed called\n"); 270 } 271 virtual void SAL_CALL terminated( ) throw (::com::sun::star::uno::RuntimeException) 272 { 273 m_bTerminated = sal_True; 274 // printf( "terminated called\n"); 275 } 276 virtual void SAL_CALL error( const ::com::sun::star::uno::Any& aException ) 277 throw (::com::sun::star::uno::RuntimeException) 278 { 279 m_bError = sal_True; 280 Exception e; 281 aException >>= e; 282 // printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() ); 283 } 284 }; 285 286 class TestCase 287 { 288 public: 289 TestCase( const Reference< XMultiServiceFactory > & rSMgr, 290 const Reference< XInterface > &r ) : m_rSmgr( rSMgr ), m_pTestListener( 0 ) 291 { 292 m_rControl = Reference<XActiveDataControl>( r, UNO_QUERY ); 293 294 Reference< XActiveDataSource > rSource ( r, UNO_QUERY ); 295 Reference< XActiveDataSink > rSink( r , UNO_QUERY ); 296 297 m_rOutSource = Reference< XOutputStream > ( createPipe() ); 298 rSink->setInputStream(Reference< XInputStream> (m_rOutSource,UNO_QUERY)); 299 300 Reference< XOutputStream > rOutSink( createPipe() ); 301 m_rInSink = Reference< XInputStream > ( rOutSink, UNO_QUERY ); 302 rSource->setOutputStream( rOutSink ); 303 304 m_pTestListener = new TestListener(); 305 m_pTestListener->acquire(); 306 m_rControl->addListener( m_pTestListener ); 307 } 308 309 ~TestCase() 310 { 311 if( m_pTestListener ) 312 m_pTestListener->release(); 313 } 314 315 TestListener *m_pTestListener; 316 Reference< XActiveDataControl > m_rControl; 317 Reference< XOutputStream > m_rOutSource; 318 Reference< XInputStream > m_rInSink; 319 Reference< XMultiServiceFactory > m_rSmgr; 320 321 private: 322 Reference< XOutputStream > createPipe() 323 { 324 Reference< XOutputStream > rOut( m_rSmgr->createInstance( 325 OUString::createFromAscii( "com.sun.star.io.Pipe" )),UNO_QUERY); 326 return rOut; 327 } 328 }; 329 330 331 332 void OPumpTest::testClose( const Reference< XInterface > &r ) 333 { 334 TestCase t( m_rSmgr, r ); 335 336 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" ); 337 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" ); 338 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 339 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" ); 340 341 t.m_rControl->start(); 342 mywait(); 343 344 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" ); 345 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" ); 346 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 347 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" ); 348 349 Reference< XStreamListener > rListener( new TestListener() ); 350 t.m_rControl->addListener( rListener ); 351 t.m_rControl->removeListener( rListener ); 352 353 t.m_rOutSource->closeOutput(); 354 mywait(); 355 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" ); 356 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" ); 357 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 358 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" ); 359 } 360 361 void OPumpTest::testTerminate( const Reference< XInterface > &r ) 362 { 363 TestCase t( m_rSmgr, r ); 364 365 ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" ); 366 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" ); 367 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 368 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" ); 369 370 t.m_rControl->start(); 371 mywait(); 372 373 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" ); 374 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "terminiation unexpected" ); 375 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 376 ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" ); 377 378 t.m_rControl->terminate(); 379 380 mywait(); 381 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" ); 382 ERROR_ASSERT( t.m_pTestListener->m_bTerminated , "should be terminiated already" ); 383 // terminte leads to an error, that is no surprise, in fact 384 // one can't tell wether the error occurs because of the terminate 385 // call or for some other reason ! 386 // ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 387 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" ); 388 } 389 390 void OPumpTest::testFunction( const Reference< XInterface > &r ) 391 { 392 TestCase t( m_rSmgr, r ); 393 394 t.m_rControl->start(); 395 396 t.m_rOutSource->writeBytes( Sequence< sal_Int8 > ( 5 ) ); 397 398 Sequence< sal_Int8 > dummy; 399 ERROR_ASSERT( 5 == t.m_rInSink->readBytes( dummy , 5 ), "couldn't read the expected number of bytes" ); 400 401 t.m_rOutSource->closeOutput(); 402 mywait(); 403 404 ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" ); 405 ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminiated already" ); 406 ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" ); 407 ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" ); 408 } 409 410 void OPumpTest::testWrongUsage( const Reference< XInterface > &r ) 411 { 412 Reference< XActiveDataSource > rSource ( r, UNO_QUERY ); 413 Reference< XActiveDataSink > rSink( r , UNO_QUERY ); 414 Reference< XActiveDataControl > rControl( r, UNO_QUERY ); 415 416 Reference< XInputStream > rIn( m_rSmgr->createInstance( 417 OUString::createFromAscii( "com.sun.star.io.DataInputStream" )),UNO_QUERY); 418 Reference< XOutputStream > rOut( m_rSmgr->createInstance( 419 OUString::createFromAscii( "com.sun.star.io.DataOutputStream" )),UNO_QUERY); 420 421 rSink->setInputStream( rIn ); 422 rSource->setOutputStream( rOut ); 423 424 rControl->start(); 425 426 mywait(); 427 } 428 429 Reference< XInterface > SAL_CALL OPumpTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw( Exception ) 430 { 431 return *new OPumpTest( rSMgr ); 432 } 433 434 Sequence<OUString> OPumpTest_getSupportedServiceNames(void) throw() 435 { 436 OUString s = OPumpTest_getServiceName(); 437 Sequence< OUString > seq( &s , 1 ); 438 return seq; 439 440 } 441 OUString OPumpTest_getServiceName() throw() 442 { 443 return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.io.Pump" ) ); 444 } 445 446 OUString OPumpTest_getImplementationName() throw() 447 { 448 return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.comp.io.Pump") ); 449 } 450