Update ooo320-m1
[ooovba.git] / io / test / stm / datatest.cxx
blobde6c678200bdbd104c475607cb2cf665ba51ab53
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: datatest.cxx,v $
10 * $Revision: 1.12 $
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"
34 #include <stdio.h>
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
59 #define NDEBUG
60 #endif
61 #include <assert.h>
62 #include <string.h>
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;
73 // streams
75 #include "testfactreg.hxx"
77 #define DATASTREAM_TEST_MAX_HANDLE 1
79 /****
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 ).
86 *****/
88 class ODataStreamTest :
89 public WeakImplHelper1< XSimpleTest >
91 public:
92 ODataStreamTest( const Reference < XMultiServiceFactory > & rFactory ) :
93 m_rFactory( rFactory )
96 public:
97 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
98 throw ( IllegalArgumentException,
99 RuntimeException);
101 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
102 const Reference < XInterface >& TestObject,
103 sal_Int32 hTestHandle)
104 throw ( IllegalArgumentException,
105 RuntimeException);
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);
112 private:
113 void testSimple( const Reference < XDataInputStream > & , const Reference < XDataOutputStream > &);
115 protected:
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,
130 RuntimeException)
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 !" );
160 if( info.is() )
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,
174 RuntimeException)
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 );
184 else {
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 );
198 if( ! rSink.is() ) {
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() );
230 catch( ... )
232 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
235 hTestHandle ++;
237 if( hTestHandle >= 2) {
238 // all tests finished.
239 hTestHandle = -1;
242 else {
243 BUILD_ERROR( 0 , "service not supported by test." );
245 return hTestHandle;
250 sal_Bool ODataStreamTest::testPassed(void) throw (RuntimeException)
252 return m_seqErrors.getLength() == 0;
256 Sequence< OUString > ODataStreamTest::getErrors(void) throw (RuntimeException)
258 return m_seqErrors;
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();
318 rInput->readLong();
319 ERROR_ASSERT( 0 , "eof-exception does not occur !" );
321 catch ( IOException & )
323 //ok
325 catch( ... )
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 & )
343 // ok
345 catch( ... ) {
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);
371 return aRet;
374 OUString ODataStreamTest_getServiceName( int i) throw ()
376 if( 1 == i ) {
377 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataInputStream" ));
379 else {
380 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataOutputStream" ));
384 OUString ODataStreamTest_getImplementationName( int i) throw ()
386 if( 1 == i ) {
387 return OUString(
388 RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.DataInputStream") );
390 else {
391 return OUString( RTL_CONSTASCII_USTRINGPARAM(
392 "test.com.sun.star.comp.extensions.stm.DataOutputStream" ) );
397 /**------------------------------------------------------
403 *------------------------------------------------------*/
404 class MyPersistObject : public WeakImplHelper2< XPersistObject , XPropertySet >
406 public:
407 MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() ) ,
408 m_l( -392 ),
409 m_f( 7883.2 ),
410 m_d( -123923.5 ),
411 m_b( sal_True ),
412 m_byte( 42 ),
413 m_c( 429 ),
414 m_s( OUString( RTL_CONSTASCII_USTRINGPARAM( "foo" ) ) )
416 MyPersistObject( const OUString & sServiceName ) : m_sServiceName( sServiceName )
420 public:
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);
427 public:
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,
437 RuntimeException);
438 virtual Any SAL_CALL getPropertyValue(const OUString& PropertyName)
439 throw ( UnknownPropertyException,
440 WrappedTargetException,
441 RuntimeException);
442 virtual void SAL_CALL addPropertyChangeListener(
443 const OUString& aPropertyName,
444 const Reference < XPropertyChangeListener > & xListener)
445 throw ( UnknownPropertyException,
446 WrappedTargetException,
447 RuntimeException);
449 virtual void SAL_CALL removePropertyChangeListener(
450 const OUString& aPropertyName,
451 const Reference< XPropertyChangeListener > & aListener)
452 throw ( UnknownPropertyException,
453 WrappedTargetException,
454 RuntimeException);
455 virtual void SAL_CALL addVetoableChangeListener(
456 const OUString& PropertyName,
457 const Reference< XVetoableChangeListener > & aListener)
458 throw ( UnknownPropertyException,
459 WrappedTargetException,
460 RuntimeException);
462 virtual void SAL_CALL removeVetoableChangeListener(
463 const OUString& PropertyName,
464 const Reference< XVetoableChangeListener >& aListener)
465 throw ( UnknownPropertyException,
466 WrappedTargetException,
467 RuntimeException);
469 public:
470 sal_Int32 m_l;
471 float m_f;
472 double m_d;
473 sal_Bool m_b;
474 sal_Int8 m_byte;
475 sal_Unicode m_c;
476 OUString m_s;
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,
491 const Any& aValue)
492 throw ( UnknownPropertyException,
493 PropertyVetoException,
494 IllegalArgumentException,
495 WrappedTargetException,
496 RuntimeException)
498 if( 0 == aPropertyName.compareToAscii("long") ) {
499 aValue >>= m_l;
501 else if ( 0 == aPropertyName.compareToAscii("float") ) {
502 aValue >>= m_f;
504 else if( 0 == aPropertyName.compareToAscii("double") ) {
505 aValue >>= m_d;
507 else if( 0 == aPropertyName.compareToAscii("bool") ) {
508 aValue >>= m_b;
510 else if( 0 == aPropertyName.compareToAscii("byte" ) ) {
511 aValue >>= m_byte;
513 else if( 0 == aPropertyName.compareToAscii("char") ) {
514 aValue >>= m_c;
516 else if( 0 == aPropertyName.compareToAscii("string") ) {
517 aValue >>= m_s;
519 else if( 0 == aPropertyName.compareToAscii("object") ) {
520 if( aValue.getValueType() == getCppuType( (Reference< XPersistObject> *)0 ) )
522 aValue >>= m_ref;
524 else
526 m_ref = 0;
532 Any MyPersistObject::getPropertyValue(const OUString& aPropertyName)
533 throw ( UnknownPropertyException,
534 WrappedTargetException,
535 RuntimeException)
537 Any aValue;
538 if( 0 == aPropertyName.compareToAscii("long" ) ) {
539 aValue <<= m_l;
541 else if ( 0 == aPropertyName.compareToAscii("float") ) {
542 aValue <<= m_f;
544 else if( 0 == aPropertyName.compareToAscii("double") ) {
545 aValue <<= m_d;
547 else if( 0 == aPropertyName.compareToAscii("bool") ) {
548 aValue <<= m_b;
550 else if( 0 == aPropertyName.compareToAscii("byte") ) {
551 aValue <<= m_byte;
553 else if( 0 == aPropertyName.compareToAscii("char" ) ) {
554 aValue <<= m_c;
556 else if( 0 == aPropertyName.compareToAscii("string") ) {
557 aValue <<= m_s;
559 else if( 0 == aPropertyName.compareToAscii("object" ) )
561 aValue <<= m_ref;
563 return aValue;
567 void MyPersistObject::addPropertyChangeListener(
568 const OUString& aPropertyName,
569 const Reference< XPropertyChangeListener > & xListener)
570 throw ( UnknownPropertyException,
571 WrappedTargetException,
572 RuntimeException)
577 void MyPersistObject::removePropertyChangeListener(
578 const OUString& aPropertyName,
579 const Reference < XPropertyChangeListener > & aListener)
580 throw ( UnknownPropertyException,
581 WrappedTargetException,
582 RuntimeException)
587 void MyPersistObject::addVetoableChangeListener(
588 const OUString& PropertyName,
589 const Reference <XVetoableChangeListener >& aListener)
590 throw ( UnknownPropertyException,
591 WrappedTargetException,
592 RuntimeException)
597 void MyPersistObject::removeVetoableChangeListener(
598 const OUString& PropertyName,
599 const Reference < XVetoableChangeListener > & aListener)
600 throw ( UnknownPropertyException,
601 WrappedTargetException,
602 RuntimeException)
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 )
644 throw(Exception)
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();
654 return aRet;
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
673 public:
674 OObjectStreamTest( const Reference < XMultiServiceFactory > &r) : ODataStreamTest(r) {}
676 public:
677 virtual void SAL_CALL testInvariant(const OUString& TestName,
678 const Reference < XInterface >& TestObject)
679 throw ( IllegalArgumentException,
680 RuntimeException);
682 virtual sal_Int32 SAL_CALL test(
683 const OUString& TestName,
684 const Reference < XInterface >& TestObject,
685 sal_Int32 hTestHandle)
686 throw ( IllegalArgumentException,
687 RuntimeException);
690 private:
691 void testObject( const Reference <XObjectOutputStream > &rOut,
692 const Reference <XObjectInputStream> &rIn );
694 private:
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" ) )
704 == TestName )
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") )
713 == TestName )
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 !" );
724 if( info.is() )
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,
736 RuntimeException)
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;
755 else {
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 );
789 if( ! rSink.is() ) {
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() );
823 catch( ... ) {
824 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
827 hTestHandle ++;
829 if( hTestHandle > 1 +DATASTREAM_TEST_MAX_HANDLE ) {
830 // all tests finished.
831 hTestHandle = -1;
834 else {
835 BUILD_ERROR( 0 , "service not supported by test." );
837 return hTestHandle;
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" );
850 return sal_False;
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")) ) );
859 if( ! b ){
860 float f1;
861 float f2;
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" );
871 sal_Bool b1 ,b2;
872 Any a =r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
873 a >>= b1;
874 a = r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
875 a >>= b2;
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;
902 o1 >>= rPersist1;
903 o2 >>= 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 );
912 else {
913 b = sal_False;
915 if( ! b ) fprintf( stderr, "compareMyPropertySet: 9\n" );
917 else {
918 if( o2.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
919 b = sal_False;
921 if( ! b ) fprintf( stderr, "compareMyPropertySet: 10\n" );
924 return b;
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 & )
956 // all is ok
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 " );
973 Any any;
974 sal_Int32 i = 0x83482;
975 any <<= i;
976 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any );
978 float f = (float)42.23;
979 any <<= f;
980 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any );
982 double d = 233.321412;
983 any <<= d;
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 );
990 sal_Int8 by = 120;
991 any <<= by;
992 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("byte")) , any );
994 sal_Unicode c = 'h';
995 any.setValue( &c , getCppuCharType() );
996 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("char")) , any );
998 OUString str( RTL_CONSTASCII_USTRINGPARAM( "hi du !" ) );
999 any <<= str;
1000 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("string")) , any );
1002 any <<= persistRef;
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 " );
1032 Any any;
1033 sal_Int32 i = 0x83482;
1034 any <<= i;
1035 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any );
1037 float f = 42.23;
1038 any <<= f;
1039 rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any );
1041 double d = 233.321412;
1042 any <<= d;
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 );
1049 sal_Int8 by = 120;
1050 any <<= by;
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 !" ) );
1058 any <<= str;
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
1065 // saved properly !
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);
1107 return aRet;
1110 OUString OObjectStreamTest_getServiceName( int i) throw ()
1112 if( 1 == i ) {
1113 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectInputStream" ));
1115 else {
1116 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectOutputStream"));
1120 OUString OObjectStreamTest_getImplementationName( int i) throw ()
1122 if( 1 == i ) {
1123 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectInputStream" ));
1125 else {
1126 return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectOutputStream"));