merge the formfield patch from ooo-build
[ooovba.git] / extensions / test / stm / pipetest.cxx
blob0ee157c533567b3995fea32648b72812098478e9
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: pipetest.cxx,v $
10 * $Revision: 1.6 $
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_extensions.hxx"
33 #include <smart/com/sun/star/test/XSimpleTest.hxx>
34 #include <smart/com/sun/star/io/XOutputStream.hxx>
35 #include <smart/com/sun/star/io/XInputStream.hxx>
37 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
39 #include <usr/factoryhlp.hxx>
41 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
42 #include <usr/weak.hxx> // OWeakObject
44 #include <vos/conditn.hxx>
45 #include <vos/mutex.hxx>
46 #include <vos/thread.hxx>
48 #if OSL_DEBUG_LEVEL == 0
49 #define NDEBUG
50 #endif
51 #include <assert.h>
52 #include <string.h>
54 #include "testfactreg.hxx"
55 #define IMPLEMENTATION_NAME L"test.com.sun.star.comp.extensions.stm.Pipe"
56 #define SERVICE_NAME L"test.com.sun.star.io.Pipe"
58 #ifndef _VOS_NO_NAMESPACE
59 using namespace vos;
60 using namespace usr;
61 #endif
63 class WriteToStreamThread :
64 public OThread
67 public:
69 WriteToStreamThread( XOutputStreamRef xOutput , int iMax )
71 m_output = xOutput;
72 m_iMax = iMax;
75 virtual ~WriteToStreamThread() {}
78 protected:
80 /// Working method which should be overridden.
81 virtual void run() {
82 for( int i = 0 ; i < m_iMax ; i ++ ) {
83 m_output->writeBytes( createIntSeq(i) );
85 m_output->closeOutput();
88 /** Called when run() is done.
89 * You might want to override it to do some cleanup.
91 virtual void onTerminated()
93 delete this;
97 private:
99 XOutputStreamRef m_output;
100 int m_iMax;
105 class OPipeTest :
106 public XSimpleTest,
107 public OWeakObject
109 public:
110 OPipeTest( const XMultiServiceFactoryRef & rFactory );
111 ~OPipeTest();
113 public: // refcounting
114 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
115 void acquire() { OWeakObject::acquire(); }
116 void release() { OWeakObject::release(); }
117 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
119 public: // implementation names
120 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS( () );
121 static UString getImplementationName_Static() THROWS( () );
123 public:
124 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
125 THROWS( ( IllegalArgumentException,
126 UsrSystemException) );
128 virtual INT32 test( const UString& TestName,
129 const XInterfaceRef& TestObject,
130 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
131 UsrSystemException) );
133 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
134 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
135 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
136 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
138 private:
139 void testSimple( const XInterfaceRef & );
140 void testBufferResizing( const XInterfaceRef & );
141 void testMultithreading( const XInterfaceRef & );
143 private:
144 Sequence<UsrAny> m_seqExceptions;
145 Sequence<UString> m_seqErrors;
146 Sequence<UString> m_seqWarnings;
152 OPipeTest::OPipeTest( const XMultiServiceFactoryRef &rFactory )
157 OPipeTest::~OPipeTest()
163 BOOL OPipeTest::queryInterface( Uik uik , XInterfaceRef &rOut )
165 if( XSimpleTest::getSmartUik() == uik ) {
166 rOut = (XSimpleTest *) this;
168 else {
169 return OWeakObject::queryInterface( uik , rOut );
171 return TRUE;
175 void OPipeTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
176 THROWS( ( IllegalArgumentException,
177 UsrSystemException) )
179 XServiceInfoRef info( TestObject, USR_QUERY );
180 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
181 if( info.is() )
183 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
184 ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
190 INT32 OPipeTest::test( const UString& TestName,
191 const XInterfaceRef& TestObject,
192 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
193 UsrSystemException) )
195 if( L"com.sun.star.io.Pipe" == TestName ) {
196 try {
197 if( 0 == hTestHandle ) {
198 testInvariant( TestName , TestObject );
200 else if( 1 == hTestHandle ) {
201 testSimple( TestObject );
203 else if( 2 == hTestHandle ) {
204 testBufferResizing( TestObject );
206 else if( 3 == hTestHandle ) {
207 testMultithreading( TestObject );
210 catch( Exception& e ) {
211 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
213 catch(...) {
214 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
217 hTestHandle ++;
219 if( 4 == hTestHandle ) {
220 // all tests finished.
221 hTestHandle = -1;
224 else {
225 THROW( IllegalArgumentException() );
227 return hTestHandle;
232 BOOL OPipeTest::testPassed(void) THROWS( (UsrSystemException) )
234 return m_seqErrors.getLen() == 0;
238 Sequence< UString > OPipeTest::getErrors(void) THROWS( (UsrSystemException) )
240 return m_seqErrors;
244 Sequence< UsrAny > OPipeTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
246 return m_seqExceptions;
250 Sequence< UString > OPipeTest::getWarnings(void) THROWS( (UsrSystemException) )
252 return m_seqWarnings;
256 /***
257 * the test methods
259 ****/
262 void OPipeTest::testSimple( const XInterfaceRef &r )
265 XInputStreamRef input( r , USR_QUERY );
266 XOutputStreamRef output( r , USR_QUERY );
268 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
269 ERROR_ASSERT( output.is() , "queryInterface onXOutputStream failed" );
271 // basic read/write
272 Sequence<BYTE> seqWrite = createSeq( "Hallo, du Ei !" );
274 Sequence<BYTE> seqRead;
275 for( int i = 0 ; i < 5000 ; i ++ ) {
276 output->writeBytes( seqWrite );
277 input->readBytes( seqRead , input->available() );
279 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
280 "error during read/write/skip" );
281 ERROR_ASSERT( 0 == input->available() ,
282 "error during read/write/skip" );
284 // available shouldn't return a negative value
285 input->skipBytes( seqWrite.getLen() - 5 );
286 ERROR_ASSERT( 0 == input->available() , "wrong available after skip" );
288 // 5 bytes should be available
289 output->writeBytes( seqWrite );
290 ERROR_ASSERT( 5 == input->available() , "wrong available after skip/write " );
292 input->readBytes( seqRead , 5 );
293 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
294 (char*) &( seqWrite.getArray()[seqWrite.getLen()-5] ) ),
295 "write/read mismatich" );
299 output->writeBytes( seqWrite );
300 ERROR_ASSERT( seqWrite.getLen() == input->available(), "wrong available() after write" );
302 ERROR_ASSERT( 10 == input->readSomeBytes( seqRead , 10 ) , "maximal number of bytes ignored" );
303 ERROR_ASSERT( seqWrite.getLen() -10 == input->readSomeBytes( seqRead , 100 ) ,
304 "something wrong with readSomeBytes" );
307 output->closeOutput();
308 try {
309 output->writeBytes( Sequence<BYTE> (100) );
310 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
312 catch (IOException& e ) {
313 e; // just to suppress warning during compile
316 ERROR_ASSERT(! input->readBytes( seqRead , 1 ), "eof not found !" );
318 input->closeInput();
319 try {
320 input->readBytes( seqRead , 1 );
321 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
323 catch( IOException& e ) {
324 e; // just to suppress warning during compile
329 void OPipeTest::testBufferResizing( const XInterfaceRef &r )
332 int iMax = 20000;
333 XInputStreamRef input( r , USR_QUERY );
334 XOutputStreamRef output( r , USR_QUERY );
336 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
337 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
339 Sequence<BYTE> seqRead;
341 // this is just to better check the
342 // internal buffers
343 output->writeBytes( Sequence<BYTE>(100) );
344 input->readBytes( Sequence<BYTE>() , 100);
346 for( int i = 0 ; i < iMax ; i ++ ) {
347 output->writeBytes( createIntSeq( i ) );
350 for( i = 0 ; i < iMax ; i ++ ) {
351 input->readBytes( seqRead, createIntSeq(i).getLen() );
352 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
353 (char*) createIntSeq(i).getArray() ) ,
354 "written/read mismatch\n" );
357 output->closeOutput();
358 ERROR_ASSERT( ! input->readBytes( seqRead , 1 ) , "eof not reached !" );
359 input->closeInput();
364 void OPipeTest::testMultithreading( const XInterfaceRef &r )
368 int iMax = 30000;
370 XInputStreamRef input( r , USR_QUERY );
371 XOutputStreamRef output( r , USR_QUERY );
373 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
374 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
376 Sequence<BYTE> seqRead;
378 // deletes itself
379 OThread *p = new WriteToStreamThread( output, iMax );
381 ERROR_ASSERT( p , "couldn't create thread for testing !\n" );
383 p->create();
385 for(int i = 0 ; TRUE ; i ++ ) {
386 if( 0 == input->readBytes( seqRead, createIntSeq(i).getLen() ) ) {
387 // eof reached !
388 break;
391 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
392 (char*) createIntSeq(i).getArray() ) ,
393 "written/read mismatch\n" );
396 ERROR_ASSERT( i == iMax , "less elements read than written !");
397 input->closeInput();
400 /* {
401 try {
402 XInterfaceRef x = xSMgr->createInstance( strService );
404 XInputStreamRef input( x , USR_QUERY );
405 XOutputStreamRef output( x , USR_QUERY );
407 assert( output.is() );
408 while( TRUE ) {
409 // basic read/write
410 Sequence<BYTE> seqWrite( 500 );
411 output->writeBytes( seqWrite );
415 catch( IOException& e ) {
416 printf( "%s %s\n" , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() ,
417 UStringToString( e.Message , CHARSET_SYSTEM ).GetCharStr() );
426 * for external binding
430 XInterfaceRef OPipeTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
432 OPipeTest *p = new OPipeTest( rSMgr );
433 XInterfaceRef xService = *p;
434 return xService;
439 Sequence<UString> OPipeTest_getSupportedServiceNames(void) THROWS( () )
441 Sequence<UString> aRet(1);
442 aRet.getArray()[0] = OPipeTest_getImplementationName();
444 return aRet;
447 UString OPipeTest_getServiceName() THROWS( () )
449 return SERVICE_NAME;
452 UString OPipeTest_getImplementationName() THROWS( () )
454 return IMPLEMENTATION_NAME;