summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Property/main.cpp
blob: d9e9a9f2a3efbad55e35568bc720bf90f11d78d0 (plain)
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
// $Id$

// ========================================================================
//
// = FILE
//     Program to test the property services.
//
// = DESCRIPTION
//     Here all the objects are at one address space. See the
//     client.cpp and server.cpp to see property calls on remote
//     objects.
//
// = AUTHOR
//     Alexander Babu Arulanthu <alex@cs.wustl.edu>
//
// ========================================================================

#include "orbsvcs/CosPropertyS.h"
#include "orbsvcs/Property/CosPropertyService_i.h"

ACE_RCSID(CosPropertyService, main, "$Id$")

class TAO_PropertySet_Tester
{
  // = TITLE
  //     The testing code for the PropertySet interface are grouped
  //     under the functions of this class.
  //
  // = DESCRIPTION
  //     Go thru the functions to understand the usage of the various
  //     methods of the PropertySet interaface.
public:
  friend class TAO_PropertyNamesIterator_Tester;
  // He has to instantiate TAO_NamesIterator class using the instance
  // of TAO_PropertySet.

  TAO_PropertySet_Tester (void);
  // Constructor.

  ~TAO_PropertySet_Tester (void);
  // Destructor.

  int test_define_property (ACE_ENV_SINGLE_ARG_DECL);
  // Defines a char,a short, a long, a float and a string property.

  int test_get_number_of_properties (ACE_ENV_SINGLE_ARG_DECL);
  // Gets the number of properties currently defined in the PropertySet.

  int test_delete_property (const char *property_name
                            ACE_ENV_ARG_DECL);
  // Delete a given property.

  int test_is_property_defined (ACE_ENV_SINGLE_ARG_DECL);
  // Check the following properties are defined or
  // no. "char_property", "short_property" and a "string_property".

  int test_get_property_value (ACE_ENV_SINGLE_ARG_DECL);
  // Get the "float_property" and "string_property" and print them
  // out.

  int test_delete_properties (ACE_ENV_SINGLE_ARG_DECL);
  // Delete char, short,long, and string properties.

  int test_delete_all_properties (ACE_ENV_SINGLE_ARG_DECL);
  // Delete all the properties.

  int test_define_properties (ACE_ENV_SINGLE_ARG_DECL);
  // Define a sequence of properties. a char, a short, a long, a float
  // and a string.

private:
  TAO_PropertySet property_set_;
  // The PropertySet.
};

class TAO_PropertyNamesIterator_Tester
{
  // = TITLE
  //     The testing code for the PropertyNamesIterator interface are grouped
  //     under the functions of this class.
  //
  // = DESCRIPTION
  //     Go thru the functions to understand the usage of the various
  //     methods of the PropertyNamesIterator interaface.
public:
  TAO_PropertyNamesIterator_Tester (TAO_PropertySet_Tester &propertyset_tester);
  // Constructor. To make names iterator from the property set object.

  ~TAO_PropertyNamesIterator_Tester (void);
  // Destructor.

  int test_next_one (ACE_ENV_SINGLE_ARG_DECL);
  // Test next one method. Iterate thru all the names in the
  // PropertySet and print them out.

  int test_reset (ACE_ENV_SINGLE_ARG_DECL);
  // Test the reset method.

  int test_next_n (size_t n ACE_ENV_ARG_DECL);
  // Test the next_n method.
private:
  TAO_PropertyNamesIterator iterator_;
  // Our names iterator.
};


// Constructor.
TAO_PropertySet_Tester::TAO_PropertySet_Tester (void)
{
}

// Destructor.
TAO_PropertySet_Tester::~TAO_PropertySet_Tester (void)
{
}

// Defines a char, a short, a long, a float and a string.
int
TAO_PropertySet_Tester::test_define_property (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking define_property\n"));

  CORBA::Any anyval;

  // Prepare a char and "define" that in the PropertySet.
  CORBA::Char ch = '#';
  anyval <<= from_char (ch);
  ch = '*';
  anyval >>= to_char (ch);
  ACE_DEBUG ((LM_DEBUG,
              "Main : Char ch = %c\n",
              ch));
  property_set_.define_property ("char_property",
                                 anyval
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  // Prepare a Short and "define" that in the PropertySet.
  CORBA::Short s = 3;
  anyval <<= s;
  s = 7;
  anyval >>= s;
  ACE_DEBUG ((LM_DEBUG,
              "Main : Short s = %d\n",
              s));
  property_set_.define_property ("short_property",
                                 anyval
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);


  // Prepare a Long and "define" that in the PropertySet.
  CORBA::Long l = 931232;
  anyval <<= l;
  l = 931233;
  anyval >>= l;
  ACE_DEBUG ((LM_DEBUG,
              "Main : Long l = %d\n",
              l));
  CORBA::Any newany(anyval);

  property_set_.define_property ("long_property",
                                 anyval
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  // Prepare a Float and "define" that in the PropertySet.
  CORBA::Float f = 3.14;
  anyval <<= f;
  f = 4.14;
  anyval >>= f;
  ACE_DEBUG ((LM_DEBUG,
              "Main : Float f = %f\n",
              f));
  property_set_.define_property ("float_property",
                                 anyval
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  // Prepare a String and "define" that in the PropertySet.
  ACE_DEBUG ((LM_DEBUG,
              "Main: Any holding String\n"));
  CORBA::String_var strvar (CORBA::string_dup ("Test_String"));
  anyval <<= strvar.in ();
  CORBA::String newstr;
  anyval >>= newstr;
  ACE_DEBUG ((LM_DEBUG,
              "Main: String :  %s, From any :  %s\n",
              strvar.in (),
              newstr));
  property_set_.define_property ("string_property",
                                 anyval
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  return 0;
}

// Testing, get_number_of_properties.
int
TAO_PropertySet_Tester::test_get_number_of_properties (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "Main : Number of props : %d\n",
              property_set_.get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER)));
  ACE_CHECK_RETURN ( 1);

  return 0;
}

// Testing the delete_property. Delets property, with the given name,
// if that exsists.
int
TAO_PropertySet_Tester::test_delete_property (const char *property_name
                                              ACE_ENV_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG, "\nChecking delete_property\n"));
  property_set_.delete_property (property_name
                                 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  return 0;
}

// Gets the value of "short_property" and "string_property".

int
TAO_PropertySet_Tester::test_get_property_value (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking get_property_value\n"));

  CORBA::Any_ptr anyptr = property_set_.get_property_value ("short_property"
                                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  // Get the short value.
  if (anyptr != 0)
    {
      CORBA::Short s;
      *anyptr >>= s;
      ACE_DEBUG ((LM_DEBUG,
                  "Short %d\n",
                  s));
    }
  else
    ACE_DEBUG ((LM_DEBUG,
                "Short property not found\n"));
  // Get the string.
  anyptr = property_set_.get_property_value ("string_property"
                                             ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);
  if (anyptr != 0)
    {
      CORBA::String str;
      *anyptr >>= str;
      ACE_DEBUG ((LM_DEBUG,
                  "Str %s\n", str));
    }
  else
    ACE_DEBUG ((LM_DEBUG,
                "string_property not found\n"));
  return 0;
}

// Check the following properties are defined or no. "short_property",
// "string_property" and "char_property".

int
TAO_PropertySet_Tester::test_is_property_defined (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking is_property_defined ()\n"));
  if (property_set_.is_property_defined ("short_property"
                                         ACE_ENV_ARG_PARAMETER) == 0)
    ACE_DEBUG ((LM_DEBUG,
                "short_property not defined\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "short_property defined\n"));
  ACE_CHECK_RETURN ( 1);
  if (property_set_.is_property_defined ("string_property"
                                         ACE_ENV_ARG_PARAMETER) == 0)
    ACE_DEBUG ((LM_DEBUG,
                "string_property not defined\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "string_property defined\n"));
  ACE_CHECK_RETURN ( 1);
  if (property_set_.is_property_defined ("char_property"
                                         ACE_ENV_ARG_PARAMETER) == 0)
    ACE_DEBUG ((LM_DEBUG,
                "char_property not defined\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "char_property defined\n"));
  ACE_CHECK_RETURN ( 1);

  return 0;
}

// Make a sequence of property names and delete them from the
// PropertySet.  Deleting char, short, long, float and string
// properties.
int
TAO_PropertySet_Tester::test_delete_properties (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking delete_properties == Deleting a sequence of Properties\n"));
  CosPropertyService::PropertyNames prop_names;
  prop_names.length (4);
  prop_names [0] = CORBA::string_dup ("char_property");
  prop_names [1] = CORBA::string_dup ("short_property");
  prop_names [2] = CORBA::string_dup ("long_property");
  prop_names [3] = CORBA::string_dup ("float_property");
  ACE_DEBUG ((LM_DEBUG,
              "Length of sequence %d, Maxlength : %d\n",
              prop_names.length (),
              prop_names.maximum ()));
  property_set_.delete_properties (prop_names
                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 0);

  return 0;
}

// Defines a sequnce of properties containing, char, short, long,
// float and string property in the property set.
int
TAO_PropertySet_Tester::test_define_properties (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking define_properties == Defining sequence of properties\n"));
  //  TAO_TRY
  //  {
  CosPropertyService::Properties nproperties;
  nproperties.length (5);
  CORBA::Any anyval;
  // Prepare a char and "define" that in the PropertySet.
  CORBA::Char ch = '#';
  anyval <<= from_char (ch);
  ch = '*';
  anyval >>= to_char (ch);
  nproperties[0].property_name  = CORBA::string_dup ("char_property");
  nproperties[0].property_value <<= from_char (ch);

  // Prepare a Short and "define" that in the PropertySet.
  CORBA::Short s = 3;
  anyval <<= s;
  s = 7;
  anyval >>= s;
  nproperties[1].property_name  = CORBA::string_dup ("short_property");
  nproperties[1].property_value <<= s;

  // Prepare a Long and "define" that in the PropertySet.
  CORBA::Long l = 931232;
  anyval <<= l;
  l = 931233;
  anyval >>= l;
  nproperties[2].property_name  = CORBA::string_dup ("long_property");
  nproperties[2].property_value <<= l;

  // Prepare a Float and "define" that in the PropertySet.
  CORBA::Float f = 3.14;
  anyval <<= f;
  f = 4.14;
  anyval >>= f;
  nproperties[3].property_name  = CORBA::string_dup ("float_property");
  nproperties[3].property_value <<= f;

  // Prepare a String and "define" that in the PropertySet.
  CORBA::String_var strvar (CORBA::string_dup ("Test_String"));
  anyval <<= strvar.in ();
  nproperties[4].property_name  = CORBA::string_dup ("string_property");
  nproperties[4].property_value <<= strvar.in ();

  // Define this sequence of properties now.
  property_set_.define_properties (nproperties ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);

  return 0;
}

// Delete all the properties.
int
TAO_PropertySet_Tester::test_delete_all_properties (ACE_ENV_SINGLE_ARG_DECL)
{
  // Deleting all the properties
  ACE_DEBUG ((LM_DEBUG,
              "\nChecking delete_all_properties\n"));
  int ret = property_set_.delete_all_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);
  if (ret == 1)
    ACE_DEBUG ((LM_DEBUG,
                "All properties deleted, I guess\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "delete_all_properties failed\n"));
  return 0;
}

// Constructor. To make names iterator from the property set object.
TAO_PropertyNamesIterator_Tester::TAO_PropertyNamesIterator_Tester (TAO_PropertySet_Tester &propertyset_tester)
  : iterator_ (propertyset_tester.property_set_)
{
}

// Destructor.
TAO_PropertyNamesIterator_Tester::~TAO_PropertyNamesIterator_Tester (void)
{
}

// Test next one method. Iterate thru all the names in the
// PropertySet and print them out.
int
TAO_PropertyNamesIterator_Tester::test_next_one (ACE_ENV_SINGLE_ARG_DECL)
{
  CORBA::String_var strvar;
  ACE_DEBUG ((LM_DEBUG,
              "\nTesting next_one of NamesIterator, Iterating thru names.\n"));
  // Let us iterate, now.
  int ret = iterator_.next_one (strvar.out () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (1);
  while (ret != 0)
    {
      ACE_DEBUG ((LM_DEBUG, "Str : %s\n", strvar.in ()));
      ret = iterator_.next_one (strvar.out () ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (1);
    }
  return 0;
}

// Reset the names iterator.
int
TAO_PropertyNamesIterator_Tester::test_reset (ACE_ENV_SINGLE_ARG_DECL)
{
  ACE_DEBUG ((LM_DEBUG,
              "Resetting (reset ()) the NamesIterator."));
  iterator_.reset (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN ( 1);
  return 0;
}

// Test the next_n method. Get the next n names and print them all.
int
TAO_PropertyNamesIterator_Tester::test_next_n (size_t n
                                               ACE_ENV_ARG_DECL)
{
  CosPropertyService::PropertyNames_var pnames_var;
  ACE_DEBUG ((LM_DEBUG,
              "Checking next_n (), next %d\n",
              n));
  int ret = iterator_.next_n (n, pnames_var.out () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (1);
  if (ret == 0)
    {
      // Return if no more items in the iterator.
      ACE_DEBUG ((LM_DEBUG,
                  "Iterator has no more items\n"));
      return 0;
    }
  for (size_t i = 0; i < pnames_var.in ().length (); i++)
    ACE_DEBUG ((LM_DEBUG,
                "str %s \n",
                (const char *) pnames_var[i]));
  return 0;
}

int
main (int argc, char *argv [])
{
  ACE_TRY
    {
      CORBA::ORB_var orb_var = CORBA::ORB_init (argc,
                                                argv,
                                                "internet"
                                                ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // = Checking PropertySet interface.

      ACE_DEBUG ((LM_DEBUG,
                  "\nTAO_PropertySet Testing\n"));
      TAO_PropertySet_Tester propertyset_tester;

      // Checking define_property. define a char, a short,a long, a float
      // and a string.
      propertyset_tester.test_define_property (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test delete property. Delete "string_property"
      propertyset_tester.test_delete_property ("string_property" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test delete_properties. Delete char, short, long and float.
      propertyset_tester.test_delete_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Define a sequence of properties. char, short, long, float and
      // string.
      propertyset_tester.test_define_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Checking get_property_value. get the value of short and string.
      propertyset_tester.test_get_property_value (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Checking delete_all_properties.
      propertyset_tester.test_delete_all_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Define a sequence of properties. char, short, long, float and
      // string.
      propertyset_tester.test_define_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Test the number of properties and print it out.
      propertyset_tester.test_get_number_of_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // = Checking the PropertyNamesIterator interface.

      ACE_DEBUG ((LM_DEBUG,
                  "\nTAO_PropertyNamesIterator Testing\n"));

      // Construct the names iterator from the PropertySet object.
      TAO_PropertyNamesIterator_Tester names_iterator_tester (propertyset_tester);

      // Checking next_one. Iterate thru the property names.
      names_iterator_tester.test_next_one (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Checking the reset () method.
      names_iterator_tester.test_reset (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Iterating again thru the property names.
      names_iterator_tester.test_next_one (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Reset again.
      names_iterator_tester.test_reset (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Checking next_n. Prints out all the names it has got.
      names_iterator_tester.test_next_n (6 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Try next_n without resetting.
      names_iterator_tester.test_next_n (6 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::SystemException, sysex)
    {
      ACE_ENV_ARG_PARAMETER.print_exception ("System Exception");
      return -1;
    }
  ACE_CATCH (CORBA::UserException, userex)
    {
      ACE_ENV_ARG_PARAMETER.print_exception ("User Exception");
      return -1;
    }
  ACE_ENDTRY;
  return 0;
}