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