Avoid potential negative array index access to cached text.
[LibreOffice.git] / io / test / stm / datatest.cxx
blobef31f099d4968f44a6d86f5fb6f3ee54a14c7a1a
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 .
20 #include <stdio.h>
22 #include <com/sun/star/test/XSimpleTest.hpp>
23 #include <com/sun/star/io/XActiveDataSink.hpp>
24 #include <com/sun/star/io/XActiveDataSource.hpp>
25 #include <com/sun/star/io/XObjectInputStream.hpp>
26 #include <com/sun/star/io/XObjectOutputStream.hpp>
27 #include <com/sun/star/io/XMarkableStream.hpp>
28 #include <com/sun/star/io/XConnectable.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/lang/WrappedTargetException.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <cppuhelper/factory.hxx>
35 #include <cppuhelper/implbase.hxx>
37 #include <osl/conditn.hxx>
39 #include <string.h>
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::io;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::test;
47 using namespace ::com::sun::star::beans;
48 // streams
50 #include "testfactreg.hxx"
52 #define DATASTREAM_TEST_MAX_HANDLE 1
55 * The following test class tests XDataInputStream and XDataOutputStream at equal terms,
56 * so when errors occur, it may be in either one implementation.
57 * The class also uses com.sun.star.io.pipe. If problems occur, make sure to run also the
58 * pipe test routines ( test.com.sun.star.io.pipe ).
61 class ODataStreamTest :
62 public WeakImplHelper< XSimpleTest >
64 public:
65 explicit ODataStreamTest( const Reference < XMultiServiceFactory > & rFactory ) :
66 m_rFactory( rFactory )
69 public:
70 virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
71 throw ( IllegalArgumentException,
72 RuntimeException);
74 virtual sal_Int32 SAL_CALL test( const OUString& TestName,
75 const Reference < XInterface >& TestObject,
76 sal_Int32 hTestHandle)
77 throw ( IllegalArgumentException,
78 RuntimeException);
80 virtual sal_Bool SAL_CALL testPassed() throw ( RuntimeException);
81 virtual Sequence< OUString > SAL_CALL getErrors() throw (RuntimeException);
82 virtual Sequence< Any > SAL_CALL getErrorExceptions() throw (RuntimeException);
83 virtual Sequence< OUString > SAL_CALL getWarnings() throw (RuntimeException);
85 private:
86 void testSimple( const Reference < XDataInputStream > & , const Reference < XDataOutputStream > &);
88 protected:
89 Sequence<Any> m_seqExceptions;
90 Sequence<OUString> m_seqErrors;
91 Sequence<OUString> m_seqWarnings;
93 Reference < XMultiServiceFactory > m_rFactory;
97 void ODataStreamTest::testInvariant(
98 const OUString& TestName,
99 const Reference < XInterface >& TestObject )
100 throw ( IllegalArgumentException,
101 RuntimeException)
103 if( OUString("com.sun.star.io.DataInputStream") == TestName ) {
104 Reference < XConnectable > connect( TestObject , UNO_QUERY );
105 Reference < XActiveDataSink > active( TestObject , UNO_QUERY );
106 Reference < XInputStream > input( TestObject , UNO_QUERY );
107 Reference < XDataInputStream > dataInput( TestObject , UNO_QUERY );
109 WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
110 WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
111 ERROR_ASSERT( input.is() , "XInputStream cannot be queried" );
112 ERROR_ASSERT( dataInput.is() , "XDataInputStream cannot be queried" );
116 else if( OUString("com.sun.star.io.DataOutputStream") == TestName ) {
117 Reference < XConnectable > connect( TestObject , UNO_QUERY );
118 Reference < XActiveDataSource > active( TestObject , UNO_QUERY );
119 Reference < XOutputStream > output( TestObject , UNO_QUERY );
120 Reference < XDataOutputStream > dataOutput( TestObject , UNO_QUERY );
122 WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
123 WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
124 ERROR_ASSERT( output.is() , "XInputStream cannot be queried" );
125 ERROR_ASSERT( dataOutput.is(), "XDataInputStream cannot be queried" );
129 Reference < XServiceInfo > info( TestObject, UNO_QUERY );
130 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
131 if( info.is() )
133 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
134 ERROR_ASSERT( ! info->supportsService("bla bluzb") , "XServiceInfo test failed" );
140 sal_Int32 ODataStreamTest::test(
141 const OUString& TestName,
142 const Reference < XInterface >& TestObject,
143 sal_Int32 hTestHandle)
144 throw ( IllegalArgumentException,
145 RuntimeException)
147 if( OUString("com.sun.star.io.DataInputStream") == TestName ||
148 OUString("com.sun.star.io.DataOutputStream") == TestName ) {
152 if( 0 == hTestHandle ) {
153 testInvariant( TestName , TestObject );
155 else {
156 Reference <XActiveDataSink > rSink( TestObject, UNO_QUERY );
157 Reference <XActiveDataSource > rSource( TestObject , UNO_QUERY );
159 Reference < XDataInputStream > rInput( TestObject , UNO_QUERY );
160 Reference < XDataOutputStream > rOutput( TestObject , UNO_QUERY );
163 Reference < XInterface > x = m_rFactory->createInstance(
164 "com.sun.star.io.Pipe" );
166 Reference < XInputStream > rPipeInput( x , UNO_QUERY );
167 Reference < XOutputStream > rPipeOutput( x , UNO_QUERY );
169 if( ! rSink.is() ) {
170 x = m_rFactory->createInstance(
171 "com.sun.star.io.DataInputStream" );
172 rInput.set( x , UNO_QUERY);
173 rSink.set( x , UNO_QUERY );
175 else if ( !rSource.is() )
177 x = m_rFactory->createInstance(
178 "com.sun.star.io.DataOutputStream" );
179 rOutput.set( x , UNO_QUERY );
180 rSource.set( x, UNO_QUERY );
183 OSL_ASSERT( rPipeInput.is() );
184 OSL_ASSERT( rPipeOutput.is() );
185 rSink->setInputStream( rPipeInput );
186 rSource->setOutputStream( rPipeOutput );
188 OSL_ASSERT( rSink->getInputStream().is() );
189 OSL_ASSERT( rSource->getOutputStream().is() );
191 if( 1 == hTestHandle ) {
192 testSimple( rInput , rOutput );
196 catch( const Exception & e )
198 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
199 BUILD_ERROR( 0 , o.getStr() );
201 catch( ... )
203 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
206 hTestHandle ++;
208 if( hTestHandle >= 2) {
209 // all tests finished.
210 hTestHandle = -1;
213 else {
214 BUILD_ERROR( 0 , "service not supported by test." );
216 return hTestHandle;
220 sal_Bool ODataStreamTest::testPassed() throw (RuntimeException)
222 return m_seqErrors.getLength() == 0;
226 Sequence< OUString > ODataStreamTest::getErrors() throw (RuntimeException)
228 return m_seqErrors;
232 Sequence< Any > ODataStreamTest::getErrorExceptions() throw (RuntimeException)
234 return m_seqExceptions;
238 Sequence< OUString > ODataStreamTest::getWarnings() throw (RuntimeException)
240 return m_seqWarnings;
243 void ODataStreamTest::testSimple( const Reference < XDataInputStream > &rInput,
244 const Reference < XDataOutputStream > &rOutput )
246 rOutput->writeLong( 0x34ff3c );
247 rOutput->writeLong( 0x34ff3d );
248 rOutput->writeLong( -1027 );
250 ERROR_ASSERT( 0x34ff3c == rInput->readLong() , "long read/write mismatch" );
251 ERROR_ASSERT( 0x34ff3d == rInput->readLong() , "long read/write mismatch" );
252 ERROR_ASSERT( -1027 == rInput->readLong() , "long read/write mismatch" );
254 rOutput->writeByte( 0x77 );
255 ERROR_ASSERT( 0x77 == rInput->readByte() , "byte read/write mismatch" );
257 rOutput->writeBoolean( 25 );
258 ERROR_ASSERT( rInput->readBoolean() , "boolean read/write mismatch" );
260 rOutput->writeBoolean( sal_False );
261 ERROR_ASSERT( ! rInput->readBoolean() , "boolean read/write mismatch" );
263 rOutput->writeFloat( (float) 42.42 );
264 ERROR_ASSERT( rInput->readFloat() == ((float)42.42) , "float read/write mismatch" );
266 rOutput->writeDouble( (double) 42.42 );
267 ERROR_ASSERT( rInput->readDouble() == 42.42 , "double read/write mismatch" );
269 rOutput->writeHyper( 0x123456789abcdefLL );
270 ERROR_ASSERT( rInput->readHyper() == 0x123456789abcdefLL , "int64 read/write mismatch" );
272 rOutput->writeUTF( OUString("Live long and prosper !") );
273 ERROR_ASSERT( rInput->readUTF() == "Live long and prosper !",
274 "UTF read/write mismatch" );
276 Sequence<sal_Unicode> wc(0x10001);
277 for( int i = 0 ; i < 0x10000 ; i ++ ) {
278 wc.getArray()[i] = L'c';
280 wc.getArray()[0x10000] = 0;
281 OUString str( wc.getArray() , 0x10000 );
282 rOutput->writeUTF( str );
283 ERROR_ASSERT( rInput->readUTF() == str , "error reading 64k block" );
285 rOutput->closeOutput();
288 rInput->readLong();
289 ERROR_ASSERT( 0 , "eof-exception does not occur !" );
291 catch ( IOException & )
293 //ok
295 catch( ... )
297 ERROR_ASSERT( 0 , "wrong exception after reading beyond eof" );
300 Sequence<sal_Int8> dummy (1);
301 ERROR_ASSERT( ! rInput->readBytes( dummy , 1 ),
302 "stream must be on eof !" );
304 rInput->closeInput();
308 rOutput->writeByte( 1 );
309 ERROR_ASSERT( 0 , "writing still possible though chain must be interrupted" );
311 catch( IOException & )
313 // ok
315 catch( ... ) {
316 ERROR_ASSERT( 0 , "IOException expected, but another exception was thrown" );
323 * for external binding
327 Reference < XInterface > SAL_CALL ODataStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
329 return getXWeak(new ODataStreamTest( rSMgr ));
332 Sequence<OUString> ODataStreamTest_getSupportedServiceNames( int i) throw ()
334 Sequence<OUString> aRet { ODataStreamTest_getImplementationName( i) };
335 return aRet;
338 OUString ODataStreamTest_getServiceName( int i) throw ()
340 if( 1 == i ) {
341 return OUString( "test.com.sun.star.io.DataInputStream" );
343 else {
344 return OUString( "test.com.sun.star.io.DataOutputStream" );
348 OUString ODataStreamTest_getImplementationName( int i) throw ()
350 if( 1 == i ) {
351 return OUString(
352 "test.com.sun.star.comp.extensions.stm.DataInputStream" );
354 else {
355 return OUString( "test.com.sun.star.comp.extensions.stm.DataOutputStream" );
359 class MyPersistObject : public WeakImplHelper< XPersistObject , XPropertySet >
361 public:
362 MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() ) ,
363 m_l( -392 ),
364 m_f( 7883.2 ),
365 m_d( -123923.5 ),
366 m_b( sal_True ),
367 m_byte( 42 ),
368 m_c( 429 ),
369 m_s( OUString( "foo" ) )
371 explicit MyPersistObject( const OUString & sServiceName ) : m_sServiceName( sServiceName )
375 public:
376 virtual OUString SAL_CALL getServiceName() throw (RuntimeException);
377 virtual void SAL_CALL write( const Reference< XObjectOutputStream >& OutStream )
378 throw (IOException, RuntimeException);
379 virtual void SAL_CALL read(const Reference< XObjectInputStream >& InStream)
380 throw (IOException, RuntimeException);
382 public:
384 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo()
385 throw (RuntimeException);
387 virtual void SAL_CALL setPropertyValue(const OUString& aPropertyName, const Any& aValue)
388 throw ( UnknownPropertyException,
389 PropertyVetoException,
390 IllegalArgumentException,
391 WrappedTargetException,
392 RuntimeException);
393 virtual Any SAL_CALL getPropertyValue(const OUString& PropertyName)
394 throw ( UnknownPropertyException,
395 WrappedTargetException,
396 RuntimeException);
397 virtual void SAL_CALL addPropertyChangeListener(
398 const OUString& aPropertyName,
399 const Reference < XPropertyChangeListener > & xListener)
400 throw ( UnknownPropertyException,
401 WrappedTargetException,
402 RuntimeException);
404 virtual void SAL_CALL removePropertyChangeListener(
405 const OUString& aPropertyName,
406 const Reference< XPropertyChangeListener > & aListener)
407 throw ( UnknownPropertyException,
408 WrappedTargetException,
409 RuntimeException);
410 virtual void SAL_CALL addVetoableChangeListener(
411 const OUString& PropertyName,
412 const Reference< XVetoableChangeListener > & aListener)
413 throw ( UnknownPropertyException,
414 WrappedTargetException,
415 RuntimeException);
417 virtual void SAL_CALL removeVetoableChangeListener(
418 const OUString& PropertyName,
419 const Reference< XVetoableChangeListener >& aListener)
420 throw ( UnknownPropertyException,
421 WrappedTargetException,
422 RuntimeException);
424 public:
425 sal_Int32 m_l;
426 float m_f;
427 double m_d;
428 sal_Bool m_b;
429 sal_Int8 m_byte;
430 sal_Unicode m_c;
431 OUString m_s;
432 Reference< XPersistObject > m_ref;
433 OUString m_sServiceName;
437 Reference <XPropertySetInfo > MyPersistObject::getPropertySetInfo()
438 throw (RuntimeException)
440 return Reference< XPropertySetInfo >();
443 void MyPersistObject::setPropertyValue(
444 const OUString& aPropertyName,
445 const Any& aValue)
446 throw ( UnknownPropertyException,
447 PropertyVetoException,
448 IllegalArgumentException,
449 WrappedTargetException,
450 RuntimeException)
452 if( aPropertyName.equalsAscii("long") ) {
453 aValue >>= m_l;
455 else if ( aPropertyName.equalsAscii("float") ) {
456 aValue >>= m_f;
458 else if( aPropertyName.equalsAscii("double") ) {
459 aValue >>= m_d;
461 else if( aPropertyName.equalsAscii("bool") ) {
462 aValue >>= m_b;
464 else if( aPropertyName.equalsAscii("byte" ) ) {
465 aValue >>= m_byte;
467 else if( aPropertyName.equalsAscii("char") ) {
468 aValue >>= m_c;
470 else if( aPropertyName.equalsAscii("string") ) {
471 aValue >>= m_s;
473 else if( aPropertyName.equalsAscii("object") ) {
474 if( aValue.getValueType() == cppu::UnoType<XPersistObject>::get())
476 aValue >>= m_ref;
478 else
480 m_ref = 0;
486 Any MyPersistObject::getPropertyValue(const OUString& aPropertyName)
487 throw ( UnknownPropertyException,
488 WrappedTargetException,
489 RuntimeException)
491 Any aValue;
492 if( aPropertyName.equalsAscii("long" ) ) {
493 aValue <<= m_l;
495 else if ( aPropertyName.equalsAscii("float") ) {
496 aValue <<= m_f;
498 else if( aPropertyName.equalsAscii("double") ) {
499 aValue <<= m_d;
501 else if( aPropertyName.equalsAscii("bool") ) {
502 aValue <<= m_b;
504 else if( aPropertyName.equalsAscii("byte") ) {
505 aValue <<= m_byte;
507 else if( aPropertyName.equalsAscii("char" ) ) {
508 aValue <<= m_c;
510 else if( aPropertyName.equalsAscii("string") ) {
511 aValue <<= m_s;
513 else if( aPropertyName.equalsAscii("object" ) )
515 aValue <<= m_ref;
517 return aValue;
521 void MyPersistObject::addPropertyChangeListener(
522 const OUString& aPropertyName,
523 const Reference< XPropertyChangeListener > & xListener)
524 throw ( UnknownPropertyException,
525 WrappedTargetException,
526 RuntimeException)
531 void MyPersistObject::removePropertyChangeListener(
532 const OUString& aPropertyName,
533 const Reference < XPropertyChangeListener > & aListener)
534 throw ( UnknownPropertyException,
535 WrappedTargetException,
536 RuntimeException)
541 void MyPersistObject::addVetoableChangeListener(
542 const OUString& PropertyName,
543 const Reference <XVetoableChangeListener >& aListener)
544 throw ( UnknownPropertyException,
545 WrappedTargetException,
546 RuntimeException)
551 void MyPersistObject::removeVetoableChangeListener(
552 const OUString& PropertyName,
553 const Reference < XVetoableChangeListener > & aListener)
554 throw ( UnknownPropertyException,
555 WrappedTargetException,
556 RuntimeException)
562 OUString MyPersistObject::getServiceName() throw (RuntimeException)
564 return m_sServiceName;
567 void MyPersistObject::write( const Reference< XObjectOutputStream > & rOut )
568 throw (IOException,RuntimeException)
570 rOut->writeLong( m_l);
571 rOut->writeFloat( m_f );
572 rOut->writeDouble( m_d );
573 rOut->writeBoolean( m_b );
574 rOut->writeByte( m_byte );
575 rOut->writeChar( m_c );
576 rOut->writeUTF( m_s );
577 rOut->writeObject( m_ref );
581 void MyPersistObject::read( const Reference< XObjectInputStream > & rIn )
582 throw (IOException, RuntimeException)
584 m_l = rIn->readLong();
585 m_f = rIn->readFloat();
586 m_d = rIn->readDouble();
587 m_b = rIn->readBoolean();
588 m_byte = rIn->readByte();
589 m_c = rIn->readChar();
590 m_s = rIn->readUTF();
591 m_ref = rIn->readObject();
594 Reference < XInterface > SAL_CALL OMyPersistObject_CreateInstance(
595 const Reference < XMultiServiceFactory > & rSMgr )
596 throw(Exception)
598 return getXWeak(new MyPersistObject( ));
601 Sequence<OUString> OMyPersistObject_getSupportedServiceNames( ) throw ()
603 Sequence<OUString> aRet { OMyPersistObject_getImplementationName() };
604 return aRet;
607 OUString OMyPersistObject_getServiceName( ) throw ()
609 return OUString( "test.com.sun.star.io.PersistTest" );
612 OUString OMyPersistObject_getImplementationName( ) throw ()
614 return OUString( "test.com.sun.star.io.PersistTest" );
617 class OObjectStreamTest :
618 public ODataStreamTest
620 public:
621 explicit OObjectStreamTest( const Reference < XMultiServiceFactory > &r) : ODataStreamTest(r) {}
623 public:
624 virtual void SAL_CALL testInvariant(const OUString& TestName,
625 const Reference < XInterface >& TestObject)
626 throw ( IllegalArgumentException,
627 RuntimeException);
629 virtual sal_Int32 SAL_CALL test(
630 const OUString& TestName,
631 const Reference < XInterface >& TestObject,
632 sal_Int32 hTestHandle)
633 throw ( IllegalArgumentException,
634 RuntimeException);
637 private:
638 void testObject( const Reference <XObjectOutputStream > &rOut,
639 const Reference <XObjectInputStream> &rIn );
641 private:
645 void OObjectStreamTest::testInvariant( const OUString& TestName,
646 const Reference < XInterface >& TestObject )
647 throw ( IllegalArgumentException, RuntimeException)
650 if( OUString( "com.sun.star.io.ObjectInputStream" )
651 == TestName )
653 ODataStreamTest::testInvariant( TestName , TestObject );
654 Reference< XObjectInputStream > dataInput( TestObject , UNO_QUERY );
655 Reference< XMarkableStream > markable( TestObject , UNO_QUERY );
656 ERROR_ASSERT( dataInput.is() , "XObjectInputStream cannot be queried" );
657 ERROR_ASSERT( markable.is() , "XMarkableStream cannot be queried" );
659 else if( OUString( "com.sun.star.io.ObjectOutputStream" )
660 == TestName )
662 ODataStreamTest::testInvariant( TestName , TestObject );
663 Reference < XMarkableStream > markable( TestObject , UNO_QUERY );
664 Reference < XObjectOutputStream > dataOutput( TestObject , UNO_QUERY );
665 ERROR_ASSERT( dataOutput.is(), "XObjectOutputStream cannot be queried" );
666 ERROR_ASSERT( markable.is() , "XMarkableStream cannot be queried" );
669 Reference < XServiceInfo > info( TestObject, UNO_QUERY );
670 ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
671 if( info.is() )
673 ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
674 ERROR_ASSERT( ! info->supportsService("bla bluzb") , "XServiceInfo test failed" );
679 sal_Int32 OObjectStreamTest::test( const OUString& TestName,
680 const Reference < XInterface >& TestObject,
681 sal_Int32 hTestHandle)
682 throw ( IllegalArgumentException,
683 RuntimeException)
685 if( TestName.equalsAscii("com.sun.star.io.ObjectInputStream") ||
686 TestName.equalsAscii("com.sun.star.io.ObjectOutputStream") ) {
690 if( 0 == hTestHandle ) {
691 testInvariant( TestName , TestObject );
693 else if( DATASTREAM_TEST_MAX_HANDLE >= hTestHandle ) {
694 sal_Int32 hOldHandle = hTestHandle;
695 hTestHandle = ODataStreamTest::test(
696 OUString( "com.sun.star.io.DataInputStream" ),
697 TestObject , hTestHandle );
698 if( hTestHandle == -1 ){
699 hTestHandle = hOldHandle;
702 else {
704 Reference<XActiveDataSink > rSink( TestObject, UNO_QUERY );
705 Reference<XActiveDataSource > rSource( TestObject , UNO_QUERY );
707 Reference< XObjectInputStream > rInput( TestObject , UNO_QUERY );
708 Reference< XObjectOutputStream > rOutput( TestObject , UNO_QUERY );
711 Reference < XInterface > x = m_rFactory->createInstance(
712 "com.sun.star.io.Pipe" );
714 Reference <XInputStream > rPipeInput( x , UNO_QUERY );
715 Reference <XOutputStream > rPipeOutput( x , UNO_QUERY );
717 x = m_rFactory->createInstance(
718 "com.sun.star.io.MarkableInputStream" );
720 Reference <XInputStream > markableInput( x , UNO_QUERY );
721 Reference <XActiveDataSink> markableSink( x , UNO_QUERY );
723 x = m_rFactory->createInstance( "com.sun.star.io.MarkableOutputStream" );
724 Reference <XOutputStream > markableOutput( x , UNO_QUERY );
725 Reference <XActiveDataSource > markableSource( x , UNO_QUERY );
727 OSL_ASSERT( markableInput.is() );
728 OSL_ASSERT( markableOutput.is() );
729 OSL_ASSERT( markableSink.is() );
730 OSL_ASSERT( markableSource.is() );
732 markableSink->setInputStream( rPipeInput );
733 markableSource->setOutputStream( rPipeOutput );
735 if( ! rSink.is() ) {
736 x = m_rFactory->createInstance(
737 "com.sun.star.io.ObjectInputStream" );
738 rInput.set( x , UNO_QUERY );
739 rSink.set( x , UNO_QUERY );
741 else if ( !rSource.is() ) {
742 x = m_rFactory->createInstance(
743 "com.sun.star.io.ObjectOutputStream" );
744 rOutput.set( x , UNO_QUERY );
745 rSource.set( x, UNO_QUERY );
748 OSL_ASSERT( rPipeInput.is() );
749 OSL_ASSERT( rPipeOutput.is() );
751 rSink->setInputStream( markableInput );
752 rSource->setOutputStream( markableOutput );
754 OSL_ASSERT( rSink->getInputStream().is() );
755 OSL_ASSERT( rSource->getOutputStream().is() );
757 if( 1 + DATASTREAM_TEST_MAX_HANDLE == hTestHandle ) {
758 testObject( rOutput , rInput);
760 rInput->closeInput();
761 rOutput->closeOutput();
765 catch( const Exception &e ) {
766 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
767 BUILD_ERROR( 0 , o.getStr() );
769 catch( ... ) {
770 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
773 hTestHandle ++;
775 if( hTestHandle > 1 +DATASTREAM_TEST_MAX_HANDLE ) {
776 // all tests finished.
777 hTestHandle = -1;
780 else {
781 BUILD_ERROR( 0 , "service not supported by test." );
783 return hTestHandle;
787 sal_Bool compareMyPropertySet( Reference< XPropertySet > &r1 , Reference < XPropertySet > &r2 )
789 sal_Bool b = sal_True;
791 if( r1->getPropertyValue("long").getValueType() == cppu::UnoType<void>::get() ||
792 r2->getPropertyValue("long").getValueType() == cppu::UnoType<void>::get() ) {
794 // one of the objects is not the correct propertyset !
795 fprintf( stderr, "compareMyPropertySet: 1\n" );
796 return sal_False;
799 b = b && ( r1->getPropertyValue("long") ==
800 r2->getPropertyValue("long") );
801 if( ! b ) fprintf( stderr, "compareMyPropertySet: 2\n" );
803 b = b && ( r1->getPropertyValue("float") ==
804 r2->getPropertyValue("float") );
805 if( ! b ){
806 float f1(0.0);
807 float f2(0.0);
808 r1->getPropertyValue("float") >>= f1;
809 r2->getPropertyValue("float") >>= f2;
810 fprintf( stderr, "compareMyPropertySet: %f %f 3\n",f1,f2 );
813 b = b && ( r1->getPropertyValue("double") ==
814 r2->getPropertyValue("double") );
815 if( ! b ) fprintf( stderr, "compareMyPropertySet: 4\n" );
817 sal_Bool b1(sal_False), b2(sal_False);
818 Any a =r1->getPropertyValue("bool");
819 a >>= b1;
820 a = r2->getPropertyValue("bool");
821 a >>= b2;
822 b = b && ( (b1 && b2) || b1 == b2 );
823 if( ! b ) fprintf( stderr, "compareMyPropertySet: 5\n" );
825 // b = b && r1->getPropertyValue("bool") ==
826 // r2->getPropertyValue("bool") );
828 b = b && ( r1->getPropertyValue("byte") ==
829 r2->getPropertyValue("byte") );
830 if( ! b ) fprintf( stderr, "compareMyPropertySet: 6\n" );
832 b = b && ( r1->getPropertyValue("char") ==
833 r2->getPropertyValue("char") );
834 if( ! b ) fprintf( stderr, "compareMyPropertySet: 7\n" );
836 b = b && ( r1->getPropertyValue("string") ==
837 r2->getPropertyValue("string"));
838 if( ! b ) fprintf( stderr, "compareMyPropertySet: 8\n" );
840 Any o1 = r1->getPropertyValue("object");
841 Any o2 = r2->getPropertyValue("object");
843 if( o1.getValueType() == cppu::UnoType<XPersistObject>::get()) {
845 if( o2.getValueType() == cppu::UnoType<XPersistObject>::get()) {
846 Reference < XPersistObject > rPersist1;
847 Reference < XPersistObject > rPersist2;
848 o1 >>= rPersist1;
849 o2 >>= rPersist2;
850 Reference <XPropertySet > rProp1( rPersist1 , UNO_QUERY );
851 Reference < XPropertySet > rProp2( rPersist2 , UNO_QUERY );
853 if( rProp1.is() && rProp2.is() && ! ( rProp1 == rProp2 )
854 &&( rProp1 != r1 )) {
855 b = b && compareMyPropertySet( rProp1 , rProp2 );
858 else {
859 b = sal_False;
861 if( ! b ) fprintf( stderr, "compareMyPropertySet: 9\n" );
863 else {
864 if( o2.getValueType() == cppu::UnoType<XPersistObject>::get()) {
865 b = sal_False;
867 if( ! b ) fprintf( stderr, "compareMyPropertySet: 10\n" );
870 return b;
873 void OObjectStreamTest::testObject( const Reference< XObjectOutputStream > &rOut,
874 const Reference < XObjectInputStream > &rIn )
876 ERROR_ASSERT( rOut.is() , "no objectOutputStream" );
877 ERROR_ASSERT( rIn.is() , "no objectInputStream" );
880 // tests, if saving an object with an unknown service name allows
881 // reading the data behind the object !
883 Reference < XInterface > x = * new MyPersistObject(
884 OUString( "bla blubs") );
886 Reference< XPersistObject > persistRef( x , UNO_QUERY );
887 ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
889 rOut->writeObject( persistRef );
890 rOut->writeLong( (sal_Int32) 0xdeadbeef );
892 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
896 Reference <XPersistObject > xReadPersistRef = rIn->readObject();
897 ERROR_ASSERT( 0 , "expected exception not thrown" );
899 catch( IOException & )
901 // all is ok
904 ERROR_ASSERT( (sal_Int32) 0xdeadbeef == rIn->readLong() ,
905 "wrong data after object with unknown service name." );
909 Reference < XInterface > x = m_rFactory->createInstance(
910 "test.com.sun.star.io.PersistTest");
911 Reference< XPersistObject > persistRef( x , UNO_QUERY );
913 ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
915 Reference < XPropertySet > rProp( persistRef , UNO_QUERY );
916 ERROR_ASSERT( rProp.is() , "test object is no property set " );
918 Any any;
919 sal_Int32 i = 0x83482;
920 any <<= i;
921 rProp->setPropertyValue("long", any );
923 float f = (float)42.23;
924 any <<= f;
925 rProp->setPropertyValue("float", any );
927 double d = 233.321412;
928 any <<= d;
929 rProp->setPropertyValue("double", any );
931 sal_Bool b = sal_True;
932 any.setValue( &b , cppu::UnoType<bool>::get() );
933 rProp->setPropertyValue("bool", any );
935 sal_Int8 by = 120;
936 any <<= by;
937 rProp->setPropertyValue("byte", any );
939 sal_Unicode c = 'h';
940 any.setValue( &c , cppu::UnoType<cppu::UnoCharType>::get() );
941 rProp->setPropertyValue("char", any );
943 OUString str( "hi du !" );
944 any <<= str;
945 rProp->setPropertyValue("string", any );
947 any <<= persistRef;
948 rProp->setPropertyValue("object", any );
950 // do read and write
951 rOut->writeObject( persistRef );
952 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
953 Reference< XPersistObject > xReadPersist = rIn->readObject( );
955 Reference< XPropertySet > rPropRead( xReadPersist , UNO_QUERY );
956 ERROR_ASSERT( compareMyPropertySet( rProp , rPropRead ) , "objects has not been read properly !" );
958 // destroy selfreferences
959 rProp->setPropertyValue("object", Any() );
960 rPropRead->setPropertyValue("object", Any() );
964 Reference< XMarkableStream > markableOut( rOut , UNO_QUERY );
965 ERROR_ASSERT( markableOut.is() , "markable stream cannot be queried" );
967 // do the same thing multiple times to check if
968 // buffering and marks work correctly
969 for( int i = 0 ; i < 2000 ; i ++ ) {
971 Reference < XInterface > x = m_rFactory->createInstance("test.com.sun.star.io.PersistTest");
972 Reference< XPersistObject > persistRef( x , UNO_QUERY );
974 Reference < XPropertySet > rProp( persistRef , UNO_QUERY );
975 ERROR_ASSERT( rProp.is() , "test object is no property set " );
977 Any any;
978 sal_Int32 i = 0x83482;
979 any <<= i;
980 rProp->setPropertyValue("long", any );
982 float f = 42.23;
983 any <<= f;
984 rProp->setPropertyValue("float", any );
986 double d = 233.321412;
987 any <<= d;
988 rProp->setPropertyValue("double", any );
990 sal_Bool b = sal_True;
991 any.setValue( &b , cppu::UnoType<bool>::get() );
992 rProp->setPropertyValue("bool", any );
994 sal_Int8 by = 120;
995 any <<= by;
996 rProp->setPropertyValue("byte", any );
998 sal_Unicode c = 'h';
999 any.setValue( &c , cppu::UnoType<cppu::UnoCharType>::get() );
1000 rProp->setPropertyValue("char", any );
1002 OUString str( "hi du !" );
1003 any <<= str;
1004 rProp->setPropertyValue("string", any );
1006 x = m_rFactory->createInstance("test.com.sun.star.io.PersistTest");
1007 Reference <XPersistObject > persist2ndRef( x , UNO_QUERY );
1009 // Note : persist2ndRef contains coincident values, but also coincident values must be
1010 // saved properly !
1011 any <<= persist2ndRef;
1012 rProp->setPropertyValue("object", any );
1014 // simply test, if markable operations and object operations do not interfere
1015 sal_Int32 nMark = markableOut->createMark();
1017 // do read and write
1018 rOut->writeObject( persistRef );
1020 // further markable tests !
1021 sal_Int32 nOffset = markableOut->offsetToMark( nMark );
1022 markableOut->jumpToMark( nMark );
1023 markableOut->deleteMark( nMark );
1024 markableOut->jumpToFurthest();
1027 ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
1028 Reference < XPersistObject > xReadPersistRef = rIn->readObject( );
1030 Reference< XPropertySet > rProp1( persistRef , UNO_QUERY );
1031 Reference< XPropertySet > rProp2( xReadPersistRef , UNO_QUERY );
1032 ERROR_ASSERT( compareMyPropertySet( rProp1, rProp2) ,
1033 "objects has not been read properly !" );
1039 Reference < XInterface > SAL_CALL OObjectStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
1041 return getXWeak(new OObjectStreamTest( rSMgr ));
1044 Sequence<OUString> OObjectStreamTest_getSupportedServiceNames( int i) throw ()
1046 Sequence<OUString> aRet { OObjectStreamTest_getImplementationName( i) };
1047 return aRet;
1050 OUString OObjectStreamTest_getServiceName( int i) throw ()
1052 if( 1 == i ) {
1053 return OUString( "test.com.sun.star.io.ObjectInputStream" );
1055 else {
1056 return OUString( "test.com.sun.star.io.ObjectOutputStream");
1060 OUString OObjectStreamTest_getImplementationName( int i) throw ()
1062 if( 1 == i ) {
1063 return OUString( "test.com.sun.star.comp.extensions.stm.ObjectInputStream" );
1065 else {
1066 return OUString( "test.com.sun.star.comp.extensions.stm.ObjectOutputStream");
1071 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */