1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/test/XSimpleTest.hpp>
21 #include <com/sun/star/io/XActiveDataSink.hpp>
22 #include <com/sun/star/io/XActiveDataSource.hpp>
23 #include <com/sun/star/io/XMarkableStream.hpp>
24 #include <com/sun/star/io/XConnectable.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/implbase1.hxx>
31 #include <cppuhelper/implbase2.hxx>
33 #include <osl/conditn.hxx>
34 #include <osl/mutex.hxx>
36 using namespace ::rtl
;
37 using namespace ::osl
;
38 using namespace ::cppu
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::io
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::test
;
45 #include "testfactreg.hxx"
48 class OMarkableOutputStreamTest
: public WeakImplHelper1
< XSimpleTest
>
51 OMarkableOutputStreamTest( const Reference
< XMultiServiceFactory
> & rFactory
);
52 ~OMarkableOutputStreamTest();
54 public: // implementation names
55 static Sequence
< OUString
> getSupportedServiceNames_Static(void) throw ();
56 static OUString
getImplementationName_Static() throw ();
59 virtual void SAL_CALL
testInvariant(
60 const OUString
& TestName
,
61 const Reference
< XInterface
>& TestObject
)
62 throw ( IllegalArgumentException
,
65 virtual sal_Int32 SAL_CALL
test( const OUString
& TestName
,
66 const Reference
< XInterface
>& TestObject
,
67 sal_Int32 hTestHandle
)
68 throw ( IllegalArgumentException
, RuntimeException
);
69 virtual sal_Bool SAL_CALL
testPassed(void)
70 throw ( RuntimeException
);
71 virtual Sequence
< OUString
> SAL_CALL
getErrors(void)
72 throw (RuntimeException
);
73 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions(void)
74 throw (RuntimeException
);
75 virtual Sequence
< OUString
> SAL_CALL
getWarnings(void)
76 throw (RuntimeException
);
79 void testSimple( const Reference
< XOutputStream
> &r
, const Reference
< XInputStream
> &rInput
);
82 Sequence
<Any
> m_seqExceptions
;
83 Sequence
<OUString
> m_seqErrors
;
84 Sequence
<OUString
> m_seqWarnings
;
85 Reference
< XMultiServiceFactory
> m_rFactory
;
89 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const Reference
< XMultiServiceFactory
> &rFactory
)
90 : m_rFactory( rFactory
)
95 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
103 void OMarkableOutputStreamTest::testInvariant( const OUString
& TestName
,
104 const Reference
< XInterface
>& TestObject
)
105 throw ( IllegalArgumentException
, RuntimeException
)
107 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
108 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
111 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
112 ERROR_ASSERT( ! info
->supportsService(
113 OUString( "bla bluzb") ) , "XServiceInfo test failed" );
118 sal_Int32
OMarkableOutputStreamTest::test(
119 const OUString
& TestName
,
120 const Reference
< XInterface
>& TestObject
,
121 sal_Int32 hTestHandle
)
122 throw ( IllegalArgumentException
, RuntimeException
)
124 if( OUString( "com.sun.star.io.MarkableOutputStream" )
128 if( 0 == hTestHandle
)
130 testInvariant( TestName
, TestObject
);
134 Reference
< XInterface
> x
= m_rFactory
->createInstance( OUString("com.sun.star.io.Pipe"));
135 Reference
< XOutputStream
> rPipeOutput( x
, UNO_QUERY
);
136 Reference
< XInputStream
> rPipeInput( x
, UNO_QUERY
);
138 Reference
< XActiveDataSource
> source( TestObject
, UNO_QUERY
);
139 source
->setOutputStream( rPipeOutput
);
141 Reference
< XOutputStream
> rOutput( TestObject
, UNO_QUERY
);
143 OSL_ASSERT( rPipeInput
.is() );
144 OSL_ASSERT( rOutput
.is() );
145 if( 1 == hTestHandle
) {
146 // checks usual streaming
147 testSimple( rOutput
, rPipeInput
);
152 catch( const Exception
&e
)
154 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
155 BUILD_ERROR( 0 , o
.getStr() );
159 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
164 if( 2 == hTestHandle
)
166 // all tests finished.
171 throw IllegalArgumentException();
178 sal_Bool
OMarkableOutputStreamTest::testPassed(void) throw (RuntimeException
)
180 return m_seqErrors
.getLength() == 0;
184 Sequence
< OUString
> OMarkableOutputStreamTest::getErrors(void) throw (RuntimeException
)
190 Sequence
< Any
> OMarkableOutputStreamTest::getErrorExceptions(void) throw (RuntimeException
)
192 return m_seqExceptions
;
196 Sequence
< OUString
> OMarkableOutputStreamTest::getWarnings(void) throw (RuntimeException
)
198 return m_seqWarnings
;
202 void OMarkableOutputStreamTest::testSimple( const Reference
< XOutputStream
> &rOutput
,
203 const Reference
< XInputStream
> &rInput
)
205 Reference
< XMarkableStream
> rMarkable( rOutput
, UNO_QUERY
);
207 ERROR_ASSERT( rMarkable
.is() , "no MarkableStream implemented" );
209 // first check normal input/output facility
210 char pcStr
[] = "Live long and prosper !";
212 Sequence
<sal_Int8
> seqWrite( strlen( pcStr
)+1 );
213 memcpy( seqWrite
.getArray() , pcStr
, seqWrite
.getLength() );
215 Sequence
<sal_Int8
> seqRead( seqWrite
.getLength() );
219 for( i
= 0 ; i
< nMax
; i
++ ) {
220 rOutput
->writeBytes( seqWrite
);
221 rInput
->readBytes( seqRead
, rInput
->available() );
222 ERROR_ASSERT( ! strcmp( (char *) seqWrite
.getArray() , (char * )seqRead
.getArray() ) ,
223 "error during read/write/skip" );
226 // Check buffer resizing
228 for( i
= 0 ; i
< nMax
; i
++ ) {
229 rOutput
->writeBytes( seqWrite
);
232 for( i
= 0 ; i
< nMax
; i
++ ) {
233 rInput
->readBytes( seqRead
, seqWrite
.getLength() );
234 ERROR_ASSERT( ! strcmp( (char *) seqWrite
.getArray() , (char * )seqRead
.getArray() ) ,
235 "error during read/write" );
238 // Check creating marks !
239 sal_Int32 nMark
= rMarkable
->createMark();
241 for( i
= 0 ; i
< nMax
; i
++ ) {
242 rOutput
->writeBytes( seqWrite
);
245 ERROR_ASSERT( 0 == rInput
->available() , "bytes available though mark is holded" );
247 ERROR_ASSERT( nMax
*seqWrite
.getLength() == rMarkable
->offsetToMark( nMark
) ,
248 "offsetToMark failure" );
250 rMarkable
->deleteMark( nMark
);
251 ERROR_ASSERT( nMax
*seqWrite
.getLength() == rInput
->available(),"bytes are not available though mark has been deleted" );
253 rInput
->skipBytes( nMax
*seqWrite
.getLength() );
254 ERROR_ASSERT( 0 == rInput
->available(), "skip bytes failure" );
258 rMarkable
->jumpToMark( nMark
);
259 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
261 catch ( IllegalArgumentException
& )
263 // ok, exception was thrown
266 // test putting marks not at the end of the stream!
267 ERROR_ASSERT( 0 == rInput
->available(), "stream isn't clean" );
269 Sequence
< sal_Int8
> aByte(256);
271 for( i
= 0 ; i
< 256 ; i
++ )
273 aByte
.getArray()[i
] = i
;
275 sal_Int32 nMark1
= rMarkable
->createMark();
277 rOutput
->writeBytes( aByte
);
278 rMarkable
->jumpToMark( nMark1
);
280 rOutput
->writeBytes( aByte
);
282 sal_Int32 nMark2
= rMarkable
->createMark( );
284 for( i
= 0 ; i
< 10 ; i
++ )
286 aByte
.getArray()[i
] = i
+10;
289 rOutput
->writeBytes( aByte
);
291 // allow the bytes to be written !
292 rMarkable
->jumpToFurthest();
293 rMarkable
->deleteMark( nMark1
);
294 rMarkable
->deleteMark( nMark2
);
296 ERROR_ASSERT( 256 == rInput
->available(), "in between mark failure" );
297 rInput
->readBytes( aByte
,256);
298 for( i
= 0 ; i
< 256 ; i
++ )
300 ERROR_ASSERT( i
== ((sal_uInt8
*)(aByte
.getArray()))[i
] , "in between mark failure" );
305 // now a more extensive mark test !
306 Sequence
<sal_Int8
> as
[4];
309 for( i
= 0 ; i
< 4 ; i
++ ) {
311 as
[i
].getArray()[0] = i
;
312 an
[i
] = rMarkable
->createMark();
313 rOutput
->writeBytes( as
[i
] );
316 // check offset to mark
317 for( i
= 0 ; i
< 4 ; i
++ ) {
318 ERROR_ASSERT( rMarkable
->offsetToMark( an
[i
] ) == 4-i
, "offsetToMark failure" );
321 rMarkable
->jumpToMark( an
[1] );
322 ERROR_ASSERT( rMarkable
->offsetToMark( an
[3] ) == -2 , "offsetToMark failure" );
324 rMarkable
->jumpToFurthest( );
325 ERROR_ASSERT( rMarkable
->offsetToMark( an
[0] ) == 4 , "offsetToMark failure" );
327 // now do a rewrite !
328 for( i
= 0 ; i
< 4 ; i
++ ) {
329 rMarkable
->jumpToMark( an
[3-i
] );
330 rOutput
->writeBytes( as
[i
] );
332 // NOTE : CursorPos 1
334 // now delete the marks !
335 for( i
= 0 ; i
< 4 ; i
++ ) {
336 rMarkable
->deleteMark( an
[i
] );
338 ERROR_ASSERT( rInput
->available() == 1 , "wrong number of bytes flushed" );
340 rMarkable
->jumpToFurthest();
342 ERROR_ASSERT( rInput
->available() == 4 , "wrong number of bytes flushed" );
344 rInput
->readBytes( seqRead
, 4 );
346 ERROR_ASSERT( 3 == seqRead
.getArray()[0] , "rewrite didn't work" );
347 ERROR_ASSERT( 2 == seqRead
.getArray()[1] , "rewrite didn't work" );
348 ERROR_ASSERT( 1 == seqRead
.getArray()[2] , "rewrite didn't work" );
349 ERROR_ASSERT( 0 == seqRead
.getArray()[3] , "rewrite didn't work" );
351 rOutput
->closeOutput();
352 rInput
->closeInput();
367 * for external binding
371 Reference
< XInterface
> SAL_CALL
OMarkableOutputStreamTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw(Exception
)
373 OMarkableOutputStreamTest
*p
= new OMarkableOutputStreamTest( rSMgr
);
374 return Reference
< XInterface
> ( (static_cast< OWeakObject
* >(p
)) );
379 Sequence
<OUString
> OMarkableOutputStreamTest_getSupportedServiceNames(void) throw ()
381 Sequence
<OUString
> aRet(1);
382 aRet
.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
387 OUString
OMarkableOutputStreamTest_getServiceName() throw ()
389 return OUString( "test.com.sun.star.io.MarkableOutputStream");
392 OUString
OMarkableOutputStreamTest_getImplementationName() throw ()
394 return OUString( "test.com.sun.starextensions.stm.MarkableOutputStream");
403 //-----------------------------------------------------
407 class OMarkableInputStreamTest
: public WeakImplHelper1
< XSimpleTest
>
410 OMarkableInputStreamTest( const Reference
< XMultiServiceFactory
> & rFactory
);
411 ~OMarkableInputStreamTest();
413 public: // implementation names
414 static Sequence
< OUString
> getSupportedServiceNames_Static(void) throw () ;
415 static OUString
getImplementationName_Static() throw () ;
418 virtual void SAL_CALL
testInvariant(
419 const OUString
& TestName
,
420 const Reference
< XInterface
>& TestObject
)
421 throw ( IllegalArgumentException
, RuntimeException
) ;
423 virtual sal_Int32 SAL_CALL
test(
424 const OUString
& TestName
,
425 const Reference
< XInterface
>& TestObject
,
426 sal_Int32 hTestHandle
)
427 throw ( IllegalArgumentException
,
430 virtual sal_Bool SAL_CALL
testPassed(void)
431 throw ( RuntimeException
);
432 virtual Sequence
< OUString
> SAL_CALL
getErrors(void)
433 throw (RuntimeException
);
434 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions(void)
435 throw (RuntimeException
);
436 virtual Sequence
< OUString
> SAL_CALL
getWarnings(void)
437 throw (RuntimeException
);
440 void testSimple( const Reference
< XOutputStream
> &r
,
441 const Reference
< XInputStream
> &rInput
);
444 Sequence
<Any
> m_seqExceptions
;
445 Sequence
<OUString
> m_seqErrors
;
446 Sequence
<OUString
> m_seqWarnings
;
447 Reference
< XMultiServiceFactory
> m_rFactory
;
451 OMarkableInputStreamTest::OMarkableInputStreamTest( const Reference
< XMultiServiceFactory
> &rFactory
)
452 : m_rFactory( rFactory
)
457 OMarkableInputStreamTest::~OMarkableInputStreamTest()
464 void OMarkableInputStreamTest::testInvariant(
465 const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
466 throw ( IllegalArgumentException
, RuntimeException
)
468 if( OUString( "com.sun.star.io.MarkableInputStream")
470 Reference
<XServiceInfo
> info( TestObject
, UNO_QUERY
);
471 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
474 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
476 ! info
->supportsService(
477 OUString("bla bluzb") ) ,
478 "XServiceInfo test failed" );
483 throw IllegalArgumentException();
488 sal_Int32
OMarkableInputStreamTest::test(
489 const OUString
& TestName
,
490 const Reference
< XInterface
>& TestObject
,
491 sal_Int32 hTestHandle
) throw ( IllegalArgumentException
, RuntimeException
)
493 if( OUString( "com.sun.star.io.MarkableInputStream") == TestName
)
497 if( 0 == hTestHandle
) {
498 testInvariant( TestName
, TestObject
);
501 Reference
< XInterface
> x
= m_rFactory
->createInstance( OUString("com.sun.star.io.Pipe"));
502 Reference
< XOutputStream
> rPipeOutput( x
, UNO_QUERY
);
503 Reference
< XInputStream
> rPipeInput( x
, UNO_QUERY
);
505 Reference
< XActiveDataSink
> sink( TestObject
, UNO_QUERY
);
506 sink
->setInputStream( rPipeInput
);
508 Reference
< XInputStream
> rInput( TestObject
, UNO_QUERY
);
510 OSL_ASSERT( rPipeOutput
.is() );
511 OSL_ASSERT( rInput
.is() );
512 if( 1 == hTestHandle
) {
513 // checks usual streaming
514 testSimple( rPipeOutput
, rInput
);
519 catch( const Exception
& e
)
521 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
522 BUILD_ERROR( 0 , o
.getStr() );
526 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
531 if( 2 == hTestHandle
) {
532 // all tests finished.
538 throw IllegalArgumentException();
545 sal_Bool
OMarkableInputStreamTest::testPassed(void) throw (RuntimeException
)
547 return m_seqErrors
.getLength() == 0;
551 Sequence
< OUString
> OMarkableInputStreamTest::getErrors(void) throw (RuntimeException
)
557 Sequence
< Any
> OMarkableInputStreamTest::getErrorExceptions(void) throw (RuntimeException
)
559 return m_seqExceptions
;
563 Sequence
< OUString
> OMarkableInputStreamTest::getWarnings(void) throw (RuntimeException
)
565 return m_seqWarnings
;
569 void OMarkableInputStreamTest::testSimple( const Reference
< XOutputStream
> &rOutput
,
570 const Reference
< XInputStream
> &rInput
)
572 Reference
< XMarkableStream
> rMarkable( rInput
, UNO_QUERY
);
574 Sequence
<sal_Int8
> seqWrite( 256 );
575 Sequence
<sal_Int8
> seqRead(10);
577 for( int i
= 0 ; i
< 256 ; i
++ )
579 seqWrite
.getArray()[i
] = i
;
582 rOutput
->writeBytes( seqWrite
);
583 ERROR_ASSERT( 256 == rInput
->available() , "basic read/write failure" );
585 rInput
->readBytes( seqRead
, 10 );
586 ERROR_ASSERT( 9 == seqRead
.getArray()[9] , "basic read/write failure" );
588 sal_Int32 nMark
= rMarkable
->createMark();
590 rInput
->skipBytes( 50 );
591 ERROR_ASSERT( 256-10-50 == rInput
->available() , "marking error" );
592 ERROR_ASSERT( 50 == rMarkable
->offsetToMark( nMark
) , "marking error" );
594 rMarkable
->jumpToMark( nMark
);
595 ERROR_ASSERT( 256-10 == rInput
->available() , "marking error" );
597 rInput
->readBytes( seqRead
, 10 );
598 ERROR_ASSERT( 10 == seqRead
.getArray()[0] , "marking error" );
602 sal_Int32 nInBetweenMark
= rMarkable
->createMark( );
603 rMarkable
->jumpToMark( nMark
);
604 rMarkable
->jumpToMark( nInBetweenMark
);
606 rInput
->readBytes( seqRead
, 10 );
607 ERROR_ASSERT( 20 == seqRead
.getArray()[0] , "Inbetween mark failed!\n" );
609 rMarkable
->deleteMark( nMark
);
611 // Check if releasing the first bytes works correct.
612 rMarkable
->jumpToMark( nInBetweenMark
);
613 rInput
->readBytes( seqRead
, 10 );
614 ERROR_ASSERT( 20 == seqRead
.getArray()[0] , "Inbetween mark failed!\n" );
616 rMarkable
->deleteMark( nInBetweenMark
);
619 rMarkable
->jumpToFurthest();
620 ERROR_ASSERT( 256-10-50 == rInput
->available() , "marking error" );
623 ERROR_ASSERT( 100 == rInput
->readSomeBytes( seqRead
, 100 ) , "wrong results using readSomeBytes" );
624 ERROR_ASSERT( 96 == rInput
->readSomeBytes( seqRead
, 1000) , "wrong results using readSomeBytes" );
625 rOutput
->closeOutput();
626 rInput
->closeInput();
639 * for external binding
643 Reference
< XInterface
> SAL_CALL
OMarkableInputStreamTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw(Exception
)
645 OMarkableInputStreamTest
*p
= new OMarkableInputStreamTest( rSMgr
);
646 return Reference
< XInterface
> ( (static_cast< OWeakObject
* >(p
)) );
651 Sequence
<OUString
> OMarkableInputStreamTest_getSupportedServiceNames(void) throw ()
653 Sequence
<OUString
> aRet(1);
654 aRet
.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
659 OUString
OMarkableInputStreamTest_getServiceName() throw ()
661 return OUString( "test.com.sun.star.io.MarkableInputStream");
664 OUString
OMarkableInputStreamTest_getImplementationName() throw ()
666 return OUString( "test.com.sun.star.extensions.stm.MarkableInputStream" );
669 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */