merge the formfield patch from ooo-build
[ooovba.git] / extensions / test / stm / marktest.cxx
blobefa5c8f12d377b358c4b8f9babe2aa32fdc380f1
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: marktest.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/XMarkableStream.hxx>
35 #include <smart/com/sun/star/io/XActiveDataSink.hxx>
36 #include <smart/com/sun/star/io/XActiveDataSource.hxx>
37 #include <smart/com/sun/star/io/XConnectable.hxx>
39 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
42 #include <usr/factoryhlp.hxx>
44 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
45 #include <usr/weak.hxx> // OWeakObject
47 #include <vos/conditn.hxx>
48 #include <vos/mutex.hxx>
49 #include <vos/thread.hxx>
51 #if OSL_DEBUG_LEVEL == 0
52 #define NDEBUG
53 #endif
54 #include <assert.h>
55 #include <string.h>
57 #include "testfactreg.hxx"
60 #ifndef _VOS_NO_NAMESPACE
61 using namespace vos;
62 using namespace usr;
63 #endif
67 class OMarkableOutputStreamTest :
68 public XSimpleTest,
69 public OWeakObject
71 public:
72 OMarkableOutputStreamTest( const XMultiServiceFactoryRef & rFactory );
73 ~OMarkableOutputStreamTest();
75 public: // refcounting
76 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
77 void acquire() { OWeakObject::acquire(); }
78 void release() { OWeakObject::release(); }
79 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
81 public: // implementation names
82 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS( () );
83 static UString getImplementationName_Static() THROWS( () );
85 public:
86 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
87 THROWS( ( IllegalArgumentException,
88 UsrSystemException) );
90 virtual INT32 test( const UString& TestName,
91 const XInterfaceRef& TestObject,
92 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
93 UsrSystemException) );
95 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
96 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
97 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
98 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
100 private:
101 void testSimple( const XOutputStreamRef &r, const XInputStreamRef &rInput );
103 private:
104 Sequence<UsrAny> m_seqExceptions;
105 Sequence<UString> m_seqErrors;
106 Sequence<UString> m_seqWarnings;
107 XMultiServiceFactoryRef m_rFactory;
111 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const XMultiServiceFactoryRef &rFactory )
112 : m_rFactory( rFactory )
117 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
123 BOOL OMarkableOutputStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
125 if( XSimpleTest::getSmartUik() == uik ) {
126 rOut = (XSimpleTest *) this;
128 else {
129 return OWeakObject::queryInterface( uik , rOut );
131 return TRUE;
135 void OMarkableOutputStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
136 THROWS( ( IllegalArgumentException,
137 UsrSystemException) )
139 XServiceInfoRef info( TestObject, USR_QUERY );
140 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
141 if( info.is() )
143 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
144 ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
149 INT32 OMarkableOutputStreamTest::test( const UString& TestName,
150 const XInterfaceRef& TestObject,
151 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
152 UsrSystemException) )
154 if( L"com.sun.star.io.MarkableOutputStream" == TestName ) {
155 try {
156 if( 0 == hTestHandle ) {
157 testInvariant( TestName , TestObject );
159 else {
160 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
161 XOutputStreamRef rPipeOutput( x , USR_QUERY );
162 XInputStreamRef rPipeInput( x , USR_QUERY );
164 XActiveDataSourceRef source( TestObject , USR_QUERY );
165 source->setOutputStream( rPipeOutput );
167 XOutputStreamRef rOutput( TestObject , USR_QUERY );
169 assert( rPipeInput.is() );
170 assert( rOutput.is() );
171 if( 1 == hTestHandle ) {
172 // checks usual streaming
173 testSimple( rOutput , rPipeInput );
178 catch( Exception& e ) {
179 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
181 catch(...) {
182 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
185 hTestHandle ++;
187 if( 2 == hTestHandle ) {
188 // all tests finished.
189 hTestHandle = -1;
192 else {
193 THROW( IllegalArgumentException() );
195 return hTestHandle;
200 BOOL OMarkableOutputStreamTest::testPassed(void) THROWS( (UsrSystemException) )
202 return m_seqErrors.getLen() == 0;
206 Sequence< UString > OMarkableOutputStreamTest::getErrors(void) THROWS( (UsrSystemException) )
208 return m_seqErrors;
212 Sequence< UsrAny > OMarkableOutputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
214 return m_seqExceptions;
218 Sequence< UString > OMarkableOutputStreamTest::getWarnings(void) THROWS( (UsrSystemException) )
220 return m_seqWarnings;
224 void OMarkableOutputStreamTest::testSimple( const XOutputStreamRef &rOutput ,
225 const XInputStreamRef &rInput )
227 XMarkableStreamRef rMarkable( rOutput , USR_QUERY );
229 ERROR_ASSERT( rMarkable.is() , "no MarkableStream implemented" );
231 // first check normal input/output facility
232 char pcStr[] = "Live long and prosper !";
234 Sequence<BYTE> seqWrite( strlen( pcStr )+1 );
235 memcpy( seqWrite.getArray() , pcStr , seqWrite.getLen() );
237 Sequence<BYTE> seqRead( seqWrite.getLen() );
239 int nMax = 10,i;
241 for( i = 0 ; i < nMax ; i ++ ) {
242 rOutput->writeBytes( seqWrite );
243 rInput->readBytes( seqRead , rInput->available() );
244 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
245 "error during read/write/skip" );
248 // Check buffer resizing
249 nMax = 3000;
250 for( i = 0 ; i < nMax ; i ++ ) {
251 rOutput->writeBytes( seqWrite );
254 for( i = 0 ; i < nMax ; i ++ ) {
255 rInput->readBytes( seqRead , seqWrite.getLen() );
256 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
257 "error during read/write" );
260 // Check creating marks !
261 INT32 nMark = rMarkable->createMark();
263 for( i = 0 ; i < nMax ; i ++ ) {
264 rOutput->writeBytes( seqWrite );
267 ERROR_ASSERT( 0 == rInput->available() , "bytes available though mark is holded" );
269 ERROR_ASSERT( nMax*seqWrite.getLen() == rMarkable->offsetToMark( nMark ) ,
270 "offsetToMark failure" );
272 rMarkable->deleteMark( nMark );
273 ERROR_ASSERT( nMax*seqWrite.getLen() == rInput->available(),"bytes are not available though mark has been deleted" );
275 rInput->skipBytes( nMax*seqWrite.getLen() );
276 ERROR_ASSERT( 0 == rInput->available(), "skip bytes failure" );
278 try {
279 rMarkable->jumpToMark( nMark );
280 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
282 catch ( IllegalArgumentException& e )
284 e;// ok, exception was thrown
287 // test putting marks not at the end of the stream!
288 ERROR_ASSERT( 0 == rInput->available(), "stream isn't clean" );
290 Sequence< BYTE > aByte(256);
292 for( i = 0 ; i < 256 ; i ++ )
294 aByte.getArray()[i] = i;
296 INT32 nMark1 = rMarkable->createMark();
298 rOutput->writeBytes( aByte );
299 rMarkable->jumpToMark( nMark1 );
300 aByte.realloc( 10 );
301 rOutput->writeBytes( aByte );
303 INT32 nMark2 = rMarkable->createMark( );
305 for( i = 0 ; i < 10 ; i ++ )
307 aByte.getArray()[i] = i+10;
310 rOutput->writeBytes( aByte );
312 // allow the bytes to be written !
313 rMarkable->jumpToFurthest();
314 rMarkable->deleteMark( nMark1 );
315 rMarkable->deleteMark( nMark2 );
317 ERROR_ASSERT( 256 == rInput->available(), "in between mark failure" );
318 rInput->readBytes( aByte ,256);
319 for( i = 0 ; i < 256 ; i ++ )
321 ERROR_ASSERT( i == aByte.getArray()[i] , "in between mark failure" );
326 // now a more extensive mark test !
327 Sequence<BYTE> as[4];
328 INT32 an[4];
330 for( i = 0 ; i < 4 ; i ++ ) {
331 as[i].realloc(1);
332 as[i].getArray()[0] = i;
333 an[i] = rMarkable->createMark();
334 rOutput->writeBytes( as[i] );
337 // check offset to mark
338 for( i = 0 ; i < 4 ; i ++ ) {
339 ERROR_ASSERT( rMarkable->offsetToMark( an[i] ) == 4-i , "offsetToMark failure" );
342 rMarkable->jumpToMark( an[1] );
343 ERROR_ASSERT( rMarkable->offsetToMark( an[3] ) == -2 , "offsetToMark failure" );
345 rMarkable->jumpToFurthest( );
346 ERROR_ASSERT( rMarkable->offsetToMark( an[0] ) == 4 , "offsetToMark failure" );
348 // now do a rewrite !
349 for( i = 0 ; i < 4 ; i ++ ) {
350 rMarkable->jumpToMark( an[3-i] );
351 rOutput->writeBytes( as[i] );
353 // NOTE : CursorPos 1
355 // now delete the marks !
356 for( i = 0 ; i < 4 ; i ++ ) {
357 rMarkable->deleteMark( an[i] );
359 ERROR_ASSERT( rInput->available() == 1 , "wrong number of bytes flushed" );
361 rMarkable->jumpToFurthest();
363 ERROR_ASSERT( rInput->available() == 4 , "wrong number of bytes flushed" );
365 rInput->readBytes( seqRead , 4 );
367 ERROR_ASSERT( 3 == seqRead.getArray()[0] , "rewrite didn't work" );
368 ERROR_ASSERT( 2 == seqRead.getArray()[1] , "rewrite didn't work" );
369 ERROR_ASSERT( 1 == seqRead.getArray()[2] , "rewrite didn't work" );
370 ERROR_ASSERT( 0 == seqRead.getArray()[3] , "rewrite didn't work" );
372 rOutput->closeOutput();
373 rInput->closeInput();
378 /***
379 * the test methods
381 ****/
388 * for external binding
392 XInterfaceRef OMarkableOutputStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
394 OMarkableOutputStreamTest *p = new OMarkableOutputStreamTest( rSMgr );
395 XInterfaceRef xService = *p;
396 return xService;
401 Sequence<UString> OMarkableOutputStreamTest_getSupportedServiceNames(void) THROWS( () )
403 Sequence<UString> aRet(1);
404 aRet.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
406 return aRet;
409 UString OMarkableOutputStreamTest_getServiceName() THROWS( () )
411 return L"test.com.sun.star.io.MarkableOutputStream";
414 UString OMarkableOutputStreamTest_getImplementationName() THROWS( () )
416 return L"test.com.sun.starextensions.stm.MarkableOutputStream";
425 //-----------------------------------------------------
426 // Input stream
429 class OMarkableInputStreamTest :
430 public XSimpleTest,
431 public OWeakObject
433 public:
434 OMarkableInputStreamTest( const XMultiServiceFactoryRef & rFactory );
435 ~OMarkableInputStreamTest();
437 public: // refcounting
438 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
439 void acquire() { OWeakObject::acquire(); }
440 void release() { OWeakObject::release(); }
441 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
443 public: // implementation names
444 static Sequence< UString > getSupportedServiceNames_Static(void) THROWS( () );
445 static UString getImplementationName_Static() THROWS( () );
447 public:
448 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
449 THROWS( ( IllegalArgumentException,
450 UsrSystemException) );
452 virtual INT32 test( const UString& TestName,
453 const XInterfaceRef& TestObject,
454 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
455 UsrSystemException) );
457 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
458 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
459 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
460 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
462 private:
463 void testSimple( const XOutputStreamRef &r, const XInputStreamRef &rInput );
465 private:
466 Sequence<UsrAny> m_seqExceptions;
467 Sequence<UString> m_seqErrors;
468 Sequence<UString> m_seqWarnings;
469 XMultiServiceFactoryRef m_rFactory;
473 OMarkableInputStreamTest::OMarkableInputStreamTest( const XMultiServiceFactoryRef &rFactory )
474 : m_rFactory( rFactory )
479 OMarkableInputStreamTest::~OMarkableInputStreamTest()
485 BOOL OMarkableInputStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
487 if( XSimpleTest::getSmartUik() == uik ) {
488 rOut = (XSimpleTest *) this;
490 else {
491 return OWeakObject::queryInterface( uik , rOut );
493 return TRUE;
497 void OMarkableInputStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
498 THROWS( ( IllegalArgumentException,
499 UsrSystemException) )
501 if( L"com.sun.star.io.MarkableInputStream" == TestName ) {
502 XServiceInfoRef info( TestObject, USR_QUERY );
503 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
504 if( info.is() )
506 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
507 ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
510 else {
511 THROW( IllegalArgumentException() );
516 INT32 OMarkableInputStreamTest::test( const UString& TestName,
517 const XInterfaceRef& TestObject,
518 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
519 UsrSystemException) )
521 if( L"com.sun.star.io.MarkableInputStream" == TestName ) {
522 try {
523 if( 0 == hTestHandle ) {
524 testInvariant( TestName , TestObject );
526 else {
527 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
528 XOutputStreamRef rPipeOutput( x , USR_QUERY );
529 XInputStreamRef rPipeInput( x , USR_QUERY );
531 XActiveDataSinkRef sink( TestObject , USR_QUERY );
532 sink->setInputStream( rPipeInput );
534 XInputStreamRef rInput( TestObject , USR_QUERY );
536 assert( rPipeOutput.is() );
537 assert( rInput.is() );
538 if( 1 == hTestHandle ) {
539 // checks usual streaming
540 testSimple( rPipeOutput , rInput );
545 catch( Exception& e ) {
546 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
548 catch(...) {
549 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
552 hTestHandle ++;
554 if( 2 == hTestHandle ) {
555 // all tests finished.
556 hTestHandle = -1;
559 else {
560 THROW( IllegalArgumentException() );
562 return hTestHandle;
567 BOOL OMarkableInputStreamTest::testPassed(void) THROWS( (UsrSystemException) )
569 return m_seqErrors.getLen() == 0;
573 Sequence< UString > OMarkableInputStreamTest::getErrors(void) THROWS( (UsrSystemException) )
575 return m_seqErrors;
579 Sequence< UsrAny > OMarkableInputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
581 return m_seqExceptions;
585 Sequence< UString > OMarkableInputStreamTest::getWarnings(void) THROWS( (UsrSystemException) )
587 return m_seqWarnings;
591 void OMarkableInputStreamTest::testSimple( const XOutputStreamRef &rOutput ,
592 const XInputStreamRef &rInput )
594 XMarkableStreamRef rMarkable( rInput , USR_QUERY );
596 Sequence<BYTE> seqWrite( 256 );
597 Sequence<BYTE> seqRead(10);
599 for( int i = 0 ; i < 256 ; i ++ )
601 seqWrite.getArray()[i] = i;
604 rOutput->writeBytes( seqWrite );
605 ERROR_ASSERT( 256 == rInput->available() , "basic read/write failure" );
607 rInput->readBytes( seqRead , 10 );
608 ERROR_ASSERT( 9 == seqRead.getArray()[9] , "basic read/write failure" );
610 INT32 nMark = rMarkable->createMark();
612 rInput->skipBytes( 50 );
613 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
614 ERROR_ASSERT( 50 == rMarkable->offsetToMark( nMark ) , "marking error" );
616 rMarkable->jumpToMark( nMark );
617 ERROR_ASSERT( 256-10 == rInput->available() , "marking error" );
619 rInput->readBytes( seqRead , 10 );
620 ERROR_ASSERT( 10 == seqRead.getArray()[0] , "marking error" );
622 // pos 20
624 INT32 nInBetweenMark = rMarkable->createMark( );
625 rMarkable->jumpToMark( nMark );
626 rMarkable->jumpToMark( nInBetweenMark );
628 rInput->readBytes( seqRead , 10 );
629 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
631 rMarkable->deleteMark( nMark );
633 // Check if releasing the first bytes works correct.
634 rMarkable->jumpToMark( nInBetweenMark);
635 rInput->readBytes( seqRead , 10 );
636 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
638 rMarkable->deleteMark( nInBetweenMark );
641 rMarkable->jumpToFurthest();
642 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
645 ERROR_ASSERT( 100 == rInput->readSomeBytes( seqRead , 100 ) , "wrong results using readSomeBytes" );
646 ERROR_ASSERT( 96 == rInput->readSomeBytes( seqRead , 1000) , "wrong results using readSomeBytes" );
647 rOutput->closeOutput();
648 rInput->closeInput();
651 /***
652 * the test methods
654 ****/
661 * for external binding
665 XInterfaceRef OMarkableInputStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
667 OMarkableInputStreamTest *p = new OMarkableInputStreamTest( rSMgr );
668 XInterfaceRef xService = *p;
669 return xService;
674 Sequence<UString> OMarkableInputStreamTest_getSupportedServiceNames(void) THROWS( () )
676 Sequence<UString> aRet(1);
677 aRet.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
679 return aRet;
682 UString OMarkableInputStreamTest_getServiceName() THROWS( () )
684 return L"test.com.sun.star.io.MarkableInputStream";
687 UString OMarkableInputStreamTest_getImplementationName() THROWS( () )
689 return L"test.com.sun.star.extensions.stm.MarkableInputStream";