bump product version to 4.1.6.2
[LibreOffice.git] / io / test / stm / datatest.cxx
blob96b4ef3b0f51858dfe7b66ceec9b3be34fa01d73
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <stdio.h>
23 #include <com/sun/star/test/XSimpleTest.hpp>
24 #include <com/sun/star/io/XActiveDataSink.hpp>
25 #include <com/sun/star/io/XActiveDataSource.hpp>
26 #include <com/sun/star/io/XObjectInputStream.hpp>
27 #include <com/sun/star/io/XObjectOutputStream.hpp>
28 #include <com/sun/star/io/XMarkableStream.hpp>
29 #include <com/sun/star/io/XConnectable.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/lang/WrappedTargetException.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase2.hxx>
42 #include <osl/conditn.hxx>
43 #include <osl/mutex.hxx>
45 #include <string.h>
47 using namespace ::rtl;
48 using namespace ::osl;
49 using namespace ::cppu;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::io;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::test;
54 using namespace ::com::sun::star::beans;
55 // streams
57 #include "testfactreg.hxx"
59 #define DATASTREAM_TEST_MAX_HANDLE 1
61 /****
62 * The following test class tests XDataInputStream and XDataOutputStream at equal terms,
63 * so when errors occur, it may be in either one implementation.
64 * The class also uses stardiv.uno.io.pipe. If problems occur, make sure to run also the
65 * pipe test routines ( test.com.sun.star.io.pipe ).
68 *****/
70 class ODataStreamTest :
71 public WeakImplHelper1< XSimpleTest >
73 public:
74 ODataStreamTest( const Reference < XMultiServiceFactory > & rFactory ) :
75 m_rFactory( rFactory )
78 public:
79 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
80 throw ( IllegalArgumentException,
81 RuntimeException);
83 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
84 const Reference < XInterface >& TestObject,
85 sal_Int32 hTestHandle)
86 throw ( IllegalArgumentException,
87 RuntimeException);
89 virtual sal_Bool SAL_CALL testPassed(void) throw ( RuntimeException);
90 virtual Sequence< OUString > SAL_CALL getErrors(void) throw (RuntimeException);
91 virtual Sequence< Any > SAL_CALL getErrorExceptions(void) throw (RuntimeException);
92 virtual Sequence< OUString > SAL_CALL getWarnings(void) throw (RuntimeException);
94 private:
95 void testSimple( const Reference < XDataInputStream > & , const Reference < XDataOutputStream > &);
97 protected:
98 Sequence<Any> m_seqExceptions;
99 Sequence<OUString> m_seqErrors;
100 Sequence<OUString> m_seqWarnings;
102 Reference < XMultiServiceFactory > m_rFactory;
108 void ODataStreamTest::testInvariant(
109 const OUString& TestName,
110 const Reference < XInterface >& TestObject )
111 throw ( IllegalArgumentException,
112 RuntimeException)
114 if( OUString("com.sun.star.io.DataInputStream") == TestName ) {
115 Reference < XConnectable > connect( TestObject , UNO_QUERY );
116 Reference < XActiveDataSink > active( TestObject , UNO_QUERY );
117 Reference < XInputStream > input( TestObject , UNO_QUERY );
118 Reference < XDataInputStream > dataInput( TestObject , UNO_QUERY );
120 WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
121 WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
122 ERROR_ASSERT( input.is() , "XInputStream cannot be queried" );
123 ERROR_ASSERT( dataInput.is() , "XDataInputStream cannot be queried" );
127 else if( OUString("com.sun.star.io.DataInputStream") == TestName ) {
128 Reference < XConnectable > connect( TestObject , UNO_QUERY );
129 Reference < XActiveDataSource > active( TestObject , UNO_QUERY );
130 Reference < XOutputStream > output( TestObject , UNO_QUERY );
131 Reference < XDataOutputStream > dataOutput( TestObject , UNO_QUERY );
133 WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
134 WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
135 ERROR_ASSERT( output.is() , "XInputStream cannot be queried" );
136 ERROR_ASSERT( dataOutput.is(), "XDataInputStream cannot be queried" );
140 Reference < XServiceInfo > info( TestObject, UNO_QUERY );
141 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
142 if( info.is() )
144 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
145 ERROR_ASSERT( ! info->supportsService( OUString("bla bluzb") ) , "XServiceInfo test failed" );
151 sal_Int32 ODataStreamTest::test(
152 const OUString& TestName,
153 const Reference < XInterface >& TestObject,
154 sal_Int32 hTestHandle)
155 throw ( IllegalArgumentException,
156 RuntimeException)
158 if( OUString("com.sun.star.io.DataInputStream") == TestName ||
159 OUString("com.sun.star.io.DataOutputStream") == TestName ) {
163 if( 0 == hTestHandle ) {
164 testInvariant( TestName , TestObject );
166 else {
167 Reference <XActiveDataSink > rSink( TestObject, UNO_QUERY );
168 Reference <XActiveDataSource > rSource( TestObject , UNO_QUERY );
170 Reference < XDataInputStream > rInput( TestObject , UNO_QUERY );
171 Reference < XDataOutputStream > rOutput( TestObject , UNO_QUERY );
174 Reference < XInterface > x = m_rFactory->createInstance(
175 OUString( "com.sun.star.io.Pipe" ));
177 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
178 Reference < XOutputStream > rPipeOutput( x , UNO_QUERY );
180 if( ! rSink.is() ) {
181 x = m_rFactory->createInstance(
182 OUString( "com.sun.star.io.DataInputStream") );
183 rInput = Reference < XDataInputStream > ( x , UNO_QUERY);
184 rSink = Reference< XActiveDataSink > ( x , UNO_QUERY );
186 else if ( !rSource.is() )
188 x = m_rFactory->createInstance(
189 OUString( "com.sun.star.io.DataOutputStream" ) );
190 rOutput = Reference< XDataOutputStream > ( x , UNO_QUERY );
191 rSource = Reference< XActiveDataSource > ( x, UNO_QUERY );
194 OSL_ASSERT( rPipeInput.is() );
195 OSL_ASSERT( rPipeOutput.is() );
196 rSink->setInputStream( rPipeInput );
197 rSource->setOutputStream( rPipeOutput );
199 OSL_ASSERT( rSink->getInputStream().is() );
200 OSL_ASSERT( rSource->getOutputStream().is() );
202 if( 1 == hTestHandle ) {
203 testSimple( rInput , rOutput );
207 catch( const Exception & e )
209 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
210 BUILD_ERROR( 0 , o.getStr() );
212 catch( ... )
214 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
217 hTestHandle ++;
219 if( hTestHandle >= 2) {
220 // all tests finished.
221 hTestHandle = -1;
224 else {
225 BUILD_ERROR( 0 , "service not supported by test." );
227 return hTestHandle;
232 sal_Bool ODataStreamTest::testPassed(void) throw (RuntimeException)
234 return m_seqErrors.getLength() == 0;
238 Sequence< OUString > ODataStreamTest::getErrors(void) throw (RuntimeException)
240 return m_seqErrors;
244 Sequence< Any > ODataStreamTest::getErrorExceptions(void) throw (RuntimeException)
246 return m_seqExceptions;
250 Sequence< OUString > ODataStreamTest::getWarnings(void) throw (RuntimeException)
252 return m_seqWarnings;
255 void ODataStreamTest::testSimple( const Reference < XDataInputStream > &rInput,
256 const Reference < XDataOutputStream > &rOutput )
258 rOutput->writeLong( 0x34ff3c );
259 rOutput->writeLong( 0x34ff3d );
260 rOutput->writeLong( -1027 );
262 ERROR_ASSERT( 0x34ff3c == rInput->readLong() , "long read/write mismatch" );
263 ERROR_ASSERT( 0x34ff3d == rInput->readLong() , "long read/write mismatch" );
264 ERROR_ASSERT( -1027 == rInput->readLong() , "long read/write mismatch" );
266 rOutput->writeByte( 0x77 );
267 ERROR_ASSERT( 0x77 == rInput->readByte() , "byte read/write mismatch" );
269 rOutput->writeBoolean( 25 );
270 ERROR_ASSERT( rInput->readBoolean() , "boolean read/write mismatch" );
272 rOutput->writeBoolean( sal_False );
273 ERROR_ASSERT( ! rInput->readBoolean() , "boolean read/write mismatch" );
275 rOutput->writeFloat( (float) 42.42 );
276 ERROR_ASSERT( rInput->readFloat() == ((float)42.42) , "float read/write mismatch" );
278 rOutput->writeDouble( (double) 42.42 );
279 ERROR_ASSERT( rInput->readDouble() == 42.42 , "double read/write mismatch" );
281 rOutput->writeHyper( 0x123456789abcdefLL );
282 ERROR_ASSERT( rInput->readHyper() == 0x123456789abcdefLL , "int64 read/write mismatch" );
284 rOutput->writeUTF( OUString("Live long and prosper !") );
285 ERROR_ASSERT( rInput->readUTF() == OUString("Live long and prosper !") ,
286 "UTF read/write mismatch" );
288 Sequence<sal_Unicode> wc(0x10001);
289 for( int i = 0 ; i < 0x10000 ; i ++ ) {
290 wc.getArray()[i] = L'c';
292 wc.getArray()[0x10000] = 0;
293 OUString str( wc.getArray() , 0x10000 );
294 rOutput->writeUTF( str );
295 ERROR_ASSERT( rInput->readUTF() == str , "error reading 64k block" );
297 rOutput->closeOutput();
300 rInput->readLong();
301 ERROR_ASSERT( 0 , "eof-exception does not occur !" );
303 catch ( IOException & )
305 //ok
307 catch( ... )
309 ERROR_ASSERT( 0 , "wrong exception after reading beyond eof" );
312 Sequence<sal_Int8> dummy (1);
313 ERROR_ASSERT( ! rInput->readBytes( dummy , 1 ),
314 "stream must be on eof !" );
316 rInput->closeInput();
320 rOutput->writeByte( 1 );
321 ERROR_ASSERT( 0 , "writing still possible though chain must be interrupted" );
323 catch( IOException & )
325 // ok
327 catch( ... ) {
328 ERROR_ASSERT( 0 , "IOException expected, but another exception was thrown" );
336 * for external binding
340 Reference < XInterface > SAL_CALL ODataStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
342 ODataStreamTest *p = new ODataStreamTest( rSMgr );
343 return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
346 Sequence<OUString> ODataStreamTest_getSupportedServiceNames( int i) throw ()
348 Sequence<OUString> aRet(1);
350 aRet.getArray()[0] = ODataStreamTest_getImplementationName( i);
353 return aRet;
356 OUString ODataStreamTest_getServiceName( int i) throw ()
358 if( 1 == i ) {
359 return OUString( "test.com.sun.star.io.DataInputStream" );
361 else {
362 return OUString( "test.com.sun.star.io.DataOutputStream" );
366 OUString ODataStreamTest_getImplementationName( int i) throw ()
368 if( 1 == i ) {
369 return OUString(
370 "test.com.sun.star.comp.extensions.stm.DataInputStream" );
372 else {
373 return OUString( "test.com.sun.star.comp.extensions.stm.DataOutputStream" );
377 class MyPersistObject : public WeakImplHelper2< XPersistObject , XPropertySet >
379 public:
380 MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() ) ,
381 m_l( -392 ),
382 m_f( 7883.2 ),
383 m_d( -123923.5 ),
384 m_b( sal_True ),
385 m_byte( 42 ),
386 m_c( 429 ),
387 m_s( OUString( "foo" ) )
389 MyPersistObject( const OUString & sServiceName ) : m_sServiceName( sServiceName )
393 public:
394 virtual OUString SAL_CALL getServiceName(void) throw (RuntimeException);
395 virtual void SAL_CALL write( const Reference< XObjectOutputStream >& OutStream )
396 throw (IOException, RuntimeException);
397 virtual void SAL_CALL read(const Reference< XObjectInputStream >& InStream)
398 throw (IOException, RuntimeException);
400 public:
402 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo(void)
403 throw (RuntimeException);
405 virtual void SAL_CALL setPropertyValue(const OUString& aPropertyName, const Any& aValue)
406 throw ( UnknownPropertyException,
407 PropertyVetoException,
408 IllegalArgumentException,
409 WrappedTargetException,
410 RuntimeException);
411 virtual Any SAL_CALL getPropertyValue(const OUString& PropertyName)
412 throw ( UnknownPropertyException,
413 WrappedTargetException,
414 RuntimeException);
415 virtual void SAL_CALL addPropertyChangeListener(
416 const OUString& aPropertyName,
417 const Reference < XPropertyChangeListener > & xListener)
418 throw ( UnknownPropertyException,
419 WrappedTargetException,
420 RuntimeException);
422 virtual void SAL_CALL removePropertyChangeListener(
423 const OUString& aPropertyName,
424 const Reference< XPropertyChangeListener > & aListener)
425 throw ( UnknownPropertyException,
426 WrappedTargetException,
427 RuntimeException);
428 virtual void SAL_CALL addVetoableChangeListener(
429 const OUString& PropertyName,
430 const Reference< XVetoableChangeListener > & aListener)
431 throw ( UnknownPropertyException,
432 WrappedTargetException,
433 RuntimeException);
435 virtual void SAL_CALL removeVetoableChangeListener(
436 const OUString& PropertyName,
437 const Reference< XVetoableChangeListener >& aListener)
438 throw ( UnknownPropertyException,
439 WrappedTargetException,
440 RuntimeException);
442 public:
443 sal_Int32 m_l;
444 float m_f;
445 double m_d;
446 sal_Bool m_b;
447 sal_Int8 m_byte;
448 sal_Unicode m_c;
449 OUString m_s;
450 Reference< XPersistObject > m_ref;
451 OUString m_sServiceName;
456 Reference <XPropertySetInfo > MyPersistObject::getPropertySetInfo(void)
457 throw (RuntimeException)
459 return Reference< XPropertySetInfo >();
462 void MyPersistObject::setPropertyValue(
463 const OUString& aPropertyName,
464 const Any& aValue)
465 throw ( UnknownPropertyException,
466 PropertyVetoException,
467 IllegalArgumentException,
468 WrappedTargetException,
469 RuntimeException)
471 if( 0 == aPropertyName.compareToAscii("long") ) {
472 aValue >>= m_l;
474 else if ( 0 == aPropertyName.compareToAscii("float") ) {
475 aValue >>= m_f;
477 else if( 0 == aPropertyName.compareToAscii("double") ) {
478 aValue >>= m_d;
480 else if( 0 == aPropertyName.compareToAscii("bool") ) {
481 aValue >>= m_b;
483 else if( 0 == aPropertyName.compareToAscii("byte" ) ) {
484 aValue >>= m_byte;
486 else if( 0 == aPropertyName.compareToAscii("char") ) {
487 aValue >>= m_c;
489 else if( 0 == aPropertyName.compareToAscii("string") ) {
490 aValue >>= m_s;
492 else if( 0 == aPropertyName.compareToAscii("object") ) {
493 if( aValue.getValueType() == getCppuType( (Reference< XPersistObject> *)0 ) )
495 aValue >>= m_ref;
497 else
499 m_ref = 0;
505 Any MyPersistObject::getPropertyValue(const OUString& aPropertyName)
506 throw ( UnknownPropertyException,
507 WrappedTargetException,
508 RuntimeException)
510 Any aValue;
511 if( 0 == aPropertyName.compareToAscii("long" ) ) {
512 aValue <<= m_l;
514 else if ( 0 == aPropertyName.compareToAscii("float") ) {
515 aValue <<= m_f;
517 else if( 0 == aPropertyName.compareToAscii("double") ) {
518 aValue <<= m_d;
520 else if( 0 == aPropertyName.compareToAscii("bool") ) {
521 aValue <<= m_b;
523 else if( 0 == aPropertyName.compareToAscii("byte") ) {
524 aValue <<= m_byte;
526 else if( 0 == aPropertyName.compareToAscii("char" ) ) {
527 aValue <<= m_c;
529 else if( 0 == aPropertyName.compareToAscii("string") ) {
530 aValue <<= m_s;
532 else if( 0 == aPropertyName.compareToAscii("object" ) )
534 aValue <<= m_ref;
536 return aValue;
540 void MyPersistObject::addPropertyChangeListener(
541 const OUString& aPropertyName,
542 const Reference< XPropertyChangeListener > & xListener)
543 throw ( UnknownPropertyException,
544 WrappedTargetException,
545 RuntimeException)
550 void MyPersistObject::removePropertyChangeListener(
551 const OUString& aPropertyName,
552 const Reference < XPropertyChangeListener > & aListener)
553 throw ( UnknownPropertyException,
554 WrappedTargetException,
555 RuntimeException)
560 void MyPersistObject::addVetoableChangeListener(
561 const OUString& PropertyName,
562 const Reference <XVetoableChangeListener >& aListener)
563 throw ( UnknownPropertyException,
564 WrappedTargetException,
565 RuntimeException)
570 void MyPersistObject::removeVetoableChangeListener(
571 const OUString& PropertyName,
572 const Reference < XVetoableChangeListener > & aListener)
573 throw ( UnknownPropertyException,
574 WrappedTargetException,
575 RuntimeException)
583 OUString MyPersistObject::getServiceName() throw (RuntimeException)
585 return m_sServiceName;
588 void MyPersistObject::write( const Reference< XObjectOutputStream > & rOut )
589 throw (IOException,RuntimeException)
591 rOut->writeLong( m_l);
592 rOut->writeFloat( m_f );
593 rOut->writeDouble( m_d );
594 rOut->writeBoolean( m_b );
595 rOut->writeByte( m_byte );
596 rOut->writeChar( m_c );
597 rOut->writeUTF( m_s );
598 rOut->writeObject( m_ref );
602 void MyPersistObject::read( const Reference< XObjectInputStream > & rIn )
603 throw (IOException, RuntimeException)
605 m_l = rIn->readLong();
606 m_f = rIn->readFloat();
607 m_d = rIn->readDouble();
608 m_b = rIn->readBoolean();
609 m_byte = rIn->readByte();
610 m_c = rIn->readChar();
611 m_s = rIn->readUTF();
612 m_ref = rIn->readObject();
615 Reference < XInterface > SAL_CALL OMyPersistObject_CreateInstance(
616 const Reference < XMultiServiceFactory > & rSMgr )
617 throw(Exception)
619 MyPersistObject *p = new MyPersistObject( );
620 return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
623 Sequence<OUString> OMyPersistObject_getSupportedServiceNames( ) throw ()
625 Sequence<OUString> aRet(1);
626 aRet.getArray()[0] = OMyPersistObject_getImplementationName();
627 return aRet;
630 OUString OMyPersistObject_getServiceName( ) throw ()
632 return OUString( "test.com.sun.star.io.PersistTest" );
635 OUString OMyPersistObject_getImplementationName( ) throw ()
637 return OUString( "test.com.sun.star.io.PersistTest" );
640 class OObjectStreamTest :
641 public ODataStreamTest
643 public:
644 OObjectStreamTest( const Reference < XMultiServiceFactory > &r) : ODataStreamTest(r) {}
646 public:
647 virtual void SAL_CALL testInvariant(const OUString& TestName,
648 const Reference < XInterface >& TestObject)
649 throw ( IllegalArgumentException,
650 RuntimeException);
652 virtual sal_Int32 SAL_CALL test(
653 const OUString& TestName,
654 const Reference < XInterface >& TestObject,
655 sal_Int32 hTestHandle)
656 throw ( IllegalArgumentException,
657 RuntimeException);
660 private:
661 void testObject( const Reference <XObjectOutputStream > &rOut,
662 const Reference <XObjectInputStream> &rIn );
664 private:
668 void OObjectStreamTest::testInvariant( const OUString& TestName,
669 const Reference < XInterface >& TestObject )
670 throw ( IllegalArgumentException, RuntimeException)
673 if( OUString( "com.sun.star.io.ObjectInputStream" )
674 == TestName )
676 ODataStreamTest::testInvariant( TestName , TestObject );
677 Reference< XObjectInputStream > dataInput( TestObject , UNO_QUERY );
678 Reference< XMarkableStream > markable( TestObject , UNO_QUERY );
679 ERROR_ASSERT( dataInput.is() , "XObjectInputStream cannot be queried" );
680 ERROR_ASSERT( markable.is() , "XMarkableStream cannot be queried" );
682 else if( OUString( "com.sun.star.io.ObjectOutputStream" )
683 == TestName )
685 ODataStreamTest::testInvariant( TestName , TestObject );
686 Reference < XMarkableStream > markable( TestObject , UNO_QUERY );
687 Reference < XObjectOutputStream > dataOutput( TestObject , UNO_QUERY );
688 ERROR_ASSERT( dataOutput.is(), "XObjectOutputStream cannot be queried" );
689 ERROR_ASSERT( markable.is() , "XMarkableStream cannot be queried" );
692 Reference < XServiceInfo > info( TestObject, UNO_QUERY );
693 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
694 if( info.is() )
696 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
697 ERROR_ASSERT( ! info->supportsService( OUString("bla bluzb") ) , "XServiceInfo test failed" );
702 sal_Int32 OObjectStreamTest::test( const OUString& TestName,
703 const Reference < XInterface >& TestObject,
704 sal_Int32 hTestHandle)
705 throw ( IllegalArgumentException,
706 RuntimeException)
708 if( 0 == TestName.compareToAscii("com.sun.star.io.ObjectInputStream") ||
709 0 == TestName.compareToAscii("com.sun.star.io.ObjectOutputStream" ) ) {
713 if( 0 == hTestHandle ) {
714 testInvariant( TestName , TestObject );
716 else if( DATASTREAM_TEST_MAX_HANDLE >= hTestHandle ) {
717 sal_Int32 hOldHandle = hTestHandle;
718 hTestHandle = ODataStreamTest::test(
719 OUString( "com.sun.star.io.DataInputStream" ),
720 TestObject , hTestHandle );
721 if( hTestHandle == -1 ){
722 hTestHandle = hOldHandle;
725 else {
727 Reference<XActiveDataSink > rSink( TestObject, UNO_QUERY );
728 Reference<XActiveDataSource > rSource( TestObject , UNO_QUERY );
730 Reference< XObjectInputStream > rInput( TestObject , UNO_QUERY );
731 Reference< XObjectOutputStream > rOutput( TestObject , UNO_QUERY );
734 Reference < XInterface > x = m_rFactory->createInstance(
735 OUString( "com.sun.star.io.Pipe" ) );
737 Reference <XInputStream > rPipeInput( x , UNO_QUERY );
738 Reference <XOutputStream > rPipeOutput( x , UNO_QUERY );
740 x = m_rFactory->createInstance(
741 OUString( "com.sun.star.io.MarkableInputStream" ) );
743 Reference <XInputStream > markableInput( x , UNO_QUERY );
744 Reference <XActiveDataSink> markableSink( x , UNO_QUERY );
746 x = m_rFactory->createInstance( OUString(
747 "com.sun.star.io.MarkableOutputStream" ) );
748 Reference <XOutputStream > markableOutput( x , UNO_QUERY );
749 Reference <XActiveDataSource > markableSource( x , UNO_QUERY );
751 OSL_ASSERT( markableInput.is() );
752 OSL_ASSERT( markableOutput.is() );
753 OSL_ASSERT( markableSink.is() );
754 OSL_ASSERT( markableSource.is() );
756 markableSink->setInputStream( rPipeInput );
757 markableSource->setOutputStream( rPipeOutput );
759 if( ! rSink.is() ) {
760 x = m_rFactory->createInstance(
761 OUString( "com.sun.star.io.ObjectInputStream" ));
762 rInput = Reference < XObjectInputStream > ( x , UNO_QUERY );
763 rSink = Reference < XActiveDataSink > ( x , UNO_QUERY );
765 else if ( !rSource.is() ) {
766 x = m_rFactory->createInstance(
767 OUString( "com.sun.star.io.ObjectOutputStream" ));
768 rOutput = Reference <XObjectOutputStream > ( x , UNO_QUERY );
769 rSource = Reference <XActiveDataSource>( x, UNO_QUERY );
772 OSL_ASSERT( rPipeInput.is() );
773 OSL_ASSERT( rPipeOutput.is() );
775 rSink->setInputStream( markableInput );
776 rSource->setOutputStream( markableOutput );
778 OSL_ASSERT( rSink->getInputStream().is() );
779 OSL_ASSERT( rSource->getOutputStream().is() );
781 if( 1 + DATASTREAM_TEST_MAX_HANDLE == hTestHandle ) {
782 testObject( rOutput , rInput);
784 rInput->closeInput();
785 rOutput->closeOutput();
789 catch( const Exception &e ) {
790 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
791 BUILD_ERROR( 0 , o.getStr() );
793 catch( ... ) {
794 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
797 hTestHandle ++;
799 if( hTestHandle > 1 +DATASTREAM_TEST_MAX_HANDLE ) {
800 // all tests finished.
801 hTestHandle = -1;
804 else {
805 BUILD_ERROR( 0 , "service not supported by test." );
807 return hTestHandle;
811 sal_Bool compareMyPropertySet( Reference< XPropertySet > &r1 , Reference < XPropertySet > &r2 )
813 sal_Bool b = sal_True;
815 if( r1->getPropertyValue( OUString("long") ).getValueType() == getCppuVoidType() ||
816 r2->getPropertyValue( OUString("long") ).getValueType() == getCppuVoidType() ) {
818 // one of the objects is not the correct propertyset !
819 fprintf( stderr, "compareMyPropertySet: 1\n" );
820 return sal_False;
823 b = b && ( r1->getPropertyValue( OUString("long") ) ==
824 r2->getPropertyValue( OUString("long") ) );
825 if( ! b ) fprintf( stderr, "compareMyPropertySet: 2\n" );
827 b = b && ( r1->getPropertyValue( OUString("float") ) ==
828 r2->getPropertyValue( OUString("float") ) );
829 if( ! b ){
830 float f1(0.0);
831 float f2(0.0);
832 r1->getPropertyValue( OUString("float") ) >>= f1;
833 r2->getPropertyValue( OUString("float") ) >>= f2;
834 fprintf( stderr, "compareMyPropertySet: %f %f 3\n",f1,f2 );
837 b = b && ( r1->getPropertyValue( OUString("double") ) ==
838 r2->getPropertyValue( OUString("double" )) );
839 if( ! b ) fprintf( stderr, "compareMyPropertySet: 4\n" );
841 sal_Bool b1(sal_False), b2(sal_False);
842 Any a =r1->getPropertyValue( OUString("bool") );
843 a >>= b1;
844 a = r2->getPropertyValue( OUString("bool") );
845 a >>= b2;
846 b = b && ( (b1 && b2) || b1 == b2 );
847 if( ! b ) fprintf( stderr, "compareMyPropertySet: 5\n" );
849 // b = b && r1->getPropertyValue( OUString("bool") ) ==
850 // r2->getPropertyValue( OUString("bool") ) );
852 b = b && ( r1->getPropertyValue( OUString("byte") ) ==
853 r2->getPropertyValue( OUString("byte") ) );
854 if( ! b ) fprintf( stderr, "compareMyPropertySet: 6\n" );
856 b = b && ( r1->getPropertyValue( OUString("char") ) ==
857 r2->getPropertyValue( OUString("char") ) );
858 if( ! b ) fprintf( stderr, "compareMyPropertySet: 7\n" );
860 b = b && ( r1->getPropertyValue( OUString("string") ) ==
861 r2->getPropertyValue( OUString("string") ));
862 if( ! b ) fprintf( stderr, "compareMyPropertySet: 8\n" );
864 Any o1 = r1->getPropertyValue( OUString("object") );
865 Any o2 = r2->getPropertyValue( OUString("object") );
867 if( o1.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
869 if( o2.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
870 Reference < XPersistObject > rPersist1;
871 Reference < XPersistObject > rPersist2;
872 o1 >>= rPersist1;
873 o2 >>= rPersist2;
874 Reference <XPropertySet > rProp1( rPersist1 , UNO_QUERY );
875 Reference < XPropertySet > rProp2( rPersist2 , UNO_QUERY );
877 if( rProp1.is() && rProp2.is() && ! ( rProp1 == rProp2 )
878 &&( rProp1 != r1 )) {
879 b = b && compareMyPropertySet( rProp1 , rProp2 );
882 else {
883 b = sal_False;
885 if( ! b ) fprintf( stderr, "compareMyPropertySet: 9\n" );
887 else {
888 if( o2.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
889 b = sal_False;
891 if( ! b ) fprintf( stderr, "compareMyPropertySet: 10\n" );
894 return b;
897 void OObjectStreamTest::testObject( const Reference< XObjectOutputStream > &rOut,
898 const Reference < XObjectInputStream > &rIn )
900 ERROR_ASSERT( rOut.is() , "no objectOutputStream" );
901 ERROR_ASSERT( rIn.is() , "no objectInputStream" );
905 // tests, if saving an object with an unknown service name allows
906 // reading the data behind the object !
908 Reference < XInterface > x = * new MyPersistObject(
909 OUString( "bla blubs") );
911 Reference< XPersistObject > persistRef( x , UNO_QUERY );
912 ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
914 rOut->writeObject( persistRef );
915 rOut->writeLong( (sal_Int32) 0xdeadbeef );
917 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
921 Reference <XPersistObject > xReadPersistRef = rIn->readObject();
922 ERROR_ASSERT( 0 , "expected exception not thrown" );
924 catch( IOException & )
926 // all is ok
929 ERROR_ASSERT( (sal_Int32) 0xdeadbeef == rIn->readLong() ,
930 "wrong data after object with unknown service name." );
934 Reference < XInterface > x = m_rFactory->createInstance(
935 OUString( "test.com.sun.star.io.PersistTest"));
936 Reference< XPersistObject > persistRef( x , UNO_QUERY );
938 ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
940 Reference < XPropertySet > rProp( persistRef , UNO_QUERY );
941 ERROR_ASSERT( rProp.is() , "test object is no property set " );
943 Any any;
944 sal_Int32 i = 0x83482;
945 any <<= i;
946 rProp->setPropertyValue( OUString( "long") , any );
948 float f = (float)42.23;
949 any <<= f;
950 rProp->setPropertyValue( OUString( "float") , any );
952 double d = 233.321412;
953 any <<= d;
954 rProp->setPropertyValue( OUString( "double") , any );
956 sal_Bool b = sal_True;
957 any.setValue( &b , getCppuBooleanType() );
958 rProp->setPropertyValue( OUString( "bool") , any );
960 sal_Int8 by = 120;
961 any <<= by;
962 rProp->setPropertyValue( OUString( "byte") , any );
964 sal_Unicode c = 'h';
965 any.setValue( &c , getCppuCharType() );
966 rProp->setPropertyValue( OUString( "char") , any );
968 OUString str( "hi du !" );
969 any <<= str;
970 rProp->setPropertyValue( OUString( "string") , any );
972 any <<= persistRef;
973 rProp->setPropertyValue( OUString( "object") , any );
975 // do read and write
976 rOut->writeObject( persistRef );
977 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
978 Reference< XPersistObject > xReadPersist = rIn->readObject( );
980 Reference< XPropertySet > rPropRead( xReadPersist , UNO_QUERY );
981 ERROR_ASSERT( compareMyPropertySet( rProp , rPropRead ) , "objects has not been read properly !" );
983 // destroy selfreferences
984 rProp->setPropertyValue( OUString("object"), Any() );
985 rPropRead->setPropertyValue( OUString("object"), Any() );
989 Reference< XMarkableStream > markableOut( rOut , UNO_QUERY );
990 ERROR_ASSERT( markableOut.is() , "markable stream cannot be queried" );
992 // do the same thing multiple times to check if
993 // buffering and marks work correctly
994 for( int i = 0 ; i < 2000 ; i ++ ) {
996 Reference < XInterface > x = m_rFactory->createInstance(OUString("test.com.sun.star.io.PersistTest"));
997 Reference< XPersistObject > persistRef( x , UNO_QUERY );
999 Reference < XPropertySet > rProp( persistRef , UNO_QUERY );
1000 ERROR_ASSERT( rProp.is() , "test object is no property set " );
1002 Any any;
1003 sal_Int32 i = 0x83482;
1004 any <<= i;
1005 rProp->setPropertyValue( OUString( "long") , any );
1007 float f = 42.23;
1008 any <<= f;
1009 rProp->setPropertyValue( OUString( "float") , any );
1011 double d = 233.321412;
1012 any <<= d;
1013 rProp->setPropertyValue( OUString( "double") , any );
1015 sal_Bool b = sal_True;
1016 any.setValue( &b , getCppuBooleanType() );
1017 rProp->setPropertyValue( OUString( "bool") , any );
1019 sal_Int8 by = 120;
1020 any <<= by;
1021 rProp->setPropertyValue( OUString( "byte") , any );
1023 sal_Unicode c = 'h';
1024 any.setValue( &c , getCppuCharType() );
1025 rProp->setPropertyValue( OUString( "char") , any );
1027 OUString str( "hi du !" );
1028 any <<= str;
1029 rProp->setPropertyValue( OUString( "string") , any );
1031 x = m_rFactory->createInstance(OUString("test.com.sun.star.io.PersistTest"));
1032 Reference <XPersistObject > persist2ndRef( x , UNO_QUERY );
1034 // Note : persist2ndRef contains coincident values, but also coincident values must be
1035 // saved properly !
1036 any <<= persist2ndRef;
1037 rProp->setPropertyValue( OUString("object") , any );
1039 // simply test, if markable operations and object operations do not interfere
1040 sal_Int32 nMark = markableOut->createMark();
1042 // do read and write
1043 rOut->writeObject( persistRef );
1045 // further markable tests !
1046 sal_Int32 nOffset = markableOut->offsetToMark( nMark );
1047 markableOut->jumpToMark( nMark );
1048 markableOut->deleteMark( nMark );
1049 markableOut->jumpToFurthest();
1055 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
1056 Reference < XPersistObject > xReadPersistRef = rIn->readObject( );
1058 Reference< XPropertySet > rProp1( persistRef , UNO_QUERY );
1059 Reference< XPropertySet > rProp2( xReadPersistRef , UNO_QUERY );
1060 ERROR_ASSERT( compareMyPropertySet( rProp1, rProp2) ,
1061 "objects has not been read properly !" );
1067 Reference < XInterface > SAL_CALL OObjectStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
1069 OObjectStreamTest *p = new OObjectStreamTest( rSMgr );
1070 return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
1073 Sequence<OUString> OObjectStreamTest_getSupportedServiceNames( int i) throw ()
1075 Sequence<OUString> aRet(1);
1076 aRet.getArray()[0] = OObjectStreamTest_getImplementationName( i);
1077 return aRet;
1080 OUString OObjectStreamTest_getServiceName( int i) throw ()
1082 if( 1 == i ) {
1083 return OUString( "test.com.sun.star.io.ObjectInputStream" );
1085 else {
1086 return OUString( "test.com.sun.star.io.ObjectOutputStream");
1090 OUString OObjectStreamTest_getImplementationName( int i) throw ()
1092 if( 1 == i ) {
1093 return OUString( "test.com.sun.star.comp.extensions.stm.ObjectInputStream" );
1095 else {
1096 return OUString( "test.com.sun.star.comp.extensions.stm.ObjectOutputStream");
1101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */