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
|
/* libcmis
* Version: MPL 1.1 / GPLv2+ / LGPLv2+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2011 SUSE <cbosdonnat@suse.com>
*
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPLv2+"), or
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
* in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
* instead of those above.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <cppunit/TestAssert.h>
#include <libcmis-c/error.h>
#include <libcmis-c/folder.h>
#include <libcmis-c/document.h>
#include <libcmis-c/object.h>
#include <libcmis-c/object-type.h>
#include <libcmis-c/property.h>
#include <libcmis-c/property-type.h>
#include <libcmis-c/vectors.h>
#include "internals.hxx"
#include "test-dummies.hxx"
using namespace std;
extern bool isOutOfMemory;
namespace
{
string lcl_readFile( FILE* file )
{
// Get the size
fseek( file, 0, SEEK_END );
long size = ftell( file );
rewind( file );
char* buf = new char[size + 1];
size_t readbytes = fread( buf, 1, size, file );
buf[ readbytes ] = '\0';
string result( buf );
delete[] buf;
return result;
}
}
class DocumentTest : public CppUnit::TestFixture
{
private:
libcmis_DocumentPtr getTested( bool isFiled, bool triggersFaults );
dummies::Document* getTestedImplementation( libcmis_DocumentPtr document );
public:
void objectCastTest( );
void objectCastFailureTest( );
void objectFunctionsTest( );
void getParentsTest( );
void getParentsUnfiledTest( );
void getParentsErrorTest( );
void getContentStreamTest( );
void getContentStreamErrorTest( );
void getContentStreamBadAllocTest( );
void setContentStreamTest( );
void setContentStreamErrorTest( );
void getContentTypeTest( );
void getContentFilenameTest( );
void getContentLengthTest( );
void checkOutTest( );
void checkOutErrorTest( );
void cancelCheckoutTest( );
void cancelCheckoutErrorTest( );
void checkInTest( );
void checkInErrorTest( );
void getAllVersionsTest( );
void getAllVersionsErrorTest( );
CPPUNIT_TEST_SUITE( DocumentTest );
CPPUNIT_TEST( objectCastTest );
CPPUNIT_TEST( objectCastFailureTest );
CPPUNIT_TEST( objectFunctionsTest );
CPPUNIT_TEST( getParentsTest );
CPPUNIT_TEST( getParentsUnfiledTest );
CPPUNIT_TEST( getParentsErrorTest );
CPPUNIT_TEST( getContentStreamTest );
CPPUNIT_TEST( getContentStreamErrorTest );
CPPUNIT_TEST( getContentStreamBadAllocTest );
CPPUNIT_TEST( setContentStreamTest );
CPPUNIT_TEST( setContentStreamErrorTest );
CPPUNIT_TEST( getContentTypeTest );
CPPUNIT_TEST( getContentFilenameTest );
CPPUNIT_TEST( getContentLengthTest );
CPPUNIT_TEST( checkOutTest );
CPPUNIT_TEST( checkOutErrorTest );
CPPUNIT_TEST( cancelCheckoutTest );
CPPUNIT_TEST( cancelCheckoutErrorTest );
CPPUNIT_TEST( checkInTest );
CPPUNIT_TEST( checkInErrorTest );
CPPUNIT_TEST( getAllVersionsTest );
CPPUNIT_TEST( getAllVersionsErrorTest );
CPPUNIT_TEST_SUITE_END( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( DocumentTest );
libcmis_DocumentPtr DocumentTest::getTested( bool isFiled, bool triggersFaults )
{
// Create the document
libcmis_DocumentPtr result = new libcmis_document( );
libcmis::DocumentPtr handle( new dummies::Document( isFiled, triggersFaults ) );
result->handle = handle;
return result;
}
dummies::Document* DocumentTest::getTestedImplementation( libcmis_DocumentPtr document )
{
dummies::Document* impl = dynamic_cast< dummies::Document* >( document->handle.get( ) );
return impl;
}
void DocumentTest::objectCastTest( )
{
// Create the test object to cast
libcmis_ObjectPtr tested = new libcmis_object( );
libcmis::DocumentPtr handle( new dummies::Document( true, false ) );
tested->handle = handle;
// Test libcmis_is_document
CPPUNIT_ASSERT( libcmis_is_document( tested ) );
// Actually cast to a document
libcmis_DocumentPtr actual = libcmis_document_cast( tested );
// Check the result
CPPUNIT_ASSERT( NULL != actual );
// Check that the libcmis_object-* functions are working with the cast result
char* actualId = libcmis_object_getId( tested );
CPPUNIT_ASSERT_EQUAL( string( "Document::Id" ), string( actualId ) );
free( actualId );
// Free it all
libcmis_document_free( actual );
libcmis_object_free( tested );
}
void DocumentTest::objectCastFailureTest( )
{
// Create the test object to cast
libcmis_ObjectPtr tested = new libcmis_object( );
libcmis::FolderPtr handle( new dummies::Folder( false, false ) );
tested->handle = handle;
// Test libcmis_is_document
CPPUNIT_ASSERT( !libcmis_is_document( tested ) );
// Actually cast to a document
libcmis_DocumentPtr actual = libcmis_document_cast( tested );
// Check the result
CPPUNIT_ASSERT( NULL == actual );
libcmis_object_free( tested );
}
void DocumentTest::objectFunctionsTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
char* actual = libcmis_object_getId( tested );
CPPUNIT_ASSERT_EQUAL( string( "Document::Id" ), string( actual ) );
free( actual );
libcmis_document_free( tested );
}
void DocumentTest::getParentsTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the parent folders (tested method)
libcmis_vector_folder_Ptr actual = libcmis_document_getParents( tested, error );
// Check
CPPUNIT_ASSERT_EQUAL( size_t( 2 ), libcmis_vector_folder_size( actual ) );
// Free it all
libcmis_vector_folder_free( actual );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getParentsUnfiledTest( )
{
libcmis_DocumentPtr tested = getTested( false, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the parent folders (tested method)
libcmis_vector_folder_Ptr actual = libcmis_document_getParents( tested, error );
// Check
CPPUNIT_ASSERT_EQUAL( size_t( 0 ), libcmis_vector_folder_size( actual ) );
// Free it all
libcmis_vector_folder_free( actual );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getParentsErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the parent folders (tested method)
libcmis_vector_folder_Ptr actual = libcmis_document_getParents( tested, error );
// Check
CPPUNIT_ASSERT( NULL == actual );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
// Free it all
libcmis_vector_folder_free( actual );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getContentStreamTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the content into a temporary file (tested method)
FILE* tmp = tmpfile( );
libcmis_document_getContentStream( tested,
( libcmis_writeFn )fwrite, tmp, error );
// Check
string expected = getTestedImplementation( tested )->getContentString( );
string actual = lcl_readFile( tmp );
fclose( tmp );
CPPUNIT_ASSERT( NULL == libcmis_error_getMessage( error ) );
CPPUNIT_ASSERT_EQUAL( expected, actual );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getContentStreamErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the content into a temporary file (tested method)
FILE* tmp = tmpfile( );
libcmis_document_getContentStream( tested,
( libcmis_writeFn )fwrite, tmp, error );
// Check
string actual = lcl_readFile( tmp );
fclose( tmp );
CPPUNIT_ASSERT_EQUAL( string( ), actual );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getContentStreamBadAllocTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// get the content into a temporary file (tested method)
FILE* tmp = tmpfile( );
isOutOfMemory= true;
libcmis_document_getContentStream( tested,
( libcmis_writeFn )fwrite, tmp, error );
isOutOfMemory = false;
// Check
string actual = lcl_readFile( tmp );
fclose( tmp );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty() );
CPPUNIT_ASSERT_EQUAL( string( ), actual );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::setContentStreamTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// Prepare the content to set
FILE* tmp = tmpfile( );
string expected( "New Content Stream" );
fwrite( expected.c_str( ), 1, expected.size( ), tmp );
rewind( tmp );
// get the content into a temporary file (tested method)
const char* contentType = "content/type";
const char* filename = "name.txt";
libcmis_document_setContentStream( tested,
( libcmis_readFn )fread, tmp, contentType, filename, true, error );
fclose( tmp );
// Check
string actual = getTestedImplementation( tested )->getContentString( );
CPPUNIT_ASSERT( NULL == libcmis_error_getMessage( error ) );
CPPUNIT_ASSERT_EQUAL( expected, actual );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::setContentStreamErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
string expected = getTestedImplementation( tested )->getContentString( );
// Prepare the content to set
FILE* tmp = tmpfile( );
string newContent( "New Content Stream" );
fwrite( newContent.c_str( ), 1, newContent.size( ), tmp );
rewind( tmp );
// get the content into a temporary file (tested method)
const char* contentType = "content/type";
const char* filename = "name.txt";
libcmis_document_setContentStream( tested,
( libcmis_readFn )fread, tmp, contentType, filename, true, error );
fclose( tmp );
// Check
string actual = getTestedImplementation( tested )->getContentString( );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
CPPUNIT_ASSERT_EQUAL( expected, actual );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getContentTypeTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
char* actual = libcmis_document_getContentType( tested );
CPPUNIT_ASSERT_EQUAL( string( "Document::ContentType" ), string( actual ) );
free( actual );
libcmis_document_free( tested );
}
void DocumentTest::getContentFilenameTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
char* actual = libcmis_document_getContentFilename( tested );
CPPUNIT_ASSERT_EQUAL( string( "Document::ContentFilename" ), string( actual ) );
free( actual );
libcmis_document_free( tested );
}
void DocumentTest::getContentLengthTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
long actual = libcmis_document_getContentLength( tested );
CPPUNIT_ASSERT( 0 != actual );
libcmis_document_free( tested );
}
void DocumentTest::checkOutTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// checkout a private working copy (tested method)
libcmis_DocumentPtr actual = libcmis_document_checkOut( tested, error );
// Check
CPPUNIT_ASSERT( NULL != actual );
// Free it all
libcmis_document_free( actual );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::checkOutErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
// checkout a private working copy (tested method)
libcmis_DocumentPtr actual = libcmis_document_checkOut( tested, error );
// Check
CPPUNIT_ASSERT( NULL == actual );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
// Free it all
libcmis_document_free( actual );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::cancelCheckoutTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// checkout a private working copy (tested method)
libcmis_document_cancelCheckout( tested, error );
// Check
CPPUNIT_ASSERT( 0 != libcmis_object_getRefreshTimestamp( tested ) );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::cancelCheckoutErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
// checkout a private working copy (tested method)
libcmis_document_cancelCheckout( tested, error );
// Check
CPPUNIT_ASSERT( 0 == libcmis_object_getRefreshTimestamp( tested ) );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
// Free it all
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::checkInTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// Prepare the content to set
FILE* tmp = tmpfile( );
string expected( "New Content Stream" );
fwrite( expected.c_str( ), 1, expected.size( ), tmp );
rewind( tmp );
// Create the properties for the new version
libcmis_vector_property_Ptr properties = libcmis_vector_property_create( );
libcmis_ObjectTypePtr objectType = libcmis_object_getTypeDescription( tested );
const char* id = "Property1";
libcmis_PropertyTypePtr propertyType = libcmis_object_type_getPropertyType( objectType, id );
size_t size = 2;
const char** values = new const char*[size];
values[0] = "Value 1";
values[1] = "Value 2";
libcmis_PropertyPtr property = libcmis_property_create( propertyType, values, size );
delete[] values;
libcmis_vector_property_append( properties, property );
// get the content into a temporary file (tested method)
const char* contentType = "content/type";
const char* comment = "Version comment";
const char* filename = "filename.txt";
libcmis_DocumentPtr newVersion = libcmis_document_checkIn(
tested, true, comment, properties,
( libcmis_readFn )fread, tmp, contentType, filename, error );
fclose( tmp );
// Check
CPPUNIT_ASSERT( NULL != newVersion );
string actual = getTestedImplementation( tested )->getContentString( );
CPPUNIT_ASSERT( NULL == libcmis_error_getMessage( error ) );
CPPUNIT_ASSERT_EQUAL( expected, actual );
libcmis_PropertyPtr checkedProperty = libcmis_object_getProperty( tested, "Property1" );
libcmis_vector_string_Ptr newValues = libcmis_property_getStrings( checkedProperty );
CPPUNIT_ASSERT_EQUAL( size_t( 2 ), libcmis_vector_string_size( newValues ) );
// Free it all
libcmis_vector_string_free( newValues );
libcmis_property_free( checkedProperty );
libcmis_document_free( newVersion );
libcmis_property_free( property );
libcmis_property_type_free( propertyType );
libcmis_object_type_free( objectType );
libcmis_vector_property_free( properties );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::checkInErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
string expected = getTestedImplementation( tested )->getContentString( );
// Prepare the content to set
FILE* tmp = tmpfile( );
string newContent( "New Content Stream" );
fwrite( newContent.c_str( ), 1, newContent.size( ), tmp );
rewind( tmp );
// Create the properties for the new version
libcmis_vector_property_Ptr properties = libcmis_vector_property_create( );
libcmis_ObjectTypePtr objectType = libcmis_object_getTypeDescription( tested );
const char* id = "Property1";
libcmis_PropertyTypePtr propertyType = libcmis_object_type_getPropertyType( objectType, id );
size_t size = 2;
const char** values = new const char*[size];
values[0] = "Value 1";
values[1] = "Value 2";
libcmis_PropertyPtr property = libcmis_property_create( propertyType, values, size );
delete[] values;
libcmis_vector_property_append( properties, property );
// get the content into a temporary file (tested method)
const char* contentType = "content/type";
const char* comment = "Version comment";
const char* filename = "filename.txt";
libcmis_DocumentPtr newVersion = libcmis_document_checkIn(
tested, true, comment, properties,
( libcmis_readFn )fread, tmp, contentType, filename, error );
fclose( tmp );
// Check
CPPUNIT_ASSERT( NULL == newVersion );
string actual = getTestedImplementation( tested )->getContentString( );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
CPPUNIT_ASSERT_EQUAL( expected, actual );
libcmis_PropertyPtr checkedProperty = libcmis_object_getProperty( tested, "Property1" );
libcmis_vector_string_Ptr newValues = libcmis_property_getStrings( checkedProperty );
CPPUNIT_ASSERT_EQUAL( size_t( 1 ), libcmis_vector_string_size( newValues ) );
// Free it all
libcmis_vector_string_free( newValues );
libcmis_property_free( checkedProperty );
libcmis_document_free( newVersion );
libcmis_property_free( property );
libcmis_property_type_free( propertyType );
libcmis_object_type_free( objectType );
libcmis_vector_property_free( properties );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getAllVersionsTest( )
{
libcmis_DocumentPtr tested = getTested( true, false );
libcmis_ErrorPtr error = libcmis_error_create( );
// get all versions (tested method)
libcmis_vector_document_Ptr versions = libcmis_document_getAllVersions( tested, error );
// Check
CPPUNIT_ASSERT_EQUAL( size_t( 2 ), libcmis_vector_document_size( versions ) );
libcmis_DocumentPtr actualVersion = libcmis_vector_document_get( versions, 0 );
char* actualId = libcmis_object_getId( actualVersion );
CPPUNIT_ASSERT( actualId != NULL );
free( actualId );
// Free it all
libcmis_document_free( actualVersion );
libcmis_vector_document_free( versions );
libcmis_error_free( error );
libcmis_document_free( tested );
}
void DocumentTest::getAllVersionsErrorTest( )
{
libcmis_DocumentPtr tested = getTested( true, true );
libcmis_ErrorPtr error = libcmis_error_create( );
// get all versions (tested method)
libcmis_vector_document_Ptr versions = libcmis_document_getAllVersions( tested, error );
// Check
CPPUNIT_ASSERT( NULL == versions );
CPPUNIT_ASSERT( !string( libcmis_error_getMessage( error ) ).empty( ) );
// Free it all
libcmis_vector_document_free( versions );
libcmis_error_free( error );
libcmis_document_free( tested );
}
|