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