bump product version to 4.1.6.2
[LibreOffice.git] / io / test / stm / pipetest.cxx
blob44f6244a09156d93966964977b55d8230781efae
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 .
21 #include <com/sun/star/test/XSimpleTest.hpp>
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <com/sun/star/io/XOutputStream.hpp>
24 #include <com/sun/star/io/XConnectable.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/implbase1.hxx> // OWeakObject
33 #include <osl/conditn.hxx>
34 #include <osl/mutex.hxx>
35 #include <osl/thread.hxx>
37 #include <string.h>
39 using namespace ::rtl;
40 using namespace ::osl;
41 using namespace ::cppu;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::io;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::test;
46 // streams
48 #include "testfactreg.hxx"
49 #define IMPLEMENTATION_NAME "test.com.sun.star.comp.extensions.stm.Pipe"
50 #define SERVICE_NAME "test.com.sun.star.io.Pipe"
53 class WriteToStreamThread :
54 public Thread
57 public:
59 WriteToStreamThread( Reference< XOutputStream > xOutput , int iMax )
61 m_output = xOutput;
62 m_iMax = iMax;
65 virtual ~WriteToStreamThread() {}
68 protected:
70 /// Working method which should be overridden.
71 virtual void SAL_CALL run() {
72 for( int i = 0 ; i < m_iMax ; i ++ ) {
73 m_output->writeBytes( createIntSeq(i) );
75 m_output->closeOutput();
78 /** Called when run() is done.
79 * You might want to override it to do some cleanup.
81 virtual void SAL_CALL onTerminated()
83 delete this;
87 private:
89 Reference < XOutputStream > m_output;
90 int m_iMax;
95 class OPipeTest : public WeakImplHelper1 < XSimpleTest >
97 public:
98 OPipeTest( const Reference< XMultiServiceFactory > & rFactory );
99 ~OPipeTest();
101 public: // implementation names
102 static Sequence< OUString > getSupportedServiceNames_Static(void) throw();
103 static OUString getImplementationName_Static() throw();
105 public:
106 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
107 throw ( IllegalArgumentException, RuntimeException) ;
109 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
110 const Reference < XInterface >& TestObject,
111 sal_Int32 hTestHandle)
112 throw ( IllegalArgumentException,
113 RuntimeException);
115 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException) ;
116 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException) ;
117 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException);
118 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException);
120 private:
121 void testSimple( const Reference < XInterface > & );
122 void testBufferResizing( const Reference < XInterface > & );
123 void testMultithreading( const Reference < XInterface > & );
125 private:
126 Sequence<Any> m_seqExceptions;
127 Sequence<OUString> m_seqErrors;
128 Sequence<OUString> m_seqWarnings;
134 OPipeTest::OPipeTest( const Reference< XMultiServiceFactory > &rFactory )
139 OPipeTest::~OPipeTest()
146 void OPipeTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject )
147 throw ( IllegalArgumentException,
148 RuntimeException)
150 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
151 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
152 if( info.is() )
154 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
155 ERROR_ASSERT( ! info->supportsService(
156 OUString("bla bluzb") ), "XServiceInfo test failed" );
162 sal_Int32 OPipeTest::test(
163 const OUString& TestName,
164 const Reference < XInterface >& TestObject,
165 sal_Int32 hTestHandle)
166 throw ( IllegalArgumentException, RuntimeException)
168 if( OUString("com.sun.star.io.Pipe") == TestName ) {
171 if( 0 == hTestHandle ) {
172 testInvariant( TestName , TestObject );
174 else if( 1 == hTestHandle ) {
175 testSimple( TestObject );
177 else if( 2 == hTestHandle ) {
178 testBufferResizing( TestObject );
180 else if( 3 == hTestHandle ) {
181 testMultithreading( TestObject );
184 catch( const Exception & e )
186 OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
187 BUILD_ERROR( 0 , s.getStr() );
189 catch( ... )
191 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
194 hTestHandle ++;
196 if( 4 == hTestHandle )
198 // all tests finished.
199 hTestHandle = -1;
202 else {
203 throw IllegalArgumentException();
205 return hTestHandle;
210 sal_Bool OPipeTest::testPassed(void) throw (RuntimeException)
212 return m_seqErrors.getLength() == 0;
216 Sequence< OUString > OPipeTest::getErrors(void) throw (RuntimeException)
218 return m_seqErrors;
222 Sequence< Any > OPipeTest::getErrorExceptions(void) throw (RuntimeException)
224 return m_seqExceptions;
228 Sequence< OUString > OPipeTest::getWarnings(void) throw (RuntimeException)
230 return m_seqWarnings;
234 /***
235 * the test methods
237 ****/
240 void OPipeTest::testSimple( const Reference < XInterface > &r )
243 Reference< XInputStream > input( r , UNO_QUERY );
244 Reference < XOutputStream > output( r , UNO_QUERY );
246 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
247 ERROR_ASSERT( output.is() , "queryInterface onXOutputStream failed" );
249 // basic read/write
250 Sequence<sal_Int8> seqWrite = createSeq( "Hallo, du Ei !" );
252 Sequence<sal_Int8> seqRead;
253 for( int i = 0 ; i < 5000 ; i ++ ) {
254 output->writeBytes( seqWrite );
255 input->readBytes( seqRead , input->available() );
257 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
258 "error during read/write/skip" );
259 ERROR_ASSERT( 0 == input->available() ,
260 "error during read/write/skip" );
262 // available shouldn't return a negative value
263 input->skipBytes( seqWrite.getLength() - 5 );
264 ERROR_ASSERT( 0 == input->available() , "wrong available after skip" );
266 // 5 bytes should be available
267 output->writeBytes( seqWrite );
268 ERROR_ASSERT( 5 == input->available() , "wrong available after skip/write " );
270 input->readBytes( seqRead , 5 );
271 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
272 (char*) &( seqWrite.getArray()[seqWrite.getLength()-5] ) ),
273 "write/read mismatich" );
277 output->writeBytes( seqWrite );
278 ERROR_ASSERT( seqWrite.getLength() == input->available(), "wrong available() after write" );
280 ERROR_ASSERT( 10 == input->readSomeBytes( seqRead , 10 ) , "maximal number of bytes ignored" );
281 ERROR_ASSERT( seqWrite.getLength() -10 == input->readSomeBytes( seqRead , 100 ) ,
282 "something wrong with readSomeBytes" );
285 output->closeOutput();
286 try{
287 output->writeBytes( Sequence<sal_Int8> (100) );
288 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
290 catch (IOException & )
294 ERROR_ASSERT(! input->readBytes( seqRead , 1 ), "eof not found !" );
296 input->closeInput();
299 input->readBytes( seqRead , 1 );
300 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
302 catch( IOException & ) {
307 input->available( );
308 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
310 catch( IOException & )
316 input->skipBytes(42 );
317 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
319 catch( IOException & )
325 void OPipeTest::testBufferResizing( const Reference < XInterface > &r )
327 int i;
328 int iMax = 20000;
329 Reference< XInputStream > input( r , UNO_QUERY );
330 Reference < XOutputStream > output( r , UNO_QUERY );
332 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
333 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
335 Sequence<sal_Int8> seqRead;
337 // this is just to better check the
338 // internal buffers
339 output->writeBytes( Sequence<sal_Int8>(100) );
340 Sequence< sal_Int8 > dummy;
341 input->readBytes( dummy , 100);
343 for( i = 0 ; i < iMax ; i ++ ) {
344 output->writeBytes( createIntSeq( i ) );
347 for( i = 0 ; i < iMax ; i ++ ) {
348 input->readBytes( seqRead, createIntSeq(i).getLength() );
349 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
350 (char*) createIntSeq(i).getArray() ) ,
351 "written/read mismatch\n" );
354 output->closeOutput();
355 ERROR_ASSERT( ! input->readBytes( seqRead , 1 ) , "eof not reached !" );
356 input->closeInput();
361 void OPipeTest::testMultithreading( const Reference < XInterface > &r )
364 int i;
365 int iMax = 30000;
367 Reference< XInputStream > input( r , UNO_QUERY );
368 Reference < XOutputStream > output( r , UNO_QUERY );
370 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
371 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
373 Sequence<sal_Int8> seqRead;
375 // deletes itself
376 Thread *p = new WriteToStreamThread( output, iMax );
378 ERROR_ASSERT( p , "couldn't create thread for testing !\n" );
380 p->create();
382 for( i = 0 ; sal_True ; i ++ ) {
383 if( 0 == input->readBytes( seqRead, createIntSeq(i).getLength() ) ) {
384 // eof reached !
385 break;
388 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
389 (char*) createIntSeq(i).getArray() ) ,
390 "written/read mismatch\n" );
393 ERROR_ASSERT( i == iMax , "less elements read than written !");
394 input->closeInput();
400 * for external binding
404 Reference < XInterface > SAL_CALL OPipeTest_CreateInstance( const Reference< XMultiServiceFactory> & rSMgr ) throw (Exception)
406 OPipeTest *p = new OPipeTest( rSMgr );
407 Reference< XInterface > x ( (static_cast< OWeakObject * >(p)) );
408 return x;
413 Sequence<OUString> OPipeTest_getSupportedServiceNames(void) throw()
415 Sequence<OUString> aRet(1);
416 aRet.getArray()[0] = OPipeTest_getServiceName();
418 return aRet;
421 OUString OPipeTest_getServiceName() throw()
423 return OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
426 OUString OPipeTest_getImplementationName() throw()
428 return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
431 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */