Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / io / test / stm / pipetest.cxx
blobc67ff9a86bf17a34d4bb26482c6f4d7978b0a765
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 ************************************************************************/
30 #include <com/sun/star/test/XSimpleTest.hpp>
31 #include <com/sun/star/io/XInputStream.hpp>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/io/XConnectable.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/implbase1.hxx> // OWeakObject
42 #include <osl/conditn.hxx>
43 #include <osl/mutex.hxx>
44 #include <osl/thread.hxx>
46 #include <string.h>
48 using namespace ::rtl;
49 using namespace ::osl;
50 using namespace ::cppu;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::io;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::test;
55 // streams
57 #include "testfactreg.hxx"
58 #define IMPLEMENTATION_NAME "test.com.sun.star.comp.extensions.stm.Pipe"
59 #define SERVICE_NAME "test.com.sun.star.io.Pipe"
62 class WriteToStreamThread :
63 public Thread
66 public:
68 WriteToStreamThread( Reference< XOutputStream > xOutput , int iMax )
70 m_output = xOutput;
71 m_iMax = iMax;
74 virtual ~WriteToStreamThread() {}
77 protected:
79 /// Working method which should be overridden.
80 virtual void SAL_CALL run() {
81 for( int i = 0 ; i < m_iMax ; i ++ ) {
82 m_output->writeBytes( createIntSeq(i) );
84 m_output->closeOutput();
87 /** Called when run() is done.
88 * You might want to override it to do some cleanup.
90 virtual void SAL_CALL onTerminated()
92 delete this;
96 private:
98 Reference < XOutputStream > m_output;
99 int m_iMax;
104 class OPipeTest : public WeakImplHelper1 < XSimpleTest >
106 public:
107 OPipeTest( const Reference< XMultiServiceFactory > & rFactory );
108 ~OPipeTest();
110 public: // implementation names
111 static Sequence< OUString > getSupportedServiceNames_Static(void) throw();
112 static OUString getImplementationName_Static() throw();
114 public:
115 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
116 throw ( IllegalArgumentException, RuntimeException) ;
118 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
119 const Reference < XInterface >& TestObject,
120 sal_Int32 hTestHandle)
121 throw ( IllegalArgumentException,
122 RuntimeException);
124 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException) ;
125 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException) ;
126 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException);
127 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException);
129 private:
130 void testSimple( const Reference < XInterface > & );
131 void testBufferResizing( const Reference < XInterface > & );
132 void testMultithreading( const Reference < XInterface > & );
134 private:
135 Sequence<Any> m_seqExceptions;
136 Sequence<OUString> m_seqErrors;
137 Sequence<OUString> m_seqWarnings;
143 OPipeTest::OPipeTest( const Reference< XMultiServiceFactory > &rFactory )
148 OPipeTest::~OPipeTest()
155 void OPipeTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject )
156 throw ( IllegalArgumentException,
157 RuntimeException)
159 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
160 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
161 if( info.is() )
163 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
164 ERROR_ASSERT( ! info->supportsService(
165 OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb") ) ), "XServiceInfo test failed" );
171 sal_Int32 OPipeTest::test(
172 const OUString& TestName,
173 const Reference < XInterface >& TestObject,
174 sal_Int32 hTestHandle)
175 throw ( IllegalArgumentException, RuntimeException)
177 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) == TestName ) {
180 if( 0 == hTestHandle ) {
181 testInvariant( TestName , TestObject );
183 else if( 1 == hTestHandle ) {
184 testSimple( TestObject );
186 else if( 2 == hTestHandle ) {
187 testBufferResizing( TestObject );
189 else if( 3 == hTestHandle ) {
190 testMultithreading( TestObject );
193 catch( const Exception & e )
195 OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
196 BUILD_ERROR( 0 , s.getStr() );
198 catch( ... )
200 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
203 hTestHandle ++;
205 if( 4 == hTestHandle )
207 // all tests finished.
208 hTestHandle = -1;
211 else {
212 throw IllegalArgumentException();
214 return hTestHandle;
219 sal_Bool OPipeTest::testPassed(void) throw (RuntimeException)
221 return m_seqErrors.getLength() == 0;
225 Sequence< OUString > OPipeTest::getErrors(void) throw (RuntimeException)
227 return m_seqErrors;
231 Sequence< Any > OPipeTest::getErrorExceptions(void) throw (RuntimeException)
233 return m_seqExceptions;
237 Sequence< OUString > OPipeTest::getWarnings(void) throw (RuntimeException)
239 return m_seqWarnings;
243 /***
244 * the test methods
246 ****/
249 void OPipeTest::testSimple( const Reference < XInterface > &r )
252 Reference< XInputStream > input( r , UNO_QUERY );
253 Reference < XOutputStream > output( r , UNO_QUERY );
255 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
256 ERROR_ASSERT( output.is() , "queryInterface onXOutputStream failed" );
258 // basic read/write
259 Sequence<sal_Int8> seqWrite = createSeq( "Hallo, du Ei !" );
261 Sequence<sal_Int8> seqRead;
262 for( int i = 0 ; i < 5000 ; i ++ ) {
263 output->writeBytes( seqWrite );
264 input->readBytes( seqRead , input->available() );
266 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
267 "error during read/write/skip" );
268 ERROR_ASSERT( 0 == input->available() ,
269 "error during read/write/skip" );
271 // available shouldn't return a negative value
272 input->skipBytes( seqWrite.getLength() - 5 );
273 ERROR_ASSERT( 0 == input->available() , "wrong available after skip" );
275 // 5 bytes should be available
276 output->writeBytes( seqWrite );
277 ERROR_ASSERT( 5 == input->available() , "wrong available after skip/write " );
279 input->readBytes( seqRead , 5 );
280 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
281 (char*) &( seqWrite.getArray()[seqWrite.getLength()-5] ) ),
282 "write/read mismatich" );
286 output->writeBytes( seqWrite );
287 ERROR_ASSERT( seqWrite.getLength() == input->available(), "wrong available() after write" );
289 ERROR_ASSERT( 10 == input->readSomeBytes( seqRead , 10 ) , "maximal number of bytes ignored" );
290 ERROR_ASSERT( seqWrite.getLength() -10 == input->readSomeBytes( seqRead , 100 ) ,
291 "something wrong with readSomeBytes" );
294 output->closeOutput();
295 try{
296 output->writeBytes( Sequence<sal_Int8> (100) );
297 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
299 catch (IOException & )
303 ERROR_ASSERT(! input->readBytes( seqRead , 1 ), "eof not found !" );
305 input->closeInput();
308 input->readBytes( seqRead , 1 );
309 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
311 catch( IOException & ) {
316 input->available( );
317 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
319 catch( IOException & )
325 input->skipBytes(42 );
326 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
328 catch( IOException & )
334 void OPipeTest::testBufferResizing( const Reference < XInterface > &r )
336 int i;
337 int iMax = 20000;
338 Reference< XInputStream > input( r , UNO_QUERY );
339 Reference < XOutputStream > output( r , UNO_QUERY );
341 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
342 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
344 Sequence<sal_Int8> seqRead;
346 // this is just to better check the
347 // internal buffers
348 output->writeBytes( Sequence<sal_Int8>(100) );
349 Sequence< sal_Int8 > dummy;
350 input->readBytes( dummy , 100);
352 for( i = 0 ; i < iMax ; i ++ ) {
353 output->writeBytes( createIntSeq( i ) );
356 for( i = 0 ; i < iMax ; i ++ ) {
357 input->readBytes( seqRead, createIntSeq(i).getLength() );
358 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
359 (char*) createIntSeq(i).getArray() ) ,
360 "written/read mismatch\n" );
363 output->closeOutput();
364 ERROR_ASSERT( ! input->readBytes( seqRead , 1 ) , "eof not reached !" );
365 input->closeInput();
370 void OPipeTest::testMultithreading( const Reference < XInterface > &r )
373 int i;
374 int iMax = 30000;
376 Reference< XInputStream > input( r , UNO_QUERY );
377 Reference < XOutputStream > output( r , UNO_QUERY );
379 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
380 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
382 Sequence<sal_Int8> seqRead;
384 // deletes itself
385 Thread *p = new WriteToStreamThread( output, iMax );
387 ERROR_ASSERT( p , "couldn't create thread for testing !\n" );
389 p->create();
391 for( i = 0 ; sal_True ; i ++ ) {
392 if( 0 == input->readBytes( seqRead, createIntSeq(i).getLength() ) ) {
393 // eof reached !
394 break;
397 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
398 (char*) createIntSeq(i).getArray() ) ,
399 "written/read mismatch\n" );
402 ERROR_ASSERT( i == iMax , "less elements read than written !");
403 input->closeInput();
409 * for external binding
413 Reference < XInterface > SAL_CALL OPipeTest_CreateInstance( const Reference< XMultiServiceFactory> & rSMgr ) throw (Exception)
415 OPipeTest *p = new OPipeTest( rSMgr );
416 Reference< XInterface > x ( (static_cast< OWeakObject * >(p)) );
417 return x;
422 Sequence<OUString> OPipeTest_getSupportedServiceNames(void) throw()
424 Sequence<OUString> aRet(1);
425 aRet.getArray()[0] = OPipeTest_getServiceName();
427 return aRet;
430 OUString OPipeTest_getServiceName() throw()
432 return OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
435 OUString OPipeTest_getImplementationName() throw()
437 return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */