Update ooo320-m1
[ooovba.git] / io / test / stm / marktest.cxx
blob992e336f8220cfb2eebfe01dfdf80768cff2857f
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.7 $
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_io.hxx"
33 #include <com/sun/star/test/XSimpleTest.hpp>
34 #include <com/sun/star/io/XActiveDataSink.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/io/XMarkableStream.hpp>
37 #include <com/sun/star/io/XConnectable.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 #include <cppuhelper/implbase2.hxx>
46 #include <osl/conditn.hxx>
47 #include <osl/mutex.hxx>
49 #if OSL_DEBUG_LEVEL == 0
50 #define NDEBUG
51 #endif
52 #include <assert.h>
53 #include <string.h>
55 using namespace ::rtl;
56 using namespace ::osl;
57 using namespace ::cppu;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::io;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::test;
62 // streams
64 #include "testfactreg.hxx"
67 class OMarkableOutputStreamTest : public WeakImplHelper1< XSimpleTest >
69 public:
70 OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
71 ~OMarkableOutputStreamTest();
73 public: // implementation names
74 static Sequence< OUString > getSupportedServiceNames_Static(void) throw ();
75 static OUString getImplementationName_Static() throw ();
77 public:
78 virtual void SAL_CALL testInvariant(
79 const OUString& TestName,
80 const Reference < XInterface >& TestObject)
81 throw ( IllegalArgumentException,
82 RuntimeException) ;
84 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
85 const Reference < XInterface >& TestObject,
86 sal_Int32 hTestHandle)
87 throw ( IllegalArgumentException, RuntimeException);
88 virtual sal_Bool SAL_CALL testPassed(void)
89 throw ( RuntimeException);
90 virtual Sequence< OUString > SAL_CALL getErrors(void)
91 throw (RuntimeException);
92 virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
93 throw (RuntimeException);
94 virtual Sequence< OUString > SAL_CALL getWarnings(void)
95 throw (RuntimeException);
97 private:
98 void testSimple( const Reference< XOutputStream > &r, const Reference < XInputStream > &rInput );
100 private:
101 Sequence<Any> m_seqExceptions;
102 Sequence<OUString> m_seqErrors;
103 Sequence<OUString> m_seqWarnings;
104 Reference< XMultiServiceFactory > m_rFactory;
108 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
109 : m_rFactory( rFactory )
114 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
122 void OMarkableOutputStreamTest::testInvariant( const OUString& TestName,
123 const Reference < XInterface >& TestObject )
124 throw ( IllegalArgumentException, RuntimeException)
126 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
127 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
128 if( info.is() )
130 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
131 ERROR_ASSERT( ! info->supportsService(
132 OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb")) ) , "XServiceInfo test failed" );
137 sal_Int32 OMarkableOutputStreamTest::test(
138 const OUString& TestName,
139 const Reference < XInterface >& TestObject,
140 sal_Int32 hTestHandle)
141 throw ( IllegalArgumentException, RuntimeException)
143 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableOutputStream") )
144 == TestName ) {
147 if( 0 == hTestHandle )
149 testInvariant( TestName , TestObject );
151 else
153 Reference < XInterface > x = m_rFactory->createInstance( OUString::createFromAscii("com.sun.star.io.Pipe"));
154 Reference< XOutputStream > rPipeOutput( x , UNO_QUERY );
155 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
157 Reference< XActiveDataSource > source( TestObject , UNO_QUERY );
158 source->setOutputStream( rPipeOutput );
160 Reference< XOutputStream > rOutput( TestObject , UNO_QUERY );
162 assert( rPipeInput.is() );
163 assert( rOutput.is() );
164 if( 1 == hTestHandle ) {
165 // checks usual streaming
166 testSimple( rOutput , rPipeInput );
171 catch( Exception &e )
173 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
174 BUILD_ERROR( 0 , o.getStr() );
176 catch( ... )
178 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
181 hTestHandle ++;
183 if( 2 == hTestHandle )
185 // all tests finished.
186 hTestHandle = -1;
189 else {
190 throw IllegalArgumentException();
192 return hTestHandle;
197 sal_Bool OMarkableOutputStreamTest::testPassed(void) throw (RuntimeException)
199 return m_seqErrors.getLength() == 0;
203 Sequence< OUString > OMarkableOutputStreamTest::getErrors(void) throw (RuntimeException)
205 return m_seqErrors;
209 Sequence< Any > OMarkableOutputStreamTest::getErrorExceptions(void) throw (RuntimeException)
211 return m_seqExceptions;
215 Sequence< OUString > OMarkableOutputStreamTest::getWarnings(void) throw (RuntimeException)
217 return m_seqWarnings;
221 void OMarkableOutputStreamTest::testSimple( const Reference< XOutputStream > &rOutput ,
222 const Reference< XInputStream > &rInput )
224 Reference < XMarkableStream > rMarkable( rOutput , UNO_QUERY );
226 ERROR_ASSERT( rMarkable.is() , "no MarkableStream implemented" );
228 // first check normal input/output facility
229 char pcStr[] = "Live long and prosper !";
231 Sequence<sal_Int8> seqWrite( strlen( pcStr )+1 );
232 memcpy( seqWrite.getArray() , pcStr , seqWrite.getLength() );
234 Sequence<sal_Int8> seqRead( seqWrite.getLength() );
236 int nMax = 10,i;
238 for( i = 0 ; i < nMax ; i ++ ) {
239 rOutput->writeBytes( seqWrite );
240 rInput->readBytes( seqRead , rInput->available() );
241 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
242 "error during read/write/skip" );
245 // Check buffer resizing
246 nMax = 3000;
247 for( i = 0 ; i < nMax ; i ++ ) {
248 rOutput->writeBytes( seqWrite );
251 for( i = 0 ; i < nMax ; i ++ ) {
252 rInput->readBytes( seqRead , seqWrite.getLength() );
253 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
254 "error during read/write" );
257 // Check creating marks !
258 sal_Int32 nMark = rMarkable->createMark();
260 for( i = 0 ; i < nMax ; i ++ ) {
261 rOutput->writeBytes( seqWrite );
264 ERROR_ASSERT( 0 == rInput->available() , "bytes available though mark is holded" );
266 ERROR_ASSERT( nMax*seqWrite.getLength() == rMarkable->offsetToMark( nMark ) ,
267 "offsetToMark failure" );
269 rMarkable->deleteMark( nMark );
270 ERROR_ASSERT( nMax*seqWrite.getLength() == rInput->available(),"bytes are not available though mark has been deleted" );
272 rInput->skipBytes( nMax*seqWrite.getLength() );
273 ERROR_ASSERT( 0 == rInput->available(), "skip bytes failure" );
277 rMarkable->jumpToMark( nMark );
278 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
280 catch ( IllegalArgumentException & )
282 // ok, exception was thrown
285 // test putting marks not at the end of the stream!
286 ERROR_ASSERT( 0 == rInput->available(), "stream isn't clean" );
288 Sequence< sal_Int8 > aByte(256);
290 for( i = 0 ; i < 256 ; i ++ )
292 aByte.getArray()[i] = i;
294 sal_Int32 nMark1 = rMarkable->createMark();
296 rOutput->writeBytes( aByte );
297 rMarkable->jumpToMark( nMark1 );
298 aByte.realloc( 10 );
299 rOutput->writeBytes( aByte );
301 sal_Int32 nMark2 = rMarkable->createMark( );
303 for( i = 0 ; i < 10 ; i ++ )
305 aByte.getArray()[i] = i+10;
308 rOutput->writeBytes( aByte );
310 // allow the bytes to be written !
311 rMarkable->jumpToFurthest();
312 rMarkable->deleteMark( nMark1 );
313 rMarkable->deleteMark( nMark2 );
315 ERROR_ASSERT( 256 == rInput->available(), "in between mark failure" );
316 rInput->readBytes( aByte ,256);
317 for( i = 0 ; i < 256 ; i ++ )
319 ERROR_ASSERT( i == ((sal_uInt8*)(aByte.getArray()))[i] , "in between mark failure" );
324 // now a more extensive mark test !
325 Sequence<sal_Int8> as[4];
326 sal_Int32 an[4];
328 for( i = 0 ; i < 4 ; i ++ ) {
329 as[i].realloc(1);
330 as[i].getArray()[0] = i;
331 an[i] = rMarkable->createMark();
332 rOutput->writeBytes( as[i] );
335 // check offset to mark
336 for( i = 0 ; i < 4 ; i ++ ) {
337 ERROR_ASSERT( rMarkable->offsetToMark( an[i] ) == 4-i , "offsetToMark failure" );
340 rMarkable->jumpToMark( an[1] );
341 ERROR_ASSERT( rMarkable->offsetToMark( an[3] ) == -2 , "offsetToMark failure" );
343 rMarkable->jumpToFurthest( );
344 ERROR_ASSERT( rMarkable->offsetToMark( an[0] ) == 4 , "offsetToMark failure" );
346 // now do a rewrite !
347 for( i = 0 ; i < 4 ; i ++ ) {
348 rMarkable->jumpToMark( an[3-i] );
349 rOutput->writeBytes( as[i] );
351 // NOTE : CursorPos 1
353 // now delete the marks !
354 for( i = 0 ; i < 4 ; i ++ ) {
355 rMarkable->deleteMark( an[i] );
357 ERROR_ASSERT( rInput->available() == 1 , "wrong number of bytes flushed" );
359 rMarkable->jumpToFurthest();
361 ERROR_ASSERT( rInput->available() == 4 , "wrong number of bytes flushed" );
363 rInput->readBytes( seqRead , 4 );
365 ERROR_ASSERT( 3 == seqRead.getArray()[0] , "rewrite didn't work" );
366 ERROR_ASSERT( 2 == seqRead.getArray()[1] , "rewrite didn't work" );
367 ERROR_ASSERT( 1 == seqRead.getArray()[2] , "rewrite didn't work" );
368 ERROR_ASSERT( 0 == seqRead.getArray()[3] , "rewrite didn't work" );
370 rOutput->closeOutput();
371 rInput->closeInput();
376 /***
377 * the test methods
379 ****/
386 * for external binding
390 Reference < XInterface > SAL_CALL OMarkableOutputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
392 OMarkableOutputStreamTest *p = new OMarkableOutputStreamTest( rSMgr );
393 return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
398 Sequence<OUString> OMarkableOutputStreamTest_getSupportedServiceNames(void) throw ()
400 Sequence<OUString> aRet(1);
401 aRet.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
403 return aRet;
406 OUString OMarkableOutputStreamTest_getServiceName() throw ()
408 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.MarkableOutputStream"));
411 OUString OMarkableOutputStreamTest_getImplementationName() throw ()
413 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.starextensions.stm.MarkableOutputStream"));
422 //-----------------------------------------------------
423 // Input stream
426 class OMarkableInputStreamTest : public WeakImplHelper1< XSimpleTest >
428 public:
429 OMarkableInputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
430 ~OMarkableInputStreamTest();
432 public: // implementation names
433 static Sequence< OUString > getSupportedServiceNames_Static(void) throw () ;
434 static OUString getImplementationName_Static() throw () ;
436 public:
437 virtual void SAL_CALL testInvariant(
438 const OUString& TestName,
439 const Reference < XInterface >& TestObject)
440 throw ( IllegalArgumentException, RuntimeException) ;
442 virtual sal_Int32 SAL_CALL test(
443 const OUString& TestName,
444 const Reference < XInterface >& TestObject,
445 sal_Int32 hTestHandle)
446 throw ( IllegalArgumentException,
447 RuntimeException) ;
449 virtual sal_Bool SAL_CALL testPassed(void)
450 throw ( RuntimeException);
451 virtual Sequence< OUString > SAL_CALL getErrors(void)
452 throw (RuntimeException);
453 virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
454 throw (RuntimeException);
455 virtual Sequence< OUString > SAL_CALL getWarnings(void)
456 throw (RuntimeException);
458 private:
459 void testSimple( const Reference< XOutputStream > &r,
460 const Reference < XInputStream > &rInput );
462 private:
463 Sequence<Any> m_seqExceptions;
464 Sequence<OUString> m_seqErrors;
465 Sequence<OUString> m_seqWarnings;
466 Reference< XMultiServiceFactory > m_rFactory;
470 OMarkableInputStreamTest::OMarkableInputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
471 : m_rFactory( rFactory )
476 OMarkableInputStreamTest::~OMarkableInputStreamTest()
483 void OMarkableInputStreamTest::testInvariant(
484 const OUString& TestName, const Reference < XInterface >& TestObject )
485 throw ( IllegalArgumentException, RuntimeException)
487 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream"))
488 == TestName ) {
489 Reference <XServiceInfo > info( TestObject, UNO_QUERY );
490 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
491 if( info.is() )
493 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
494 ERROR_ASSERT(
495 ! info->supportsService(
496 OUString(RTL_CONSTASCII_USTRINGPARAM("bla bluzb")) ) ,
497 "XServiceInfo test failed" );
500 else
502 throw IllegalArgumentException();
507 sal_Int32 OMarkableInputStreamTest::test(
508 const OUString& TestName,
509 const Reference < XInterface >& TestObject,
510 sal_Int32 hTestHandle) throw ( IllegalArgumentException, RuntimeException)
512 if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream")) == TestName )
516 if( 0 == hTestHandle ) {
517 testInvariant( TestName , TestObject );
519 else {
520 Reference < XInterface > x = m_rFactory->createInstance( OUString::createFromAscii("com.sun.star.io.Pipe"));
521 Reference< XOutputStream > rPipeOutput( x , UNO_QUERY );
522 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
524 Reference < XActiveDataSink > sink( TestObject , UNO_QUERY );
525 sink->setInputStream( rPipeInput );
527 Reference < XInputStream > rInput( TestObject , UNO_QUERY );
529 assert( rPipeOutput.is() );
530 assert( rInput.is() );
531 if( 1 == hTestHandle ) {
532 // checks usual streaming
533 testSimple( rPipeOutput , rInput );
538 catch( Exception & e )
540 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
541 BUILD_ERROR( 0 , o.getStr() );
543 catch( ... )
545 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
548 hTestHandle ++;
550 if( 2 == hTestHandle ) {
551 // all tests finished.
552 hTestHandle = -1;
555 else
557 throw IllegalArgumentException();
559 return hTestHandle;
564 sal_Bool OMarkableInputStreamTest::testPassed(void) throw (RuntimeException)
566 return m_seqErrors.getLength() == 0;
570 Sequence< OUString > OMarkableInputStreamTest::getErrors(void) throw (RuntimeException)
572 return m_seqErrors;
576 Sequence< Any > OMarkableInputStreamTest::getErrorExceptions(void) throw (RuntimeException)
578 return m_seqExceptions;
582 Sequence< OUString > OMarkableInputStreamTest::getWarnings(void) throw (RuntimeException)
584 return m_seqWarnings;
588 void OMarkableInputStreamTest::testSimple( const Reference< XOutputStream > &rOutput ,
589 const Reference < XInputStream > &rInput )
591 Reference < XMarkableStream > rMarkable( rInput , UNO_QUERY );
593 Sequence<sal_Int8> seqWrite( 256 );
594 Sequence<sal_Int8> seqRead(10);
596 for( int i = 0 ; i < 256 ; i ++ )
598 seqWrite.getArray()[i] = i;
601 rOutput->writeBytes( seqWrite );
602 ERROR_ASSERT( 256 == rInput->available() , "basic read/write failure" );
604 rInput->readBytes( seqRead , 10 );
605 ERROR_ASSERT( 9 == seqRead.getArray()[9] , "basic read/write failure" );
607 sal_Int32 nMark = rMarkable->createMark();
609 rInput->skipBytes( 50 );
610 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
611 ERROR_ASSERT( 50 == rMarkable->offsetToMark( nMark ) , "marking error" );
613 rMarkable->jumpToMark( nMark );
614 ERROR_ASSERT( 256-10 == rInput->available() , "marking error" );
616 rInput->readBytes( seqRead , 10 );
617 ERROR_ASSERT( 10 == seqRead.getArray()[0] , "marking error" );
619 // pos 20
621 sal_Int32 nInBetweenMark = rMarkable->createMark( );
622 rMarkable->jumpToMark( nMark );
623 rMarkable->jumpToMark( nInBetweenMark );
625 rInput->readBytes( seqRead , 10 );
626 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
628 rMarkable->deleteMark( nMark );
630 // Check if releasing the first bytes works correct.
631 rMarkable->jumpToMark( nInBetweenMark);
632 rInput->readBytes( seqRead , 10 );
633 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
635 rMarkable->deleteMark( nInBetweenMark );
638 rMarkable->jumpToFurthest();
639 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
642 ERROR_ASSERT( 100 == rInput->readSomeBytes( seqRead , 100 ) , "wrong results using readSomeBytes" );
643 ERROR_ASSERT( 96 == rInput->readSomeBytes( seqRead , 1000) , "wrong results using readSomeBytes" );
644 rOutput->closeOutput();
645 rInput->closeInput();
648 /***
649 * the test methods
651 ****/
658 * for external binding
662 Reference < XInterface > SAL_CALL OMarkableInputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
664 OMarkableInputStreamTest *p = new OMarkableInputStreamTest( rSMgr );
665 return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
670 Sequence<OUString> OMarkableInputStreamTest_getSupportedServiceNames(void) throw ()
672 Sequence<OUString> aRet(1);
673 aRet.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
675 return aRet;
678 OUString OMarkableInputStreamTest_getServiceName() throw ()
680 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.MarkableInputStream"));
683 OUString OMarkableInputStreamTest_getImplementationName() throw ()
685 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.extensions.stm.MarkableInputStream" ));