1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: datatest.cxx,v $
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"
36 #include <com/sun/star/test/XSimpleTest.hpp>
37 #include <com/sun/star/io/XActiveDataSink.hpp>
38 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <com/sun/star/io/XObjectInputStream.hpp>
40 #include <com/sun/star/io/XObjectOutputStream.hpp>
41 #include <com/sun/star/io/XMarkableStream.hpp>
42 #include <com/sun/star/io/XConnectable.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/lang/WrappedTargetException.hpp>
46 #include <com/sun/star/lang/IllegalArgumentException.hpp>
48 #include <com/sun/star/lang/XServiceInfo.hpp>
50 #include <cppuhelper/factory.hxx>
52 #include <cppuhelper/implbase1.hxx>
53 #include <cppuhelper/implbase2.hxx>
55 #include <osl/conditn.hxx>
56 #include <osl/mutex.hxx>
58 #if OSL_DEBUG_LEVEL == 0
64 using namespace ::rtl
;
65 using namespace ::osl
;
66 using namespace ::cppu
;
67 //using namespace ::vos;
68 using namespace ::com::sun::star::uno
;
69 using namespace ::com::sun::star::io
;
70 using namespace ::com::sun::star::lang
;
71 using namespace ::com::sun::star::test
;
72 using namespace ::com::sun::star::beans
;
75 #include "testfactreg.hxx"
77 #define DATASTREAM_TEST_MAX_HANDLE 1
80 * The following test class tests XDataInputStream and XDataOutputStream at equal terms,
81 * so when errors occur, it may be in either one implementation.
82 * The class also uses stardiv.uno.io.pipe. If problems occur, make sure to run also the
83 * pipe test routines ( test.com.sun.star.io.pipe ).
88 class ODataStreamTest
:
89 public WeakImplHelper1
< XSimpleTest
>
92 ODataStreamTest( const Reference
< XMultiServiceFactory
> & rFactory
) :
93 m_rFactory( rFactory
)
97 virtual void SAL_CALL
testInvariant(const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
98 throw ( IllegalArgumentException
,
101 virtual sal_Int32 SAL_CALL
test( const OUString
& TestName
,
102 const Reference
< XInterface
>& TestObject
,
103 sal_Int32 hTestHandle
)
104 throw ( IllegalArgumentException
,
107 virtual sal_Bool SAL_CALL
testPassed(void) throw ( RuntimeException
);
108 virtual Sequence
< OUString
> SAL_CALL
getErrors(void) throw (RuntimeException
);
109 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions(void) throw (RuntimeException
);
110 virtual Sequence
< OUString
> SAL_CALL
getWarnings(void) throw (RuntimeException
);
113 void testSimple( const Reference
< XDataInputStream
> & , const Reference
< XDataOutputStream
> &);
116 Sequence
<Any
> m_seqExceptions
;
117 Sequence
<OUString
> m_seqErrors
;
118 Sequence
<OUString
> m_seqWarnings
;
120 Reference
< XMultiServiceFactory
> m_rFactory
;
126 void ODataStreamTest::testInvariant(
127 const OUString
& TestName
,
128 const Reference
< XInterface
>& TestObject
)
129 throw ( IllegalArgumentException
,
132 if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName
) {
133 Reference
< XConnectable
> connect( TestObject
, UNO_QUERY
);
134 Reference
< XActiveDataSink
> active( TestObject
, UNO_QUERY
);
135 Reference
< XInputStream
> input( TestObject
, UNO_QUERY
);
136 Reference
< XDataInputStream
> dataInput( TestObject
, UNO_QUERY
);
138 WARNING_ASSERT( connect
.is(), "XConnectable cannot be queried" );
139 WARNING_ASSERT( active
.is() , "XActiveDataSink cannot be queried" );
140 ERROR_ASSERT( input
.is() , "XInputStream cannot be queried" );
141 ERROR_ASSERT( dataInput
.is() , "XDataInputStream cannot be queried" );
145 else if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName
) {
146 Reference
< XConnectable
> connect( TestObject
, UNO_QUERY
);
147 Reference
< XActiveDataSource
> active( TestObject
, UNO_QUERY
);
148 Reference
< XOutputStream
> output( TestObject
, UNO_QUERY
);
149 Reference
< XDataOutputStream
> dataOutput( TestObject
, UNO_QUERY
);
151 WARNING_ASSERT( connect
.is(), "XConnectable cannot be queried" );
152 WARNING_ASSERT( active
.is() , "XActiveDataSink cannot be queried" );
153 ERROR_ASSERT( output
.is() , "XInputStream cannot be queried" );
154 ERROR_ASSERT( dataOutput
.is(), "XDataInputStream cannot be queried" );
158 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
159 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
162 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
163 ERROR_ASSERT( ! info
->supportsService( OUString::createFromAscii("bla bluzb") ) , "XServiceInfo test failed" );
169 sal_Int32
ODataStreamTest::test(
170 const OUString
& TestName
,
171 const Reference
< XInterface
>& TestObject
,
172 sal_Int32 hTestHandle
)
173 throw ( IllegalArgumentException
,
176 if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName
||
177 OUString::createFromAscii("com.sun.star.io.DataOutputStream") == TestName
) {
181 if( 0 == hTestHandle
) {
182 testInvariant( TestName
, TestObject
);
185 Reference
<XActiveDataSink
> rSink( TestObject
, UNO_QUERY
);
186 Reference
<XActiveDataSource
> rSource( TestObject
, UNO_QUERY
);
188 Reference
< XDataInputStream
> rInput( TestObject
, UNO_QUERY
);
189 Reference
< XDataOutputStream
> rOutput( TestObject
, UNO_QUERY
);
192 Reference
< XInterface
> x
= m_rFactory
->createInstance(
193 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe" )));
195 Reference
< XInputStream
> rPipeInput( x
, UNO_QUERY
);
196 Reference
< XOutputStream
> rPipeOutput( x
, UNO_QUERY
);
199 x
= m_rFactory
->createInstance(
200 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataInputStream")) );
201 rInput
= Reference
< XDataInputStream
> ( x
, UNO_QUERY
);
202 rSink
= Reference
< XActiveDataSink
> ( x
, UNO_QUERY
);
204 else if ( !rSource
.is() )
206 x
= m_rFactory
->createInstance(
207 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataOutputStream") ) );
208 rOutput
= Reference
< XDataOutputStream
> ( x
, UNO_QUERY
);
209 rSource
= Reference
< XActiveDataSource
> ( x
, UNO_QUERY
);
212 assert( rPipeInput
.is() );
213 assert( rPipeOutput
.is() );
214 rSink
->setInputStream( rPipeInput
);
215 rSource
->setOutputStream( rPipeOutput
);
217 assert( rSink
->getInputStream().is() );
218 assert( rSource
->getOutputStream().is() );
220 if( 1 == hTestHandle
) {
221 testSimple( rInput
, rOutput
);
225 catch( Exception
& e
)
227 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
228 BUILD_ERROR( 0 , o
.getStr() );
232 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
237 if( hTestHandle
>= 2) {
238 // all tests finished.
243 BUILD_ERROR( 0 , "service not supported by test." );
250 sal_Bool
ODataStreamTest::testPassed(void) throw (RuntimeException
)
252 return m_seqErrors
.getLength() == 0;
256 Sequence
< OUString
> ODataStreamTest::getErrors(void) throw (RuntimeException
)
262 Sequence
< Any
> ODataStreamTest::getErrorExceptions(void) throw (RuntimeException
)
264 return m_seqExceptions
;
268 Sequence
< OUString
> ODataStreamTest::getWarnings(void) throw (RuntimeException
)
270 return m_seqWarnings
;
273 void ODataStreamTest::testSimple( const Reference
< XDataInputStream
> &rInput
,
274 const Reference
< XDataOutputStream
> &rOutput
)
276 rOutput
->writeLong( 0x34ff3c );
277 rOutput
->writeLong( 0x34ff3d );
278 rOutput
->writeLong( -1027 );
280 ERROR_ASSERT( 0x34ff3c == rInput
->readLong() , "long read/write mismatch" );
281 ERROR_ASSERT( 0x34ff3d == rInput
->readLong() , "long read/write mismatch" );
282 ERROR_ASSERT( -1027 == rInput
->readLong() , "long read/write mismatch" );
284 rOutput
->writeByte( 0x77 );
285 ERROR_ASSERT( 0x77 == rInput
->readByte() , "byte read/write mismatch" );
287 rOutput
->writeBoolean( 25 );
288 ERROR_ASSERT( rInput
->readBoolean() , "boolean read/write mismatch" );
290 rOutput
->writeBoolean( sal_False
);
291 ERROR_ASSERT( ! rInput
->readBoolean() , "boolean read/write mismatch" );
293 rOutput
->writeFloat( (float) 42.42 );
294 ERROR_ASSERT( rInput
->readFloat() == ((float)42.42) , "float read/write mismatch" );
296 rOutput
->writeDouble( (double) 42.42 );
297 ERROR_ASSERT( rInput
->readDouble() == 42.42 , "double read/write mismatch" );
299 rOutput
->writeHyper( 0x123456789abcdefLL
);
300 ERROR_ASSERT( rInput
->readHyper() == 0x123456789abcdefLL
, "int64 read/write mismatch" );
302 rOutput
->writeUTF( OUString::createFromAscii("Live long and prosper !") );
303 ERROR_ASSERT( rInput
->readUTF() == OUString::createFromAscii("Live long and prosper !") ,
304 "UTF read/write mismatch" );
306 Sequence
<sal_Unicode
> wc(0x10001);
307 for( int i
= 0 ; i
< 0x10000 ; i
++ ) {
308 wc
.getArray()[i
] = L
'c';
310 wc
.getArray()[0x10000] = 0;
311 OUString
str( wc
.getArray() , 0x10000 );
312 rOutput
->writeUTF( str
);
313 ERROR_ASSERT( rInput
->readUTF() == str
, "error reading 64k block" );
315 rOutput
->closeOutput();
319 ERROR_ASSERT( 0 , "eof-exception does not occur !" );
321 catch ( IOException
& )
327 ERROR_ASSERT( 0 , "wrong exception after reading beyond eof" );
330 Sequence
<sal_Int8
> dummy (1);
331 ERROR_ASSERT( ! rInput
->readBytes( dummy
, 1 ),
332 "stream must be on eof !" );
334 rInput
->closeInput();
338 rOutput
->writeByte( 1 );
339 ERROR_ASSERT( 0 , "writing still possible though chain must be interrupted" );
341 catch( IOException
& )
346 ERROR_ASSERT( 0 , "IOException expected, but another exception was thrown" );
354 * for external binding
358 Reference
< XInterface
> SAL_CALL
ODataStreamTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw(Exception
)
360 ODataStreamTest
*p
= new ODataStreamTest( rSMgr
);
361 return Reference
< XInterface
> ( SAL_STATIC_CAST( OWeakObject
* , p
) );
364 Sequence
<OUString
> ODataStreamTest_getSupportedServiceNames( int i
) throw ()
366 Sequence
<OUString
> aRet(1);
368 aRet
.getArray()[0] = ODataStreamTest_getImplementationName( i
);
374 OUString
ODataStreamTest_getServiceName( int i
) throw ()
377 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataInputStream" ));
380 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataOutputStream" ));
384 OUString
ODataStreamTest_getImplementationName( int i
) throw ()
388 RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.DataInputStream") );
391 return OUString( RTL_CONSTASCII_USTRINGPARAM(
392 "test.com.sun.star.comp.extensions.stm.DataOutputStream" ) );
397 /**------------------------------------------------------
403 *------------------------------------------------------*/
404 class MyPersistObject
: public WeakImplHelper2
< XPersistObject
, XPropertySet
>
407 MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() ) ,
414 m_s( OUString( RTL_CONSTASCII_USTRINGPARAM( "foo" ) ) )
416 MyPersistObject( const OUString
& sServiceName
) : m_sServiceName( sServiceName
)
421 virtual OUString SAL_CALL
getServiceName(void) throw (RuntimeException
);
422 virtual void SAL_CALL
write( const Reference
< XObjectOutputStream
>& OutStream
)
423 throw (IOException
, RuntimeException
);
424 virtual void SAL_CALL
read(const Reference
< XObjectInputStream
>& InStream
)
425 throw (IOException
, RuntimeException
);
429 virtual Reference
< XPropertySetInfo
> SAL_CALL
getPropertySetInfo(void)
430 throw (RuntimeException
);
432 virtual void SAL_CALL
setPropertyValue(const OUString
& aPropertyName
, const Any
& aValue
)
433 throw ( UnknownPropertyException
,
434 PropertyVetoException
,
435 IllegalArgumentException
,
436 WrappedTargetException
,
438 virtual Any SAL_CALL
getPropertyValue(const OUString
& PropertyName
)
439 throw ( UnknownPropertyException
,
440 WrappedTargetException
,
442 virtual void SAL_CALL
addPropertyChangeListener(
443 const OUString
& aPropertyName
,
444 const Reference
< XPropertyChangeListener
> & xListener
)
445 throw ( UnknownPropertyException
,
446 WrappedTargetException
,
449 virtual void SAL_CALL
removePropertyChangeListener(
450 const OUString
& aPropertyName
,
451 const Reference
< XPropertyChangeListener
> & aListener
)
452 throw ( UnknownPropertyException
,
453 WrappedTargetException
,
455 virtual void SAL_CALL
addVetoableChangeListener(
456 const OUString
& PropertyName
,
457 const Reference
< XVetoableChangeListener
> & aListener
)
458 throw ( UnknownPropertyException
,
459 WrappedTargetException
,
462 virtual void SAL_CALL
removeVetoableChangeListener(
463 const OUString
& PropertyName
,
464 const Reference
< XVetoableChangeListener
>& aListener
)
465 throw ( UnknownPropertyException
,
466 WrappedTargetException
,
477 Reference
< XPersistObject
> m_ref
;
478 OUString m_sServiceName
;
483 Reference
<XPropertySetInfo
> MyPersistObject::getPropertySetInfo(void)
484 throw (RuntimeException
)
486 return Reference
< XPropertySetInfo
>();
489 void MyPersistObject::setPropertyValue(
490 const OUString
& aPropertyName
,
492 throw ( UnknownPropertyException
,
493 PropertyVetoException
,
494 IllegalArgumentException
,
495 WrappedTargetException
,
498 if( 0 == aPropertyName
.compareToAscii("long") ) {
501 else if ( 0 == aPropertyName
.compareToAscii("float") ) {
504 else if( 0 == aPropertyName
.compareToAscii("double") ) {
507 else if( 0 == aPropertyName
.compareToAscii("bool") ) {
510 else if( 0 == aPropertyName
.compareToAscii("byte" ) ) {
513 else if( 0 == aPropertyName
.compareToAscii("char") ) {
516 else if( 0 == aPropertyName
.compareToAscii("string") ) {
519 else if( 0 == aPropertyName
.compareToAscii("object") ) {
520 if( aValue
.getValueType() == getCppuType( (Reference
< XPersistObject
> *)0 ) )
532 Any
MyPersistObject::getPropertyValue(const OUString
& aPropertyName
)
533 throw ( UnknownPropertyException
,
534 WrappedTargetException
,
538 if( 0 == aPropertyName
.compareToAscii("long" ) ) {
541 else if ( 0 == aPropertyName
.compareToAscii("float") ) {
544 else if( 0 == aPropertyName
.compareToAscii("double") ) {
547 else if( 0 == aPropertyName
.compareToAscii("bool") ) {
550 else if( 0 == aPropertyName
.compareToAscii("byte") ) {
553 else if( 0 == aPropertyName
.compareToAscii("char" ) ) {
556 else if( 0 == aPropertyName
.compareToAscii("string") ) {
559 else if( 0 == aPropertyName
.compareToAscii("object" ) )
567 void MyPersistObject::addPropertyChangeListener(
568 const OUString
& aPropertyName
,
569 const Reference
< XPropertyChangeListener
> & xListener
)
570 throw ( UnknownPropertyException
,
571 WrappedTargetException
,
577 void MyPersistObject::removePropertyChangeListener(
578 const OUString
& aPropertyName
,
579 const Reference
< XPropertyChangeListener
> & aListener
)
580 throw ( UnknownPropertyException
,
581 WrappedTargetException
,
587 void MyPersistObject::addVetoableChangeListener(
588 const OUString
& PropertyName
,
589 const Reference
<XVetoableChangeListener
>& aListener
)
590 throw ( UnknownPropertyException
,
591 WrappedTargetException
,
597 void MyPersistObject::removeVetoableChangeListener(
598 const OUString
& PropertyName
,
599 const Reference
< XVetoableChangeListener
> & aListener
)
600 throw ( UnknownPropertyException
,
601 WrappedTargetException
,
610 OUString
MyPersistObject::getServiceName() throw (RuntimeException
)
612 return m_sServiceName
;
615 void MyPersistObject::write( const Reference
< XObjectOutputStream
> & rOut
)
616 throw (IOException
,RuntimeException
)
618 rOut
->writeLong( m_l
);
619 rOut
->writeFloat( m_f
);
620 rOut
->writeDouble( m_d
);
621 rOut
->writeBoolean( m_b
);
622 rOut
->writeByte( m_byte
);
623 rOut
->writeChar( m_c
);
624 rOut
->writeUTF( m_s
);
625 rOut
->writeObject( m_ref
);
629 void MyPersistObject::read( const Reference
< XObjectInputStream
> & rIn
)
630 throw (IOException
, RuntimeException
)
632 m_l
= rIn
->readLong();
633 m_f
= rIn
->readFloat();
634 m_d
= rIn
->readDouble();
635 m_b
= rIn
->readBoolean();
636 m_byte
= rIn
->readByte();
637 m_c
= rIn
->readChar();
638 m_s
= rIn
->readUTF();
639 m_ref
= rIn
->readObject();
642 Reference
< XInterface
> SAL_CALL
OMyPersistObject_CreateInstance(
643 const Reference
< XMultiServiceFactory
> & rSMgr
)
646 MyPersistObject
*p
= new MyPersistObject( );
647 return Reference
< XInterface
> ( SAL_STATIC_CAST( OWeakObject
* , p
) );
650 Sequence
<OUString
> OMyPersistObject_getSupportedServiceNames( ) throw ()
652 Sequence
<OUString
> aRet(1);
653 aRet
.getArray()[0] = OMyPersistObject_getImplementationName();
657 OUString
OMyPersistObject_getServiceName( ) throw ()
659 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.PersistTest" ));
662 OUString
OMyPersistObject_getImplementationName( ) throw ()
664 return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.io.PersistTest" ) );
668 // ---------------------------------------------
669 // -----------------------------------------------
670 class OObjectStreamTest
:
671 public ODataStreamTest
674 OObjectStreamTest( const Reference
< XMultiServiceFactory
> &r
) : ODataStreamTest(r
) {}
677 virtual void SAL_CALL
testInvariant(const OUString
& TestName
,
678 const Reference
< XInterface
>& TestObject
)
679 throw ( IllegalArgumentException
,
682 virtual sal_Int32 SAL_CALL
test(
683 const OUString
& TestName
,
684 const Reference
< XInterface
>& TestObject
,
685 sal_Int32 hTestHandle
)
686 throw ( IllegalArgumentException
,
691 void testObject( const Reference
<XObjectOutputStream
> &rOut
,
692 const Reference
<XObjectInputStream
> &rIn
);
698 void OObjectStreamTest::testInvariant( const OUString
& TestName
,
699 const Reference
< XInterface
>& TestObject
)
700 throw ( IllegalArgumentException
, RuntimeException
)
703 if( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.ObjectInputStream" ) )
706 ODataStreamTest::testInvariant( TestName
, TestObject
);
707 Reference
< XObjectInputStream
> dataInput( TestObject
, UNO_QUERY
);
708 Reference
< XMarkableStream
> markable( TestObject
, UNO_QUERY
);
709 ERROR_ASSERT( dataInput
.is() , "XObjectInputStream cannot be queried" );
710 ERROR_ASSERT( markable
.is() , "XMarkableStream cannot be queried" );
712 else if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectOutputStream") )
715 ODataStreamTest::testInvariant( TestName
, TestObject
);
716 Reference
< XMarkableStream
> markable( TestObject
, UNO_QUERY
);
717 Reference
< XObjectOutputStream
> dataOutput( TestObject
, UNO_QUERY
);
718 ERROR_ASSERT( dataOutput
.is(), "XObjectOutputStream cannot be queried" );
719 ERROR_ASSERT( markable
.is() , "XMarkableStream cannot be queried" );
722 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
723 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
726 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
727 ERROR_ASSERT( ! info
->supportsService( OUString::createFromAscii("bla bluzb") ) , "XServiceInfo test failed" );
732 sal_Int32
OObjectStreamTest::test( const OUString
& TestName
,
733 const Reference
< XInterface
>& TestObject
,
734 sal_Int32 hTestHandle
)
735 throw ( IllegalArgumentException
,
738 if( 0 == TestName
.compareToAscii("com.sun.star.io.ObjectInputStream") ||
739 0 == TestName
.compareToAscii("com.sun.star.io.ObjectOutputStream" ) ) {
743 if( 0 == hTestHandle
) {
744 testInvariant( TestName
, TestObject
);
746 else if( DATASTREAM_TEST_MAX_HANDLE
>= hTestHandle
) {
747 sal_Int32 hOldHandle
= hTestHandle
;
748 hTestHandle
= ODataStreamTest::test(
749 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataInputStream" )),
750 TestObject
, hTestHandle
);
751 if( hTestHandle
== -1 ){
752 hTestHandle
= hOldHandle
;
757 Reference
<XActiveDataSink
> rSink( TestObject
, UNO_QUERY
);
758 Reference
<XActiveDataSource
> rSource( TestObject
, UNO_QUERY
);
760 Reference
< XObjectInputStream
> rInput( TestObject
, UNO_QUERY
);
761 Reference
< XObjectOutputStream
> rOutput( TestObject
, UNO_QUERY
);
764 Reference
< XInterface
> x
= m_rFactory
->createInstance(
765 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe" )) );
767 Reference
<XInputStream
> rPipeInput( x
, UNO_QUERY
);
768 Reference
<XOutputStream
> rPipeOutput( x
, UNO_QUERY
);
770 x
= m_rFactory
->createInstance(
771 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream") ) );
773 Reference
<XInputStream
> markableInput( x
, UNO_QUERY
);
774 Reference
<XActiveDataSink
> markableSink( x
, UNO_QUERY
);
776 x
= m_rFactory
->createInstance( OUString(
777 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableOutputStream" ) ) );
778 Reference
<XOutputStream
> markableOutput( x
, UNO_QUERY
);
779 Reference
<XActiveDataSource
> markableSource( x
, UNO_QUERY
);
781 assert( markableInput
.is() );
782 assert( markableOutput
.is() );
783 assert( markableSink
.is() );
784 assert( markableSource
.is() );
786 markableSink
->setInputStream( rPipeInput
);
787 markableSource
->setOutputStream( rPipeOutput
);
790 x
= m_rFactory
->createInstance(
791 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectInputStream") ));
792 rInput
= Reference
< XObjectInputStream
> ( x
, UNO_QUERY
);
793 rSink
= Reference
< XActiveDataSink
> ( x
, UNO_QUERY
);
795 else if ( !rSource
.is() ) {
796 x
= m_rFactory
->createInstance(
797 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectOutputStream" )));
798 rOutput
= Reference
<XObjectOutputStream
> ( x
, UNO_QUERY
);
799 rSource
= Reference
<XActiveDataSource
>( x
, UNO_QUERY
);
802 assert( rPipeInput
.is() );
803 assert( rPipeOutput
.is() );
805 rSink
->setInputStream( markableInput
);
806 rSource
->setOutputStream( markableOutput
);
808 assert( rSink
->getInputStream().is() );
809 assert( rSource
->getOutputStream().is() );
811 if( 1 + DATASTREAM_TEST_MAX_HANDLE
== hTestHandle
) {
812 testObject( rOutput
, rInput
);
814 rInput
->closeInput();
815 rOutput
->closeOutput();
819 catch( Exception
&e
) {
820 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
821 BUILD_ERROR( 0 , o
.getStr() );
824 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
829 if( hTestHandle
> 1 +DATASTREAM_TEST_MAX_HANDLE
) {
830 // all tests finished.
835 BUILD_ERROR( 0 , "service not supported by test." );
841 sal_Bool
compareMyPropertySet( Reference
< XPropertySet
> &r1
, Reference
< XPropertySet
> &r2
)
843 sal_Bool b
= sal_True
;
845 if( r1
->getPropertyValue( OUString::createFromAscii("long") ).getValueType() == getCppuVoidType() ||
846 r2
->getPropertyValue( OUString::createFromAscii("long") ).getValueType() == getCppuVoidType() ) {
848 // one of the objects is not the correct propertyset !
849 fprintf( stderr
, "compareMyPropertySet: 1\n" );
853 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("long")) ) ==
854 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("long")) ) );
855 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 2\n" );
857 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) ==
858 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) );
862 r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) >>= f1
;
863 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) >>= f2
;
864 fprintf( stderr
, "compareMyPropertySet: %f %f 3\n",f1
,f2
);
867 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("double")) ) ==
868 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("double" ))) );
869 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 4\n" );
872 Any a
=r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
874 a
= r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
876 b
= b
&& ( (b1
&& b2
) || b1
== b2
);
877 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 5\n" );
879 // b = b && r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) ) ==
880 // r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) ) );
882 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("byte")) ) ==
883 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("byte")) ) );
884 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 6\n" );
886 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("char")) ) ==
887 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("char")) ) );
888 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 7\n" );
890 b
= b
&& ( r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("string")) ) ==
891 r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("string")) ));
892 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 8\n" );
894 Any o1
= r1
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("object")) );
895 Any o2
= r2
->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("object")) );
897 if( o1
.getValueType() == getCppuType( (Reference
<XPersistObject
>*)0 ) ) {
899 if( o2
.getValueType() == getCppuType( (Reference
<XPersistObject
>*)0 ) ) {
900 Reference
< XPersistObject
> rPersist1
;
901 Reference
< XPersistObject
> rPersist2
;
904 Reference
<XPropertySet
> rProp1( rPersist1
, UNO_QUERY
);
905 Reference
< XPropertySet
> rProp2( rPersist2
, UNO_QUERY
);
907 if( rProp1
.is() && rProp2
.is() && ! ( rProp1
== rProp2
)
908 &&( rProp1
!= r1
)) {
909 b
= b
&& compareMyPropertySet( rProp1
, rProp2
);
915 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 9\n" );
918 if( o2
.getValueType() == getCppuType( (Reference
<XPersistObject
>*)0 ) ) {
921 if( ! b
) fprintf( stderr
, "compareMyPropertySet: 10\n" );
927 void OObjectStreamTest::testObject( const Reference
< XObjectOutputStream
> &rOut
,
928 const Reference
< XObjectInputStream
> &rIn
)
930 ERROR_ASSERT( rOut
.is() , "no objectOutputStream" );
931 ERROR_ASSERT( rIn
.is() , "no objectInputStream" );
935 // tests, if saving an object with an unknown service name allows
936 // reading the data behind the object !
938 Reference
< XInterface
> x
= * new MyPersistObject(
939 OUString( RTL_CONSTASCII_USTRINGPARAM("bla blubs")) );
941 Reference
< XPersistObject
> persistRef( x
, UNO_QUERY
);
942 ERROR_ASSERT( persistRef
.is() , "couldn't instantiate PersistTest object" );
944 rOut
->writeObject( persistRef
);
945 rOut
->writeLong( (sal_Int32
) 0xdeadbeef );
947 ERROR_ASSERT( 0 != rIn
->available() , "no data arrived at input" );
951 Reference
<XPersistObject
> xReadPersistRef
= rIn
->readObject();
952 ERROR_ASSERT( 0 , "expected exception not thrown" );
954 catch( IOException
& )
959 ERROR_ASSERT( (sal_Int32
) 0xdeadbeef == rIn
->readLong() ,
960 "wrong data after object with unknown service name." );
964 Reference
< XInterface
> x
= m_rFactory
->createInstance(
965 OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.PersistTest")));
966 Reference
< XPersistObject
> persistRef( x
, UNO_QUERY
);
968 ERROR_ASSERT( persistRef
.is() , "couldn't instantiate PersistTest object" );
970 Reference
< XPropertySet
> rProp( persistRef
, UNO_QUERY
);
971 ERROR_ASSERT( rProp
.is() , "test object is no property set " );
974 sal_Int32 i
= 0x83482;
976 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any
);
978 float f
= (float)42.23;
980 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any
);
982 double d
= 233.321412;
984 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("double")) , any
);
986 sal_Bool b
= sal_True
;
987 any
.setValue( &b
, getCppuBooleanType() );
988 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("bool")) , any
);
992 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("byte")) , any
);
995 any
.setValue( &c
, getCppuCharType() );
996 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("char")) , any
);
998 OUString
str( RTL_CONSTASCII_USTRINGPARAM( "hi du !" ) );
1000 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("string")) , any
);
1003 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("object")) , any
);
1005 // do read and write
1006 rOut
->writeObject( persistRef
);
1007 ERROR_ASSERT( 0 != rIn
->available() , "no data arrived at input" );
1008 Reference
< XPersistObject
> xReadPersist
= rIn
->readObject( );
1010 Reference
< XPropertySet
> rPropRead( xReadPersist
, UNO_QUERY
);
1011 ERROR_ASSERT( compareMyPropertySet( rProp
, rPropRead
) , "objects has not been read properly !" );
1013 // destroy selfreferences
1014 rProp
->setPropertyValue( OUString::createFromAscii("object"), Any() );
1015 rPropRead
->setPropertyValue( OUString::createFromAscii("object"), Any() );
1019 Reference
< XMarkableStream
> markableOut( rOut
, UNO_QUERY
);
1020 ERROR_ASSERT( markableOut
.is() , "markable stream cannot be queried" );
1022 // do the same thing multiple times to check if
1023 // buffering and marks work correctly
1024 for( int i
= 0 ; i
< 2000 ; i
++ ) {
1026 Reference
< XInterface
> x
= m_rFactory
->createInstance(OUString::createFromAscii("test.com.sun.star.io.PersistTest"));
1027 Reference
< XPersistObject
> persistRef( x
, UNO_QUERY
);
1029 Reference
< XPropertySet
> rProp( persistRef
, UNO_QUERY
);
1030 ERROR_ASSERT( rProp
.is() , "test object is no property set " );
1033 sal_Int32 i
= 0x83482;
1035 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any
);
1039 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any
);
1041 double d
= 233.321412;
1043 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("double")) , any
);
1045 sal_Bool b
= sal_True
;
1046 any
.setValue( &b
, getCppuBooleanType() );
1047 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("bool")) , any
);
1051 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("byte")) , any
);
1053 sal_Unicode c
= 'h';
1054 any
.setValue( &c
, getCppuCharType() );
1055 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("char")) , any
);
1057 OUString
str( RTL_CONSTASCII_USTRINGPARAM( "hi du !" ) );
1059 rProp
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("string")) , any
);
1061 x
= m_rFactory
->createInstance(OUString::createFromAscii("test.com.sun.star.io.PersistTest"));
1062 Reference
<XPersistObject
> persist2ndRef( x
, UNO_QUERY
);
1064 // Note : persist2ndRef contains coincident values, but also coincident values must be
1066 any
<<= persist2ndRef
;
1067 rProp
->setPropertyValue( OUString::createFromAscii("object") , any
);
1069 // simply test, if markable operations and object operations do not interfere
1070 sal_Int32 nMark
= markableOut
->createMark();
1072 // do read and write
1073 rOut
->writeObject( persistRef
);
1075 // further markable tests !
1076 sal_Int32 nOffset
= markableOut
->offsetToMark( nMark
);
1077 markableOut
->jumpToMark( nMark
);
1078 markableOut
->deleteMark( nMark
);
1079 markableOut
->jumpToFurthest();
1085 ERROR_ASSERT( 0 != rIn
->available() , "no data arrived at input" );
1086 Reference
< XPersistObject
> xReadPersistRef
= rIn
->readObject( );
1088 Reference
< XPropertySet
> rProp1( persistRef
, UNO_QUERY
);
1089 Reference
< XPropertySet
> rProp2( xReadPersistRef
, UNO_QUERY
);
1090 ERROR_ASSERT( compareMyPropertySet( rProp1
, rProp2
) ,
1091 "objects has not been read properly !" );
1097 Reference
< XInterface
> SAL_CALL
OObjectStreamTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw(Exception
)
1099 OObjectStreamTest
*p
= new OObjectStreamTest( rSMgr
);
1100 return Reference
< XInterface
> ( SAL_STATIC_CAST( OWeakObject
* , p
) );
1103 Sequence
<OUString
> OObjectStreamTest_getSupportedServiceNames( int i
) throw ()
1105 Sequence
<OUString
> aRet(1);
1106 aRet
.getArray()[0] = OObjectStreamTest_getImplementationName( i
);
1110 OUString
OObjectStreamTest_getServiceName( int i
) throw ()
1113 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectInputStream" ));
1116 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectOutputStream"));
1120 OUString
OObjectStreamTest_getImplementationName( int i
) throw ()
1123 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectInputStream" ));
1126 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectOutputStream"));