xref: /trunk/main/io/test/stm/marktest.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*3716f815SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*3716f815SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3716f815SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3716f815SAndrew Rist  * distributed with this work for additional information
6*3716f815SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3716f815SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3716f815SAndrew Rist  * "License"); you may not use this file except in compliance
9*3716f815SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*3716f815SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*3716f815SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3716f815SAndrew Rist  * software distributed under the License is distributed on an
15*3716f815SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3716f815SAndrew Rist  * KIND, either express or implied.  See the License for the
17*3716f815SAndrew Rist  * specific language governing permissions and limitations
18*3716f815SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*3716f815SAndrew Rist  *************************************************************/
21*3716f815SAndrew Rist 
22*3716f815SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_io.hxx"
26cdf0e10cSrcweir #include <com/sun/star/test/XSimpleTest.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
28cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
29cdf0e10cSrcweir #include <com/sun/star/io/XMarkableStream.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XConnectable.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <osl/conditn.hxx>
40cdf0e10cSrcweir #include <osl/mutex.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::rtl;
43cdf0e10cSrcweir using namespace ::osl;
44cdf0e10cSrcweir using namespace ::cppu;
45cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46cdf0e10cSrcweir using namespace ::com::sun::star::io;
47cdf0e10cSrcweir using namespace ::com::sun::star::lang;
48cdf0e10cSrcweir using namespace ::com::sun::star::test;
49cdf0e10cSrcweir // streams
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include "testfactreg.hxx"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir class OMarkableOutputStreamTest : public WeakImplHelper1< XSimpleTest >
55cdf0e10cSrcweir {
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir     OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
58cdf0e10cSrcweir     ~OMarkableOutputStreamTest();
59cdf0e10cSrcweir 
60cdf0e10cSrcweir public: // implementation names
61cdf0e10cSrcweir     static Sequence< OUString >     getSupportedServiceNames_Static(void) throw ();
62cdf0e10cSrcweir     static OUString                 getImplementationName_Static() throw ();
63cdf0e10cSrcweir 
64cdf0e10cSrcweir public:
65cdf0e10cSrcweir     virtual void SAL_CALL testInvariant(
66cdf0e10cSrcweir         const OUString& TestName,
67cdf0e10cSrcweir         const Reference < XInterface >& TestObject)
68cdf0e10cSrcweir         throw ( IllegalArgumentException,
69cdf0e10cSrcweir                 RuntimeException) ;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL  test(   const OUString& TestName,
72cdf0e10cSrcweir                         const Reference < XInterface >& TestObject,
73cdf0e10cSrcweir                         sal_Int32 hTestHandle)
74cdf0e10cSrcweir         throw ( IllegalArgumentException, RuntimeException);
75cdf0e10cSrcweir     virtual sal_Bool SAL_CALL testPassed(void)
76cdf0e10cSrcweir         throw ( RuntimeException);
77cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getErrors(void)
78cdf0e10cSrcweir         throw (RuntimeException);
79cdf0e10cSrcweir     virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
80cdf0e10cSrcweir         throw (RuntimeException);
81cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getWarnings(void)
82cdf0e10cSrcweir         throw (RuntimeException);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir private:
85cdf0e10cSrcweir     void testSimple( const Reference< XOutputStream > &r, const Reference < XInputStream > &rInput );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir private:
88cdf0e10cSrcweir     Sequence<Any>  m_seqExceptions;
89cdf0e10cSrcweir     Sequence<OUString> m_seqErrors;
90cdf0e10cSrcweir     Sequence<OUString> m_seqWarnings;
91cdf0e10cSrcweir     Reference< XMultiServiceFactory > m_rFactory;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir };
94cdf0e10cSrcweir 
OMarkableOutputStreamTest(const Reference<XMultiServiceFactory> & rFactory)95cdf0e10cSrcweir OMarkableOutputStreamTest::OMarkableOutputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
96cdf0e10cSrcweir         : m_rFactory( rFactory )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
~OMarkableOutputStreamTest()101cdf0e10cSrcweir OMarkableOutputStreamTest::~OMarkableOutputStreamTest()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
testInvariant(const OUString & TestName,const Reference<XInterface> & TestObject)109cdf0e10cSrcweir void OMarkableOutputStreamTest::testInvariant( const OUString& TestName,
110cdf0e10cSrcweir                                                const Reference < XInterface >& TestObject )
111cdf0e10cSrcweir     throw ( IllegalArgumentException, RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     Reference< XServiceInfo > info( TestObject, UNO_QUERY );
114cdf0e10cSrcweir     ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
115cdf0e10cSrcweir     if( info.is() )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
118cdf0e10cSrcweir         ERROR_ASSERT( ! info->supportsService(
119cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("bla bluzb")) ) , "XServiceInfo test failed" );
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 
test(const OUString & TestName,const Reference<XInterface> & TestObject,sal_Int32 hTestHandle)124cdf0e10cSrcweir sal_Int32 OMarkableOutputStreamTest::test(
125cdf0e10cSrcweir     const OUString& TestName,
126cdf0e10cSrcweir     const Reference < XInterface >& TestObject,
127cdf0e10cSrcweir     sal_Int32 hTestHandle)
128cdf0e10cSrcweir     throw ( IllegalArgumentException, RuntimeException)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableOutputStream") )
131cdf0e10cSrcweir                   == TestName  )  {
132cdf0e10cSrcweir         try
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             if( 0 == hTestHandle )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 testInvariant( TestName , TestObject );
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir             else
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 Reference < XInterface > x = m_rFactory->createInstance( OUString::createFromAscii("com.sun.star.io.Pipe"));
141cdf0e10cSrcweir                 Reference< XOutputStream >  rPipeOutput( x , UNO_QUERY );
142cdf0e10cSrcweir                 Reference < XInputStream >  rPipeInput( x , UNO_QUERY );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir                 Reference<  XActiveDataSource >  source( TestObject , UNO_QUERY );
145cdf0e10cSrcweir                 source->setOutputStream( rPipeOutput );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                 Reference< XOutputStream > rOutput( TestObject , UNO_QUERY );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir                 OSL_ASSERT( rPipeInput.is() );
150cdf0e10cSrcweir                 OSL_ASSERT( rOutput.is() );
151cdf0e10cSrcweir                 if( 1 == hTestHandle ) {
152cdf0e10cSrcweir                     // checks usual streaming
153cdf0e10cSrcweir                     testSimple( rOutput , rPipeInput );
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         catch( Exception &e )
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
161cdf0e10cSrcweir             BUILD_ERROR( 0 , o.getStr() );
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         catch( ... )
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         hTestHandle ++;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         if( 2 == hTestHandle )
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             // all tests finished.
173cdf0e10cSrcweir             hTestHandle = -1;
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir     else {
177cdf0e10cSrcweir         throw IllegalArgumentException();
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir     return hTestHandle;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
testPassed(void)184cdf0e10cSrcweir sal_Bool OMarkableOutputStreamTest::testPassed(void)                        throw (RuntimeException)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     return m_seqErrors.getLength() == 0;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
getErrors(void)190cdf0e10cSrcweir Sequence< OUString > OMarkableOutputStreamTest::getErrors(void)         throw (RuntimeException)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     return m_seqErrors;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
getErrorExceptions(void)196cdf0e10cSrcweir Sequence< Any > OMarkableOutputStreamTest::getErrorExceptions(void) throw (RuntimeException)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir     return m_seqExceptions;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
getWarnings(void)202cdf0e10cSrcweir Sequence< OUString > OMarkableOutputStreamTest::getWarnings(void)       throw (RuntimeException)
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     return m_seqWarnings;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
testSimple(const Reference<XOutputStream> & rOutput,const Reference<XInputStream> & rInput)208cdf0e10cSrcweir void OMarkableOutputStreamTest::testSimple(     const Reference< XOutputStream > &rOutput ,
209cdf0e10cSrcweir                                                 const Reference< XInputStream > &rInput )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     Reference < XMarkableStream > rMarkable( rOutput , UNO_QUERY );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     ERROR_ASSERT( rMarkable.is() , "no MarkableStream implemented" );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     // first check normal input/output facility
216cdf0e10cSrcweir     char pcStr[] = "Live long and prosper !";
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     Sequence<sal_Int8> seqWrite( strlen( pcStr )+1 );
219cdf0e10cSrcweir     memcpy( seqWrite.getArray() , pcStr , seqWrite.getLength() );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     Sequence<sal_Int8> seqRead( seqWrite.getLength() );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     int nMax = 10,i;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     for( i = 0 ; i < nMax ; i ++ ) {
226cdf0e10cSrcweir         rOutput->writeBytes( seqWrite );
227cdf0e10cSrcweir         rInput->readBytes( seqRead , rInput->available() );
228cdf0e10cSrcweir         ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
229cdf0e10cSrcweir                       "error during read/write/skip" );
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     // Check buffer resizing
233cdf0e10cSrcweir     nMax = 3000;
234cdf0e10cSrcweir     for( i = 0 ; i < nMax ; i ++ ) {
235cdf0e10cSrcweir         rOutput->writeBytes( seqWrite );
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     for( i = 0 ; i < nMax ; i ++ ) {
239cdf0e10cSrcweir         rInput->readBytes( seqRead , seqWrite.getLength() );
240cdf0e10cSrcweir         ERROR_ASSERT( ! strcmp( (char *) seqWrite.getArray() , (char * )seqRead.getArray() ) ,
241cdf0e10cSrcweir                       "error during read/write" );
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     // Check creating marks !
245cdf0e10cSrcweir     sal_Int32 nMark = rMarkable->createMark();
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     for( i = 0 ; i < nMax ; i ++ ) {
248cdf0e10cSrcweir         rOutput->writeBytes( seqWrite );
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     ERROR_ASSERT( 0 == rInput->available() , "bytes available though mark is holded" );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     ERROR_ASSERT( nMax*seqWrite.getLength() == rMarkable->offsetToMark( nMark ) ,
254cdf0e10cSrcweir                                              "offsetToMark failure" );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     rMarkable->deleteMark( nMark );
257cdf0e10cSrcweir     ERROR_ASSERT( nMax*seqWrite.getLength() == rInput->available(),"bytes are not available though mark has been deleted" );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     rInput->skipBytes( nMax*seqWrite.getLength() );
260cdf0e10cSrcweir     ERROR_ASSERT( 0 == rInput->available(), "skip bytes failure" );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     try
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         rMarkable->jumpToMark( nMark );
265cdf0e10cSrcweir         ERROR_ASSERT( 0 , "jump to non existing mark possible !" );
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir     catch ( IllegalArgumentException & )
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir         // ok, exception was thrown
270cdf0e10cSrcweir     }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     // test putting marks not at the end of the stream!
273cdf0e10cSrcweir     ERROR_ASSERT( 0 == rInput->available(), "stream isn't clean" );
274cdf0e10cSrcweir     {
275cdf0e10cSrcweir         Sequence< sal_Int8 > aByte(256);
276cdf0e10cSrcweir 
277cdf0e10cSrcweir         for( i = 0 ; i < 256 ; i ++ )
278cdf0e10cSrcweir         {
279cdf0e10cSrcweir             aByte.getArray()[i] = i;
280cdf0e10cSrcweir         }
281cdf0e10cSrcweir         sal_Int32 nMark1 = rMarkable->createMark();
282cdf0e10cSrcweir 
283cdf0e10cSrcweir         rOutput->writeBytes( aByte );
284cdf0e10cSrcweir         rMarkable->jumpToMark( nMark1 );
285cdf0e10cSrcweir         aByte.realloc( 10 );
286cdf0e10cSrcweir         rOutput->writeBytes( aByte );
287cdf0e10cSrcweir 
288cdf0e10cSrcweir         sal_Int32 nMark2 = rMarkable->createMark( );
289cdf0e10cSrcweir 
290cdf0e10cSrcweir         for( i = 0 ; i < 10 ; i ++ )
291cdf0e10cSrcweir         {
292cdf0e10cSrcweir             aByte.getArray()[i] = i+10;
293cdf0e10cSrcweir         }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir         rOutput->writeBytes( aByte );
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         // allow the bytes to be written !
298cdf0e10cSrcweir         rMarkable->jumpToFurthest();
299cdf0e10cSrcweir         rMarkable->deleteMark( nMark1 );
300cdf0e10cSrcweir         rMarkable->deleteMark( nMark2 );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir         ERROR_ASSERT( 256 == rInput->available(), "in between mark failure" );
303cdf0e10cSrcweir         rInput->readBytes( aByte ,256);
304cdf0e10cSrcweir         for( i = 0 ; i < 256 ; i ++ )
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             ERROR_ASSERT( i == ((sal_uInt8*)(aByte.getArray()))[i] , "in between mark failure" );
307cdf0e10cSrcweir         }
308cdf0e10cSrcweir     }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     {
311cdf0e10cSrcweir         // now a more extensive mark test !
312cdf0e10cSrcweir         Sequence<sal_Int8> as[4];
313cdf0e10cSrcweir         sal_Int32 an[4];
314cdf0e10cSrcweir 
315cdf0e10cSrcweir         for( i = 0 ; i < 4 ; i ++ ) {
316cdf0e10cSrcweir             as[i].realloc(1);
317cdf0e10cSrcweir             as[i].getArray()[0] = i;
318cdf0e10cSrcweir             an[i] = rMarkable->createMark();
319cdf0e10cSrcweir             rOutput->writeBytes( as[i] );
320cdf0e10cSrcweir         }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         // check offset to mark
323cdf0e10cSrcweir         for( i = 0 ; i < 4 ; i ++ ) {
324cdf0e10cSrcweir             ERROR_ASSERT( rMarkable->offsetToMark( an[i] ) == 4-i , "offsetToMark failure" );
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         rMarkable->jumpToMark( an[1] );
328cdf0e10cSrcweir         ERROR_ASSERT( rMarkable->offsetToMark( an[3] ) == -2 , "offsetToMark failure" );
329cdf0e10cSrcweir 
330cdf0e10cSrcweir         rMarkable->jumpToFurthest( );
331cdf0e10cSrcweir         ERROR_ASSERT( rMarkable->offsetToMark( an[0] ) == 4 , "offsetToMark failure" );
332cdf0e10cSrcweir 
333cdf0e10cSrcweir         // now do a rewrite !
334cdf0e10cSrcweir         for( i = 0 ; i < 4 ; i ++ ) {
335cdf0e10cSrcweir             rMarkable->jumpToMark( an[3-i] );
336cdf0e10cSrcweir             rOutput->writeBytes( as[i] );
337cdf0e10cSrcweir         }
338cdf0e10cSrcweir         // NOTE : CursorPos 1
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         // now delete the marks !
341cdf0e10cSrcweir         for( i = 0 ; i < 4 ; i ++ ) {
342cdf0e10cSrcweir             rMarkable->deleteMark( an[i] );
343cdf0e10cSrcweir         }
344cdf0e10cSrcweir         ERROR_ASSERT( rInput->available() == 1 , "wrong number of bytes flushed" );
345cdf0e10cSrcweir 
346cdf0e10cSrcweir         rMarkable->jumpToFurthest();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         ERROR_ASSERT( rInput->available() == 4 , "wrong number of bytes flushed" );
349cdf0e10cSrcweir 
350cdf0e10cSrcweir         rInput->readBytes( seqRead , 4 );
351cdf0e10cSrcweir 
352cdf0e10cSrcweir         ERROR_ASSERT( 3 == seqRead.getArray()[0] , "rewrite didn't work" );
353cdf0e10cSrcweir         ERROR_ASSERT( 2 == seqRead.getArray()[1] , "rewrite didn't work" );
354cdf0e10cSrcweir         ERROR_ASSERT( 1 == seqRead.getArray()[2] , "rewrite didn't work" );
355cdf0e10cSrcweir         ERROR_ASSERT( 0 == seqRead.getArray()[3] , "rewrite didn't work" );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir         rOutput->closeOutput();
358cdf0e10cSrcweir         rInput->closeInput();
359cdf0e10cSrcweir     }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir }
362cdf0e10cSrcweir 
363cdf0e10cSrcweir /***
364cdf0e10cSrcweir * the test methods
365cdf0e10cSrcweir *
366cdf0e10cSrcweir ****/
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 
372cdf0e10cSrcweir /**
373cdf0e10cSrcweir * for external binding
374cdf0e10cSrcweir *
375cdf0e10cSrcweir *
376cdf0e10cSrcweir **/
OMarkableOutputStreamTest_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)377cdf0e10cSrcweir Reference < XInterface > SAL_CALL OMarkableOutputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
378cdf0e10cSrcweir {
379cdf0e10cSrcweir     OMarkableOutputStreamTest *p = new OMarkableOutputStreamTest( rSMgr );
380cdf0e10cSrcweir     return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
381cdf0e10cSrcweir }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 
384cdf0e10cSrcweir 
OMarkableOutputStreamTest_getSupportedServiceNames(void)385cdf0e10cSrcweir Sequence<OUString> OMarkableOutputStreamTest_getSupportedServiceNames(void) throw ()
386cdf0e10cSrcweir {
387cdf0e10cSrcweir     Sequence<OUString> aRet(1);
388cdf0e10cSrcweir     aRet.getArray()[0] = OMarkableOutputStreamTest_getImplementationName();
389cdf0e10cSrcweir 
390cdf0e10cSrcweir     return aRet;
391cdf0e10cSrcweir }
392cdf0e10cSrcweir 
OMarkableOutputStreamTest_getServiceName()393cdf0e10cSrcweir OUString     OMarkableOutputStreamTest_getServiceName() throw ()
394cdf0e10cSrcweir {
395cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.MarkableOutputStream"));
396cdf0e10cSrcweir }
397cdf0e10cSrcweir 
OMarkableOutputStreamTest_getImplementationName()398cdf0e10cSrcweir OUString    OMarkableOutputStreamTest_getImplementationName() throw ()
399cdf0e10cSrcweir {
400cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.starextensions.stm.MarkableOutputStream"));
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 
409cdf0e10cSrcweir //-----------------------------------------------------
410cdf0e10cSrcweir // Input stream
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 
413cdf0e10cSrcweir class OMarkableInputStreamTest : public WeakImplHelper1< XSimpleTest >
414cdf0e10cSrcweir {
415cdf0e10cSrcweir public:
416cdf0e10cSrcweir     OMarkableInputStreamTest( const Reference< XMultiServiceFactory > & rFactory );
417cdf0e10cSrcweir     ~OMarkableInputStreamTest();
418cdf0e10cSrcweir 
419cdf0e10cSrcweir public: // implementation names
420cdf0e10cSrcweir     static Sequence< OUString >     getSupportedServiceNames_Static(void) throw () ;
421cdf0e10cSrcweir     static OUString                 getImplementationName_Static() throw () ;
422cdf0e10cSrcweir 
423cdf0e10cSrcweir public:
424cdf0e10cSrcweir     virtual void SAL_CALL testInvariant(
425cdf0e10cSrcweir         const OUString& TestName,
426cdf0e10cSrcweir         const Reference < XInterface >& TestObject)
427cdf0e10cSrcweir         throw ( IllegalArgumentException, RuntimeException) ;
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL test(
430cdf0e10cSrcweir         const OUString& TestName,
431cdf0e10cSrcweir         const Reference < XInterface >& TestObject,
432cdf0e10cSrcweir         sal_Int32 hTestHandle)
433cdf0e10cSrcweir         throw ( IllegalArgumentException,
434cdf0e10cSrcweir                 RuntimeException) ;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir     virtual sal_Bool SAL_CALL testPassed(void)
437cdf0e10cSrcweir         throw ( RuntimeException);
438cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getErrors(void)
439cdf0e10cSrcweir         throw (RuntimeException);
440cdf0e10cSrcweir     virtual Sequence< Any > SAL_CALL getErrorExceptions(void)
441cdf0e10cSrcweir         throw (RuntimeException);
442cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getWarnings(void)
443cdf0e10cSrcweir         throw (RuntimeException);
444cdf0e10cSrcweir 
445cdf0e10cSrcweir private:
446cdf0e10cSrcweir     void testSimple( const Reference< XOutputStream > &r,
447cdf0e10cSrcweir                      const Reference < XInputStream > &rInput );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir private:
450cdf0e10cSrcweir     Sequence<Any>  m_seqExceptions;
451cdf0e10cSrcweir     Sequence<OUString> m_seqErrors;
452cdf0e10cSrcweir     Sequence<OUString> m_seqWarnings;
453cdf0e10cSrcweir     Reference< XMultiServiceFactory > m_rFactory;
454cdf0e10cSrcweir 
455cdf0e10cSrcweir };
456cdf0e10cSrcweir 
OMarkableInputStreamTest(const Reference<XMultiServiceFactory> & rFactory)457cdf0e10cSrcweir OMarkableInputStreamTest::OMarkableInputStreamTest( const Reference< XMultiServiceFactory > &rFactory )
458cdf0e10cSrcweir         : m_rFactory( rFactory )
459cdf0e10cSrcweir {
460cdf0e10cSrcweir 
461cdf0e10cSrcweir }
462cdf0e10cSrcweir 
~OMarkableInputStreamTest()463cdf0e10cSrcweir OMarkableInputStreamTest::~OMarkableInputStreamTest()
464cdf0e10cSrcweir {
465cdf0e10cSrcweir 
466cdf0e10cSrcweir }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 
testInvariant(const OUString & TestName,const Reference<XInterface> & TestObject)470cdf0e10cSrcweir void OMarkableInputStreamTest::testInvariant(
471cdf0e10cSrcweir     const OUString& TestName, const Reference < XInterface >& TestObject )
472cdf0e10cSrcweir     throw ( IllegalArgumentException, RuntimeException)
473cdf0e10cSrcweir {
474cdf0e10cSrcweir     if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream"))
475cdf0e10cSrcweir         == TestName )  {
476cdf0e10cSrcweir         Reference <XServiceInfo >  info( TestObject, UNO_QUERY );
477cdf0e10cSrcweir         ERROR_ASSERT( info.is() , "XServiceInfo not supported !" );
478cdf0e10cSrcweir         if( info.is() )
479cdf0e10cSrcweir         {
480cdf0e10cSrcweir             ERROR_ASSERT( info->supportsService( TestName ), "XServiceInfo test failed" );
481cdf0e10cSrcweir             ERROR_ASSERT(
482cdf0e10cSrcweir                 ! info->supportsService(
483cdf0e10cSrcweir                     OUString(RTL_CONSTASCII_USTRINGPARAM("bla bluzb")) ) ,
484cdf0e10cSrcweir                 "XServiceInfo test failed" );
485cdf0e10cSrcweir         }
486cdf0e10cSrcweir     }
487cdf0e10cSrcweir     else
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         throw IllegalArgumentException();
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 
test(const OUString & TestName,const Reference<XInterface> & TestObject,sal_Int32 hTestHandle)494cdf0e10cSrcweir sal_Int32 OMarkableInputStreamTest::test(
495cdf0e10cSrcweir     const OUString& TestName,
496cdf0e10cSrcweir     const Reference < XInterface >& TestObject,
497cdf0e10cSrcweir     sal_Int32 hTestHandle)  throw ( IllegalArgumentException, RuntimeException)
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.MarkableInputStream")) == TestName )
500cdf0e10cSrcweir     {
501cdf0e10cSrcweir         try
502cdf0e10cSrcweir         {
503cdf0e10cSrcweir             if( 0 == hTestHandle ) {
504cdf0e10cSrcweir                 testInvariant( TestName , TestObject );
505cdf0e10cSrcweir             }
506cdf0e10cSrcweir             else  {
507cdf0e10cSrcweir                 Reference < XInterface > x = m_rFactory->createInstance( OUString::createFromAscii("com.sun.star.io.Pipe"));
508cdf0e10cSrcweir                 Reference< XOutputStream >  rPipeOutput( x , UNO_QUERY );
509cdf0e10cSrcweir                 Reference < XInputStream >  rPipeInput( x , UNO_QUERY );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir                 Reference < XActiveDataSink >  sink( TestObject , UNO_QUERY );
512cdf0e10cSrcweir                 sink->setInputStream( rPipeInput );
513cdf0e10cSrcweir 
514cdf0e10cSrcweir                 Reference < XInputStream > rInput( TestObject , UNO_QUERY );
515cdf0e10cSrcweir 
516cdf0e10cSrcweir                 OSL_ASSERT( rPipeOutput.is() );
517cdf0e10cSrcweir                 OSL_ASSERT( rInput.is() );
518cdf0e10cSrcweir                 if( 1 == hTestHandle ) {
519cdf0e10cSrcweir                     // checks usual streaming
520cdf0e10cSrcweir                     testSimple( rPipeOutput , rInput );
521cdf0e10cSrcweir                 }
522cdf0e10cSrcweir             }
523cdf0e10cSrcweir 
524cdf0e10cSrcweir         }
525cdf0e10cSrcweir         catch( Exception & e )
526cdf0e10cSrcweir         {
527cdf0e10cSrcweir             OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
528cdf0e10cSrcweir             BUILD_ERROR( 0 , o.getStr() );
529cdf0e10cSrcweir         }
530cdf0e10cSrcweir         catch( ... )
531cdf0e10cSrcweir         {
532cdf0e10cSrcweir             BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
533cdf0e10cSrcweir         }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir         hTestHandle ++;
536cdf0e10cSrcweir 
537cdf0e10cSrcweir         if( 2 == hTestHandle ) {
538cdf0e10cSrcweir             // all tests finished.
539cdf0e10cSrcweir             hTestHandle = -1;
540cdf0e10cSrcweir         }
541cdf0e10cSrcweir     }
542cdf0e10cSrcweir     else
543cdf0e10cSrcweir     {
544cdf0e10cSrcweir         throw IllegalArgumentException();
545cdf0e10cSrcweir     }
546cdf0e10cSrcweir     return hTestHandle;
547cdf0e10cSrcweir }
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 
testPassed(void)551cdf0e10cSrcweir sal_Bool OMarkableInputStreamTest::testPassed(void)                                         throw (RuntimeException)
552cdf0e10cSrcweir {
553cdf0e10cSrcweir     return m_seqErrors.getLength() == 0;
554cdf0e10cSrcweir }
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 
getErrors(void)557cdf0e10cSrcweir Sequence< OUString > OMarkableInputStreamTest::getErrors(void)                          throw (RuntimeException)
558cdf0e10cSrcweir {
559cdf0e10cSrcweir     return m_seqErrors;
560cdf0e10cSrcweir }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir 
getErrorExceptions(void)563cdf0e10cSrcweir Sequence< Any > OMarkableInputStreamTest::getErrorExceptions(void)                  throw (RuntimeException)
564cdf0e10cSrcweir {
565cdf0e10cSrcweir     return m_seqExceptions;
566cdf0e10cSrcweir }
567cdf0e10cSrcweir 
568cdf0e10cSrcweir 
getWarnings(void)569cdf0e10cSrcweir Sequence< OUString > OMarkableInputStreamTest::getWarnings(void)                        throw (RuntimeException)
570cdf0e10cSrcweir {
571cdf0e10cSrcweir     return m_seqWarnings;
572cdf0e10cSrcweir }
573cdf0e10cSrcweir 
574cdf0e10cSrcweir 
testSimple(const Reference<XOutputStream> & rOutput,const Reference<XInputStream> & rInput)575cdf0e10cSrcweir void OMarkableInputStreamTest::testSimple(      const Reference< XOutputStream > &rOutput ,
576cdf0e10cSrcweir                                                 const Reference < XInputStream > &rInput )
577cdf0e10cSrcweir {
578cdf0e10cSrcweir     Reference < XMarkableStream > rMarkable( rInput , UNO_QUERY );
579cdf0e10cSrcweir 
580cdf0e10cSrcweir     Sequence<sal_Int8> seqWrite( 256 );
581cdf0e10cSrcweir     Sequence<sal_Int8> seqRead(10);
582cdf0e10cSrcweir 
583cdf0e10cSrcweir     for( int i = 0 ; i < 256 ; i ++ )
584cdf0e10cSrcweir     {
585cdf0e10cSrcweir         seqWrite.getArray()[i] = i;
586cdf0e10cSrcweir     }
587cdf0e10cSrcweir 
588cdf0e10cSrcweir     rOutput->writeBytes( seqWrite );
589cdf0e10cSrcweir     ERROR_ASSERT( 256 == rInput->available() , "basic read/write failure" );
590cdf0e10cSrcweir 
591cdf0e10cSrcweir     rInput->readBytes( seqRead , 10 );
592cdf0e10cSrcweir     ERROR_ASSERT( 9 == seqRead.getArray()[9] , "basic read/write failure" );
593cdf0e10cSrcweir 
594cdf0e10cSrcweir     sal_Int32 nMark = rMarkable->createMark();
595cdf0e10cSrcweir 
596cdf0e10cSrcweir     rInput->skipBytes( 50 );
597cdf0e10cSrcweir     ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
598cdf0e10cSrcweir     ERROR_ASSERT( 50 == rMarkable->offsetToMark( nMark ) , "marking error" );
599cdf0e10cSrcweir 
600cdf0e10cSrcweir     rMarkable->jumpToMark( nMark );
601cdf0e10cSrcweir     ERROR_ASSERT( 256-10 == rInput->available() , "marking error" );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir     rInput->readBytes( seqRead , 10 );
604cdf0e10cSrcweir     ERROR_ASSERT( 10 == seqRead.getArray()[0] , "marking error" );
605cdf0e10cSrcweir 
606cdf0e10cSrcweir     // pos 20
607cdf0e10cSrcweir     {
608cdf0e10cSrcweir         sal_Int32 nInBetweenMark = rMarkable->createMark( );
609cdf0e10cSrcweir         rMarkable->jumpToMark( nMark );
610cdf0e10cSrcweir         rMarkable->jumpToMark( nInBetweenMark );
611cdf0e10cSrcweir 
612cdf0e10cSrcweir         rInput->readBytes( seqRead , 10 );
613cdf0e10cSrcweir         ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
614cdf0e10cSrcweir 
615cdf0e10cSrcweir         rMarkable->deleteMark( nMark );
616cdf0e10cSrcweir 
617cdf0e10cSrcweir         // Check if releasing the first bytes works correct.
618cdf0e10cSrcweir         rMarkable->jumpToMark( nInBetweenMark);
619cdf0e10cSrcweir         rInput->readBytes( seqRead , 10 );
620cdf0e10cSrcweir         ERROR_ASSERT( 20 == seqRead.getArray()[0] , "Inbetween mark failed!\n" );
621cdf0e10cSrcweir 
622cdf0e10cSrcweir         rMarkable->deleteMark( nInBetweenMark );
623cdf0e10cSrcweir     }
624cdf0e10cSrcweir 
625cdf0e10cSrcweir     rMarkable->jumpToFurthest();
626cdf0e10cSrcweir     ERROR_ASSERT( 256-10-50 == rInput->available() , "marking error" );
627cdf0e10cSrcweir 
628cdf0e10cSrcweir 
629cdf0e10cSrcweir     ERROR_ASSERT( 100 == rInput->readSomeBytes( seqRead , 100   ) , "wrong results using readSomeBytes" );
630cdf0e10cSrcweir     ERROR_ASSERT( 96 == rInput->readSomeBytes( seqRead , 1000) , "wrong results using readSomeBytes" );
631cdf0e10cSrcweir     rOutput->closeOutput();
632cdf0e10cSrcweir     rInput->closeInput();
633cdf0e10cSrcweir }
634cdf0e10cSrcweir 
635cdf0e10cSrcweir /***
636cdf0e10cSrcweir * the test methods
637cdf0e10cSrcweir *
638cdf0e10cSrcweir ****/
639cdf0e10cSrcweir 
640cdf0e10cSrcweir 
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 
644cdf0e10cSrcweir /**
645cdf0e10cSrcweir * for external binding
646cdf0e10cSrcweir *
647cdf0e10cSrcweir *
648cdf0e10cSrcweir **/
OMarkableInputStreamTest_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)649cdf0e10cSrcweir Reference < XInterface > SAL_CALL OMarkableInputStreamTest_CreateInstance( const Reference< XMultiServiceFactory > & rSMgr ) throw(Exception)
650cdf0e10cSrcweir {
651cdf0e10cSrcweir     OMarkableInputStreamTest *p = new OMarkableInputStreamTest( rSMgr );
652cdf0e10cSrcweir     return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
655cdf0e10cSrcweir 
656cdf0e10cSrcweir 
OMarkableInputStreamTest_getSupportedServiceNames(void)657cdf0e10cSrcweir Sequence<OUString> OMarkableInputStreamTest_getSupportedServiceNames(void) throw ()
658cdf0e10cSrcweir {
659cdf0e10cSrcweir     Sequence<OUString> aRet(1);
660cdf0e10cSrcweir     aRet.getArray()[0] = OMarkableInputStreamTest_getImplementationName();
661cdf0e10cSrcweir 
662cdf0e10cSrcweir     return aRet;
663cdf0e10cSrcweir }
664cdf0e10cSrcweir 
OMarkableInputStreamTest_getServiceName()665cdf0e10cSrcweir OUString     OMarkableInputStreamTest_getServiceName() throw ()
666cdf0e10cSrcweir {
667cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.io.MarkableInputStream"));
668cdf0e10cSrcweir }
669cdf0e10cSrcweir 
OMarkableInputStreamTest_getImplementationName()670cdf0e10cSrcweir OUString    OMarkableInputStreamTest_getImplementationName() throw ()
671cdf0e10cSrcweir {
672cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.extensions.stm.MarkableInputStream" ));
673cdf0e10cSrcweir }
674