xref: /trunk/main/extensions/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_extensions.hxx"
30 #include <smart/com/sun/star/test/XSimpleTest.hxx>
31 #include <smart/com/sun/star/io/XObjectOutputStream.hxx>
32 #include <smart/com/sun/star/io/XObjectInputStream.hxx>
33 #include <smart/com/sun/star/io/XActiveDataSink.hxx>
34 #include <smart/com/sun/star/io/XActiveDataSource.hxx>
35 #include <smart/com/sun/star/io/XConnectable.hxx>
36 #include <smart/com/sun/star/io/XMarkableStream.hxx>
37 
38 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
39 
40 #include <smart/com/sun/star/beans/XPropertySet.hxx>
41 
42 #include <usr/services.hxx>
43 #include <usr/factoryhlp.hxx>
44 
45 #include <usr/reflserv.hxx>  // for EXTERN_SERVICE_CALLTYPE
46 #include <usr/weak.hxx>      // OWeakObject
47 
48 #include <vos/conditn.hxx>
49 #include <vos/mutex.hxx>
50 #include <vos/thread.hxx>
51 
52 #include <string.h>
53 
54 #include "testfactreg.hxx"
55 
56 using namespace vos;
57 using namespace usr;
58 
59 #define DATASTREAM_TEST_MAX_HANDLE 1
60 
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 ).
66 *
67 *
68 *****/
69 
70 class ODataStreamTest :
71         public XSimpleTest,
72         public OWeakObject
73 {
74 public:
75     ODataStreamTest( const XMultiServiceFactoryRef & rFactory ) : m_rFactory( rFactory ){}
76 
77 public: // refcounting
78     BOOL                        queryInterface( Uik aUik, XInterfaceRef & rOut );
79     void                        acquire()                        { OWeakObject::acquire(); }
80     void                        release()                        { OWeakObject::release(); }
81     void*                       getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
82 
83 public:
84     virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
85                                                                 THROWS( (   IllegalArgumentException,
86                                                                             UsrSystemException) );
87 
88     virtual INT32 test( const UString& TestName,
89                         const XInterfaceRef& TestObject,
90                         INT32 hTestHandle)                      THROWS( (   IllegalArgumentException,
91                                                                             UsrSystemException) );
92 
93     virtual BOOL testPassed(void)                               THROWS( (   UsrSystemException) );
94     virtual Sequence< UString > getErrors(void)                 THROWS( (UsrSystemException) );
95     virtual Sequence< UsrAny > getErrorExceptions(void)         THROWS( (UsrSystemException) );
96     virtual Sequence< UString > getWarnings(void)               THROWS( (UsrSystemException) );
97 
98 private:
99     void testSimple( const XDataInputStreamRef & , const XDataOutputStreamRef &);
100 
101 protected:
102     Sequence<UsrAny>  m_seqExceptions;
103     Sequence<UString> m_seqErrors;
104     Sequence<UString> m_seqWarnings;
105 
106     XMultiServiceFactoryRef m_rFactory;
107 };
108 
109 
110 BOOL ODataStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
111 {
112     if( XSimpleTest::getSmartUik() == uik ) {
113         rOut = (XSimpleTest *) this;
114     }
115     else {
116         return OWeakObject::queryInterface( uik , rOut );
117     }
118     return TRUE;
119 }
120 
121 
122 void ODataStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
123                                                                 THROWS( (   IllegalArgumentException,
124                                                                             UsrSystemException) )
125 {
126     if( L"com.sun.star.io.DataInputStream" == TestName ) {
127         XConnectableRef connect( TestObject , USR_QUERY );
128         XActiveDataSinkRef active( TestObject , USR_QUERY );
129         XInputStreamRef input( TestObject , USR_QUERY );
130         XDataInputStreamRef dataInput( TestObject , USR_QUERY );
131 
132         WARNING_ASSERT( connect.is(), "XConnectable cannot be queried" );
133         WARNING_ASSERT( active.is() , "XActiveDataSink cannot be queried" );
134         ERROR_ASSERT( input.is() , "XInputStream cannot be queried" );
135         ERROR_ASSERT( dataInput.is() , "XDataInputStream cannot be queried" );
136 
137 
138     }
139     else if( L"com.sun.star.io.DataInputStream" == TestName ) {
140         XConnectableRef connect( TestObject , USR_QUERY );
141         XActiveDataSourceRef active( TestObject , USR_QUERY );
142         XOutputStreamRef output( TestObject , USR_QUERY );
143         XDataOutputStreamRef dataOutput( TestObject , USR_QUERY );
144 
145         WARNING_ASSERT( connect.is(),       "XConnectable cannot be queried" );
146         WARNING_ASSERT( active.is() ,   "XActiveDataSink cannot be queried" );
147         ERROR_ASSERT(   output.is() ,   "XInputStream cannot be queried" );
148         ERROR_ASSERT(   dataOutput.is(),    "XDataInputStream cannot be queried" );
149 
150     }
151 
152     XServiceInfoRef info( TestObject, USR_QUERY );
153     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
154     if( info.is() )
155     {
156         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
157         ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
158     }
159 
160 }
161 
162 
163 INT32 ODataStreamTest::test(    const UString& TestName,
164                         const XInterfaceRef& TestObject,
165                         INT32 hTestHandle)                      THROWS( (   IllegalArgumentException,
166                                                                             UsrSystemException) )
167 {
168     if( L"com.sun.star.io.DataInputStream" == TestName ||
169         L"com.sun.star.io.DataOutputStream" == TestName )  {
170 
171         try {
172             if( 0 == hTestHandle ) {
173                 testInvariant( TestName , TestObject );
174             }
175             else {
176                 XActiveDataSinkRef rSink( TestObject, USR_QUERY );
177                 XActiveDataSourceRef rSource( TestObject , USR_QUERY );
178 
179                 XDataInputStreamRef rInput( TestObject , USR_QUERY );
180                 XDataOutputStreamRef rOutput( TestObject , USR_QUERY );
181 
182 
183                 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
184 
185                 XInputStreamRef  rPipeInput( x , USR_QUERY );
186                 XOutputStreamRef rPipeOutput( x , USR_QUERY );
187 
188                 if( ! rSink.is() ) {
189                     x = m_rFactory->createInstance( L"com.sun.star.io.DataInputStream" );
190                     x->queryInterface( XDataInputStream::getSmartUik() , rInput );
191                     x->queryInterface( XActiveDataSink::getSmartUik() , rSink );
192                 }
193                 else if ( !rSource.is() ) {
194 
195                     x = m_rFactory->createInstance( L"com.sun.star.io.DataOutputStream" );
196                     x->queryInterface( XDataOutputStream::getSmartUik() , rOutput );
197                     x->queryInterface( XActiveDataSource::getSmartUik() , rSource );
198                 }
199 
200                 OSL_ASSERT( rPipeInput.is() );
201                 OSL_ASSERT( rPipeOutput.is() );
202                 rSink->setInputStream( rPipeInput );
203                 rSource->setOutputStream( rPipeOutput );
204 
205                 OSL_ASSERT( rSink->getInputStream().is() );
206                 OSL_ASSERT( rSource->getOutputStream().is() );
207 
208                 if( 1 == hTestHandle ) {
209                     testSimple( rInput , rOutput );
210                 }
211             }
212         }
213         catch( Exception& e )  {
214             BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
215         }
216         catch(...) {
217             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
218         }
219 
220         hTestHandle ++;
221 
222         if( hTestHandle >= 2) {
223             // all tests finished.
224             hTestHandle = -1;
225         }
226     }
227     else {
228         BUILD_ERROR( 0 , "service not supported by test." );
229     }
230     return hTestHandle;
231 }
232 
233 
234 
235 BOOL ODataStreamTest::testPassed(void)                                      THROWS( (UsrSystemException) )
236 {
237     return m_seqErrors.getLen() == 0;
238 }
239 
240 
241 Sequence< UString > ODataStreamTest::getErrors(void)                            THROWS( (UsrSystemException) )
242 {
243     return m_seqErrors;
244 }
245 
246 
247 Sequence< UsrAny > ODataStreamTest::getErrorExceptions(void)                    THROWS( (UsrSystemException) )
248 {
249     return m_seqExceptions;
250 }
251 
252 
253 Sequence< UString > ODataStreamTest::getWarnings(void)                      THROWS( (UsrSystemException) )
254 {
255     return m_seqWarnings;
256 }
257 
258 void ODataStreamTest::testSimple(   const XDataInputStreamRef &rInput,
259                                     const XDataOutputStreamRef &rOutput )
260 {
261     rOutput->writeLong( 0x34ff3c );
262     rOutput->writeLong( 0x34ff3d );
263 
264     ERROR_ASSERT( 0x34ff3c ==  rInput->readLong() , "long read/write mismatch" );
265     ERROR_ASSERT( 0x34ff3d ==  rInput->readLong() , "long read/write mismatch" );
266 
267     rOutput->writeByte( 0x87 );
268     ERROR_ASSERT( 0x87 == rInput->readByte() , "byte read/write mismatch" );
269 
270     rOutput->writeBoolean( 25 );
271     ERROR_ASSERT( rInput->readBoolean() , "boolean read/write mismatch" );
272 
273     rOutput->writeBoolean( FALSE );
274     ERROR_ASSERT( ! rInput->readBoolean() , "boolean read/write mismatch" );
275 
276     rOutput->writeFloat( (float) 42.42 );
277     ERROR_ASSERT( rInput->readFloat() == ((float)42.42) , "float read/write mismatch" );
278 
279     rOutput->writeDouble( (double) 42.42 );
280     ERROR_ASSERT( rInput->readDouble() == 42.42 , "double read/write mismatch" );
281 
282     rOutput->writeUTF( L"Live long and prosper !" );
283     ERROR_ASSERT( rInput->readUTF() == L"Live long and prosper !" ,
284                     "UTF read/write mismatch" );
285 
286     Sequence<wchar_t> wc(0x10001);
287     for( int i = 0 ; i < 0x10000  ; i ++ ) {
288         wc.getArray()[i] = L'c';
289     }
290     wc.getArray()[0x10000] = 0;
291     UString str( wc.getArray() , 0x10000 );
292     rOutput->writeUTF( str );
293     ERROR_ASSERT( rInput->readUTF() == str , "error reading 64k block" );
294 
295     rOutput->closeOutput();
296     try {
297         rInput->readLong();
298         ERROR_ASSERT( 0 , "eof-exception does not occur !" );
299     }
300     catch ( IOException& e ){
301         //ok
302         e.getName();
303     }
304     catch(...)  {
305         ERROR_ASSERT( 0 , "wrong exception after reading beyond eof" );
306     }
307 
308     ERROR_ASSERT( ! rInput->readBytes( Sequence<BYTE> (1) , 1 ),
309                     "stream must be on eof !" );
310 
311     rInput->closeInput();
312 
313     try {
314         rOutput->writeByte( 1 );
315         ERROR_ASSERT( 0 , "writing still possible though chain must be interrupted" );
316     }
317     catch( IOException& e )
318     {
319         e.getName();
320         // ok
321     }
322     catch( ... ) {
323         ERROR_ASSERT( 0 , "IOException  expected, but another exception was thrown" );
324     }
325 
326 }
327 
328 
329 
330 /**
331 * for external binding
332 *
333 *
334 **/
335 XInterfaceRef ODataStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
336 {
337     ODataStreamTest *p = new ODataStreamTest( rSMgr );
338     XInterfaceRef xService = *p;
339     return xService;
340 }
341 
342 Sequence<UString> ODataStreamTest_getSupportedServiceNames( int i) THROWS( () )
343 {
344     Sequence<UString> aRet(1);
345 
346     aRet.getArray()[0] = ODataStreamTest_getImplementationName( i);
347 
348 
349     return aRet;
350 }
351 
352 UString     ODataStreamTest_getServiceName( int i) THROWS( () )
353 {
354     if( 1 == i ) {
355         return L"test.com.sun.star.io.DataInputStream";
356     }
357     else {
358         return L"test.com.sun.star.io.DataOutputStream";
359     }
360 }
361 
362 UString     ODataStreamTest_getImplementationName( int i) THROWS( () )
363 {
364     if( 1 == i ) {
365         return L"test.com.sun.star.comp.extensions.stm.DataInputStream";
366     }
367     else {
368         return L"test.com.sun.star.comp.extensions.stm.DataOutputStream";
369     }
370 }
371 
372 
373 /**------------------------------------------------------
374 *
375 *
376 *
377 *
378 *
379 *------------------------------------------------------*/
380 class MyPersistObject :
381             public XPersistObject,
382             public XPropertySet,
383             public OWeakObject
384 {
385 public:
386     MyPersistObject( ) : m_sServiceName( OMyPersistObject_getServiceName() )
387         {}
388     MyPersistObject( const UString & sServiceName ) : m_sServiceName( sServiceName )
389         {}
390     ~MyPersistObject()
391         {}
392 
393 public:
394     BOOL                        queryInterface( Uik aUik, XInterfaceRef & rOut );
395     void                        acquire()                        { OWeakObject::acquire(); }
396     void                        release()                        { OWeakObject::release(); }
397     void*                       getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
398 
399 public:
400     virtual UString getServiceName(void) const THROWS( (UsrSystemException) );
401     virtual void write(const XObjectOutputStreamRef& OutStream) THROWS( (IOException, UsrSystemException) );
402     virtual void read(const XObjectInputStreamRef& InStream) THROWS( (IOException, UsrSystemException) );
403 
404 public:
405 
406     virtual XPropertySetInfoRef getPropertySetInfo(void) const  THROWS( (UsrSystemException) );
407     virtual void setPropertyValue(const UString& aPropertyName, const UsrAny& aValue)
408                                                                 THROWS( (   UnknownPropertyException,
409                                                                             PropertyVetoException,
410                                                                             IllegalArgumentException,
411                                                                             WrappedTargetException,
412                                                                             UsrSystemException) );
413     virtual UsrAny getPropertyValue(const UString& PropertyName) const
414                                                                 THROWS( (   UnknownPropertyException,
415                                                                             WrappedTargetException,
416                                                                             UsrSystemException) );
417     virtual void addPropertyChangeListener( const UString& aPropertyName,
418                                             const XPropertyChangeListenerRef& xListener)
419                                                                 THROWS( (   UnknownPropertyException,
420                                                                             WrappedTargetException,
421                                                                             UsrSystemException) );
422     virtual void removePropertyChangeListener(  const UString& aPropertyName,
423                                                 const XPropertyChangeListenerRef& aListener)
424                                                                 THROWS( (   UnknownPropertyException,
425                                                                             WrappedTargetException,
426                                                                             UsrSystemException) );
427     virtual void addVetoableChangeListener(     const UString& PropertyName,
428                                                 const XVetoableChangeListenerRef& aListener)
429                                                                 THROWS( (   UnknownPropertyException,
430                                                                             WrappedTargetException,
431                                                                             UsrSystemException) );
432     virtual void removeVetoableChangeListener(  const UString& PropertyName,
433                                                 const XVetoableChangeListenerRef& aListener)
434                                                                 THROWS( (   UnknownPropertyException,
435                                                                             WrappedTargetException,
436                                                                             UsrSystemException) );
437 
438 public:
439     INT32   m_l;
440     float   m_f;
441     double  m_d;
442     BOOL    m_b;
443     BYTE    m_byte;
444     wchar_t m_c;
445     UString m_s;
446     XPersistObjectRef   m_ref;
447     UString m_sServiceName;
448 };
449 
450 
451 
452 XPropertySetInfoRef MyPersistObject::getPropertySetInfo(void) const     THROWS( (UsrSystemException) )
453 {
454     return XPropertySetInfoRef();
455 }
456 
457 void MyPersistObject::setPropertyValue(const UString& aPropertyName, const UsrAny& aValue)
458                                                                 THROWS( (   UnknownPropertyException,
459                                                                             PropertyVetoException,
460                                                                             IllegalArgumentException,
461                                                                             WrappedTargetException,
462                                                                             UsrSystemException) )
463 {
464     if( L"long" == aPropertyName  ) {
465         m_l = aValue.getINT32();
466     }
467     else if ( L"float" == aPropertyName ) {
468         m_f = aValue.getFloat();
469     }
470     else if( L"double" == aPropertyName ) {
471         m_d = aValue.getDouble();
472     }
473     else if( L"bool" == aPropertyName ) {
474         m_b = aValue.getBOOL();
475     }
476     else if( L"byte" == aPropertyName ) {
477         m_byte = aValue.getBYTE();
478     }
479     else if( L"char" == aPropertyName ) {
480         m_c = aValue.getChar();
481     }
482     else if( L"string" == aPropertyName ) {
483         m_s = aValue.getString();
484     }
485     else if( L"object" == aPropertyName ) {
486         if( aValue.getReflection() == XPersistObject_getReflection() ) {
487             XPersistObjectRef *pRef = (XPersistObjectRef*) aValue.get();
488             if( pRef ) {
489                 m_ref = *pRef;
490             }
491             else {
492                 m_ref = 0;
493             }
494         }
495         else
496         {
497             m_ref = 0;
498         }
499     }
500 }
501 
502 
503 UsrAny MyPersistObject::getPropertyValue(const UString& aPropertyName) const
504                                                                 THROWS( (   UnknownPropertyException,
505                                                                             WrappedTargetException,
506                                                                             UsrSystemException) )
507 {
508     UsrAny aValue;
509     if( L"long" == aPropertyName  ) {
510         aValue.setINT32( m_l );
511     }
512     else if ( L"float" == aPropertyName ) {
513         aValue.setFloat( m_f );
514     }
515     else if( L"double" == aPropertyName ) {
516         aValue.setDouble( m_d );
517     }
518     else if( L"bool" == aPropertyName ) {
519         aValue.setBOOL( m_b );
520     }
521     else if( L"byte" == aPropertyName ) {
522         aValue.setBYTE( m_byte );
523     }
524     else if( L"char" == aPropertyName ) {
525         aValue.setChar( m_c );
526     }
527     else if( L"string" == aPropertyName ) {
528         aValue.setString( m_s );
529     }
530     else if( L"object" == aPropertyName ) {
531         aValue.set( &m_ref , XPersistObject_getReflection() );
532     }
533     return aValue;
534 }
535 
536 
537 void MyPersistObject::addPropertyChangeListener(    const UString& aPropertyName,
538                                                     const XPropertyChangeListenerRef& xListener)
539                                                                 THROWS( (   UnknownPropertyException,
540                                                                             WrappedTargetException,
541                                                                             UsrSystemException) )
542 {
543 
544 }
545 
546 void MyPersistObject::removePropertyChangeListener( const UString& aPropertyName,
547                                                     const XPropertyChangeListenerRef& aListener)
548                                                                 THROWS( (   UnknownPropertyException,
549                                                                             WrappedTargetException,
550                                                                             UsrSystemException) )
551 {
552 }
553 void MyPersistObject::addVetoableChangeListener(const UString& PropertyName,
554                                                 const XVetoableChangeListenerRef& aListener)
555                                                                 THROWS( (   UnknownPropertyException,
556                                                                             WrappedTargetException,
557                                                                             UsrSystemException) )
558 {
559 
560 }
561 
562 void MyPersistObject::removeVetoableChangeListener( const UString& PropertyName,
563                                     const XVetoableChangeListenerRef& aListener)
564                                                                 THROWS( (   UnknownPropertyException,
565                                                                             WrappedTargetException,
566                                                                             UsrSystemException) )
567 {
568 
569 }
570 
571 
572 
573 BOOL MyPersistObject::queryInterface( Uik aUik , XInterfaceRef &rOut )
574 {
575     if( XPersistObject::getSmartUik() == aUik ) {
576         rOut = ( XPersistObject * ) this;
577     }
578     else if( XPropertySet::getSmartUik() == aUik ) {
579         rOut = ( XPropertySet * ) this;
580     }
581     else
582         return OWeakObject::queryInterface( aUik , rOut );
583 
584     return TRUE;
585 }
586 
587 UString MyPersistObject::getServiceName() const THROWS( (UsrSystemException) )
588 {
589     return m_sServiceName;
590 }
591 
592 void MyPersistObject::write( const XObjectOutputStreamRef & rOut )
593                                                     THROWS( (IOException,UsrSystemException))
594 {
595     rOut->writeLong( m_l);
596     rOut->writeFloat( m_f );
597     rOut->writeDouble( m_d );
598     rOut->writeBoolean( m_b );
599     rOut->writeByte( m_byte );
600     rOut->writeChar( m_c );
601     rOut->writeUTF( m_s );
602     rOut->writeObject( m_ref );
603 }
604 
605 
606 void MyPersistObject::read( const XObjectInputStreamRef & rIn )
607                                                     THROWS( (IOException, UsrSystemException) )
608 {
609     m_l = rIn->readLong();
610     m_f = rIn->readFloat();
611     m_d = rIn->readDouble();
612     m_b = rIn->readBoolean();
613     m_byte = rIn->readByte();
614     m_c = rIn->readChar();
615     m_s = rIn->readUTF();
616     m_ref = rIn->readObject();
617 }
618 
619 XInterfaceRef OMyPersistObject_CreateInstance( const XMultiServiceFactoryRef & rSMgr )
620                                                     THROWS((Exception))
621 {
622     MyPersistObject *p = new MyPersistObject( );
623     XInterfaceRef xService = *p;
624     return xService;
625 }
626 
627 Sequence<UString> OMyPersistObject_getSupportedServiceNames( ) THROWS( () )
628 {
629     Sequence<UString> aRet(1);
630     aRet.getArray()[0] = OMyPersistObject_getImplementationName();
631     return aRet;
632 }
633 
634 UString     OMyPersistObject_getServiceName( ) THROWS( () )
635 {
636     return L"test.com.sun.star.io.PersistTest";
637 }
638 
639 UString     OMyPersistObject_getImplementationName( ) THROWS( () )
640 {
641     return L"test.com.sun.star.io.PersistTest";
642 }
643 
644 
645 // ---------------------------------------------
646 // -----------------------------------------------
647 class OObjectStreamTest :
648         public ODataStreamTest
649 {
650 public:
651     OObjectStreamTest( const XMultiServiceFactoryRef &r) : ODataStreamTest(r) {}
652 public: // refcounting
653     BOOL                        queryInterface( Uik aUik, XInterfaceRef & rOut );
654 
655 public:
656     virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
657                                                                 THROWS( (   IllegalArgumentException,
658                                                                             UsrSystemException) );
659 
660     virtual INT32 test( const UString& TestName,
661                         const XInterfaceRef& TestObject,
662                         INT32 hTestHandle)                      THROWS( (   IllegalArgumentException,
663                                                                             UsrSystemException) );
664 
665 
666 private:
667     void OObjectStreamTest::testObject(     const XObjectOutputStreamRef &rOut,
668                                             const XObjectInputStreamRef &rIn );
669 
670 private:
671 };
672 
673 
674 
675 
676 BOOL OObjectStreamTest::queryInterface( Uik uik , XInterfaceRef &rOut )
677 {
678     if( XSimpleTest::getSmartUik() == uik ) {
679         rOut = (XSimpleTest *) this;
680     }
681     else {
682         return ODataStreamTest::queryInterface( uik , rOut );
683     }
684     return TRUE;
685 }
686 
687 
688 void OObjectStreamTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
689                                                                 THROWS( (   IllegalArgumentException,
690                                                                             UsrSystemException) )
691 {
692 
693 
694     if( L"com.sun.star.io.ObjectInputStream" == TestName ) {
695         ODataStreamTest::testInvariant( TestName , TestObject );
696         XObjectInputStreamRef dataInput( TestObject , USR_QUERY );
697         XMarkableStreamRef markable( TestObject , USR_QUERY );
698         ERROR_ASSERT( dataInput.is() , "XObjectInputStream cannot be queried" );
699         ERROR_ASSERT(   markable.is() , "XMarkableStream cannot be queried" );
700     }
701     else if( L"com.sun.star.io.ObjectOutputStream" == TestName ) {
702         ODataStreamTest::testInvariant( TestName , TestObject );
703         XMarkableStreamRef markable( TestObject , USR_QUERY );
704         XObjectOutputStreamRef dataOutput( TestObject , USR_QUERY );
705         ERROR_ASSERT(   dataOutput.is(),    "XObjectOutputStream cannot be queried" );
706         ERROR_ASSERT(   markable.is() , "XMarkableStream cannot be queried" );
707     }
708 
709     XServiceInfoRef info( TestObject, USR_QUERY );
710     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
711     if( info.is() )
712     {
713         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
714         ERROR_ASSERT( ! info->supportsService( L"bla bluzb" ) , "XServiceInfo test failed" );
715     }
716 
717 }
718 
719 INT32 OObjectStreamTest::test(  const UString& TestName,
720                         const XInterfaceRef& TestObject,
721                         INT32 hTestHandle)                      THROWS( (   IllegalArgumentException,
722                                                                             UsrSystemException) )
723 {
724     if( L"com.sun.star.io.ObjectInputStream" == TestName ||
725         L"com.sun.star.io.ObjectOutputStream" == TestName )  {
726 
727         try {
728             if( 0 == hTestHandle ) {
729                 testInvariant( TestName , TestObject );
730             }
731             else if( DATASTREAM_TEST_MAX_HANDLE >= hTestHandle ) {
732                 INT32 hOldHandle = hTestHandle;
733                 hTestHandle = ODataStreamTest::test( L"com.sun.star.io.DataInputStream" ,
734                                                         TestObject , hTestHandle );
735                 if( hTestHandle == -1 ){
736                     hTestHandle = hOldHandle;
737                 }
738             }
739             else {
740 
741                 XActiveDataSinkRef rSink( TestObject, USR_QUERY );
742                 XActiveDataSourceRef rSource( TestObject , USR_QUERY );
743 
744                 XObjectInputStreamRef rInput( TestObject , USR_QUERY );
745                 XObjectOutputStreamRef rOutput( TestObject , USR_QUERY );
746 
747 
748                 XInterfaceRef x = m_rFactory->createInstance( L"com.sun.star.io.Pipe");
749 
750                 XInputStreamRef  rPipeInput( x , USR_QUERY );
751                 XOutputStreamRef rPipeOutput( x , USR_QUERY );
752 
753                 x = m_rFactory->createInstance( L"com.sun.star.io.MarkableInputStream" );
754                 XInputStreamRef     markableInput( x , USR_QUERY );
755                 XActiveDataSinkRef markableSink( x , USR_QUERY );
756 
757                 x = m_rFactory->createInstance( L"com.sun.star.io.MarkableOutputStream" );
758                 XOutputStreamRef    markableOutput( x , USR_QUERY );
759                 XActiveDataSourceRef    markableSource( x , USR_QUERY );
760 
761                 OSL_ASSERT( markableInput.is()  );
762                 OSL_ASSERT( markableOutput.is() );
763                 OSL_ASSERT( markableSink.is()   );
764                 OSL_ASSERT( markableSource.is() );
765 
766                 markableSink->setInputStream( rPipeInput );
767                 markableSource->setOutputStream( rPipeOutput );
768 
769                 if( ! rSink.is() ) {
770                     x = m_rFactory->createInstance( L"com.sun.star.io.ObjectInputStream" );
771                     x->queryInterface( XObjectInputStream::getSmartUik() , rInput );
772                     x->queryInterface( XActiveDataSink::getSmartUik() , rSink );
773                 }
774                 else if ( !rSource.is() ) {
775                     x = m_rFactory->createInstance( L"com.sun.star.io.ObjectOutputStream" );
776                     x->queryInterface( XObjectOutputStream::getSmartUik() , rOutput );
777                     x->queryInterface( XActiveDataSource::getSmartUik() , rSource );
778                 }
779 
780                 OSL_ASSERT( rPipeInput.is() );
781                 OSL_ASSERT( rPipeOutput.is() );
782 
783                 rSink->setInputStream( markableInput );
784                 rSource->setOutputStream( markableOutput );
785 
786                 OSL_ASSERT( rSink->getInputStream().is() );
787                 OSL_ASSERT( rSource->getOutputStream().is() );
788 
789                 if( 1 + DATASTREAM_TEST_MAX_HANDLE == hTestHandle ) {
790                     testObject( rOutput , rInput);
791                 }
792                 rInput->closeInput();
793                 rOutput->closeOutput();
794 
795             }
796         }
797         catch( Exception& e )  {
798             BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
799         }
800         catch(...) {
801             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
802         }
803 
804         hTestHandle ++;
805 
806         if( hTestHandle > 1 +DATASTREAM_TEST_MAX_HANDLE ) {
807             // all tests finished.
808             hTestHandle = -1;
809         }
810     }
811     else {
812         BUILD_ERROR( 0 , "service not supported by test." );
813     }
814     return hTestHandle;
815 }
816 
817 
818 BOOL compareMyPropertySet( XPropertySetRef &r1 , XPropertySetRef &r2 )
819 {
820     BOOL b = TRUE;
821 
822     if( r1->getPropertyValue( L"long" ).getReflection() == Void_getReflection() ||
823         r2->getPropertyValue( L"long" ).getReflection() == Void_getReflection() ) {
824 
825         // one of the objects is not the correct propertyset !
826         return FALSE;
827     }
828 
829     b = b && (  r1->getPropertyValue( L"long" ).getINT32() ==
830                 r2->getPropertyValue( L"long" ).getINT32() );
831 
832     b = b && (  r1->getPropertyValue( L"float" ).getFloat() ==
833                 r2->getPropertyValue( L"float" ).getFloat() );
834 
835     b = b && (  r1->getPropertyValue( L"double" ).getDouble() ==
836                 r2->getPropertyValue( L"double" ).getDouble() );
837 
838     b = b && (  r1->getPropertyValue( L"bool" ).getBOOL() ==
839                 r2->getPropertyValue( L"bool" ).getBOOL() );
840 
841     b = b && (  r1->getPropertyValue( L"byte" ).getBYTE() ==
842                 r2->getPropertyValue( L"byte" ).getBYTE() );
843 
844     b = b && (  r1->getPropertyValue( L"char" ).getChar() ==
845                 r2->getPropertyValue( L"char" ).getChar() );
846 
847     b = b && (  r1->getPropertyValue( L"string" ).getString() ==
848                 r2->getPropertyValue( L"string" ).getString() );
849 
850     UsrAny o1 = r1->getPropertyValue( L"object" );
851     UsrAny o2 = r2->getPropertyValue( L"object" );
852 
853     if( o1.getReflection() == XPersistObject_getReflection() ) {
854 
855         if( o2.getReflection() == XPersistObject_getReflection() ) {
856             XPersistObjectRef rPersist1 = *((XPersistObjectRef*) o1.get());
857             XPersistObjectRef rPersist2 = *((XPersistObjectRef*) o2.get());
858             XPropertySetRef rProp1( rPersist1 , USR_QUERY );
859             XPropertySetRef rProp2( rPersist2 , USR_QUERY );
860 
861             if( rProp1.is() && rProp2.is() && ! ( rProp1 == rProp2 )
862                 &&( rProp1 != r1 )) {
863                     compareMyPropertySet( rProp1 , rProp2 );
864             }
865         }
866         else {
867             b = FALSE;
868         }
869     }
870     else {
871         if( o2.getReflection() == XPersistObject_getReflection() ) {
872             b = FALSE;
873         }
874     }
875 
876     return b;
877 }
878 
879 void OObjectStreamTest::testObject(     const XObjectOutputStreamRef &rOut,
880                                         const XObjectInputStreamRef &rIn )
881 {
882     ERROR_ASSERT( rOut.is() , "no objectOutputStream" );
883     ERROR_ASSERT( rIn.is() , "no objectInputStream" );
884 
885     XMultiServiceFactoryRef xProv = getProcessServiceManager();
886     ERROR_ASSERT( xProv.is() , "no global service manager" );
887 
888 
889     // tests, if saving an object with an unknown service name allows
890     // reading the data behind the object !
891     {
892         XInterfaceRef x = * new MyPersistObject( L"bla blubs" );
893 
894         XPersistObjectRef persistRef( x , USR_QUERY );
895         ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
896 
897         rOut->writeObject( persistRef );
898         rOut->writeLong( (INT32) 0xdeadbeef );
899 
900         ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
901 
902         try
903         {
904             XPersistObjectRef xReadPersistRef = rIn->readObject();
905             ERROR_ASSERT( 0 , "expected exception not thrown" );
906         }
907         catch( IOException& e )
908         {
909             // all is ok
910         }
911 
912         ERROR_ASSERT( (INT32) 0xdeadbeef == rIn->readLong() ,
913                       "wrong data after object with unknown service name." );
914     }
915 
916     {
917         XInterfaceRef x = xProv->createInstance(L"test.com.sun.star.io.PersistTest");
918         XPersistObjectRef persistRef( x , USR_QUERY );
919 
920         ERROR_ASSERT( persistRef.is() , "couldn't instantiate PersistTest object" );
921 
922         XPropertySetRef rProp( persistRef , USR_QUERY );
923         ERROR_ASSERT( rProp.is() , "test object is no property set " );
924 
925         UsrAny any;
926         any.setINT32( 0x83482 );
927         rProp->setPropertyValue( L"long" , any );
928 
929         any.setFloat( 42.23 );
930         rProp->setPropertyValue( L"float" , any );
931 
932         any.setDouble(  233.321412 );
933         rProp->setPropertyValue( L"double" , any );
934 
935         any.setBOOL( TRUE );
936         rProp->setPropertyValue( L"bool" , any );
937 
938         any.setBYTE( 130 );
939         rProp->setPropertyValue( L"byte" , any );
940 
941         any.setChar( 'h' );
942         rProp->setPropertyValue( L"char" , any );
943 
944         any.setString( L"hi du !" );
945         rProp->setPropertyValue( L"string" , any );
946 
947         any.set( &persistRef, XPersistObject_getReflection() ); // try a self reference
948         rProp->setPropertyValue( L"object" , any );
949 
950         // do read and write
951         rOut->writeObject( persistRef );
952         ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
953         XPersistObjectRef xReadPersistRef   =   rIn->readObject( );
954 
955         XPropertySetRef rPropRead( xReadPersistRef , USR_QUERY );
956         ERROR_ASSERT( compareMyPropertySet( rProp , rPropRead ) , "objects has not been read properly !" );
957 
958         // destroy selfreferences
959         rProp->setPropertyValue( L"object", UsrAny() );
960         rPropRead->setPropertyValue( L"object", UsrAny() );
961     }
962 
963     {
964         XMarkableStreamRef markableOut( rOut , USR_QUERY );
965         ERROR_ASSERT( markableOut.is() , "markable stream cannot be queried" );
966 
967         // do the same thing multiple times to check if
968         // buffering and marks work correctly
969         for( int i = 0 ; i < 2000 ; i ++ ) {
970 
971             XInterfaceRef x = xProv->createInstance(L"test.com.sun.star.io.PersistTest");
972             XPersistObjectRef persistRef( x , USR_QUERY );
973 
974             XPropertySetRef rProp( persistRef , USR_QUERY );
975             ERROR_ASSERT( rProp.is() , "test object is no property set " );
976 
977             UsrAny any;
978             any.setINT32( 0x83482 );
979             rProp->setPropertyValue( L"long" , any );
980 
981             any.setFloat( 42.23 );
982             rProp->setPropertyValue( L"float" , any );
983 
984             any.setDouble(  233.321412 );
985             rProp->setPropertyValue( L"double" , any );
986 
987             any.setBOOL( TRUE );
988             rProp->setPropertyValue( L"bool" , any );
989 
990             any.setBYTE( 130 );
991             rProp->setPropertyValue( L"byte" , any );
992 
993             any.setChar( 'h' );
994             rProp->setPropertyValue( L"char" , any );
995 
996             any.setString( L"hi du !" );
997             rProp->setPropertyValue( L"string" , any );
998 
999 
1000             x = xProv->createInstance(L"test.com.sun.star.io.PersistTest");
1001             XPersistObjectRef persist2ndRef( x , USR_QUERY );
1002 
1003 
1004             // Note : persist2ndRef contains coincident values, but also coincident values must be
1005             // saved properly !
1006             any.set( &persist2ndRef, XPersistObject_getReflection() ); // try a self reference
1007             rProp->setPropertyValue( L"object" , any );
1008 
1009             // simply test, if markable operations and object operations do not interfere
1010             INT32 nMark = markableOut->createMark();
1011 
1012             // do read and write
1013             rOut->writeObject( persistRef );
1014 
1015             // further markable tests !
1016             INT32 nOffset = markableOut->offsetToMark( nMark );
1017             markableOut->jumpToMark( nMark );
1018             markableOut->deleteMark( nMark );
1019             markableOut->jumpToFurthest();
1020 
1021 
1022 
1023 
1024 
1025             ERROR_ASSERT( 0 != rIn->available() , "no data arrived at input" );
1026             XPersistObjectRef xReadPersistRef   =   rIn->readObject( );
1027 
1028             XPropertySetRef rProp1( persistRef , USR_QUERY );
1029             XPropertySetRef rProp2( xReadPersistRef , USR_QUERY );
1030             ERROR_ASSERT( compareMyPropertySet( rProp1, rProp2) , "objects has not been read properly !" );
1031         }
1032     }
1033 }
1034 
1035 
1036 XInterfaceRef OObjectStreamTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
1037 {
1038     OObjectStreamTest *p = new OObjectStreamTest( rSMgr );
1039     XInterfaceRef xService = *p;
1040     return xService;
1041 }
1042 
1043 Sequence<UString> OObjectStreamTest_getSupportedServiceNames( int i) THROWS( () )
1044 {
1045     Sequence<UString> aRet(1);
1046     aRet.getArray()[0] = OObjectStreamTest_getImplementationName( i);
1047     return aRet;
1048 }
1049 
1050 UString     OObjectStreamTest_getServiceName( int i) THROWS( () )
1051 {
1052     if( 1 == i ) {
1053         return L"test.com.sun.star.io.ObjectInputStream";
1054     }
1055     else {
1056         return L"test.com.sun.star.io.ObjectOutputStream";
1057     }
1058 }
1059 
1060 UString     OObjectStreamTest_getImplementationName( int i) THROWS( () )
1061 {
1062     if( 1 == i ) {
1063         return L"test.com.sun.star.comp.extensions.stm.ObjectInputStream";
1064     }
1065     else {
1066         return L"test.com.sun.star.comp.extensions.stm.ObjectOutputStream";
1067     }
1068 }
1069 
1070 
1071