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