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