xref: /trunk/main/io/test/stm/datatest.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_io.hxx"
30 
31 #include <stdio.h>
32 
33 #include <com/sun/star/test/XSimpleTest.hpp>
34 #include <com/sun/star/io/XActiveDataSink.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/io/XObjectInputStream.hpp>
37 #include <com/sun/star/io/XObjectOutputStream.hpp>
38 #include <com/sun/star/io/XMarkableStream.hpp>
39 #include <com/sun/star/io/XConnectable.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/lang/WrappedTargetException.hpp>
42 
43 #include <com/sun/star/lang/IllegalArgumentException.hpp>
44 
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 
47 #include <cppuhelper/factory.hxx>
48 
49 #include <cppuhelper/implbase1.hxx>
50 #include <cppuhelper/implbase2.hxx>
51 
52 #include <osl/conditn.hxx>
53 #include <osl/mutex.hxx>
54 
55 #include <string.h>
56 
57 using namespace ::rtl;
58 using namespace ::osl;
59 using namespace ::cppu;
60 //using namespace ::vos;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::io;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::test;
65 using namespace ::com::sun::star::beans;
66 // streams
67 
68 #include "testfactreg.hxx"
69 
70 #define DATASTREAM_TEST_MAX_HANDLE 1
71 
72 /****
73 * The following test class tests XDataInputStream and XDataOutputStream at equal terms,
74 * so when errors occur, it may be in either one implementation.
75 * The class also  uses stardiv.uno.io.pipe. If problems occur, make sure to run also the
76 * pipe test routines ( test.com.sun.star.io.pipe ).
77 *
78 *
79 *****/
80 
81 class ODataStreamTest :
82         public WeakImplHelper1< XSimpleTest >
83 {
84 public:
85     ODataStreamTest( const Reference < XMultiServiceFactory > & rFactory ) :
86         m_rFactory( rFactory )
87         {}
88 
89 public:
90     virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject)
91         throw ( IllegalArgumentException,
92                     RuntimeException);
93 
94     virtual sal_Int32 SAL_CALL test(    const OUString& TestName,
95                                         const Reference < XInterface >& TestObject,
96                                         sal_Int32 hTestHandle)
97         throw ( IllegalArgumentException,
98                 RuntimeException);
99 
100     virtual sal_Bool SAL_CALL testPassed(void)                              throw ( RuntimeException);
101     virtual Sequence< OUString > SAL_CALL getErrors(void)               throw (RuntimeException);
102     virtual Sequence< Any > SAL_CALL getErrorExceptions(void)       throw (RuntimeException);
103     virtual Sequence< OUString > SAL_CALL getWarnings(void)                 throw (RuntimeException);
104 
105 private:
106     void testSimple( const Reference < XDataInputStream > & , const Reference < XDataOutputStream > &);
107 
108 protected:
109     Sequence<Any>  m_seqExceptions;
110     Sequence<OUString> m_seqErrors;
111     Sequence<OUString> m_seqWarnings;
112 
113     Reference < XMultiServiceFactory > m_rFactory;
114 };
115 
116 
117 
118 
119 void ODataStreamTest::testInvariant(
120     const OUString& TestName,
121     const Reference < XInterface >& TestObject )
122     throw ( IllegalArgumentException,
123             RuntimeException)
124 {
125     if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName ) {
126         Reference < XConnectable > connect( TestObject , UNO_QUERY );
127         Reference < XActiveDataSink > active( TestObject , UNO_QUERY );
128         Reference < XInputStream >  input( TestObject , UNO_QUERY );
129         Reference < XDataInputStream > dataInput( TestObject , UNO_QUERY );
130 
131         WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
132         WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
133         ERROR_ASSERT( input.is() , "XInputStream cannot be queried" );
134         ERROR_ASSERT( dataInput.is() , "XDataInputStream cannot be queried" );
135 
136 
137     }
138     else if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName ) {
139         Reference < XConnectable >  connect( TestObject , UNO_QUERY );
140         Reference < XActiveDataSource > active( TestObject , UNO_QUERY );
141         Reference < XOutputStream > output( TestObject , UNO_QUERY );
142         Reference < XDataOutputStream > dataOutput( TestObject , UNO_QUERY );
143 
144         WARNING_ASSERT( connect.is(),       "XConnectable cannot be queried" );
145         WARNING_ASSERT( active.is() ,   "XActiveDataSink cannot be queried" );
146         ERROR_ASSERT(   output.is() ,   "XInputStream cannot be queried" );
147         ERROR_ASSERT(   dataOutput.is(),    "XDataInputStream cannot be queried" );
148 
149     }
150 
151     Reference < XServiceInfo >  info( TestObject, UNO_QUERY );
152     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
153     if( info.is() )
154     {
155         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
156         ERROR_ASSERT( ! info->supportsService( OUString::createFromAscii("bla bluzb") ) , "XServiceInfo test failed" );
157     }
158 
159 }
160 
161 
162 sal_Int32 ODataStreamTest::test(
163     const OUString& TestName,
164     const Reference < XInterface >& TestObject,
165     sal_Int32 hTestHandle)
166     throw ( IllegalArgumentException,
167             RuntimeException)
168 {
169     if( OUString::createFromAscii("com.sun.star.io.DataInputStream") == TestName ||
170         OUString::createFromAscii("com.sun.star.io.DataOutputStream") == TestName )  {
171 
172         try
173         {
174             if( 0 == hTestHandle ) {
175                 testInvariant( TestName , TestObject );
176             }
177             else {
178                 Reference <XActiveDataSink > rSink( TestObject, UNO_QUERY );
179                 Reference <XActiveDataSource > rSource( TestObject , UNO_QUERY );
180 
181                 Reference < XDataInputStream > rInput( TestObject , UNO_QUERY );
182                 Reference < XDataOutputStream > rOutput( TestObject , UNO_QUERY );
183 
184 
185                 Reference < XInterface > x = m_rFactory->createInstance(
186                     OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe" )));
187 
188                 Reference < XInputStream >   rPipeInput( x , UNO_QUERY );
189                 Reference < XOutputStream >  rPipeOutput( x , UNO_QUERY );
190 
191                 if( ! rSink.is() ) {
192                     x = m_rFactory->createInstance(
193                         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataInputStream")) );
194                     rInput = Reference < XDataInputStream > ( x , UNO_QUERY);
195                     rSink = Reference<  XActiveDataSink > ( x , UNO_QUERY );
196                 }
197                 else if ( !rSource.is() )
198                 {
199                     x = m_rFactory->createInstance(
200                         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataOutputStream") ) );
201                     rOutput = Reference< XDataOutputStream > ( x , UNO_QUERY );
202                     rSource = Reference< XActiveDataSource > ( x, UNO_QUERY );
203                 }
204 
205                 OSL_ASSERT( rPipeInput.is() );
206                 OSL_ASSERT( rPipeOutput.is() );
207                 rSink->setInputStream( rPipeInput );
208                 rSource->setOutputStream( rPipeOutput );
209 
210                 OSL_ASSERT( rSink->getInputStream().is() );
211                 OSL_ASSERT( rSource->getOutputStream().is() );
212 
213                 if( 1 == hTestHandle ) {
214                     testSimple( rInput , rOutput );
215                 }
216             }
217         }
218         catch( Exception & e )
219         {
220             OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
221             BUILD_ERROR( 0 , o.getStr() );
222         }
223         catch( ... )
224         {
225             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
226         }
227 
228         hTestHandle ++;
229 
230         if( hTestHandle >= 2) {
231             // all tests finished.
232             hTestHandle = -1;
233         }
234     }
235     else {
236         BUILD_ERROR( 0 , "service not supported by test." );
237     }
238     return hTestHandle;
239 }
240 
241 
242 
243 sal_Bool ODataStreamTest::testPassed(void)                                      throw (RuntimeException)
244 {
245     return m_seqErrors.getLength() == 0;
246 }
247 
248 
249 Sequence< OUString > ODataStreamTest::getErrors(void)                           throw (RuntimeException)
250 {
251     return m_seqErrors;
252 }
253 
254 
255 Sequence< Any > ODataStreamTest::getErrorExceptions(void)                   throw (RuntimeException)
256 {
257     return m_seqExceptions;
258 }
259 
260 
261 Sequence< OUString > ODataStreamTest::getWarnings(void)                         throw (RuntimeException)
262 {
263     return m_seqWarnings;
264 }
265 
266 void ODataStreamTest::testSimple(   const Reference < XDataInputStream > &rInput,
267                                     const Reference < XDataOutputStream > &rOutput )
268 {
269     rOutput->writeLong( 0x34ff3c );
270     rOutput->writeLong( 0x34ff3d );
271     rOutput->writeLong( -1027 );
272 
273     ERROR_ASSERT( 0x34ff3c ==  rInput->readLong() , "long read/write mismatch" );
274     ERROR_ASSERT( 0x34ff3d ==  rInput->readLong() , "long read/write mismatch" );
275     ERROR_ASSERT( -1027    ==  rInput->readLong() , "long read/write mismatch" );
276 
277     rOutput->writeByte( 0x77 );
278     ERROR_ASSERT( 0x77 == rInput->readByte() , "byte read/write mismatch" );
279 
280     rOutput->writeBoolean( 25 );
281     ERROR_ASSERT( rInput->readBoolean() , "boolean read/write mismatch" );
282 
283     rOutput->writeBoolean( sal_False );
284     ERROR_ASSERT( ! rInput->readBoolean() , "boolean read/write mismatch" );
285 
286     rOutput->writeFloat( (float) 42.42 );
287     ERROR_ASSERT( rInput->readFloat() == ((float)42.42) , "float read/write mismatch" );
288 
289     rOutput->writeDouble( (double) 42.42 );
290     ERROR_ASSERT( rInput->readDouble() == 42.42 , "double read/write mismatch" );
291 
292     rOutput->writeHyper( 0x123456789abcdefLL );
293     ERROR_ASSERT( rInput->readHyper() == 0x123456789abcdefLL , "int64 read/write mismatch" );
294 
295     rOutput->writeUTF( OUString::createFromAscii("Live long and prosper !") );
296     ERROR_ASSERT( rInput->readUTF() == OUString::createFromAscii("Live long and prosper !") ,
297                     "UTF read/write mismatch" );
298 
299     Sequence<sal_Unicode> wc(0x10001);
300     for( int i = 0 ; i < 0x10000  ; i ++ ) {
301         wc.getArray()[i] = L'c';
302     }
303     wc.getArray()[0x10000] = 0;
304     OUString str( wc.getArray() , 0x10000 );
305     rOutput->writeUTF( str );
306     ERROR_ASSERT( rInput->readUTF() == str , "error reading 64k block" );
307 
308     rOutput->closeOutput();
309     try
310     {
311         rInput->readLong();
312         ERROR_ASSERT( 0 , "eof-exception does not occur !" );
313     }
314     catch ( IOException & )
315     {
316         //ok
317     }
318     catch( ... )
319     {
320         ERROR_ASSERT( 0 , "wrong exception after reading beyond eof" );
321     }
322 
323     Sequence<sal_Int8> dummy (1);
324     ERROR_ASSERT( ! rInput->readBytes( dummy , 1 ),
325                     "stream must be on eof !" );
326 
327     rInput->closeInput();
328 
329     try
330     {
331         rOutput->writeByte( 1 );
332         ERROR_ASSERT( 0 , "writing still possible though chain must be interrupted" );
333     }
334     catch( IOException & )
335     {
336         // ok
337     }
338     catch( ... ) {
339         ERROR_ASSERT( 0 , "IOException  expected, but another exception was thrown" );
340     }
341 
342 }
343 
344 
345 
346 /**
347 * for external binding
348 *
349 *
350 **/
351 Reference < XInterface > SAL_CALL ODataStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
352 {
353     ODataStreamTest *p = new ODataStreamTest( rSMgr );
354     return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
355 }
356 
357 Sequence<OUString> ODataStreamTest_getSupportedServiceNames( int i) throw ()
358 {
359     Sequence<OUString> aRet(1);
360 
361     aRet.getArray()[0] = ODataStreamTest_getImplementationName( i);
362 
363 
364     return aRet;
365 }
366 
367 OUString     ODataStreamTest_getServiceName( int i) throw ()
368 {
369     if( 1 == i ) {
370         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataInputStream" ));
371     }
372     else {
373         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.DataOutputStream" ));
374     }
375 }
376 
377 OUString    ODataStreamTest_getImplementationName( int i) throw ()
378 {
379     if( 1 == i ) {
380         return OUString(
381             RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.DataInputStream") );
382     }
383     else {
384         return OUString( RTL_CONSTASCII_USTRINGPARAM(
385             "test.com.sun.star.comp.extensions.stm.DataOutputStream" ) );
386     }
387 }
388 
389 
390 /**------------------------------------------------------
391 *
392 *
393 *
394 *
395 *
396 *------------------------------------------------------*/
397 class MyPersistObject : public WeakImplHelper2< XPersistObject , XPropertySet >
398 {
399 public:
400     MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() ) ,
401         m_l( -392 ),
402         m_f( 7883.2 ),
403         m_d( -123923.5 ),
404         m_b( sal_True ),
405         m_byte( 42 ),
406         m_c( 429 ),
407         m_s( OUString( RTL_CONSTASCII_USTRINGPARAM( "foo" ) ) )
408         {}
409     MyPersistObject( const OUString & sServiceName ) : m_sServiceName( sServiceName )
410         {}
411 
412 
413 public:
414     virtual OUString SAL_CALL getServiceName(void)  throw (RuntimeException);
415     virtual void SAL_CALL write( const Reference< XObjectOutputStream >& OutStream )
416         throw (IOException, RuntimeException);
417     virtual void SAL_CALL read(const Reference< XObjectInputStream >& InStream)
418         throw (IOException, RuntimeException);
419 
420 public:
421 
422     virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo(void)
423         throw (RuntimeException);
424 
425     virtual void SAL_CALL setPropertyValue(const OUString& aPropertyName, const Any& aValue)
426         throw ( UnknownPropertyException,
427                 PropertyVetoException,
428                 IllegalArgumentException,
429                 WrappedTargetException,
430                 RuntimeException);
431     virtual Any SAL_CALL getPropertyValue(const OUString& PropertyName)
432         throw ( UnknownPropertyException,
433                 WrappedTargetException,
434                 RuntimeException);
435     virtual void SAL_CALL addPropertyChangeListener(
436         const OUString& aPropertyName,
437         const Reference < XPropertyChangeListener > & xListener)
438         throw ( UnknownPropertyException,
439                 WrappedTargetException,
440                 RuntimeException);
441 
442     virtual void SAL_CALL removePropertyChangeListener(
443         const OUString& aPropertyName,
444         const Reference< XPropertyChangeListener > & aListener)
445         throw ( UnknownPropertyException,
446                 WrappedTargetException,
447                 RuntimeException);
448     virtual void SAL_CALL addVetoableChangeListener(
449         const OUString& PropertyName,
450         const Reference< XVetoableChangeListener > & aListener)
451         throw ( UnknownPropertyException,
452                 WrappedTargetException,
453                 RuntimeException);
454 
455     virtual void SAL_CALL removeVetoableChangeListener(
456         const OUString& PropertyName,
457         const Reference<  XVetoableChangeListener >& aListener)
458         throw ( UnknownPropertyException,
459                 WrappedTargetException,
460                 RuntimeException);
461 
462 public:
463     sal_Int32   m_l;
464     float   m_f;
465     double  m_d;
466     sal_Bool    m_b;
467     sal_Int8    m_byte;
468     sal_Unicode m_c;
469     OUString    m_s;
470     Reference< XPersistObject > m_ref;
471     OUString m_sServiceName;
472 };
473 
474 
475 
476 Reference <XPropertySetInfo > MyPersistObject::getPropertySetInfo(void)
477     throw (RuntimeException)
478 {
479     return Reference< XPropertySetInfo >();
480 }
481 
482 void MyPersistObject::setPropertyValue(
483     const OUString& aPropertyName,
484     const Any& aValue)
485     throw ( UnknownPropertyException,
486             PropertyVetoException,
487             IllegalArgumentException,
488             WrappedTargetException,
489             RuntimeException)
490 {
491     if( 0 == aPropertyName.compareToAscii("long")  ) {
492         aValue >>= m_l;
493     }
494     else if ( 0 == aPropertyName.compareToAscii("float") ) {
495         aValue >>= m_f;
496     }
497     else if( 0 == aPropertyName.compareToAscii("double") ) {
498         aValue >>= m_d;
499     }
500     else if( 0  == aPropertyName.compareToAscii("bool") ) {
501         aValue >>= m_b;
502     }
503     else if( 0 == aPropertyName.compareToAscii("byte" ) ) {
504         aValue >>= m_byte;
505     }
506     else if( 0 == aPropertyName.compareToAscii("char") ) {
507         aValue >>= m_c;
508     }
509     else if( 0 == aPropertyName.compareToAscii("string") ) {
510         aValue >>= m_s;
511     }
512     else if( 0 == aPropertyName.compareToAscii("object") ) {
513         if( aValue.getValueType() == getCppuType( (Reference< XPersistObject> *)0 ) )
514         {
515             aValue >>= m_ref;
516         }
517         else
518         {
519             m_ref = 0;
520         }
521     }
522 }
523 
524 
525 Any MyPersistObject::getPropertyValue(const OUString& aPropertyName)
526     throw ( UnknownPropertyException,
527             WrappedTargetException,
528             RuntimeException)
529 {
530     Any aValue;
531     if( 0 == aPropertyName.compareToAscii("long" )  ) {
532         aValue <<= m_l;
533     }
534     else if ( 0 == aPropertyName.compareToAscii("float") ) {
535         aValue <<= m_f;
536     }
537     else if( 0 == aPropertyName.compareToAscii("double") ) {
538         aValue <<= m_d;
539     }
540     else if( 0 == aPropertyName.compareToAscii("bool") ) {
541         aValue <<= m_b;
542     }
543     else if( 0 == aPropertyName.compareToAscii("byte") ) {
544         aValue <<= m_byte;
545     }
546     else if( 0 == aPropertyName.compareToAscii("char" ) ) {
547         aValue <<= m_c;
548     }
549     else if( 0 == aPropertyName.compareToAscii("string") ) {
550         aValue <<= m_s;
551     }
552     else if( 0 == aPropertyName.compareToAscii("object" ) )
553     {
554         aValue <<= m_ref;
555     }
556     return aValue;
557 }
558 
559 
560 void MyPersistObject::addPropertyChangeListener(
561     const OUString& aPropertyName,
562     const Reference< XPropertyChangeListener > & xListener)
563     throw ( UnknownPropertyException,
564             WrappedTargetException,
565             RuntimeException)
566 {
567 
568 }
569 
570 void MyPersistObject::removePropertyChangeListener(
571     const OUString& aPropertyName,
572     const Reference < XPropertyChangeListener > & aListener)
573     throw ( UnknownPropertyException,
574             WrappedTargetException,
575             RuntimeException)
576 {
577 }
578 
579 
580 void MyPersistObject::addVetoableChangeListener(
581     const OUString& PropertyName,
582     const Reference <XVetoableChangeListener >& aListener)
583     throw ( UnknownPropertyException,
584             WrappedTargetException,
585             RuntimeException)
586 {
587 
588 }
589 
590 void MyPersistObject::removeVetoableChangeListener(
591     const OUString& PropertyName,
592     const Reference < XVetoableChangeListener > & aListener)
593     throw ( UnknownPropertyException,
594             WrappedTargetException,
595             RuntimeException)
596 {
597 
598 }
599 
600 
601 
602 
603 OUString MyPersistObject::getServiceName() throw (RuntimeException)
604 {
605     return m_sServiceName;
606 }
607 
608 void MyPersistObject::write( const Reference< XObjectOutputStream >  & rOut )
609     throw (IOException,RuntimeException)
610 {
611     rOut->writeLong( m_l);
612     rOut->writeFloat( m_f );
613     rOut->writeDouble( m_d );
614     rOut->writeBoolean( m_b );
615     rOut->writeByte( m_byte );
616     rOut->writeChar( m_c );
617     rOut->writeUTF( m_s );
618     rOut->writeObject( m_ref );
619 }
620 
621 
622 void MyPersistObject::read( const Reference< XObjectInputStream > & rIn )
623     throw (IOException, RuntimeException)
624 {
625     m_l = rIn->readLong();
626     m_f = rIn->readFloat();
627     m_d = rIn->readDouble();
628     m_b = rIn->readBoolean();
629     m_byte = rIn->readByte();
630     m_c = rIn->readChar();
631     m_s = rIn->readUTF();
632     m_ref = rIn->readObject();
633 }
634 
635 Reference < XInterface > SAL_CALL OMyPersistObject_CreateInstance(
636     const Reference < XMultiServiceFactory > & rSMgr )
637     throw(Exception)
638 {
639     MyPersistObject *p = new MyPersistObject( );
640     return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
641 }
642 
643 Sequence<OUString> OMyPersistObject_getSupportedServiceNames( ) throw ()
644 {
645     Sequence<OUString> aRet(1);
646     aRet.getArray()[0] = OMyPersistObject_getImplementationName();
647     return aRet;
648 }
649 
650 OUString     OMyPersistObject_getServiceName( ) throw ()
651 {
652     return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.PersistTest" ));
653 }
654 
655 OUString    OMyPersistObject_getImplementationName( ) throw ()
656 {
657     return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.io.PersistTest" ) );
658 }
659 
660 
661 // ---------------------------------------------
662 // -----------------------------------------------
663 class OObjectStreamTest :
664         public ODataStreamTest
665 {
666 public:
667     OObjectStreamTest( const Reference < XMultiServiceFactory > &r) : ODataStreamTest(r) {}
668 
669 public:
670     virtual void SAL_CALL testInvariant(const OUString& TestName,
671                                const Reference < XInterface >& TestObject)
672         throw ( IllegalArgumentException,
673                 RuntimeException);
674 
675     virtual sal_Int32 SAL_CALL test(
676         const OUString& TestName,
677         const Reference < XInterface >& TestObject,
678         sal_Int32 hTestHandle)
679         throw ( IllegalArgumentException,
680                 RuntimeException);
681 
682 
683 private:
684     void OObjectStreamTest::testObject(     const Reference <XObjectOutputStream > &rOut,
685                                             const Reference <XObjectInputStream> &rIn );
686 
687 private:
688 };
689 
690 
691 void OObjectStreamTest::testInvariant( const OUString& TestName,
692                                        const Reference < XInterface >& TestObject )
693     throw ( IllegalArgumentException, RuntimeException)
694 {
695 
696     if( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.ObjectInputStream" ) )
697                   == TestName )
698     {
699         ODataStreamTest::testInvariant( TestName , TestObject );
700         Reference< XObjectInputStream > dataInput( TestObject , UNO_QUERY );
701         Reference< XMarkableStream >  markable( TestObject , UNO_QUERY );
702         ERROR_ASSERT( dataInput.is() , "XObjectInputStream cannot be queried" );
703         ERROR_ASSERT(   markable.is() , "XMarkableStream cannot be queried" );
704     }
705     else if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectOutputStream") )
706              == TestName )
707     {
708         ODataStreamTest::testInvariant( TestName , TestObject );
709         Reference < XMarkableStream > markable( TestObject , UNO_QUERY );
710         Reference < XObjectOutputStream > dataOutput( TestObject , UNO_QUERY );
711         ERROR_ASSERT(   dataOutput.is(),    "XObjectOutputStream cannot be queried" );
712         ERROR_ASSERT(   markable.is() , "XMarkableStream cannot be queried" );
713     }
714 
715     Reference < XServiceInfo > info( TestObject, UNO_QUERY );
716     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
717     if( info.is() )
718     {
719         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
720         ERROR_ASSERT( ! info->supportsService( OUString::createFromAscii("bla bluzb") ) , "XServiceInfo test failed" );
721     }
722 
723 }
724 
725 sal_Int32 OObjectStreamTest::test(  const OUString& TestName,
726                         const Reference < XInterface >& TestObject,
727                         sal_Int32 hTestHandle)
728     throw ( IllegalArgumentException,
729             RuntimeException)
730 {
731     if( 0 == TestName.compareToAscii("com.sun.star.io.ObjectInputStream") ||
732         0 == TestName.compareToAscii("com.sun.star.io.ObjectOutputStream" ) )  {
733 
734         try
735         {
736             if( 0 == hTestHandle ) {
737                 testInvariant( TestName , TestObject );
738             }
739             else if( DATASTREAM_TEST_MAX_HANDLE >= hTestHandle ) {
740                 sal_Int32 hOldHandle = hTestHandle;
741                 hTestHandle = ODataStreamTest::test(
742                     OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.DataInputStream" )),
743                     TestObject , hTestHandle );
744                 if( hTestHandle == -1 ){
745                     hTestHandle = hOldHandle;
746                 }
747             }
748             else {
749 
750                 Reference<XActiveDataSink >  rSink( TestObject, UNO_QUERY );
751                 Reference<XActiveDataSource >  rSource( TestObject , UNO_QUERY );
752 
753                 Reference< XObjectInputStream >  rInput( TestObject , UNO_QUERY );
754                 Reference< XObjectOutputStream > rOutput( TestObject , UNO_QUERY );
755 
756 
757                 Reference < XInterface > x = m_rFactory->createInstance(
758                     OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe" )) );
759 
760                 Reference <XInputStream > rPipeInput( x , UNO_QUERY );
761                 Reference <XOutputStream >  rPipeOutput( x , UNO_QUERY );
762 
763                 x = m_rFactory->createInstance(
764                     OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream") ) );
765 
766                 Reference <XInputStream > markableInput( x , UNO_QUERY );
767                 Reference <XActiveDataSink> markableSink( x , UNO_QUERY );
768 
769                 x = m_rFactory->createInstance( OUString(
770                     RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableOutputStream" ) ) );
771                 Reference <XOutputStream >  markableOutput( x , UNO_QUERY );
772                 Reference <XActiveDataSource >  markableSource( x , UNO_QUERY );
773 
774                 OSL_ASSERT( markableInput.is()  );
775                 OSL_ASSERT( markableOutput.is() );
776                 OSL_ASSERT( markableSink.is()   );
777                 OSL_ASSERT( markableSource.is() );
778 
779                 markableSink->setInputStream( rPipeInput );
780                 markableSource->setOutputStream( rPipeOutput );
781 
782                 if( ! rSink.is() ) {
783                     x = m_rFactory->createInstance(
784                         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectInputStream") ));
785                     rInput = Reference < XObjectInputStream > ( x , UNO_QUERY );
786                     rSink = Reference < XActiveDataSink > ( x , UNO_QUERY );
787                 }
788                 else if ( !rSource.is() ) {
789                     x = m_rFactory->createInstance(
790                         OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.ObjectOutputStream" )));
791                     rOutput = Reference <XObjectOutputStream > ( x , UNO_QUERY );
792                     rSource = Reference <XActiveDataSource>( x, UNO_QUERY );
793                 }
794 
795                 OSL_ASSERT( rPipeInput.is() );
796                 OSL_ASSERT( rPipeOutput.is() );
797 
798                 rSink->setInputStream( markableInput );
799                 rSource->setOutputStream( markableOutput );
800 
801                 OSL_ASSERT( rSink->getInputStream().is() );
802                 OSL_ASSERT( rSource->getOutputStream().is() );
803 
804                 if( 1 + DATASTREAM_TEST_MAX_HANDLE == hTestHandle ) {
805                     testObject( rOutput , rInput);
806                 }
807                 rInput->closeInput();
808                 rOutput->closeOutput();
809 
810             }
811         }
812         catch( Exception &e )  {
813             OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
814             BUILD_ERROR( 0 , o.getStr() );
815         }
816         catch( ... ) {
817             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
818         }
819 
820         hTestHandle ++;
821 
822         if( hTestHandle > 1 +DATASTREAM_TEST_MAX_HANDLE ) {
823             // all tests finished.
824             hTestHandle = -1;
825         }
826     }
827     else {
828         BUILD_ERROR( 0 , "service not supported by test." );
829     }
830     return hTestHandle;
831 }
832 
833 
834 sal_Bool compareMyPropertySet( Reference< XPropertySet > &r1 , Reference < XPropertySet > &r2 )
835 {
836     sal_Bool b = sal_True;
837 
838     if( r1->getPropertyValue( OUString::createFromAscii("long") ).getValueType() == getCppuVoidType() ||
839         r2->getPropertyValue( OUString::createFromAscii("long") ).getValueType() == getCppuVoidType() ) {
840 
841         // one of the objects is not the correct propertyset !
842         fprintf( stderr, "compareMyPropertySet: 1\n" );
843         return sal_False;
844     }
845 
846     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("long")) ) ==
847                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("long")) ) );
848     if( ! b ) fprintf( stderr, "compareMyPropertySet: 2\n" );
849 
850     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) ==
851                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) );
852     if( ! b ){
853         float f1(0.0);
854         float f2(0.0);
855         r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) >>= f1;
856         r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("float")) ) >>= f2;
857         fprintf( stderr, "compareMyPropertySet: %f %f 3\n",f1,f2 );
858     }
859 
860     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("double")) ) ==
861                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("double" ))) );
862     if( ! b ) fprintf( stderr, "compareMyPropertySet: 4\n" );
863 
864     sal_Bool b1(sal_False), b2(sal_False);
865     Any a =r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
866     a >>= b1;
867     a = r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) );
868     a >>= b2;
869     b = b && ( (b1 && b2) || b1 == b2 );
870     if( ! b ) fprintf( stderr, "compareMyPropertySet: 5\n" );
871 
872 //      b = b &&    r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) ) ==
873 //                  r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("bool")) ) );
874 
875     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("byte")) ) ==
876                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("byte")) ) );
877     if( ! b ) fprintf( stderr, "compareMyPropertySet: 6\n" );
878 
879     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("char")) ) ==
880                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("char")) ) );
881     if( ! b ) fprintf( stderr, "compareMyPropertySet: 7\n" );
882 
883     b = b && (  r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("string")) ) ==
884                 r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("string")) ));
885     if( ! b ) fprintf( stderr, "compareMyPropertySet: 8\n" );
886 
887     Any o1 = r1->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("object")) );
888     Any o2 = r2->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("object")) );
889 
890     if( o1.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
891 
892         if( o2.getValueType() == getCppuType( (Reference<XPersistObject>*)0 ) ) {
893             Reference < XPersistObject > rPersist1;
894             Reference < XPersistObject > rPersist2;
895             o1 >>= rPersist1;
896             o2 >>= rPersist2;
897             Reference <XPropertySet > rProp1( rPersist1 , UNO_QUERY );
898             Reference < XPropertySet > rProp2( rPersist2 , UNO_QUERY );
899 
900             if( rProp1.is() && rProp2.is() && ! ( rProp1 == rProp2 )
901                 &&( rProp1 != r1 )) {
902                     b = b && compareMyPropertySet( rProp1 , rProp2 );
903             }
904         }
905         else {
906             b = sal_False;
907         }
908         if( ! b ) fprintf( stderr, "compareMyPropertySet: 9\n" );
909     }
910     else {
911         if( o2.getValueType()  == getCppuType( (Reference<XPersistObject>*)0 ) ) {
912             b = sal_False;
913         }
914         if( ! b ) fprintf( stderr, "compareMyPropertySet: 10\n" );
915     }
916 
917     return b;
918 }
919 
920 void OObjectStreamTest::testObject(     const Reference<  XObjectOutputStream >  &rOut,
921                                         const Reference < XObjectInputStream > &rIn )
922 {
923     ERROR_ASSERT( rOut.is() , "no objectOutputStream" );
924     ERROR_ASSERT( rIn.is() , "no objectInputStream" );
925 
926 
927 
928     // tests, if saving an object with an unknown service name allows
929     // reading the data behind the object !
930     {
931         Reference < XInterface > x = * new MyPersistObject(
932             OUString( RTL_CONSTASCII_USTRINGPARAM("bla blubs")) );
933 
934         Reference< XPersistObject > persistRef( x , UNO_QUERY );
935         ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
936 
937         rOut->writeObject( persistRef );
938         rOut->writeLong( (sal_Int32) 0xdeadbeef );
939 
940         ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
941 
942         try
943         {
944             Reference <XPersistObject >  xReadPersistRef = rIn->readObject();
945             ERROR_ASSERT( 0 , "expected exception not thrown" );
946         }
947         catch( IOException & )
948         {
949             // all is ok
950         }
951 
952         ERROR_ASSERT( (sal_Int32) 0xdeadbeef == rIn->readLong() ,
953                       "wrong data after object with unknown service name." );
954     }
955 
956     {
957         Reference < XInterface > x = m_rFactory->createInstance(
958             OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.PersistTest")));
959         Reference< XPersistObject > persistRef( x , UNO_QUERY );
960 
961         ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
962 
963         Reference < XPropertySet > rProp( persistRef , UNO_QUERY );
964         ERROR_ASSERT( rProp.is() , "test object is no property set " );
965 
966         Any any;
967         sal_Int32 i = 0x83482;
968         any <<= i;
969         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any );
970 
971         float f = (float)42.23;
972         any <<= f;
973         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any );
974 
975         double d = 233.321412;
976         any <<= d;
977         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("double")) , any );
978 
979         sal_Bool b = sal_True;
980         any.setValue( &b , getCppuBooleanType() );
981         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("bool")) , any );
982 
983         sal_Int8 by = 120;
984         any <<= by;
985         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("byte")) , any );
986 
987         sal_Unicode c = 'h';
988         any.setValue( &c , getCppuCharType() );
989         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("char")) , any );
990 
991         OUString str( RTL_CONSTASCII_USTRINGPARAM( "hi du !" ) );
992         any <<= str;
993         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("string")) , any );
994 
995         any <<= persistRef;
996         rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("object")) , any );
997 
998         // do read and write
999         rOut->writeObject( persistRef );
1000         ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
1001         Reference< XPersistObject > xReadPersist    =   rIn->readObject( );
1002 
1003         Reference< XPropertySet >  rPropRead( xReadPersist , UNO_QUERY );
1004         ERROR_ASSERT( compareMyPropertySet( rProp , rPropRead ) , "objects has not been read properly !" );
1005 
1006         // destroy selfreferences
1007         rProp->setPropertyValue( OUString::createFromAscii("object"), Any() );
1008         rPropRead->setPropertyValue( OUString::createFromAscii("object"), Any() );
1009     }
1010 
1011     {
1012         Reference< XMarkableStream > markableOut( rOut , UNO_QUERY );
1013         ERROR_ASSERT( markableOut.is() , "markable stream cannot be queried" );
1014 
1015         // do the same thing multiple times to check if
1016         // buffering and marks work correctly
1017         for( int i = 0 ; i < 2000 ; i ++ ) {
1018 
1019             Reference < XInterface > x = m_rFactory->createInstance(OUString::createFromAscii("test.com.sun.star.io.PersistTest"));
1020             Reference< XPersistObject >  persistRef( x , UNO_QUERY );
1021 
1022             Reference < XPropertySet >  rProp( persistRef , UNO_QUERY );
1023             ERROR_ASSERT( rProp.is() , "test object is no property set " );
1024 
1025             Any any;
1026             sal_Int32 i = 0x83482;
1027             any <<= i;
1028             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("long")) , any );
1029 
1030             float f = 42.23;
1031             any <<= f;
1032             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("float")) , any );
1033 
1034             double d = 233.321412;
1035             any <<= d;
1036             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("double")) , any );
1037 
1038             sal_Bool b = sal_True;
1039             any.setValue( &b , getCppuBooleanType() );
1040             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("bool")) , any );
1041 
1042             sal_Int8 by = 120;
1043             any <<= by;
1044             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("byte")) , any );
1045 
1046             sal_Unicode c = 'h';
1047             any.setValue( &c , getCppuCharType() );
1048             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("char")) , any );
1049 
1050             OUString str( RTL_CONSTASCII_USTRINGPARAM( "hi du !" ) );
1051             any <<= str;
1052             rProp->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("string")) , any );
1053 
1054             x = m_rFactory->createInstance(OUString::createFromAscii("test.com.sun.star.io.PersistTest"));
1055             Reference <XPersistObject > persist2ndRef( x , UNO_QUERY );
1056 
1057             // Note : persist2ndRef contains coincident values, but also coincident values must be
1058             // saved properly !
1059             any <<= persist2ndRef;
1060             rProp->setPropertyValue( OUString::createFromAscii("object") , any );
1061 
1062             // simply test, if markable operations and object operations do not interfere
1063             sal_Int32 nMark = markableOut->createMark();
1064 
1065             // do read and write
1066             rOut->writeObject( persistRef );
1067 
1068             // further markable tests !
1069             sal_Int32 nOffset = markableOut->offsetToMark( nMark );
1070             markableOut->jumpToMark( nMark );
1071             markableOut->deleteMark( nMark );
1072             markableOut->jumpToFurthest();
1073 
1074 
1075 
1076 
1077 
1078             ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
1079             Reference < XPersistObject > xReadPersistRef    =   rIn->readObject( );
1080 
1081             Reference< XPropertySet > rProp1( persistRef , UNO_QUERY );
1082             Reference< XPropertySet >  rProp2( xReadPersistRef , UNO_QUERY );
1083             ERROR_ASSERT( compareMyPropertySet( rProp1, rProp2) ,
1084                           "objects has not been read properly !" );
1085         }
1086     }
1087 }
1088 
1089 
1090 Reference < XInterface > SAL_CALL OObjectStreamTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
1091 {
1092     OObjectStreamTest *p = new OObjectStreamTest( rSMgr );
1093     return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
1094 }
1095 
1096 Sequence<OUString> OObjectStreamTest_getSupportedServiceNames( int i) throw ()
1097 {
1098     Sequence<OUString> aRet(1);
1099     aRet.getArray()[0] = OObjectStreamTest_getImplementationName( i);
1100     return aRet;
1101 }
1102 
1103 OUString     OObjectStreamTest_getServiceName( int i) throw ()
1104 {
1105     if( 1 == i ) {
1106         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectInputStream" ));
1107     }
1108     else {
1109         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.ObjectOutputStream"));
1110     }
1111 }
1112 
1113 OUString    OObjectStreamTest_getImplementationName( int i) throw ()
1114 {
1115     if( 1 == i ) {
1116         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectInputStream" ));
1117     }
1118     else {
1119         return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.comp.extensions.stm.ObjectOutputStream"));
1120     }
1121 }
1122 
1123 
1124