1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <smart/com/sun/star/test/XSimpleTest.hxx>
21 #include <smart/com/sun/star/io/XOutputStream.hxx>
22 #include <smart/com/sun/star/io/XInputStream.hxx>
24 #include <smart/com/sun/star/lang/XServiceInfo.hxx>
26 #include <usr/factoryhlp.hxx>
28 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
29 #include <usr/weak.hxx> // OWeakObject
31 #include <osl/conditn.hxx>
32 #include <osl/mutex.hxx>
33 #include <osl/thread.hxx>
37 #include "testfactreg.hxx"
38 #define IMPLEMENTATION_NAME L"test.com.sun.star.comp.extensions.stm.Pipe"
39 #define SERVICE_NAME L"test.com.sun.star.io.Pipe"
43 class WriteToStreamThread
:
49 WriteToStreamThread( XOutputStreamRef xOutput
, int iMax
)
55 virtual ~WriteToStreamThread() {}
60 /// Working method which should be overridden.
62 for( int i
= 0 ; i
< m_iMax
; i
++ ) {
63 m_output
->writeBytes( createIntSeq(i
) );
65 m_output
->closeOutput();
68 /** Called when run() is done.
69 * You might want to override it to do some cleanup.
71 virtual void onTerminated()
79 XOutputStreamRef m_output
;
90 OPipeTest( const XMultiServiceFactoryRef
& rFactory
);
93 public: // refcounting
94 BOOL
queryInterface( Uik aUik
, XInterfaceRef
& rOut
);
95 void acquire() { OWeakObject::acquire(); }
96 void release() { OWeakObject::release(); }
97 void* getImplementation(Reflection
*p
) { return OWeakObject::getImplementation(p
); }
99 public: // implementation names
100 static Sequence
< UString
> getSupportedServiceNames_Static(void) THROWS(());
101 static UString
getImplementationName_Static() THROWS(());
104 virtual void testInvariant(const UString
& TestName
, const XInterfaceRef
& TestObject
)
105 THROWS( ( IllegalArgumentException
,
106 UsrSystemException
) );
108 virtual INT32
test( const UString
& TestName
,
109 const XInterfaceRef
& TestObject
,
110 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
111 UsrSystemException
) );
113 virtual BOOL
testPassed(void) THROWS( ( UsrSystemException
) );
114 virtual Sequence
< UString
> getErrors(void) THROWS( (UsrSystemException
) );
115 virtual Sequence
< UsrAny
> getErrorExceptions(void) THROWS( (UsrSystemException
) );
116 virtual Sequence
< UString
> getWarnings(void) THROWS( (UsrSystemException
) );
119 void testSimple( const XInterfaceRef
& );
120 void testBufferResizing( const XInterfaceRef
& );
121 void testMultithreading( const XInterfaceRef
& );
124 Sequence
<UsrAny
> m_seqExceptions
;
125 Sequence
<UString
> m_seqErrors
;
126 Sequence
<UString
> m_seqWarnings
;
132 OPipeTest::OPipeTest( const XMultiServiceFactoryRef
&rFactory
)
137 OPipeTest::~OPipeTest()
143 BOOL
OPipeTest::queryInterface( Uik uik
, XInterfaceRef
&rOut
)
145 if( XSimpleTest::getSmartUik() == uik
) {
146 rOut
= (XSimpleTest
*) this;
149 return OWeakObject::queryInterface( uik
, rOut
);
155 void OPipeTest::testInvariant( const UString
& TestName
, const XInterfaceRef
& TestObject
)
156 THROWS( ( IllegalArgumentException
,
157 UsrSystemException
) )
159 XServiceInfoRef
info( TestObject
, USR_QUERY
);
160 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
163 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
164 ERROR_ASSERT( ! info
->supportsService( L
"bla bluzb" ) , "XServiceInfo test failed" );
170 INT32
OPipeTest::test( const UString
& TestName
,
171 const XInterfaceRef
& TestObject
,
172 INT32 hTestHandle
) THROWS( ( IllegalArgumentException
,
173 UsrSystemException
) )
175 if( L
"com.sun.star.io.Pipe" == TestName
) {
177 if( 0 == hTestHandle
) {
178 testInvariant( TestName
, TestObject
);
180 else if( 1 == hTestHandle
) {
181 testSimple( TestObject
);
183 else if( 2 == hTestHandle
) {
184 testBufferResizing( TestObject
);
186 else if( 3 == hTestHandle
) {
187 testMultithreading( TestObject
);
190 catch( Exception
& e
) {
191 BUILD_ERROR( 0 , UStringToString( e
.getName() , CHARSET_SYSTEM
).GetCharStr() );
194 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
199 if( 4 == hTestHandle
) {
200 // all tests finished.
205 THROW( IllegalArgumentException() );
212 BOOL
OPipeTest::testPassed(void) THROWS( (UsrSystemException
) )
214 return m_seqErrors
.getLen() == 0;
218 Sequence
< UString
> OPipeTest::getErrors(void) THROWS( (UsrSystemException
) )
224 Sequence
< UsrAny
> OPipeTest::getErrorExceptions(void) THROWS( (UsrSystemException
) )
226 return m_seqExceptions
;
230 Sequence
< UString
> OPipeTest::getWarnings(void) THROWS( (UsrSystemException
) )
232 return m_seqWarnings
;
242 void OPipeTest::testSimple( const XInterfaceRef
&r
)
245 XInputStreamRef
input( r
, USR_QUERY
);
246 XOutputStreamRef
output( r
, USR_QUERY
);
248 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
249 ERROR_ASSERT( output
.is() , "queryInterface onXOutputStream failed" );
252 Sequence
<BYTE
> seqWrite
= createSeq( "Hallo, du Ei !" );
254 Sequence
<BYTE
> seqRead
;
255 for( int i
= 0 ; i
< 5000 ; i
++ ) {
256 output
->writeBytes( seqWrite
);
257 input
->readBytes( seqRead
, input
->available() );
259 ERROR_ASSERT( ! strcmp( (char *) seqWrite
.getArray() , (char * )seqRead
.getArray() ) ,
260 "error during read/write/skip" );
261 ERROR_ASSERT( 0 == input
->available() ,
262 "error during read/write/skip" );
264 // available shouldn't return a negative value
265 input
->skipBytes( seqWrite
.getLen() - 5 );
266 ERROR_ASSERT( 0 == input
->available() , "wrong available after skip" );
268 // 5 bytes should be available
269 output
->writeBytes( seqWrite
);
270 ERROR_ASSERT( 5 == input
->available() , "wrong available after skip/write " );
272 input
->readBytes( seqRead
, 5 );
273 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
274 (char*) &( seqWrite
.getArray()[seqWrite
.getLen()-5] ) ),
275 "write/read mismatich" );
279 output
->writeBytes( seqWrite
);
280 ERROR_ASSERT( seqWrite
.getLen() == input
->available(), "wrong available() after write" );
282 ERROR_ASSERT( 10 == input
->readSomeBytes( seqRead
, 10 ) , "maximal number of bytes ignored" );
283 ERROR_ASSERT( seqWrite
.getLen() -10 == input
->readSomeBytes( seqRead
, 100 ) ,
284 "something wrong with readSomeBytes" );
287 output
->closeOutput();
289 output
->writeBytes( Sequence
<BYTE
> (100) );
290 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
292 catch (IOException
& e
) {
293 e
; // just to suppress warning during compile
296 ERROR_ASSERT(! input
->readBytes( seqRead
, 1 ), "eof not found !" );
300 input
->readBytes( seqRead
, 1 );
301 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
303 catch( IOException
& e
) {
304 e
; // just to suppress warning during compile
309 void OPipeTest::testBufferResizing( const XInterfaceRef
&r
)
313 XInputStreamRef
input( r
, USR_QUERY
);
314 XOutputStreamRef
output( r
, USR_QUERY
);
316 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
317 ERROR_ASSERT( output
.is() , "queryInterface on XOutputStream failed" );
319 Sequence
<BYTE
> seqRead
;
321 // this is just to better check the
323 output
->writeBytes( Sequence
<BYTE
>(100) );
324 input
->readBytes( Sequence
<BYTE
>() , 100);
326 for( int i
= 0 ; i
< iMax
; i
++ ) {
327 output
->writeBytes( createIntSeq( i
) );
330 for( i
= 0 ; i
< iMax
; i
++ ) {
331 input
->readBytes( seqRead
, createIntSeq(i
).getLen() );
332 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
333 (char*) createIntSeq(i
).getArray() ) ,
334 "written/read mismatch\n" );
337 output
->closeOutput();
338 ERROR_ASSERT( ! input
->readBytes( seqRead
, 1 ) , "eof not reached !" );
344 void OPipeTest::testMultithreading( const XInterfaceRef
&r
)
350 XInputStreamRef
input( r
, USR_QUERY
);
351 XOutputStreamRef
output( r
, USR_QUERY
);
353 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
354 ERROR_ASSERT( output
.is() , "queryInterface on XOutputStream failed" );
356 Sequence
<BYTE
> seqRead
;
359 osl::Thread
*p
= new WriteToStreamThread( output
, iMax
);
361 ERROR_ASSERT( p
, "couldn't create thread for testing !\n" );
365 for(int i
= 0 ; TRUE
; i
++ ) {
366 if( 0 == input
->readBytes( seqRead
, createIntSeq(i
).getLen() ) ) {
371 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
372 (char*) createIntSeq(i
).getArray() ) ,
373 "written/read mismatch\n" );
376 ERROR_ASSERT( i
== iMax
, "less elements read than written !");
382 XInterfaceRef x = xSMgr->createInstance( strService );
384 XInputStreamRef input( x , USR_QUERY );
385 XOutputStreamRef output( x , USR_QUERY );
387 OSL_ASSERT( output.is() );
390 Sequence<BYTE> seqWrite( 500 );
391 output->writeBytes( seqWrite );
395 catch( IOException& e ) {
396 printf( "%s %s\n" , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() ,
397 UStringToString( e.Message , CHARSET_SYSTEM ).GetCharStr() );
406 * for external binding
410 XInterfaceRef
OPipeTest_CreateInstance( const XMultiServiceFactoryRef
& rSMgr
) THROWS((Exception
))
412 OPipeTest
*p
= new OPipeTest( rSMgr
);
413 XInterfaceRef xService
= *p
;
419 Sequence
<UString
> OPipeTest_getSupportedServiceNames(void) THROWS(())
421 Sequence
<UString
> aRet(1);
422 aRet
.getArray()[0] = OPipeTest_getImplementationName();
427 UString
OPipeTest_getServiceName() THROWS(())
432 UString
OPipeTest_getImplementationName() THROWS(())
434 return IMPLEMENTATION_NAME
;
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */