xref: /trunk/main/io/test/stm/pumptest.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 #include <stdio.h>
27 #include <osl/time.h>
28 
29 #include <osl/diagnose.h>
30 #include <com/sun/star/test/XSimpleTest.hpp>
31 
32 #include <com/sun/star/io/XActiveDataSource.hpp>
33 #include <com/sun/star/io/XActiveDataSink.hpp>
34 #include <com/sun/star/io/XActiveDataControl.hpp>
35 #include <com/sun/star/io/XConnectable.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/registry/XRegistryKey.hpp>
40 
41 #include <uno/dispatcher.h>
42 #include <uno/mapping.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 #include <cppuhelper/factory.hxx>
45 #include <osl/mutex.hxx>
46 #include <osl/thread.h>
47 #include <list>
48 
49 
50 
51 
52 using namespace ::rtl;
53 using namespace ::osl;
54 using namespace ::cppu;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::test;
59 
60 #include "testfactreg.hxx"
61 
mywait()62 static void mywait()
63 {
64     TimeValue a = { 0, 10000 };
65     osl_waitThread( &a );
66     osl_yieldThread();
67     osl_yieldThread();
68 }
69 
70 class OPumpTest : public WeakImplHelper1 < XSimpleTest >
71 {
72 public:
73     OPumpTest( const Reference< XMultiServiceFactory >  & rFactory );
74     ~OPumpTest();
75 
76 public: // implementation names
77     static Sequence< OUString >     getSupportedServiceNames_Static(void) throw();
78     static OUString                 getImplementationName_Static() throw();
79 
80 public:
81     virtual void SAL_CALL testInvariant(const OUString& TestName, const Reference < XInterface >& TestObject) ;
82 
83     virtual sal_Int32 SAL_CALL test(    const OUString& TestName,
84                                         const Reference < XInterface >& TestObject,
85                                         sal_Int32 hTestHandle);
86 
87     virtual sal_Bool SAL_CALL testPassed(void) ;
88     virtual Sequence< OUString > SAL_CALL getErrors(void) ;
89     virtual Sequence< Any > SAL_CALL getErrorExceptions(void);
90     virtual Sequence< OUString > SAL_CALL getWarnings(void);
91 
92 private:
93     void testSimple( const Reference < XInterface > & );
94     void testWrongUsage( const Reference < XInterface > & );
95     void testClose( const Reference< XInterface >& );
96     void testTerminate( const Reference< XInterface >& );
97     void testFunction( const Reference< XInterface >& );
98 private:
99     Sequence<Any>  m_seqExceptions;
100     Sequence<OUString> m_seqErrors;
101     Sequence<OUString> m_seqWarnings;
102     Reference< XMultiServiceFactory > m_rSmgr;
103 
104 };
105 
OPumpTest(const Reference<XMultiServiceFactory> & rFactory)106 OPumpTest::OPumpTest( const Reference< XMultiServiceFactory > &rFactory ) :
107     m_rSmgr( rFactory )
108 {
109 
110 }
111 
~OPumpTest()112 OPumpTest::~OPumpTest()
113 {
114 
115 }
116 
117 
118 
testInvariant(const OUString & TestName,const Reference<XInterface> & TestObject)119 void OPumpTest::testInvariant( const OUString& TestName, const Reference < XInterface >& TestObject )
120 {
121     Reference< XServiceInfo > info( TestObject, UNO_QUERY );
122     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
123     if( info.is() )
124     {
125         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
126         ERROR_ASSERT( ! info->supportsService(
127             OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb") ) ), "XServiceInfo test failed" );
128     }
129 
130     Reference < XActiveDataSource > xActiveDataSource( TestObject, UNO_QUERY );
131     Reference < XActiveDataSink > xActiveDataSink( TestObject, UNO_QUERY );
132     Reference < XActiveDataControl > xActiveDataControl( TestObject , UNO_QUERY );
133     Reference < XConnectable > xConnectable( TestObject , UNO_QUERY );
134 
135     ERROR_ASSERT( xActiveDataSource.is() && xActiveDataSink.is() && xActiveDataControl.is () &&
136                   xConnectable.is(), "specified interface not supported" );
137 }
138 
139 
test(const OUString & TestName,const Reference<XInterface> & TestObject,sal_Int32 hTestHandle)140 sal_Int32 OPumpTest::test(
141     const OUString& TestName,
142     const Reference < XInterface >& TestObject,
143     sal_Int32 hTestHandle)
144 {
145     if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pump") ) == TestName )  {
146         try
147         {
148             if( 0 == hTestHandle ) {
149                 testInvariant( TestName , TestObject );
150             }
151             else if ( 1 == hTestHandle )
152             {
153                 testWrongUsage( TestObject);
154             }
155             else if ( 2 == hTestHandle )
156             {
157                 testClose( TestObject);
158             }
159             else if ( 3 == hTestHandle )
160             {
161                 testTerminate( TestObject );
162             }
163             else if ( 4 == hTestHandle )
164             {
165                 testFunction( TestObject );
166             }
167         }
168         catch( Exception & e )
169         {
170             OString s = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
171             BUILD_ERROR( 0 , s.getStr() );
172         }
173         catch( ... )
174         {
175             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
176         }
177 
178         hTestHandle ++;
179 
180         if( 5 == hTestHandle )
181         {
182             // all tests finished.
183             hTestHandle = -1;
184         }
185     }
186     else {
187         throw IllegalArgumentException();
188     }
189     return hTestHandle;
190 }
191 
192 
193 
testPassed(void)194 sal_Bool OPumpTest::testPassed(void)
195 {
196     return m_seqErrors.getLength() == 0;
197 }
198 
199 
getErrors(void)200 Sequence< OUString > OPumpTest::getErrors(void)
201 {
202     return m_seqErrors;
203 }
204 
205 
getErrorExceptions(void)206 Sequence< Any > OPumpTest::getErrorExceptions(void)
207 {
208     return m_seqExceptions;
209 }
210 
211 
getWarnings(void)212 Sequence< OUString > OPumpTest::getWarnings(void)
213 {
214     return m_seqWarnings;
215 }
216 
217 
218 /***
219 * the test methods
220 *
221 ****/
222 
223 
testSimple(const Reference<XInterface> & r)224 void OPumpTest::testSimple( const Reference < XInterface > &r )
225 {
226     // jbu todo: add sensible test
227 
228 }
229 
230 class TestListener: public WeakImplHelper1< XStreamListener >
231 {
232 public:
233     sal_Bool m_bStarted;
234     sal_Bool m_bClosed;
235     sal_Bool m_bTerminated;
236     sal_Bool m_bError;
237     sal_Bool m_bDisposed;
TestListener()238     TestListener() : m_bStarted (sal_False),
239                      m_bClosed (sal_False),
240                      m_bTerminated ( sal_False ),
241                      m_bError( sal_False ),
242                      m_bDisposed( sal_False )
243     {}
244 
disposing(const EventObject & obj)245     virtual void SAL_CALL disposing( const EventObject &obj  )
246     {
247         m_bDisposed = sal_True;
248 //         printf( "disposing called\n");
249     }
250 
started()251     virtual void SAL_CALL started(  )
252     {
253         m_bStarted = sal_True;
254 //         printf( "started called\n");
255     }
closed()256     virtual void SAL_CALL closed(  )
257     {
258         m_bClosed = sal_True;
259 //         printf( "closed called\n");
260     }
terminated()261     virtual void SAL_CALL terminated(  )
262     {
263         m_bTerminated = sal_True;
264 //         printf( "terminated called\n");
265     }
error(const::com::sun::star::uno::Any & aException)266     virtual void SAL_CALL error( const ::com::sun::star::uno::Any& aException )
267     {
268         m_bError = sal_True;
269         Exception e;
270         aException >>= e;
271 //         printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
272     }
273 };
274 
275 class TestCase
276 {
277 public:
TestCase(const Reference<XMultiServiceFactory> & rSMgr,const Reference<XInterface> & r)278     TestCase( const Reference< XMultiServiceFactory > & rSMgr,
279               const Reference< XInterface > &r ) : m_rSmgr( rSMgr ), m_pTestListener( 0 )
280     {
281         m_rControl = Reference<XActiveDataControl>( r, UNO_QUERY );
282 
283         Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
284         Reference< XActiveDataSink > rSink( r , UNO_QUERY );
285 
286         m_rOutSource = Reference< XOutputStream > ( createPipe() );
287         rSink->setInputStream(Reference< XInputStream> (m_rOutSource,UNO_QUERY));
288 
289         Reference< XOutputStream > rOutSink( createPipe() );
290         m_rInSink = Reference< XInputStream > ( rOutSink, UNO_QUERY );
291         rSource->setOutputStream( rOutSink );
292 
293         m_pTestListener = new TestListener();
294         m_pTestListener->acquire();
295         m_rControl->addListener( m_pTestListener );
296     }
297 
~TestCase()298     ~TestCase()
299     {
300         if( m_pTestListener )
301             m_pTestListener->release();
302     }
303 
304     TestListener *m_pTestListener;
305     Reference< XActiveDataControl > m_rControl;
306     Reference< XOutputStream > m_rOutSource;
307     Reference< XInputStream > m_rInSink;
308     Reference< XMultiServiceFactory > m_rSmgr;
309 
310 private:
createPipe()311     Reference< XOutputStream > createPipe()
312     {
313         Reference< XOutputStream > rOut( m_rSmgr->createInstance(
314              OUString::createFromAscii( "com.sun.star.io.Pipe" )),UNO_QUERY);
315         return rOut;
316     }
317 };
318 
319 
320 
testClose(const Reference<XInterface> & r)321 void OPumpTest::testClose( const Reference< XInterface > &r )
322 {
323     TestCase t( m_rSmgr, r );
324 
325     ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
326     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "termination unexpected" );
327     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
328     ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
329 
330     t.m_rControl->start();
331     mywait();
332 
333     ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
334     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "termination unexpected" );
335     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
336     ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
337 
338     Reference< XStreamListener > rListener( new TestListener() );
339     t.m_rControl->addListener( rListener );
340     t.m_rControl->removeListener( rListener );
341 
342     t.m_rOutSource->closeOutput();
343     mywait();
344     ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
345     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminated already" );
346     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
347     ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
348 }
349 
testTerminate(const Reference<XInterface> & r)350 void OPumpTest::testTerminate( const Reference< XInterface > &r )
351 {
352     TestCase t( m_rSmgr, r );
353 
354     ERROR_ASSERT( ! t.m_pTestListener->m_bStarted , "started too early" );
355     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "termination unexpected" );
356     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
357     ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
358 
359     t.m_rControl->start();
360     mywait();
361 
362     ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
363     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "termination unexpected" );
364     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
365     ERROR_ASSERT( ! t.m_pTestListener->m_bClosed, "unexpected clase" );
366 
367     t.m_rControl->terminate();
368 
369     mywait();
370     ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
371     ERROR_ASSERT( t.m_pTestListener->m_bTerminated , "should be terminated already" );
372     // terminte leads to an error, that is no surprise, in fact
373     // one can't tell whether the error occurs because of the terminate
374     // call or for some other reason !
375 //     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
376     ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
377 }
378 
testFunction(const Reference<XInterface> & r)379 void OPumpTest::testFunction( const Reference< XInterface > &r )
380 {
381     TestCase t( m_rSmgr, r );
382 
383     t.m_rControl->start();
384 
385     t.m_rOutSource->writeBytes( Sequence< sal_Int8 > ( 5 ) );
386 
387     Sequence< sal_Int8 > dummy;
388     ERROR_ASSERT( 5 == t.m_rInSink->readBytes( dummy , 5 ), "couldn't read the expected number of bytes" );
389 
390     t.m_rOutSource->closeOutput();
391     mywait();
392 
393     ERROR_ASSERT( t.m_pTestListener->m_bStarted , "should have been started already" );
394     ERROR_ASSERT( ! t.m_pTestListener->m_bTerminated , "should be terminated already" );
395     ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
396     ERROR_ASSERT( t.m_pTestListener->m_bClosed, "should be closed already" );
397 }
398 
testWrongUsage(const Reference<XInterface> & r)399 void OPumpTest::testWrongUsage( const Reference< XInterface > &r )
400 {
401     Reference< XActiveDataSource > rSource ( r, UNO_QUERY );
402     Reference< XActiveDataSink > rSink( r , UNO_QUERY );
403     Reference< XActiveDataControl > rControl( r, UNO_QUERY );
404 
405     Reference< XInputStream > rIn( m_rSmgr->createInstance(
406         OUString::createFromAscii( "com.sun.star.io.DataInputStream" )),UNO_QUERY);
407     Reference< XOutputStream > rOut( m_rSmgr->createInstance(
408         OUString::createFromAscii( "com.sun.star.io.DataOutputStream" )),UNO_QUERY);
409 
410     rSink->setInputStream( rIn );
411     rSource->setOutputStream( rOut );
412 
413     rControl->start();
414 
415     mywait();
416 }
417 
OPumpTest_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)418 Reference< XInterface > SAL_CALL OPumpTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr )
419 {
420     return *new OPumpTest( rSMgr );
421 }
422 
OPumpTest_getSupportedServiceNames(void)423 Sequence<OUString> OPumpTest_getSupportedServiceNames(void) throw()
424 {
425     OUString s = OPumpTest_getServiceName();
426     Sequence< OUString > seq( &s , 1 );
427     return seq;
428 
429 }
OPumpTest_getServiceName()430 OUString     OPumpTest_getServiceName() throw()
431 {
432     return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.io.Pump" ) );
433 }
434 
OPumpTest_getImplementationName()435 OUString    OPumpTest_getImplementationName() throw()
436 {
437     return OUString( RTL_CONSTASCII_USTRINGPARAM( "test.com.sun.star.comp.io.Pump") );
438 }
439