merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / test / stm / marktest.cxx
blob019de72cf46c2f637d011a3be39405eb13758d1e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include <smart/com/sun/star/test/XSimpleTest.hxx>
31 #include <smart/com/sun/star/io/XMarkableStream.hxx>
32 #include <smart/com/sun/star/io/XActiveDataSink.hxx>
33 #include <smart/com/sun/star/io/XActiveDataSource.hxx>
34 #include <smart/com/sun/star/io/XConnectable.hxx>
36 #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 #include <string.h>
50 #include "testfactreg.hxx"
53 #ifndef _VOS_NO_NAMESPACE
54 using namespace vos;
55 using namespace usr;
56 #endif
60 class OMarkableOutputStreamTest :
61 public XSimpleTest,
62 public OWeakObject
64 public:
65 OMarkableOutputStreamTest( const XMultiServiceFactoryRef & rFactory );
66 ~OMarkableOutputStreamTest();
68 public: // refcounting
69 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
70 void acquire() { OWeakObject::acquire(); }
71 void release() { OWeakObject::release(); }
72 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
74 public: // implementation names
75 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS( () );
76 static UString getImplementationName_Static() THROWS( () );
78 public:
79 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
80 THROWS( ( IllegalArgumentException,
81 UsrSystemException) );
83 virtual INT32 test( const UString& TestName,
84 const XInterfaceRef& TestObject,
85 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
86 UsrSystemException) );
88 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
89 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
90 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
91 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
93 private:
94 void testSimple( const XOutputStreamRef &r, const XInputStreamRef &rInput );
96 private:
97 Sequence<UsrAny> m_seqExceptions;
98 Sequence<UString> m_seqErrors;
99 Sequence<UString> m_seqWarnings;
100 XMultiServiceFactoryRef m_rFactory;
104 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const XMultiServiceFactoryRef &rFactory )
105 : m_rFactory( rFactory )
110 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
116 BOOL OMarkableOutputStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
118 if( XSimpleTest::getSmartUik() == uik ) {
119 rOut = (XSimpleTest *) this;
121 else {
122 return OWeakObject::queryInterface( uik , rOut );
124 return TRUE;
128 void OMarkableOutputStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
129 THROWS( ( IllegalArgumentException,
130 UsrSystemException) )
132 XServiceInfoRef info( TestObject, USR_QUERY );
133 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
134 if( info.is() )
136 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
137 ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
142 INT32 OMarkableOutputStreamTest::test( const UString& TestName,
143 const XInterfaceRef& TestObject,
144 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
145 UsrSystemException) )
147 if( L"com.sun.star.io.MarkableOutputStream" == TestName ) {
148 try {
149 if( 0 == hTestHandle ) {
150 testInvariant( TestName , TestObject );
152 else {
153 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
154 XOutputStreamRef rPipeOutput( x , USR_QUERY );
155 XInputStreamRef rPipeInput( x , USR_QUERY );
157 XActiveDataSourceRef source( TestObject , USR_QUERY );
158 source->setOutputStream( rPipeOutput );
160 XOutputStreamRef rOutput( TestObject , USR_QUERY );
162 OSL_ASSERT( rPipeInput.is() );
163 OSL_ASSERT( rOutput.is() );
164 if( 1 == hTestHandle ) {
165 // checks usual streaming
166 testSimple( rOutput , rPipeInput );
171 catch( Exception& e ) {
172 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
174 catch(...) {
175 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
178 hTestHandle ++;
180 if( 2 == hTestHandle ) {
181 // all tests finished.
182 hTestHandle = -1;
185 else {
186 THROW( IllegalArgumentException() );
188 return hTestHandle;
193 BOOL OMarkableOutputStreamTest::testPassed(void) THROWS( (UsrSystemException) )
195 return m_seqErrors.getLen() == 0;
199 Sequence< UString > OMarkableOutputStreamTest::getErrors(void) THROWS( (UsrSystemException) )
201 return m_seqErrors;
205 Sequence< UsrAny > OMarkableOutputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
207 return m_seqExceptions;
211 Sequence< UString > OMarkableOutputStreamTest::getWarnings(void) THROWS( (UsrSystemException) )
213 return m_seqWarnings;
217 void OMarkableOutputStreamTest::testSimple( const XOutputStreamRef &rOutput ,
218 const XInputStreamRef &rInput )
220 XMarkableStreamRef rMarkable( rOutput , USR_QUERY );
222 ERROR_ASSERT( rMarkable.is() , "no MarkableStream implemented" );
224 // first check normal input/output facility
225 char pcStr[] = "Live long and prosper !";
227 Sequence<BYTE> seqWrite( strlen( pcStr )+1 );
228 memcpy( seqWrite.getArray() , pcStr , seqWrite.getLen() );
230 Sequence<BYTE> seqRead( seqWrite.getLen() );
232 int nMax = 10,i;
234 for( i = 0 ; i < nMax ; i ++ ) {
235 rOutput->writeBytes( seqWrite );
236 rInput->readBytes( seqRead , rInput->available() );
237 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
238 "error during read/write/skip" );
241 // Check buffer resizing
242 nMax = 3000;
243 for( i = 0 ; i < nMax ; i ++ ) {
244 rOutput->writeBytes( seqWrite );
247 for( i = 0 ; i < nMax ; i ++ ) {
248 rInput->readBytes( seqRead , seqWrite.getLen() );
249 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
250 "error during read/write" );
253 // Check creating marks !
254 INT32 nMark = rMarkable->createMark();
256 for( i = 0 ; i < nMax ; i ++ ) {
257 rOutput->writeBytes( seqWrite );
260 ERROR_ASSERT( 0 == rInput->available() , "bytes available though mark is holded" );
262 ERROR_ASSERT( nMax*seqWrite.getLen() == rMarkable->offsetToMark( nMark ) ,
263 "offsetToMark failure" );
265 rMarkable->deleteMark( nMark );
266 ERROR_ASSERT( nMax*seqWrite.getLen() == rInput->available(),"bytes are not available though mark has been deleted" );
268 rInput->skipBytes( nMax*seqWrite.getLen() );
269 ERROR_ASSERT( 0 == rInput->available(), "skip bytes failure" );
271 try {
272 rMarkable->jumpToMark( nMark );
273 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
275 catch ( IllegalArgumentException& e )
277 e;// ok, exception was thrown
280 // test putting marks not at the end of the stream!
281 ERROR_ASSERT( 0 == rInput->available(), "stream isn't clean" );
283 Sequence< BYTE > aByte(256);
285 for( i = 0 ; i < 256 ; i ++ )
287 aByte.getArray()[i] = i;
289 INT32 nMark1 = rMarkable->createMark();
291 rOutput->writeBytes( aByte );
292 rMarkable->jumpToMark( nMark1 );
293 aByte.realloc( 10 );
294 rOutput->writeBytes( aByte );
296 INT32 nMark2 = rMarkable->createMark( );
298 for( i = 0 ; i < 10 ; i ++ )
300 aByte.getArray()[i] = i+10;
303 rOutput->writeBytes( aByte );
305 // allow the bytes to be written !
306 rMarkable->jumpToFurthest();
307 rMarkable->deleteMark( nMark1 );
308 rMarkable->deleteMark( nMark2 );
310 ERROR_ASSERT( 256 == rInput->available(), "in between mark failure" );
311 rInput->readBytes( aByte ,256);
312 for( i = 0 ; i < 256 ; i ++ )
314 ERROR_ASSERT( i == aByte.getArray()[i] , "in between mark failure" );
319 // now a more extensive mark test !
320 Sequence<BYTE> as[4];
321 INT32 an[4];
323 for( i = 0 ; i < 4 ; i ++ ) {
324 as[i].realloc(1);
325 as[i].getArray()[0] = i;
326 an[i] = rMarkable->createMark();
327 rOutput->writeBytes( as[i] );
330 // check offset to mark
331 for( i = 0 ; i < 4 ; i ++ ) {
332 ERROR_ASSERT( rMarkable->offsetToMark( an[i] ) == 4-i , "offsetToMark failure" );
335 rMarkable->jumpToMark( an[1] );
336 ERROR_ASSERT( rMarkable->offsetToMark( an[3] ) == -2 , "offsetToMark failure" );
338 rMarkable->jumpToFurthest( );
339 ERROR_ASSERT( rMarkable->offsetToMark( an[0] ) == 4 , "offsetToMark failure" );
341 // now do a rewrite !
342 for( i = 0 ; i < 4 ; i ++ ) {
343 rMarkable->jumpToMark( an[3-i] );
344 rOutput->writeBytes( as[i] );
346 // NOTE : CursorPos 1
348 // now delete the marks !
349 for( i = 0 ; i < 4 ; i ++ ) {
350 rMarkable->deleteMark( an[i] );
352 ERROR_ASSERT( rInput->available() == 1 , "wrong number of bytes flushed" );
354 rMarkable->jumpToFurthest();
356 ERROR_ASSERT( rInput->available() == 4 , "wrong number of bytes flushed" );
358 rInput->readBytes( seqRead , 4 );
360 ERROR_ASSERT( 3 == seqRead.getArray()[0] , "rewrite didn't work" );
361 ERROR_ASSERT( 2 == seqRead.getArray()[1] , "rewrite didn't work" );
362 ERROR_ASSERT( 1 == seqRead.getArray()[2] , "rewrite didn't work" );
363 ERROR_ASSERT( 0 == seqRead.getArray()[3] , "rewrite didn't work" );
365 rOutput->closeOutput();
366 rInput->closeInput();
371 /***
372 * the test methods
374 ****/
381 * for external binding
385 XInterfaceRef OMarkableOutputStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
387 OMarkableOutputStreamTest *p = new OMarkableOutputStreamTest( rSMgr );
388 XInterfaceRef xService = *p;
389 return xService;
394 Sequence<UString> OMarkableOutputStreamTest_getSupportedServiceNames(void) THROWS( () )
396 Sequence<UString> aRet(1);
397 aRet.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
399 return aRet;
402 UString OMarkableOutputStreamTest_getServiceName() THROWS( () )
404 return L"test.com.sun.star.io.MarkableOutputStream";
407 UString OMarkableOutputStreamTest_getImplementationName() THROWS( () )
409 return L"test.com.sun.starextensions.stm.MarkableOutputStream";
418 //-----------------------------------------------------
419 // Input stream
422 class OMarkableInputStreamTest :
423 public XSimpleTest,
424 public OWeakObject
426 public:
427 OMarkableInputStreamTest( const XMultiServiceFactoryRef & rFactory );
428 ~OMarkableInputStreamTest();
430 public: // refcounting
431 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
432 void acquire() { OWeakObject::acquire(); }
433 void release() { OWeakObject::release(); }
434 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
436 public: // implementation names
437 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS( () );
438 static UString getImplementationName_Static() THROWS( () );
440 public:
441 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
442 THROWS( ( IllegalArgumentException,
443 UsrSystemException) );
445 virtual INT32 test( const UString& TestName,
446 const XInterfaceRef& TestObject,
447 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
448 UsrSystemException) );
450 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
451 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
452 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
453 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
455 private:
456 void testSimple( const XOutputStreamRef &r, const XInputStreamRef &rInput );
458 private:
459 Sequence<UsrAny> m_seqExceptions;
460 Sequence<UString> m_seqErrors;
461 Sequence<UString> m_seqWarnings;
462 XMultiServiceFactoryRef m_rFactory;
466 OMarkableInputStreamTest::OMarkableInputStreamTest( const XMultiServiceFactoryRef &rFactory )
467 : m_rFactory( rFactory )
472 OMarkableInputStreamTest::~OMarkableInputStreamTest()
478 BOOL OMarkableInputStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
480 if( XSimpleTest::getSmartUik() == uik ) {
481 rOut = (XSimpleTest *) this;
483 else {
484 return OWeakObject::queryInterface( uik , rOut );
486 return TRUE;
490 void OMarkableInputStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
491 THROWS( ( IllegalArgumentException,
492 UsrSystemException) )
494 if( L"com.sun.star.io.MarkableInputStream" == TestName ) {
495 XServiceInfoRef info( TestObject, USR_QUERY );
496 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
497 if( info.is() )
499 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
500 ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
503 else {
504 THROW( IllegalArgumentException() );
509 INT32 OMarkableInputStreamTest::test( const UString& TestName,
510 const XInterfaceRef& TestObject,
511 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
512 UsrSystemException) )
514 if( L"com.sun.star.io.MarkableInputStream" == TestName ) {
515 try {
516 if( 0 == hTestHandle ) {
517 testInvariant( TestName , TestObject );
519 else {
520 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
521 XOutputStreamRef rPipeOutput( x , USR_QUERY );
522 XInputStreamRef rPipeInput( x , USR_QUERY );
524 XActiveDataSinkRef sink( TestObject , USR_QUERY );
525 sink->setInputStream( rPipeInput );
527 XInputStreamRef rInput( TestObject , USR_QUERY );
529 OSL_ASSERT( rPipeOutput.is() );
530 OSL_ASSERT( rInput.is() );
531 if( 1 == hTestHandle ) {
532 // checks usual streaming
533 testSimple( rPipeOutput , rInput );
538 catch( Exception& e ) {
539 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
541 catch(...) {
542 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
545 hTestHandle ++;
547 if( 2 == hTestHandle ) {
548 // all tests finished.
549 hTestHandle = -1;
552 else {
553 THROW( IllegalArgumentException() );
555 return hTestHandle;
560 BOOL OMarkableInputStreamTest::testPassed(void) THROWS( (UsrSystemException) )
562 return m_seqErrors.getLen() == 0;
566 Sequence< UString > OMarkableInputStreamTest::getErrors(void) THROWS( (UsrSystemException) )
568 return m_seqErrors;
572 Sequence< UsrAny > OMarkableInputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
574 return m_seqExceptions;
578 Sequence< UString > OMarkableInputStreamTest::getWarnings(void) THROWS( (UsrSystemException) )
580 return m_seqWarnings;
584 void OMarkableInputStreamTest::testSimple( const XOutputStreamRef &rOutput ,
585 const XInputStreamRef &rInput )
587 XMarkableStreamRef rMarkable( rInput , USR_QUERY );
589 Sequence<BYTE> seqWrite( 256 );
590 Sequence<BYTE> seqRead(10);
592 for( int i = 0 ; i < 256 ; i ++ )
594 seqWrite.getArray()[i] = i;
597 rOutput->writeBytes( seqWrite );
598 ERROR_ASSERT( 256 == rInput->available() , "basic read/write failure" );
600 rInput->readBytes( seqRead , 10 );
601 ERROR_ASSERT( 9 == seqRead.getArray()[9] , "basic read/write failure" );
603 INT32 nMark = rMarkable->createMark();
605 rInput->skipBytes( 50 );
606 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
607 ERROR_ASSERT( 50 == rMarkable->offsetToMark( nMark ) , "marking error" );
609 rMarkable->jumpToMark( nMark );
610 ERROR_ASSERT( 256-10 == rInput->available() , "marking error" );
612 rInput->readBytes( seqRead , 10 );
613 ERROR_ASSERT( 10 == seqRead.getArray()[0] , "marking error" );
615 // pos 20
617 INT32 nInBetweenMark = rMarkable->createMark( );
618 rMarkable->jumpToMark( nMark );
619 rMarkable->jumpToMark( nInBetweenMark );
621 rInput->readBytes( seqRead , 10 );
622 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
624 rMarkable->deleteMark( nMark );
626 // Check if releasing the first bytes works correct.
627 rMarkable->jumpToMark( nInBetweenMark);
628 rInput->readBytes( seqRead , 10 );
629 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
631 rMarkable->deleteMark( nInBetweenMark );
634 rMarkable->jumpToFurthest();
635 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
638 ERROR_ASSERT( 100 == rInput->readSomeBytes( seqRead , 100 ) , "wrong results using readSomeBytes" );
639 ERROR_ASSERT( 96 == rInput->readSomeBytes( seqRead , 1000) , "wrong results using readSomeBytes" );
640 rOutput->closeOutput();
641 rInput->closeInput();
644 /***
645 * the test methods
647 ****/
654 * for external binding
658 XInterfaceRef OMarkableInputStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
660 OMarkableInputStreamTest *p = new OMarkableInputStreamTest( rSMgr );
661 XInterfaceRef xService = *p;
662 return xService;
667 Sequence<UString> OMarkableInputStreamTest_getSupportedServiceNames(void) THROWS( () )
669 Sequence<UString> aRet(1);
670 aRet.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
672 return aRet;
675 UString OMarkableInputStreamTest_getServiceName() THROWS( () )
677 return L"test.com.sun.star.io.MarkableInputStream";
680 UString OMarkableInputStreamTest_getImplementationName() THROWS( () )
682 return L"test.com.sun.star.extensions.stm.MarkableInputStream";