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 <smart/com/sun/star/test/XSimpleTest.hxx>
21 #include <smart/com/sun/star/io/XMarkableStream.hxx>
22 #include <smart/com/sun/star/io/XActiveDataSink.hxx>
23 #include <smart/com/sun/star/io/XActiveDataSource.hxx>
24 #include <smart/com/sun/star/io/XConnectable.hxx>
26 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
29 #include <usr/factoryhlp.hxx>
31 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
32 #include <usr/weak.hxx> // OWeakObject
34 #include <osl/conditn.hxx>
35 #include <osl/mutex.hxx>
36 #include <osl/thread.hxx>
40 #include "testfactreg.hxx"
45 class OMarkableOutputStreamTest
:
50 OMarkableOutputStreamTest( const XMultiServiceFactoryRef
& rFactory
);
51 ~OMarkableOutputStreamTest();
53 public: // refcounting
54 BOOL
queryInterface( Uik aUik
, XInterfaceRef
& rOut
);
55 void acquire() { OWeakObject::acquire(); }
56 void release() { OWeakObject::release(); }
57 void* getImplementation(Reflection
*p
) { return OWeakObject::getImplementation(p
); }
59 public: // implementation names
60 static Sequence
< UString
> getSupportedServiceNames_Static(void) THROWS(());
61 static UString
getImplementationName_Static() THROWS(());
64 virtual void testInvariant(const UString
& TestName
, const XInterfaceRef
& TestObject
)
65 THROWS( ( IllegalArgumentException
,
66 UsrSystemException
) );
68 virtual INT32
test( const UString
& TestName
,
69 const XInterfaceRef
& TestObject
,
70 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
71 UsrSystemException
) );
73 virtual BOOL
testPassed(void) THROWS( ( UsrSystemException
) );
74 virtual Sequence
< UString
> getErrors(void) THROWS( (UsrSystemException
) );
75 virtual Sequence
< UsrAny
> getErrorExceptions(void) THROWS( (UsrSystemException
) );
76 virtual Sequence
< UString
> getWarnings(void) THROWS( (UsrSystemException
) );
79 void testSimple( const XOutputStreamRef
&r
, const XInputStreamRef
&rInput
);
82 Sequence
<UsrAny
> m_seqExceptions
;
83 Sequence
<UString
> m_seqErrors
;
84 Sequence
<UString
> m_seqWarnings
;
85 XMultiServiceFactoryRef m_rFactory
;
89 OMarkableOutputStreamTest::OMarkableOutputStreamTest( const XMultiServiceFactoryRef
&rFactory
)
90 : m_rFactory( rFactory
)
95 OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
101 BOOL
OMarkableOutputStreamTest::queryInterface( Uik uik
, XInterfaceRef
&rOut
)
103 if( XSimpleTest::getSmartUik() == uik
) {
104 rOut
= (XSimpleTest
*) this;
107 return OWeakObject::queryInterface( uik
, rOut
);
113 void OMarkableOutputStreamTest::testInvariant( const UString
& TestName
, const XInterfaceRef
& TestObject
)
114 THROWS( ( IllegalArgumentException
,
115 UsrSystemException
) )
117 XServiceInfoRef
info( TestObject
, USR_QUERY
);
118 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
121 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
122 ERROR_ASSERT( ! info
->supportsService( L
"bla bluzb" ) , "XServiceInfo test failed" );
127 INT32
OMarkableOutputStreamTest::test( const UString
& TestName
,
128 const XInterfaceRef
& TestObject
,
129 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
130 UsrSystemException
) )
132 if( L
"com.sun.star.io.MarkableOutputStream" == TestName
) {
134 if( 0 == hTestHandle
) {
135 testInvariant( TestName
, TestObject
);
138 XInterfaceRef x
= m_rFactory
->createInstance( L
"com.sun.star.io.Pipe");
139 XOutputStreamRef
rPipeOutput( x
, USR_QUERY
);
140 XInputStreamRef
rPipeInput( x
, USR_QUERY
);
142 XActiveDataSourceRef
source( TestObject
, USR_QUERY
);
143 source
->setOutputStream( rPipeOutput
);
145 XOutputStreamRef
rOutput( TestObject
, USR_QUERY
);
147 OSL_ASSERT( rPipeInput
.is() );
148 OSL_ASSERT( rOutput
.is() );
149 if( 1 == hTestHandle
) {
150 // checks usual streaming
151 testSimple( rOutput
, rPipeInput
);
156 catch( Exception
& e
) {
157 BUILD_ERROR( 0 , UStringToString( e
.getName() , CHARSET_SYSTEM
).GetCharStr() );
160 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
165 if( 2 == hTestHandle
) {
166 // all tests finished.
171 THROW( IllegalArgumentException() );
178 BOOL
OMarkableOutputStreamTest::testPassed(void) THROWS( (UsrSystemException
) )
180 return m_seqErrors
.getLen() == 0;
184 Sequence
< UString
> OMarkableOutputStreamTest::getErrors(void) THROWS( (UsrSystemException
) )
190 Sequence
< UsrAny
> OMarkableOutputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException
) )
192 return m_seqExceptions
;
196 Sequence
< UString
> OMarkableOutputStreamTest::getWarnings(void) THROWS( (UsrSystemException
) )
198 return m_seqWarnings
;
202 void OMarkableOutputStreamTest::testSimple( const XOutputStreamRef
&rOutput
,
203 const XInputStreamRef
&rInput
)
205 XMarkableStreamRef
rMarkable( rOutput
, USR_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
<BYTE
> seqWrite( strlen( pcStr
)+1 );
213 memcpy( seqWrite
.getArray() , pcStr
, seqWrite
.getLen() );
215 Sequence
<BYTE
> seqRead( seqWrite
.getLen() );
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
.getLen() );
234 ERROR_ASSERT( ! strcmp( (char *) seqWrite
.getArray() , (char * )seqRead
.getArray() ) ,
235 "error during read/write" );
238 // Check creating marks !
239 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
.getLen() == rMarkable
->offsetToMark( nMark
) ,
248 "offsetToMark failure" );
250 rMarkable
->deleteMark( nMark
);
251 ERROR_ASSERT( nMax
*seqWrite
.getLen() == rInput
->available(),"bytes are not available though mark has been deleted" );
253 rInput
->skipBytes( nMax
*seqWrite
.getLen() );
254 ERROR_ASSERT( 0 == rInput
->available(), "skip bytes failure" );
257 rMarkable
->jumpToMark( nMark
);
258 ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
260 catch ( IllegalArgumentException
& e
)
262 e
;// ok, exception was thrown
265 // test putting marks not at the end of the stream!
266 ERROR_ASSERT( 0 == rInput
->available(), "stream isn't clean" );
268 Sequence
< BYTE
> aByte(256);
270 for( i
= 0 ; i
< 256 ; i
++ )
272 aByte
.getArray()[i
] = i
;
274 INT32 nMark1
= rMarkable
->createMark();
276 rOutput
->writeBytes( aByte
);
277 rMarkable
->jumpToMark( nMark1
);
279 rOutput
->writeBytes( aByte
);
281 INT32 nMark2
= rMarkable
->createMark( );
283 for( i
= 0 ; i
< 10 ; i
++ )
285 aByte
.getArray()[i
] = i
+10;
288 rOutput
->writeBytes( aByte
);
290 // allow the bytes to be written !
291 rMarkable
->jumpToFurthest();
292 rMarkable
->deleteMark( nMark1
);
293 rMarkable
->deleteMark( nMark2
);
295 ERROR_ASSERT( 256 == rInput
->available(), "in between mark failure" );
296 rInput
->readBytes( aByte
,256);
297 for( i
= 0 ; i
< 256 ; i
++ )
299 ERROR_ASSERT( i
== aByte
.getArray()[i
] , "in between mark failure" );
304 // now a more extensive mark test !
305 Sequence
<BYTE
> as
[4];
308 for( i
= 0 ; i
< 4 ; i
++ ) {
310 as
[i
].getArray()[0] = i
;
311 an
[i
] = rMarkable
->createMark();
312 rOutput
->writeBytes( as
[i
] );
315 // check offset to mark
316 for( i
= 0 ; i
< 4 ; i
++ ) {
317 ERROR_ASSERT( rMarkable
->offsetToMark( an
[i
] ) == 4-i
, "offsetToMark failure" );
320 rMarkable
->jumpToMark( an
[1] );
321 ERROR_ASSERT( rMarkable
->offsetToMark( an
[3] ) == -2 , "offsetToMark failure" );
323 rMarkable
->jumpToFurthest( );
324 ERROR_ASSERT( rMarkable
->offsetToMark( an
[0] ) == 4 , "offsetToMark failure" );
326 // now do a rewrite !
327 for( i
= 0 ; i
< 4 ; i
++ ) {
328 rMarkable
->jumpToMark( an
[3-i
] );
329 rOutput
->writeBytes( as
[i
] );
331 // NOTE : CursorPos 1
333 // now delete the marks !
334 for( i
= 0 ; i
< 4 ; i
++ ) {
335 rMarkable
->deleteMark( an
[i
] );
337 ERROR_ASSERT( rInput
->available() == 1 , "wrong number of bytes flushed" );
339 rMarkable
->jumpToFurthest();
341 ERROR_ASSERT( rInput
->available() == 4 , "wrong number of bytes flushed" );
343 rInput
->readBytes( seqRead
, 4 );
345 ERROR_ASSERT( 3 == seqRead
.getArray()[0] , "rewrite didn't work" );
346 ERROR_ASSERT( 2 == seqRead
.getArray()[1] , "rewrite didn't work" );
347 ERROR_ASSERT( 1 == seqRead
.getArray()[2] , "rewrite didn't work" );
348 ERROR_ASSERT( 0 == seqRead
.getArray()[3] , "rewrite didn't work" );
350 rOutput
->closeOutput();
351 rInput
->closeInput();
357 * for external binding
359 XInterfaceRef
OMarkableOutputStreamTest_CreateInstance( const XMultiServiceFactoryRef
& rSMgr
) THROWS((Exception
))
361 OMarkableOutputStreamTest
*p
= new OMarkableOutputStreamTest( rSMgr
);
362 XInterfaceRef xService
= *p
;
368 Sequence
<UString
> OMarkableOutputStreamTest_getSupportedServiceNames(void) THROWS(())
370 Sequence
<UString
> aRet(1);
371 aRet
.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
376 UString
OMarkableOutputStreamTest_getServiceName() THROWS(())
378 return L
"test.com.sun.star.io.MarkableOutputStream";
381 UString
OMarkableOutputStreamTest_getImplementationName() THROWS(())
383 return L
"test.com.sun.starextensions.stm.MarkableOutputStream";
392 //-----------------------------------------------------
395 class OMarkableInputStreamTest
:
400 OMarkableInputStreamTest( const XMultiServiceFactoryRef
& rFactory
);
401 ~OMarkableInputStreamTest();
403 public: // refcounting
404 BOOL
queryInterface( Uik aUik
, XInterfaceRef
& rOut
);
405 void acquire() { OWeakObject::acquire(); }
406 void release() { OWeakObject::release(); }
407 void* getImplementation(Reflection
*p
) { return OWeakObject::getImplementation(p
); }
409 public: // implementation names
410 static Sequence
< UString
> getSupportedServiceNames_Static(void) THROWS(());
411 static UString
getImplementationName_Static() THROWS(());
414 virtual void testInvariant(const UString
& TestName
, const XInterfaceRef
& TestObject
)
415 THROWS( ( IllegalArgumentException
,
416 UsrSystemException
) );
418 virtual INT32
test( const UString
& TestName
,
419 const XInterfaceRef
& TestObject
,
420 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
421 UsrSystemException
) );
423 virtual BOOL
testPassed(void) THROWS( ( UsrSystemException
) );
424 virtual Sequence
< UString
> getErrors(void) THROWS( (UsrSystemException
) );
425 virtual Sequence
< UsrAny
> getErrorExceptions(void) THROWS( (UsrSystemException
) );
426 virtual Sequence
< UString
> getWarnings(void) THROWS( (UsrSystemException
) );
429 void testSimple( const XOutputStreamRef
&r
, const XInputStreamRef
&rInput
);
432 Sequence
<UsrAny
> m_seqExceptions
;
433 Sequence
<UString
> m_seqErrors
;
434 Sequence
<UString
> m_seqWarnings
;
435 XMultiServiceFactoryRef m_rFactory
;
439 OMarkableInputStreamTest::OMarkableInputStreamTest( const XMultiServiceFactoryRef
&rFactory
)
440 : m_rFactory( rFactory
)
445 OMarkableInputStreamTest::~OMarkableInputStreamTest()
451 BOOL
OMarkableInputStreamTest::queryInterface( Uik uik
, XInterfaceRef
&rOut
)
453 if( XSimpleTest::getSmartUik() == uik
) {
454 rOut
= (XSimpleTest
*) this;
457 return OWeakObject::queryInterface( uik
, rOut
);
463 void OMarkableInputStreamTest::testInvariant( const UString
& TestName
, const XInterfaceRef
& TestObject
)
464 THROWS( ( IllegalArgumentException
,
465 UsrSystemException
) )
467 if( L
"com.sun.star.io.MarkableInputStream" == TestName
) {
468 XServiceInfoRef
info( TestObject
, USR_QUERY
);
469 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
472 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
473 ERROR_ASSERT( ! info
->supportsService( L
"bla bluzb" ) , "XServiceInfo test failed" );
477 THROW( IllegalArgumentException() );
482 INT32
OMarkableInputStreamTest::test( const UString
& TestName
,
483 const XInterfaceRef
& TestObject
,
484 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
485 UsrSystemException
) )
487 if( L
"com.sun.star.io.MarkableInputStream" == TestName
) {
489 if( 0 == hTestHandle
) {
490 testInvariant( TestName
, TestObject
);
493 XInterfaceRef x
= m_rFactory
->createInstance( L
"com.sun.star.io.Pipe");
494 XOutputStreamRef
rPipeOutput( x
, USR_QUERY
);
495 XInputStreamRef
rPipeInput( x
, USR_QUERY
);
497 XActiveDataSinkRef
sink( TestObject
, USR_QUERY
);
498 sink
->setInputStream( rPipeInput
);
500 XInputStreamRef
rInput( TestObject
, USR_QUERY
);
502 OSL_ASSERT( rPipeOutput
.is() );
503 OSL_ASSERT( rInput
.is() );
504 if( 1 == hTestHandle
) {
505 // checks usual streaming
506 testSimple( rPipeOutput
, rInput
);
511 catch( Exception
& e
) {
512 BUILD_ERROR( 0 , UStringToString( e
.getName() , CHARSET_SYSTEM
).GetCharStr() );
515 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
520 if( 2 == hTestHandle
) {
521 // all tests finished.
526 THROW( IllegalArgumentException() );
533 BOOL
OMarkableInputStreamTest::testPassed(void) THROWS( (UsrSystemException
) )
535 return m_seqErrors
.getLen() == 0;
539 Sequence
< UString
> OMarkableInputStreamTest::getErrors(void) THROWS( (UsrSystemException
) )
545 Sequence
< UsrAny
> OMarkableInputStreamTest::getErrorExceptions(void) THROWS( (UsrSystemException
) )
547 return m_seqExceptions
;
551 Sequence
< UString
> OMarkableInputStreamTest::getWarnings(void) THROWS( (UsrSystemException
) )
553 return m_seqWarnings
;
557 void OMarkableInputStreamTest::testSimple( const XOutputStreamRef
&rOutput
,
558 const XInputStreamRef
&rInput
)
560 XMarkableStreamRef
rMarkable( rInput
, USR_QUERY
);
562 Sequence
<BYTE
> seqWrite( 256 );
563 Sequence
<BYTE
> seqRead(10);
565 for( int i
= 0 ; i
< 256 ; i
++ )
567 seqWrite
.getArray()[i
] = i
;
570 rOutput
->writeBytes( seqWrite
);
571 ERROR_ASSERT( 256 == rInput
->available() , "basic read/write failure" );
573 rInput
->readBytes( seqRead
, 10 );
574 ERROR_ASSERT( 9 == seqRead
.getArray()[9] , "basic read/write failure" );
576 INT32 nMark
= rMarkable
->createMark();
578 rInput
->skipBytes( 50 );
579 ERROR_ASSERT( 256-10-50 == rInput
->available() , "marking error" );
580 ERROR_ASSERT( 50 == rMarkable
->offsetToMark( nMark
) , "marking error" );
582 rMarkable
->jumpToMark( nMark
);
583 ERROR_ASSERT( 256-10 == rInput
->available() , "marking error" );
585 rInput
->readBytes( seqRead
, 10 );
586 ERROR_ASSERT( 10 == seqRead
.getArray()[0] , "marking error" );
590 INT32 nInBetweenMark
= rMarkable
->createMark( );
591 rMarkable
->jumpToMark( nMark
);
592 rMarkable
->jumpToMark( nInBetweenMark
);
594 rInput
->readBytes( seqRead
, 10 );
595 ERROR_ASSERT( 20 == seqRead
.getArray()[0] , "Inbetween mark failed!\n" );
597 rMarkable
->deleteMark( nMark
);
599 // Check if releasing the first bytes works correct.
600 rMarkable
->jumpToMark( nInBetweenMark
);
601 rInput
->readBytes( seqRead
, 10 );
602 ERROR_ASSERT( 20 == seqRead
.getArray()[0] , "Inbetween mark failed!\n" );
604 rMarkable
->deleteMark( nInBetweenMark
);
607 rMarkable
->jumpToFurthest();
608 ERROR_ASSERT( 256-10-50 == rInput
->available() , "marking error" );
611 ERROR_ASSERT( 100 == rInput
->readSomeBytes( seqRead
, 100 ) , "wrong results using readSomeBytes" );
612 ERROR_ASSERT( 96 == rInput
->readSomeBytes( seqRead
, 1000) , "wrong results using readSomeBytes" );
613 rOutput
->closeOutput();
614 rInput
->closeInput();
618 * for external binding
620 XInterfaceRef
OMarkableInputStreamTest_CreateInstance( const XMultiServiceFactoryRef
& rSMgr
) THROWS((Exception
))
622 OMarkableInputStreamTest
*p
= new OMarkableInputStreamTest( rSMgr
);
623 XInterfaceRef xService
= *p
;
629 Sequence
<UString
> OMarkableInputStreamTest_getSupportedServiceNames(void) THROWS(())
631 Sequence
<UString
> aRet(1);
632 aRet
.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
637 UString
OMarkableInputStreamTest_getServiceName() THROWS(())
639 return L
"test.com.sun.star.io.MarkableInputStream";
642 UString
OMarkableInputStreamTest_getImplementationName() THROWS(())
644 return L
"test.com.sun.star.extensions.stm.MarkableInputStream";
647 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */