Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / io / test / stm / marktest.cxx
blobef0aace779d5a6eda3e9cc035f56905e6e966b6c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/test/XSimpleTest.hpp>
30 #include <com/sun/star/io/XActiveDataSink.hpp>
31 #include <com/sun/star/io/XActiveDataSource.hpp>
32 #include <com/sun/star/io/XMarkableStream.hpp>
33 #include <com/sun/star/io/XConnectable.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase2.hxx>
42 #include <osl/conditn.hxx>
43 #include <osl/mutex.hxx>
45 using namespace ::rtl;
46 using namespace ::osl;
47 using namespace ::cppu;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::io;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::test;
52 // streams
54 #include "testfactreg.hxx"
57 class OMarkableOutputStreamTest : public WeakImplHelper1< XSimpleTest >
59 public:
60 OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
61 ~OMarkableOutputStreamTest();
63 public: // implementation names
64 static Sequence< OUString > getSupportedServiceNames_Static(void) throw ();
65 static OUString getImplementationName_Static() throw ();
67 public:
68 virtual void SAL_CALL testInvariant(
69 const OUString& TestName,
70 const Reference < XInterface >& TestObject)
71 throw ( IllegalArgumentException,
72 RuntimeException) ;
74 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
75 const Reference < XInterface >& TestObject,
76 sal_Int32 hTestHandle)
77 throw ( IllegalArgumentException, RuntimeException);
78 virtual sal_Bool SAL_CALL testPassed(void)
79 throw ( RuntimeException);
80 virtual Sequence< OUString > SAL_CALL getErrors(void)
81 throw (RuntimeException);
82 virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
83 throw (RuntimeException);
84 virtual Sequence< OUString > SAL_CALL getWarnings(void)
85 throw (RuntimeException);
87 private:
88 void testSimple( const Reference< XOutputStream > &r, const Reference < XInputStream > &rInput );
90 private:
91 Sequence<Any> m_seqExceptions;
92 Sequence<OUString> m_seqErrors;
93 Sequence<OUString> m_seqWarnings;
94 Reference< XMultiServiceFactory > m_rFactory;
98 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
99 : m_rFactory( rFactory )
104 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
112 void OMarkableOutputStreamTest::testInvariant( const OUString& TestName,
113 const Reference < XInterface >& TestObject )
114 throw ( IllegalArgumentException, RuntimeException)
116 Reference< XServiceInfo > info( TestObject, UNO_QUERY );
117 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
118 if( info.is() )
120 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
121 ERROR_ASSERT( ! info->supportsService(
122 OUString( "bla bluzb") ) , "XServiceInfo test failed" );
127 sal_Int32 OMarkableOutputStreamTest::test(
128 const OUString& TestName,
129 const Reference < XInterface >& TestObject,
130 sal_Int32 hTestHandle)
131 throw ( IllegalArgumentException, RuntimeException)
133 if( OUString( "com.sun.star.io.MarkableOutputStream" )
134 == TestName ) {
137 if( 0 == hTestHandle )
139 testInvariant( TestName , TestObject );
141 else
143 Reference < XInterface > x = m_rFactory->createInstance( OUString("com.sun.star.io.Pipe"));
144 Reference< XOutputStream > rPipeOutput( x , UNO_QUERY );
145 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
147 Reference< XActiveDataSource > source( TestObject , UNO_QUERY );
148 source->setOutputStream( rPipeOutput );
150 Reference< XOutputStream > rOutput( TestObject , UNO_QUERY );
152 OSL_ASSERT( rPipeInput.is() );
153 OSL_ASSERT( rOutput.is() );
154 if( 1 == hTestHandle ) {
155 // checks usual streaming
156 testSimple( rOutput , rPipeInput );
161 catch( const Exception &e )
163 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
164 BUILD_ERROR( 0 , o.getStr() );
166 catch( ... )
168 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
171 hTestHandle ++;
173 if( 2 == hTestHandle )
175 // all tests finished.
176 hTestHandle = -1;
179 else {
180 throw IllegalArgumentException();
182 return hTestHandle;
187 sal_Bool OMarkableOutputStreamTest::testPassed(void) throw (RuntimeException)
189 return m_seqErrors.getLength() == 0;
193 Sequence< OUString > OMarkableOutputStreamTest::getErrors(void) throw (RuntimeException)
195 return m_seqErrors;
199 Sequence< Any > OMarkableOutputStreamTest::getErrorExceptions(void) throw (RuntimeException)
201 return m_seqExceptions;
205 Sequence< OUString > OMarkableOutputStreamTest::getWarnings(void) throw (RuntimeException)
207 return m_seqWarnings;
211 void OMarkableOutputStreamTest::testSimple( const Reference< XOutputStream > &rOutput ,
212 const Reference< XInputStream > &rInput )
214 Reference < XMarkableStream > rMarkable( rOutput , UNO_QUERY );
216 ERROR_ASSERT( rMarkable.is() , "no MarkableStream implemented" );
218 // first check normal input/output facility
219 char pcStr[] = "Live long and prosper !";
221 Sequence<sal_Int8> seqWrite( strlen( pcStr )+1 );
222 memcpy( seqWrite.getArray() , pcStr , seqWrite.getLength() );
224 Sequence<sal_Int8> seqRead( seqWrite.getLength() );
226 int nMax = 10,i;
228 for( i = 0 ; i < nMax ; i ++ ) {
229 rOutput->writeBytes( seqWrite );
230 rInput->readBytes( seqRead , rInput->available() );
231 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
232 "error during read/write/skip" );
235 // Check buffer resizing
236 nMax = 3000;
237 for( i = 0 ; i < nMax ; i ++ ) {
238 rOutput->writeBytes( seqWrite );
241 for( i = 0 ; i < nMax ; i ++ ) {
242 rInput->readBytes( seqRead , seqWrite.getLength() );
243 ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
244 "error during read/write" );
247 // Check creating marks !
248 sal_Int32 nMark = rMarkable->createMark();
250 for( i = 0 ; i < nMax ; i ++ ) {
251 rOutput->writeBytes( seqWrite );
254 ERROR_ASSERT( 0 == rInput->available() , "bytes available though mark is holded" );
256 ERROR_ASSERT( nMax*seqWrite.getLength() == rMarkable->offsetToMark( nMark ) ,
257 "offsetToMark failure" );
259 rMarkable->deleteMark( nMark );
260 ERROR_ASSERT( nMax*seqWrite.getLength() == rInput->available(),"bytes are not available though mark has been deleted" );
262 rInput->skipBytes( nMax*seqWrite.getLength() );
263 ERROR_ASSERT( 0 == rInput->available(), "skip bytes failure" );
267 rMarkable->jumpToMark( nMark );
268 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
270 catch ( IllegalArgumentException & )
272 // ok, exception was thrown
275 // test putting marks not at the end of the stream!
276 ERROR_ASSERT( 0 == rInput->available(), "stream isn't clean" );
278 Sequence< sal_Int8 > aByte(256);
280 for( i = 0 ; i < 256 ; i ++ )
282 aByte.getArray()[i] = i;
284 sal_Int32 nMark1 = rMarkable->createMark();
286 rOutput->writeBytes( aByte );
287 rMarkable->jumpToMark( nMark1 );
288 aByte.realloc( 10 );
289 rOutput->writeBytes( aByte );
291 sal_Int32 nMark2 = rMarkable->createMark( );
293 for( i = 0 ; i < 10 ; i ++ )
295 aByte.getArray()[i] = i+10;
298 rOutput->writeBytes( aByte );
300 // allow the bytes to be written !
301 rMarkable->jumpToFurthest();
302 rMarkable->deleteMark( nMark1 );
303 rMarkable->deleteMark( nMark2 );
305 ERROR_ASSERT( 256 == rInput->available(), "in between mark failure" );
306 rInput->readBytes( aByte ,256);
307 for( i = 0 ; i < 256 ; i ++ )
309 ERROR_ASSERT( i == ((sal_uInt8*)(aByte.getArray()))[i] , "in between mark failure" );
314 // now a more extensive mark test !
315 Sequence<sal_Int8> as[4];
316 sal_Int32 an[4];
318 for( i = 0 ; i < 4 ; i ++ ) {
319 as[i].realloc(1);
320 as[i].getArray()[0] = i;
321 an[i] = rMarkable->createMark();
322 rOutput->writeBytes( as[i] );
325 // check offset to mark
326 for( i = 0 ; i < 4 ; i ++ ) {
327 ERROR_ASSERT( rMarkable->offsetToMark( an[i] ) == 4-i , "offsetToMark failure" );
330 rMarkable->jumpToMark( an[1] );
331 ERROR_ASSERT( rMarkable->offsetToMark( an[3] ) == -2 , "offsetToMark failure" );
333 rMarkable->jumpToFurthest( );
334 ERROR_ASSERT( rMarkable->offsetToMark( an[0] ) == 4 , "offsetToMark failure" );
336 // now do a rewrite !
337 for( i = 0 ; i < 4 ; i ++ ) {
338 rMarkable->jumpToMark( an[3-i] );
339 rOutput->writeBytes( as[i] );
341 // NOTE : CursorPos 1
343 // now delete the marks !
344 for( i = 0 ; i < 4 ; i ++ ) {
345 rMarkable->deleteMark( an[i] );
347 ERROR_ASSERT( rInput->available() == 1 , "wrong number of bytes flushed" );
349 rMarkable->jumpToFurthest();
351 ERROR_ASSERT( rInput->available() == 4 , "wrong number of bytes flushed" );
353 rInput->readBytes( seqRead , 4 );
355 ERROR_ASSERT( 3 == seqRead.getArray()[0] , "rewrite didn't work" );
356 ERROR_ASSERT( 2 == seqRead.getArray()[1] , "rewrite didn't work" );
357 ERROR_ASSERT( 1 == seqRead.getArray()[2] , "rewrite didn't work" );
358 ERROR_ASSERT( 0 == seqRead.getArray()[3] , "rewrite didn't work" );
360 rOutput->closeOutput();
361 rInput->closeInput();
366 /***
367 * the test methods
369 ****/
376 * for external binding
380 Reference < XInterface > SAL_CALL OMarkableOutputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
382 OMarkableOutputStreamTest *p = new OMarkableOutputStreamTest( rSMgr );
383 return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
388 Sequence<OUString> OMarkableOutputStreamTest_getSupportedServiceNames(void) throw ()
390 Sequence<OUString> aRet(1);
391 aRet.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
393 return aRet;
396 OUString OMarkableOutputStreamTest_getServiceName() throw ()
398 return OUString( "test.com.sun.star.io.MarkableOutputStream");
401 OUString OMarkableOutputStreamTest_getImplementationName() throw ()
403 return OUString( "test.com.sun.starextensions.stm.MarkableOutputStream");
412 //-----------------------------------------------------
413 // Input stream
416 class OMarkableInputStreamTest : public WeakImplHelper1< XSimpleTest >
418 public:
419 OMarkableInputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
420 ~OMarkableInputStreamTest();
422 public: // implementation names
423 static Sequence< OUString > getSupportedServiceNames_Static(void) throw () ;
424 static OUString getImplementationName_Static() throw () ;
426 public:
427 virtual void SAL_CALL testInvariant(
428 const OUString& TestName,
429 const Reference < XInterface >& TestObject)
430 throw ( IllegalArgumentException, RuntimeException) ;
432 virtual sal_Int32 SAL_CALL test(
433 const OUString& TestName,
434 const Reference < XInterface >& TestObject,
435 sal_Int32 hTestHandle)
436 throw ( IllegalArgumentException,
437 RuntimeException) ;
439 virtual sal_Bool SAL_CALL testPassed(void)
440 throw ( RuntimeException);
441 virtual Sequence< OUString > SAL_CALL getErrors(void)
442 throw (RuntimeException);
443 virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
444 throw (RuntimeException);
445 virtual Sequence< OUString > SAL_CALL getWarnings(void)
446 throw (RuntimeException);
448 private:
449 void testSimple( const Reference< XOutputStream > &r,
450 const Reference < XInputStream > &rInput );
452 private:
453 Sequence<Any> m_seqExceptions;
454 Sequence<OUString> m_seqErrors;
455 Sequence<OUString> m_seqWarnings;
456 Reference< XMultiServiceFactory > m_rFactory;
460 OMarkableInputStreamTest::OMarkableInputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
461 : m_rFactory( rFactory )
466 OMarkableInputStreamTest::~OMarkableInputStreamTest()
473 void OMarkableInputStreamTest::testInvariant(
474 const OUString& TestName, const Reference < XInterface >& TestObject )
475 throw ( IllegalArgumentException, RuntimeException)
477 if( OUString( "com.sun.star.io.MarkableInputStream")
478 == TestName ) {
479 Reference <XServiceInfo > info( TestObject, UNO_QUERY );
480 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
481 if( info.is() )
483 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
484 ERROR_ASSERT(
485 ! info->supportsService(
486 OUString("bla bluzb") ) ,
487 "XServiceInfo test failed" );
490 else
492 throw IllegalArgumentException();
497 sal_Int32 OMarkableInputStreamTest::test(
498 const OUString& TestName,
499 const Reference < XInterface >& TestObject,
500 sal_Int32 hTestHandle) throw ( IllegalArgumentException, RuntimeException)
502 if( OUString( "com.sun.star.io.MarkableInputStream") == TestName )
506 if( 0 == hTestHandle ) {
507 testInvariant( TestName , TestObject );
509 else {
510 Reference < XInterface > x = m_rFactory->createInstance( OUString("com.sun.star.io.Pipe"));
511 Reference< XOutputStream > rPipeOutput( x , UNO_QUERY );
512 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
514 Reference < XActiveDataSink > sink( TestObject , UNO_QUERY );
515 sink->setInputStream( rPipeInput );
517 Reference < XInputStream > rInput( TestObject , UNO_QUERY );
519 OSL_ASSERT( rPipeOutput.is() );
520 OSL_ASSERT( rInput.is() );
521 if( 1 == hTestHandle ) {
522 // checks usual streaming
523 testSimple( rPipeOutput , rInput );
528 catch( const Exception & e )
530 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
531 BUILD_ERROR( 0 , o.getStr() );
533 catch( ... )
535 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
538 hTestHandle ++;
540 if( 2 == hTestHandle ) {
541 // all tests finished.
542 hTestHandle = -1;
545 else
547 throw IllegalArgumentException();
549 return hTestHandle;
554 sal_Bool OMarkableInputStreamTest::testPassed(void) throw (RuntimeException)
556 return m_seqErrors.getLength() == 0;
560 Sequence< OUString > OMarkableInputStreamTest::getErrors(void) throw (RuntimeException)
562 return m_seqErrors;
566 Sequence< Any > OMarkableInputStreamTest::getErrorExceptions(void) throw (RuntimeException)
568 return m_seqExceptions;
572 Sequence< OUString > OMarkableInputStreamTest::getWarnings(void) throw (RuntimeException)
574 return m_seqWarnings;
578 void OMarkableInputStreamTest::testSimple( const Reference< XOutputStream > &rOutput ,
579 const Reference < XInputStream > &rInput )
581 Reference < XMarkableStream > rMarkable( rInput , UNO_QUERY );
583 Sequence<sal_Int8> seqWrite( 256 );
584 Sequence<sal_Int8> seqRead(10);
586 for( int i = 0 ; i < 256 ; i ++ )
588 seqWrite.getArray()[i] = i;
591 rOutput->writeBytes( seqWrite );
592 ERROR_ASSERT( 256 == rInput->available() , "basic read/write failure" );
594 rInput->readBytes( seqRead , 10 );
595 ERROR_ASSERT( 9 == seqRead.getArray()[9] , "basic read/write failure" );
597 sal_Int32 nMark = rMarkable->createMark();
599 rInput->skipBytes( 50 );
600 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
601 ERROR_ASSERT( 50 == rMarkable->offsetToMark( nMark ) , "marking error" );
603 rMarkable->jumpToMark( nMark );
604 ERROR_ASSERT( 256-10 == rInput->available() , "marking error" );
606 rInput->readBytes( seqRead , 10 );
607 ERROR_ASSERT( 10 == seqRead.getArray()[0] , "marking error" );
609 // pos 20
611 sal_Int32 nInBetweenMark = rMarkable->createMark( );
612 rMarkable->jumpToMark( nMark );
613 rMarkable->jumpToMark( nInBetweenMark );
615 rInput->readBytes( seqRead , 10 );
616 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
618 rMarkable->deleteMark( nMark );
620 // Check if releasing the first bytes works correct.
621 rMarkable->jumpToMark( nInBetweenMark);
622 rInput->readBytes( seqRead , 10 );
623 ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
625 rMarkable->deleteMark( nInBetweenMark );
628 rMarkable->jumpToFurthest();
629 ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
632 ERROR_ASSERT( 100 == rInput->readSomeBytes( seqRead , 100 ) , "wrong results using readSomeBytes" );
633 ERROR_ASSERT( 96 == rInput->readSomeBytes( seqRead , 1000) , "wrong results using readSomeBytes" );
634 rOutput->closeOutput();
635 rInput->closeInput();
638 /***
639 * the test methods
641 ****/
648 * for external binding
652 Reference < XInterface > SAL_CALL OMarkableInputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
654 OMarkableInputStreamTest *p = new OMarkableInputStreamTest( rSMgr );
655 return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
660 Sequence<OUString> OMarkableInputStreamTest_getSupportedServiceNames(void) throw ()
662 Sequence<OUString> aRet(1);
663 aRet.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
665 return aRet;
668 OUString OMarkableInputStreamTest_getServiceName() throw ()
670 return OUString( "test.com.sun.star.io.MarkableInputStream");
673 OUString OMarkableInputStreamTest_getImplementationName() throw ()
675 return OUString( "test.com.sun.star.extensions.stm.MarkableInputStream" );
678 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */