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 .
21 #include <com/sun/star/test/XSimpleTest.hpp>
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <com/sun/star/io/XOutputStream.hpp>
24 #include <com/sun/star/io/XConnectable.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/implbase.hxx>
33 #include <osl/conditn.hxx>
34 #include <osl/thread.hxx>
38 using namespace ::osl
;
39 using namespace ::cppu
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::io
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::test
;
46 #include "testfactreg.hxx"
47 #define IMPLEMENTATION_NAME "test.com.sun.star.comp.extensions.stm.Pipe"
48 #define SERVICE_NAME "test.com.sun.star.io.Pipe"
51 class WriteToStreamThread
:
57 WriteToStreamThread( Reference
< XOutputStream
> xOutput
, int iMax
)
63 virtual ~WriteToStreamThread() {}
68 /// Working method which should be overridden.
69 virtual void SAL_CALL
run() {
70 for( int i
= 0 ; i
< m_iMax
; i
++ ) {
71 m_output
->writeBytes( createIntSeq(i
) );
73 m_output
->closeOutput();
76 /** Called when run() is done.
77 * You might want to override it to do some cleanup.
79 virtual void SAL_CALL
onTerminated()
87 Reference
< XOutputStream
> m_output
;
92 class OPipeTest
: public WeakImplHelper
< XSimpleTest
>
95 explicit OPipeTest( const Reference
< XMultiServiceFactory
> & rFactory
);
98 public: // implementation names
99 static Sequence
< OUString
> getSupportedServiceNames_Static() throw();
100 static OUString
getImplementationName_Static() throw();
103 virtual void SAL_CALL
testInvariant(const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
104 throw ( IllegalArgumentException
, RuntimeException
) ;
106 virtual sal_Int32 SAL_CALL
test( const OUString
& TestName
,
107 const Reference
< XInterface
>& TestObject
,
108 sal_Int32 hTestHandle
)
109 throw ( IllegalArgumentException
,
112 virtual sal_Bool SAL_CALL
testPassed() throw ( RuntimeException
) ;
113 virtual Sequence
< OUString
> SAL_CALL
getErrors() throw (RuntimeException
) ;
114 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions() throw (RuntimeException
);
115 virtual Sequence
< OUString
> SAL_CALL
getWarnings() throw (RuntimeException
);
118 void testSimple( const Reference
< XInterface
> & );
119 void testBufferResizing( const Reference
< XInterface
> & );
120 void testMultithreading( const Reference
< XInterface
> & );
123 Sequence
<Any
> m_seqExceptions
;
124 Sequence
<OUString
> m_seqErrors
;
125 Sequence
<OUString
> m_seqWarnings
;
130 OPipeTest::OPipeTest( const Reference
< XMultiServiceFactory
> &rFactory
)
135 OPipeTest::~OPipeTest()
141 void OPipeTest::testInvariant( const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
142 throw ( IllegalArgumentException
,
145 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
146 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
149 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
150 ERROR_ASSERT( ! info
->supportsService(
151 OUString("bla bluzb") ), "XServiceInfo test failed" );
157 sal_Int32
OPipeTest::test(
158 const OUString
& TestName
,
159 const Reference
< XInterface
>& TestObject
,
160 sal_Int32 hTestHandle
)
161 throw ( IllegalArgumentException
, RuntimeException
)
163 if( OUString("com.sun.star.io.Pipe") == TestName
) {
166 if( 0 == hTestHandle
) {
167 testInvariant( TestName
, TestObject
);
169 else if( 1 == hTestHandle
) {
170 testSimple( TestObject
);
172 else if( 2 == hTestHandle
) {
173 testBufferResizing( TestObject
);
175 else if( 3 == hTestHandle
) {
176 testMultithreading( TestObject
);
179 catch( const Exception
& e
)
181 OString s
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
182 BUILD_ERROR( 0 , s
.getStr() );
186 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
191 if( 4 == hTestHandle
)
193 // all tests finished.
198 throw IllegalArgumentException();
204 sal_Bool
OPipeTest::testPassed() throw (RuntimeException
)
206 return m_seqErrors
.getLength() == 0;
210 Sequence
< OUString
> OPipeTest::getErrors() throw (RuntimeException
)
216 Sequence
< Any
> OPipeTest::getErrorExceptions() throw (RuntimeException
)
218 return m_seqExceptions
;
222 Sequence
< OUString
> OPipeTest::getWarnings() throw (RuntimeException
)
224 return m_seqWarnings
;
234 void OPipeTest::testSimple( const Reference
< XInterface
> &r
)
237 Reference
< XInputStream
> input( r
, UNO_QUERY
);
238 Reference
< XOutputStream
> output( r
, UNO_QUERY
);
240 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
241 ERROR_ASSERT( output
.is() , "queryInterface onXOutputStream failed" );
244 Sequence
<sal_Int8
> seqWrite
= createSeq( "Hallo, du Ei !" );
246 Sequence
<sal_Int8
> seqRead
;
247 for( int i
= 0 ; i
< 5000 ; i
++ ) {
248 output
->writeBytes( seqWrite
);
249 input
->readBytes( seqRead
, input
->available() );
251 ERROR_ASSERT( ! strcmp( (char *) seqWrite
.getArray() , (char * )seqRead
.getArray() ) ,
252 "error during read/write/skip" );
253 ERROR_ASSERT( 0 == input
->available() ,
254 "error during read/write/skip" );
256 // available shouldn't return a negative value
257 input
->skipBytes( seqWrite
.getLength() - 5 );
258 ERROR_ASSERT( 0 == input
->available() , "wrong available after skip" );
260 // 5 bytes should be available
261 output
->writeBytes( seqWrite
);
262 ERROR_ASSERT( 5 == input
->available() , "wrong available after skip/write " );
264 input
->readBytes( seqRead
, 5 );
265 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
266 (char*) &( seqWrite
.getArray()[seqWrite
.getLength()-5] ) ),
267 "write/read mismatch" );
271 output
->writeBytes( seqWrite
);
272 ERROR_ASSERT( seqWrite
.getLength() == input
->available(), "wrong available() after write" );
274 ERROR_ASSERT( 10 == input
->readSomeBytes( seqRead
, 10 ) , "maximal number of bytes ignored" );
275 ERROR_ASSERT( seqWrite
.getLength() -10 == input
->readSomeBytes( seqRead
, 100 ) ,
276 "something wrong with readSomeBytes" );
279 output
->closeOutput();
281 output
->writeBytes( Sequence
<sal_Int8
> (100) );
282 ERROR_ASSERT( 0 , "writing on a closed stream does not cause an exception" );
284 catch (IOException
& )
288 ERROR_ASSERT(! input
->readBytes( seqRead
, 1 ), "eof not found !" );
293 input
->readBytes( seqRead
, 1 );
294 ERROR_ASSERT( 0 , "reading from a closed stream does not cause an exception" );
296 catch( IOException
& ) {
302 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
304 catch( IOException
& )
310 input
->skipBytes(42 );
311 ERROR_ASSERT( 0 , "calling available from a closed stream should thrown an io exception" );
313 catch( IOException
& )
319 void OPipeTest::testBufferResizing( const Reference
< XInterface
> &r
)
323 Reference
< XInputStream
> input( r
, UNO_QUERY
);
324 Reference
< XOutputStream
> output( r
, UNO_QUERY
);
326 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
327 ERROR_ASSERT( output
.is() , "queryInterface on XOutputStream failed" );
329 Sequence
<sal_Int8
> seqRead
;
331 // this is just to better check the
333 output
->writeBytes( Sequence
<sal_Int8
>(100) );
334 Sequence
< sal_Int8
> dummy
;
335 input
->readBytes( dummy
, 100);
337 for( i
= 0 ; i
< iMax
; i
++ ) {
338 output
->writeBytes( createIntSeq( i
) );
341 for( i
= 0 ; i
< iMax
; i
++ ) {
342 input
->readBytes( seqRead
, createIntSeq(i
).getLength() );
343 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
344 (char*) createIntSeq(i
).getArray() ) ,
345 "written/read mismatch\n" );
348 output
->closeOutput();
349 ERROR_ASSERT( ! input
->readBytes( seqRead
, 1 ) , "eof not reached !" );
354 void OPipeTest::testMultithreading( const Reference
< XInterface
> &r
)
360 Reference
< XInputStream
> input( r
, UNO_QUERY
);
361 Reference
< XOutputStream
> output( r
, UNO_QUERY
);
363 ERROR_ASSERT( input
.is() , "queryInterface on XInputStream failed" );
364 ERROR_ASSERT( output
.is() , "queryInterface on XOutputStream failed" );
366 Sequence
<sal_Int8
> seqRead
;
369 Thread
*p
= new WriteToStreamThread( output
, iMax
);
371 ERROR_ASSERT( p
, "couldn't create thread for testing !\n" );
375 for( i
= 0 ; sal_True
; i
++ ) {
376 if( 0 == input
->readBytes( seqRead
, createIntSeq(i
).getLength() ) ) {
381 ERROR_ASSERT( ! strcmp( (char*) seqRead
.getArray() ,
382 (char*) createIntSeq(i
).getArray() ) ,
383 "written/read mismatch\n" );
386 ERROR_ASSERT( i
== iMax
, "less elements read than written !");
392 * for external binding
396 Reference
< XInterface
> SAL_CALL
OPipeTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw (Exception
)
398 OPipeTest
*p
= new OPipeTest( rSMgr
);
399 Reference
< XInterface
> x ( (static_cast< OWeakObject
* >(p
)) );
404 Sequence
<OUString
> OPipeTest_getSupportedServiceNames() throw()
406 Sequence
<OUString
> aRet
{ OPipeTest_getServiceName() };
411 OUString
OPipeTest_getServiceName() throw()
413 return OUString( SERVICE_NAME
);
416 OUString
OPipeTest_getImplementationName() throw()
418 return OUString( IMPLEMENTATION_NAME
);
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */