bump product version to 4.1.6.2
[LibreOffice.git] / extensions / test / stm / pipetest.cxx
blob563cbda1907451e87550c5fde311368d2ec8ee89
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 .
20 #include <smart/com/sun/star/test/XSimpleTest.hxx>
21 #include <smart/com/sun/star/io/XOutputStream.hxx>
22 #include <smart/com/sun/star/io/XInputStream.hxx>
24 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
26 #include <usr/factoryhlp.hxx>
28 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
29 #include <usr/weak.hxx> // OWeakObject
31 #include <osl/conditn.hxx>
32 #include <osl/mutex.hxx>
33 #include <osl/thread.hxx>
35 #include <string.h>
37 #include "testfactreg.hxx"
38 #define IMPLEMENTATION_NAME L"test.com.sun.star.comp.extensions.stm.Pipe"
39 #define SERVICE_NAME L"test.com.sun.star.io.Pipe"
41 using namespace usr;
43 class WriteToStreamThread :
44 public osl::Thread
47 public:
49 WriteToStreamThread( XOutputStreamRef xOutput , int iMax )
51 m_output = xOutput;
52 m_iMax = iMax;
55 virtual ~WriteToStreamThread() {}
58 protected:
60 /// Working method which should be overridden.
61 virtual void run() {
62 for( int i = 0 ; i < m_iMax ; i ++ ) {
63 m_output->writeBytes( createIntSeq(i) );
65 m_output->closeOutput();
68 /** Called when run() is done.
69 * You might want to override it to do some cleanup.
71 virtual void onTerminated()
73 delete this;
77 private:
79 XOutputStreamRef m_output;
80 int m_iMax;
85 class OPipeTest :
86 public XSimpleTest,
87 public OWeakObject
89 public:
90 OPipeTest( const XMultiServiceFactoryRef & rFactory );
91 ~OPipeTest();
93 public: // refcounting
94 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
95 void acquire() { OWeakObject::acquire(); }
96 void release() { OWeakObject::release(); }
97 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
99 public: // implementation names
100 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS(());
101 static UString getImplementationName_Static() THROWS(());
103 public:
104 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
105 THROWS( ( IllegalArgumentException,
106 UsrSystemException) );
108 virtual INT32 test( const UString& TestName,
109 const XInterfaceRef& TestObject,
110 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
111 UsrSystemException) );
113 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
114 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
115 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
116 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
118 private:
119 void testSimple( const XInterfaceRef & );
120 void testBufferResizing( const XInterfaceRef & );
121 void testMultithreading( const XInterfaceRef & );
123 private:
124 Sequence<UsrAny> m_seqExceptions;
125 Sequence<UString> m_seqErrors;
126 Sequence<UString> m_seqWarnings;
132 OPipeTest::OPipeTest( const XMultiServiceFactoryRef &rFactory )
137 OPipeTest::~OPipeTest()
143 BOOL OPipeTest::queryInterface( Uik uik , XInterfaceRef &rOut )
145 if( XSimpleTest::getSmartUik() == uik ) {
146 rOut = (XSimpleTest *) this;
148 else {
149 return OWeakObject::queryInterface( uik , rOut );
151 return TRUE;
155 void OPipeTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
156 THROWS( ( IllegalArgumentException,
157 UsrSystemException) )
159 XServiceInfoRef info( TestObject, USR_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( L"bla bluzb" ) , "XServiceInfo test failed" );
170 INT32 OPipeTest::test( const UString& TestName,
171 const XInterfaceRef& TestObject,
172 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
173 UsrSystemException) )
175 if( L"com.sun.star.io.Pipe" == TestName ) {
176 try {
177 if( 0 == hTestHandle ) {
178 testInvariant( TestName , TestObject );
180 else if( 1 == hTestHandle ) {
181 testSimple( TestObject );
183 else if( 2 == hTestHandle ) {
184 testBufferResizing( TestObject );
186 else if( 3 == hTestHandle ) {
187 testMultithreading( TestObject );
190 catch( Exception& e ) {
191 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
193 catch(...) {
194 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
197 hTestHandle ++;
199 if( 4 == hTestHandle ) {
200 // all tests finished.
201 hTestHandle = -1;
204 else {
205 THROW( IllegalArgumentException() );
207 return hTestHandle;
212 BOOL OPipeTest::testPassed(void) THROWS( (UsrSystemException) )
214 return m_seqErrors.getLen() == 0;
218 Sequence< UString > OPipeTest::getErrors(void) THROWS( (UsrSystemException) )
220 return m_seqErrors;
224 Sequence< UsrAny > OPipeTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
226 return m_seqExceptions;
230 Sequence< UString > OPipeTest::getWarnings(void) THROWS( (UsrSystemException) )
232 return m_seqWarnings;
236 /***
237 * the test methods
239 ****/
242 void OPipeTest::testSimple( const XInterfaceRef &r )
245 XInputStreamRef input( r , USR_QUERY );
246 XOutputStreamRef output( r , USR_QUERY );
248 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
249 ERROR_ASSERT( output.is() , "queryInterface onXOutputStream failed" );
251 // basic read/write
252 Sequence<BYTE> seqWrite = createSeq( "Hallo, du Ei !" );
254 Sequence<BYTE> seqRead;
255 for( int i = 0 ; i < 5000 ; i ++ ) {
256 output->writeBytes( seqWrite );
257 input->readBytes( seqRead , input->available() );
259 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
260 "error during read/write/skip" );
261 ERROR_ASSERT( 0 == input->available() ,
262 "error during read/write/skip" );
264 // available shouldn't return a negative value
265 input->skipBytes( seqWrite.getLen() - 5 );
266 ERROR_ASSERT( 0 == input->available() , "wrong available after skip" );
268 // 5 bytes should be available
269 output->writeBytes( seqWrite );
270 ERROR_ASSERT( 5 == input->available() , "wrong available after skip/write " );
272 input->readBytes( seqRead , 5 );
273 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
274 (char*) &( seqWrite.getArray()[seqWrite.getLen()-5] ) ),
275 "write/read mismatich" );
279 output->writeBytes( seqWrite );
280 ERROR_ASSERT( seqWrite.getLen() == input->available(), "wrong available() after write" );
282 ERROR_ASSERT( 10 == input->readSomeBytes( seqRead , 10 ) , "maximal number of bytes ignored" );
283 ERROR_ASSERT( seqWrite.getLen() -10 == input->readSomeBytes( seqRead , 100 ) ,
284 "something wrong with readSomeBytes" );
287 output->closeOutput();
288 try {
289 output->writeBytes( Sequence<BYTE> (100) );
290 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
292 catch (IOException& e ) {
293 e; // just to suppress warning during compile
296 ERROR_ASSERT(! input->readBytes( seqRead , 1 ), "eof not found !" );
298 input->closeInput();
299 try {
300 input->readBytes( seqRead , 1 );
301 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
303 catch( IOException& e ) {
304 e; // just to suppress warning during compile
309 void OPipeTest::testBufferResizing( const XInterfaceRef &r )
312 int iMax = 20000;
313 XInputStreamRef input( r , USR_QUERY );
314 XOutputStreamRef output( r , USR_QUERY );
316 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
317 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
319 Sequence<BYTE> seqRead;
321 // this is just to better check the
322 // internal buffers
323 output->writeBytes( Sequence<BYTE>(100) );
324 input->readBytes( Sequence<BYTE>() , 100);
326 for( int i = 0 ; i < iMax ; i ++ ) {
327 output->writeBytes( createIntSeq( i ) );
330 for( i = 0 ; i < iMax ; i ++ ) {
331 input->readBytes( seqRead, createIntSeq(i).getLen() );
332 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
333 (char*) createIntSeq(i).getArray() ) ,
334 "written/read mismatch\n" );
337 output->closeOutput();
338 ERROR_ASSERT( ! input->readBytes( seqRead , 1 ) , "eof not reached !" );
339 input->closeInput();
344 void OPipeTest::testMultithreading( const XInterfaceRef &r )
348 int iMax = 30000;
350 XInputStreamRef input( r , USR_QUERY );
351 XOutputStreamRef output( r , USR_QUERY );
353 ERROR_ASSERT( input.is() , "queryInterface on XInputStream failed" );
354 ERROR_ASSERT( output.is() , "queryInterface on XOutputStream failed" );
356 Sequence<BYTE> seqRead;
358 // deletes itself
359 osl::Thread *p = new WriteToStreamThread( output, iMax );
361 ERROR_ASSERT( p , "couldn't create thread for testing !\n" );
363 p->create();
365 for(int i = 0 ; TRUE ; i ++ ) {
366 if( 0 == input->readBytes( seqRead, createIntSeq(i).getLen() ) ) {
367 // eof reached !
368 break;
371 ERROR_ASSERT( ! strcmp( (char*) seqRead.getArray() ,
372 (char*) createIntSeq(i).getArray() ) ,
373 "written/read mismatch\n" );
376 ERROR_ASSERT( i == iMax , "less elements read than written !");
377 input->closeInput();
380 /* {
381 try {
382 XInterfaceRef x = xSMgr->createInstance( strService );
384 XInputStreamRef input( x , USR_QUERY );
385 XOutputStreamRef output( x , USR_QUERY );
387 OSL_ASSERT( output.is() );
388 while( TRUE ) {
389 // basic read/write
390 Sequence<BYTE> seqWrite( 500 );
391 output->writeBytes( seqWrite );
395 catch( IOException& e ) {
396 printf( "%s %s\n" , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() ,
397 UStringToString( e.Message , CHARSET_SYSTEM ).GetCharStr() );
406 * for external binding
410 XInterfaceRef OPipeTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
412 OPipeTest *p = new OPipeTest( rSMgr );
413 XInterfaceRef xService = *p;
414 return xService;
419 Sequence<UString> OPipeTest_getSupportedServiceNames(void) THROWS(())
421 Sequence<UString> aRet(1);
422 aRet.getArray()[0] = OPipeTest_getImplementationName();
424 return aRet;
427 UString OPipeTest_getServiceName() THROWS(())
429 return SERVICE_NAME;
432 UString OPipeTest_getImplementationName() THROWS(())
434 return IMPLEMENTATION_NAME;
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */