Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / Simple_Naming / client.cpp
blob0b8e258dbcf1685c531bc7b24a3e596b1bebce45
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * This class implements a simple CORBA client for the CosNaming
7 * example using stubs generated by the TAO ORB IDL compiler.
9 * @author Sergio Flores-Gaitan <sergio@cs.wustl.edu>
10 * @author Marina Spivak <marina@cs.wustl.edu>
11 * @author and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
13 //=============================================================================
16 #include "client.h"
17 #include "tao/debug.h"
18 #include "ace/Get_Opt.h"
20 #if defined (_MSC_VER)
21 # pragma warning (disable : 4250)
22 #endif /* _MSC_VER */
24 class My_Test_Object :
25 public virtual POA_Test_Object
27 public:
28 /// Constructor.
29 My_Test_Object (CORBA::Short id = 0);
31 /// Destructor.
32 ~My_Test_Object ();
34 // = Interface implementation accessor methods.
36 /// Sets id.
37 void id (CORBA::Short id);
39 /// Gets id.
40 CORBA::Short id ();
42 private:
43 short id_;
46 My_Test_Object::My_Test_Object (CORBA::Short id)
47 : id_ (id)
51 My_Test_Object::~My_Test_Object ()
55 CORBA::Short
56 My_Test_Object::id ()
58 return id_;
61 void
62 My_Test_Object::id (CORBA::Short id)
64 id_ = id;
67 // Constructor.
69 CosNaming_Client::CosNaming_Client ()
70 : argc_ (0),
71 argv_ (0),
72 test_ (0)
76 // Parses the command line arguments and returns an error status.
78 int
79 CosNaming_Client::parse_args ()
81 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("p:dstieym:c:l"));
82 int c;
84 while ((c = get_opts ()) != -1)
85 switch (c)
87 case 'd': // debug flag
88 TAO_debug_level++;
89 break;
90 case 's':
91 if (this->test_ == 0)
92 ACE_NEW_RETURN (this->test_,
93 Simple_Test (this->orbmgr_.root_poa ()),
94 -1);
95 break;
96 case 'm':
97 if (this->test_ == 0)
99 int size = ACE_OS::atoi (get_opts.opt_arg ());
100 if (size <= 0)
101 size = 10;
103 ACE_NEW_RETURN (this->test_,
104 MT_Test (this->orbmgr_.orb (),
105 this->orbmgr_.root_poa (),
106 size),
107 -1);
110 break;
111 case 't':
112 if (this->test_ == 0)
113 ACE_NEW_RETURN (this->test_,
114 Tree_Test (this->orbmgr_.root_poa ()),
115 -1);
116 break;
117 case 'i':
118 if (this->test_ == 0)
119 ACE_NEW_RETURN (this->test_,
120 Iterator_Test (this->orbmgr_.root_poa ()),
121 -1);
122 break;
123 case 'e':
124 if (this->test_ == 0)
125 ACE_NEW_RETURN (this->test_,
126 Exceptions_Test (this->orbmgr_.root_poa ()),
127 -1);
128 break;
129 case 'y':
130 if (this->test_ == 0)
131 ACE_NEW_RETURN (this->test_,
132 Destroy_Test (this->orbmgr_.root_poa ()),
133 -1);
134 break;
135 case 'p':
136 if (this->test_ == 0)
138 FILE * ior_output_file =
139 ACE_OS::fopen (get_opts.opt_arg (), "w");
141 if (ior_output_file == 0)
142 ACE_ERROR_RETURN ((LM_ERROR,
143 "Unable to open %s for writing: %p\n",
144 get_opts.opt_arg ()), -1);
146 ACE_NEW_RETURN (this->test_,
147 Persistent_Test_Begin (this->orbmgr_.orb (),
148 this->orbmgr_.root_poa (),
149 ior_output_file),
150 -1);
152 break;
153 case 'c':
154 if (this->test_ == 0)
155 ACE_NEW_RETURN (this->test_,
156 Persistent_Test_End (this->orbmgr_.orb (),
157 this->orbmgr_.root_poa (),
158 get_opts.opt_arg ()),
159 -1);
160 break;
161 case 'l':
162 if (this->test_ == 0)
163 ACE_NEW_RETURN (this->test_,
164 Persistent_List_Test (this->orbmgr_.orb (),
165 this->orbmgr_.root_poa ()),
166 -1);
167 break;
168 default:
169 ACE_ERROR_RETURN ((LM_ERROR,
170 "Argument %c \n usage: %s"
171 " [-d]"
172 " [-s or -e or -t or -i or -y or -p or -c<ior> or -l<ior> or -m<size>]"
173 "\n",
175 this->argv_ [0]),
176 -1);
179 if (this->test_ == 0)
180 ACE_NEW_RETURN (this->test_,
181 Simple_Test (this->orbmgr_.root_poa ()),
182 -1);
184 // Indicates successful parsing of command line.
185 return 0;
188 // Execute client example code.
191 CosNaming_Client::run ()
193 return test_->execute (naming_client_);
196 CosNaming_Client::~CosNaming_Client ()
198 delete test_;
202 CosNaming_Client::init (int argc, ACE_TCHAR **argv)
204 this->argc_ = argc;
205 this->argv_ = argv;
209 // Initialize ORB.
210 this->orbmgr_.init (this->argc_,
211 this->argv_);
213 this->orbmgr_.activate_poa_manager ();
215 // Parse command line and verify parameters.
216 if (this->parse_args () == -1)
217 return -1;
219 CORBA::ORB_var orb = this->orbmgr_.orb ();
220 return this->naming_client_.init (orb.in ());
222 catch (const CORBA::Exception& ex)
224 ex._tao_print_exception ("init");
225 // and return -1 below . . .
228 return -1;
231 Naming_Test::Naming_Test (PortableServer::POA_ptr poa)
232 : poa_ (poa)
236 Naming_Test::~Naming_Test ()
240 MT_Test::MT_Test (CORBA::ORB_ptr orb,
241 PortableServer::POA_ptr poa,
242 int size)
243 : Naming_Test (poa),
244 size_ (size),
245 orb_ (orb),
246 name_service_ior_ ()
251 MT_Test::svc ()
253 // Obtain object reference to the Naming Service (create new stub.)
255 CosNaming::NamingContext_var name_service;
259 CORBA::Object_var name_service_obj =
260 orb_->string_to_object (name_service_ior_.in ());
262 name_service =
263 CosNaming::NamingContext::_narrow (name_service_obj.in ());
265 catch (const CORBA::Exception& ex)
267 ex._tao_print_exception (
268 "Unexpected exception in MT test setup");
269 return -1;
272 if (name_service.in () == 0)
274 ACE_ERROR ((LM_ERROR,
275 "ERROR: Unable to narrow naming IOR to a NamingContext.\n"));
276 return -1;
279 // Bind the object.
282 name_service->bind (test_name_,
283 test_ref_.in ());
284 ACE_DEBUG ((LM_DEBUG,
285 "Bound name OK in thread %t\n"));
287 catch (const CosNaming::NamingContext::AlreadyBound&)
289 ACE_DEBUG ((LM_DEBUG,
290 "Unable to bind in thread %t\n"));
292 catch (const CORBA::Exception& ex)
294 ex._tao_print_exception (
295 "Unexpected exception in MT test bind");
296 return -1;
299 // Resolve the object from the Naming Context.
302 CORBA::Object_var result_obj_ref =
303 name_service->resolve (test_name_);
305 Test_Object_var result_object =
306 Test_Object::_narrow (result_obj_ref.in ());
308 if (!CORBA::is_nil (result_object.in ()))
310 CORBA::Short id = result_object->id ();
312 if (id == CosNaming_Client::OBJ1_ID)
313 ACE_DEBUG ((LM_DEBUG,
314 "Resolved name OK in thread %t\n"));
317 catch (const CosNaming::NamingContext::NotFound&)
319 ACE_DEBUG ((LM_DEBUG,
320 "Unable to resolve in thread %t\n"));
322 catch (const CORBA::Exception& ex)
324 ex._tao_print_exception (
325 "Unexpected exception in MT test resolve");
326 return -1;
329 // Unbind the object from the Naming Context.
332 name_service->unbind (test_name_);
333 ACE_DEBUG ((LM_DEBUG,
334 "Unbound name OK in thread %t\n"));
336 catch (const CosNaming::NamingContext::NotFound&)
338 ACE_DEBUG ((LM_DEBUG,
339 "Unable to unbind in thread %t\n"));
340 return -1;
342 catch (const CORBA::Exception& ex)
344 ex._tao_print_exception (
345 "Unexpected exception in MT test unbind");
346 return -1;
349 return 0;
353 MT_Test::execute (TAO_Naming_Client &root_context)
355 if (CORBA::is_nil (this->orb_.in ()))
356 return -1;
358 // Create data which will be used by all threads.
360 // Dummy object instantiation.
361 My_Test_Object *test_obj_impl =
362 new My_Test_Object (CosNaming_Client::OBJ1_ID);
366 PortableServer::ObjectId_var id_act =
367 this->poa_->activate_object (test_obj_impl);
369 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
371 test_ref_ =
372 Test_Object::_narrow (object_act.in ());
374 test_obj_impl->_remove_ref ();
376 // Get the IOR for the Naming Service. Each thread can use it
377 // in <string_to_object> to create its own stub for the Naming
378 // Service. This 'trick' is necessary, because multiple threads
379 // cannot be using the same stub - bad things happen... This is
380 // just a way to give each thread its own stub.
382 CosNaming::NamingContext_var context =
383 root_context.get_context ();
385 name_service_ior_ =
386 orb_->object_to_string (context.in ());
388 catch (const CORBA::Exception& ex)
390 ex._tao_print_exception (
391 "Unexpected exception while instantiating dummy");
392 return -1;
395 // Create a name for dummy.
396 test_name_.length (1);
397 test_name_[0].id = CORBA::string_dup ("Foo");
399 // Spawn threads, each of which will be executing svc ().
400 int status = this->activate (THR_NEW_LWP | THR_JOINABLE,
401 size_);
403 if (status == -1)
404 return -1;
406 status = this->wait ();
407 return status;
410 Simple_Test::Simple_Test(PortableServer::POA_ptr poa)
411 : Naming_Test (poa)
416 Simple_Test::execute (TAO_Naming_Client &root_context)
420 // Dummy object instantiation.
421 My_Test_Object *test_obj_impl = new My_Test_Object (CosNaming_Client::OBJ1_ID);
422 PortableServer::ObjectId_var id_act =
423 this->poa_->activate_object (test_obj_impl);
425 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
427 Test_Object_var test_obj_ref =
428 Test_Object::_narrow (object_act.in ());
430 // Give ownership of this object to POA.
431 test_obj_impl->_remove_ref ();
433 // Bind an object to the Naming Context.
434 CosNaming::Name test_name;
435 test_name.length (1);
436 test_name[0].id = CORBA::string_dup ("Foo");
437 root_context->bind (test_name,
438 test_obj_ref.in ());
439 ACE_DEBUG ((LM_DEBUG,
440 "Bound name OK\n"));
442 // Resolve the object from the Naming Context.
443 CORBA::Object_var result_obj_ref =
444 root_context->resolve (test_name);
445 Test_Object_var result_object =
446 Test_Object::_narrow (result_obj_ref.in ());
447 if (!CORBA::is_nil (result_object.in ()))
449 CORBA::Short id = result_object->id ();
450 if (id == CosNaming_Client::OBJ1_ID)
451 ACE_DEBUG ((LM_DEBUG, "Resolved name OK\n"));
454 // Unbind the object from the Naming Context.
455 root_context->unbind (test_name);
456 ACE_DEBUG ((LM_DEBUG,
457 "Unbound name OK\n"));
459 catch (const CORBA::Exception& ex)
461 ex._tao_print_exception (
462 "Unexpected exception in Simple test");
463 return -1;
466 return 0;
469 Tree_Test::Tree_Test(PortableServer::POA_ptr poa)
470 : Naming_Test (poa)
475 Tree_Test::execute (TAO_Naming_Client &root_context)
479 // Create a tree of contexts: root->level1->level2. Bind object
480 // foo under context level2.
482 // Bind level1 context under root.
483 CosNaming::Name level1;
484 level1.length (1);
485 level1[0].id = CORBA::string_dup ("level1_context");
486 CosNaming::NamingContext_var level1_context;
487 level1_context = root_context->bind_new_context (level1);
489 // Create a new context.
490 CosNaming::NamingContext_var level2_context;
491 level2_context = root_context->new_context ();
493 // Instantiate a dummy object and bind it under the new context.
494 My_Test_Object *impl1 =
495 new My_Test_Object (CosNaming_Client::OBJ1_ID);
496 PortableServer::ObjectId_var id_act =
497 this->poa_->activate_object (impl1);
499 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
501 Test_Object_var obj1 = Test_Object::_narrow (object_act.in ());
502 impl1->_remove_ref ();
504 CosNaming::Name obj_name;
505 obj_name.length (1);
506 obj_name[0].id = CORBA::string_dup ("foo");
507 level2_context->bind (obj_name, obj1.in ());
509 // Bind the context we just created under level1.
510 CosNaming::Name level2 (level1);
511 level2.length (2);
512 level2[1].id = CORBA::string_dup ("level2_context");
513 root_context->bind_context (level2,
514 level2_context.in ());
516 // Resolve and unbind level1/level2/foo, and bind it back.
517 CosNaming::Name test_name (level2);
518 test_name.length (3);
519 test_name[2].id = obj_name[0].id;
520 CORBA::Object_var result_obj_ref =
521 root_context->resolve (test_name);
522 Test_Object_var result_object =
523 Test_Object::_narrow (result_obj_ref.in ());
525 if (CORBA::is_nil (result_object.in ()))
526 ACE_ERROR_RETURN ((LM_ERROR,
527 "Problems with resolving foo in Tree Test - nil object ref.\n"),
528 -1);
530 CORBA::Short id = result_object->id ();
532 if (id != CosNaming_Client::OBJ1_ID)
533 ACE_ERROR_RETURN ((LM_ERROR,
534 "Problems with resolving foo in Tree Test - wrong id.\n"),
535 -1);
537 // Unbind the object from the Naming Context and bind it back
538 // in.
539 root_context->unbind (test_name);
540 root_context->bind (test_name,
541 obj1.in ());
543 // Create new context and rebind under the name level1/level2.
544 CosNaming::NamingContext_var new_level2_context;
545 new_level2_context =
546 root_context->new_context ();
547 root_context->rebind_context (level2,
548 new_level2_context.in ());
550 // Bind, resolve, rebind, and resolve foo under level1/level2.
551 root_context->bind (test_name,
552 obj1.in ());
553 result_obj_ref = root_context->resolve (test_name);
554 result_object = Test_Object::_narrow (result_obj_ref.in ());
556 CORBA::Short obj_id = result_object->id ();
558 if (CORBA::is_nil (result_object.in ())
559 || !(obj_id == CosNaming_Client::OBJ1_ID))
560 ACE_ERROR_RETURN ((LM_ERROR,
561 "Problems in the Tree Test\n"),
562 -1);
564 My_Test_Object *impl2 =
565 new My_Test_Object (CosNaming_Client::OBJ2_ID);
566 id_act = this->poa_->activate_object (impl2);
568 object_act = this->poa_->id_to_reference (id_act.in ());
570 Test_Object_var obj2 = Test_Object::_narrow (object_act.in ());
572 impl2->_remove_ref ();
574 root_context->rebind (test_name,
575 obj2.in ());
576 result_obj_ref = root_context->resolve (test_name);
577 result_object = Test_Object::_narrow (result_obj_ref.in ());
579 obj_id = result_object->id ();
581 if (CORBA::is_nil (result_object.in ())
582 || !( obj_id == CosNaming_Client::OBJ2_ID))
583 ACE_ERROR_RETURN ((LM_ERROR,
584 "Problems with rebind in Tree Test\n"),
585 -1);
588 catch (const CORBA::Exception& ex)
590 ex._tao_print_exception (
591 "Unexpected exception in Tree test");
592 return -1;
596 ACE_DEBUG ((LM_DEBUG,
597 "All functions work properly\n"));
598 return 0;
601 Exceptions_Test::Exceptions_Test(PortableServer::POA_ptr poa)
602 : Naming_Test (poa)
607 Exceptions_Test::execute (TAO_Naming_Client &root_context)
611 // Set things up.
613 // Create a tree of contexts root->level1->level2.
614 CosNaming::Name context_name;
615 context_name.length (1);
616 context_name[0].id = CORBA::string_dup ("level1_context");
617 CosNaming::NamingContext_var level1_context;
618 level1_context = root_context->bind_new_context (context_name);
619 context_name[0].id = CORBA::string_dup ("level2_context");
620 CosNaming::NamingContext_var level2_context;
621 level2_context = level1_context->bind_new_context (context_name);
623 // Bind a dummy object foo under each context.
624 My_Test_Object *impl = new My_Test_Object;
625 PortableServer::ObjectId_var id_act =
626 this->poa_->activate_object (impl);
628 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
630 Test_Object_var obj = Test_Object::_narrow (object_act.in ());
631 impl->_remove_ref ();
633 CosNaming::Name object_name;
634 object_name.length (1);
635 object_name[0].id = CORBA::string_dup ("foo");
636 root_context->bind (object_name,
637 obj.in ());
638 level1_context->bind (object_name,
639 obj.in ());
640 level2_context->bind (object_name,
641 obj.in ());
643 // Run exceptions tests.
644 invalid_name_test (root_context);
645 already_bound_test (root_context);
646 already_bound_test2 (root_context);
647 not_found_test (root_context);
648 not_found_test2 (root_context);
649 not_found_test3 (root_context);
651 catch (const CORBA::Exception& ex)
653 ex._tao_print_exception (
654 "Unexpected exception in Exceptions test");
655 return -1;
658 return 0;
661 void
662 Exceptions_Test::invalid_name_test (TAO_Naming_Client &root_context)
666 CosNaming::Name test_name;
667 test_name.length (0);
669 root_context->resolve (test_name);
670 ACE_DEBUG ((LM_DEBUG, "Invalid name test failed\n"));
672 catch (const CosNaming::NamingContext::InvalidName&)
674 ACE_DEBUG ((LM_DEBUG,
675 "InvalidName exception works properly\n"));
677 catch (const CORBA::Exception&)
679 ACE_DEBUG ((LM_DEBUG, "Invalid name test failed\n"));
680 throw;
684 void
685 Exceptions_Test::already_bound_test (TAO_Naming_Client &root_context)
689 CosNaming::Name test_name;
690 test_name.length (1);
691 test_name[0].id = CORBA::string_dup ("foo");
692 My_Test_Object *impl = new My_Test_Object;
693 PortableServer::ObjectId_var id_act =
694 this->poa_->activate_object (impl);
696 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
698 Test_Object_var obj = Test_Object::_narrow (object_act.in ());
699 impl->_remove_ref ();
701 root_context->bind (test_name,
702 obj.in ());
703 ACE_DEBUG ((LM_DEBUG, "Already bound (case 1) test failed\n"));
705 catch (const CosNaming::NamingContext::AlreadyBound&)
707 ACE_DEBUG ((LM_DEBUG,
708 "AlreadyBound exception (case 1) works properly\n"));
710 catch (const CORBA::Exception&)
712 ACE_DEBUG ((LM_DEBUG, "Already bound (case 1) test failed\n"));
713 throw;
718 void
719 Exceptions_Test::already_bound_test2 (TAO_Naming_Client &root_context)
723 CosNaming::Name test_name;
724 test_name.length (2);
725 test_name[0].id = CORBA::string_dup ("level1_context");
726 test_name[1].id = CORBA::string_dup ("foo");
727 My_Test_Object *impl = new My_Test_Object;
728 PortableServer::ObjectId_var id_act =
729 this->poa_->activate_object (impl);
731 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
733 Test_Object_var obj = Test_Object::_narrow (object_act.in ());
734 impl->_remove_ref ();
736 root_context->bind (test_name,
737 obj.in ());
738 ACE_DEBUG ((LM_DEBUG, "Already bound (case 2) test failed\n"));
740 catch (const CosNaming::NamingContext::AlreadyBound&)
742 ACE_DEBUG ((LM_DEBUG,
743 "AlreadyBound exception (case 2) works properly\n"));
745 catch (const CORBA::Exception&)
747 ACE_DEBUG ((LM_DEBUG, "Already bound (case 2) test failed\n"));
748 throw;
752 void
753 Exceptions_Test::not_found_test (TAO_Naming_Client &root_context)
757 CosNaming::Name test_name;
758 test_name.length (3);
759 test_name[0].id = CORBA::string_dup ("level1_context");
760 test_name[1].id = CORBA::string_dup ("level2_context");
761 test_name[2].id = CORBA::string_dup ("bar");
763 root_context->unbind (test_name);
764 ACE_DEBUG ((LM_DEBUG, "Not found test failed\n"));
766 catch (const CosNaming::NamingContext::NotFound& ex)
768 if (ex.why == CosNaming::NamingContext::missing_node &&
769 ex.rest_of_name.length () == 1
770 && ACE_OS::strcmp (ex.rest_of_name[0].id.in (),
771 "bar") == 0)
772 ACE_DEBUG ((LM_DEBUG,
773 "NotFound exception (case 1) works properly\n"));
774 else
775 ACE_DEBUG ((LM_DEBUG,
776 "NotFound exception (case 1)"
777 " - parameters aren't set correctly\n"));
779 catch (const CORBA::Exception&)
781 ACE_DEBUG ((LM_DEBUG, "Not found test failed\n"));
782 throw;
786 void
787 Exceptions_Test::not_found_test2 (TAO_Naming_Client &root_context)
791 CosNaming::Name test_name;
792 test_name.length (3);
793 test_name[0].id = CORBA::string_dup ("level1_context");
794 test_name[1].id = CORBA::string_dup ("level3_context");
795 test_name[2].id = CORBA::string_dup ("foo");
797 root_context->unbind (test_name);
798 ACE_DEBUG ((LM_DEBUG, "Unbind test failed\n"));
800 catch (const CosNaming::NamingContext::NotFound& ex)
802 if (ex.why == CosNaming::NamingContext::missing_node
803 && ex.rest_of_name.length () == 2
804 && ACE_OS::strcmp (ex.rest_of_name[0].id.in (),
805 "level3_context") == 0
806 && ACE_OS::strcmp (ex.rest_of_name[1].id.in (),
807 "foo") == 0)
808 ACE_DEBUG ((LM_DEBUG,
809 "NotFound exception (case 2) works properly\n"));
810 else
811 ACE_DEBUG ((LM_DEBUG,
812 "NotFound exception (case 2)"
813 " - parameters aren't set correctly\n"));
815 catch (const CORBA::Exception&)
817 ACE_DEBUG ((LM_DEBUG, "Unbind test failed\n"));
818 throw;
823 void
824 Exceptions_Test::not_found_test3 (TAO_Naming_Client &root_context)
828 CosNaming::Name test_name;
829 test_name.length (3);
830 test_name[0].id = CORBA::string_dup ("level1_context");
831 test_name[1].id = CORBA::string_dup ("foo");
832 test_name[2].id = CORBA::string_dup ("foo");
834 root_context->unbind (test_name);
835 ACE_DEBUG ((LM_DEBUG, "Not found (case 3) test failed - no exception was thrown\n"));
837 catch (const CosNaming::NamingContext::NotFound& ex)
839 if (ex.why == CosNaming::NamingContext::not_context
840 && ex.rest_of_name.length () == 2
841 && ACE_OS::strcmp (ex.rest_of_name[0].id.in (),
842 "foo") == 0
843 && ACE_OS::strcmp (ex.rest_of_name[1].id.in (),
844 "foo") == 0)
845 ACE_DEBUG ((LM_DEBUG,
846 "NotFound exception (case 3) works properly\n"));
847 else
848 ACE_DEBUG ((LM_DEBUG,
849 "NotFound exception (case 3)"
850 " - parameters aren't set correctly\n"));
852 catch (const CORBA::Exception&)
854 ACE_DEBUG ((LM_DEBUG, "Not found (case 3) test failed\n"));
855 throw;
859 Iterator_Test::Iterator_Test(PortableServer::POA_ptr poa)
860 : Naming_Test (poa)
865 Iterator_Test::execute (TAO_Naming_Client &root_context)
869 // Instantiate four dummy objects.
870 My_Test_Object *impl = new My_Test_Object;
871 PortableServer::ObjectId_var id_act =
872 this->poa_->activate_object (impl);
874 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
876 Test_Object_var obj = Test_Object::_narrow (object_act.in ());
877 impl->_remove_ref ();
879 // Bind objects to the naming context.
880 CosNaming::Name name1;
881 name1.length (1);
882 name1[0].id = CORBA::string_dup ("foo1");
883 CosNaming::Name name2;
884 name2.length (1);
885 name2[0].id = CORBA::string_dup ("foo2");
886 CosNaming::Name name3;
887 name3.length (1);
888 name3[0].id = CORBA::string_dup ("foo3");
889 CosNaming::Name name4;
890 name4.length (1);
891 name4[0].id = CORBA::string_dup ("foo4");
892 root_context->bind (name1,
893 obj.in ());
894 root_context->bind (name2,
895 obj.in ());
896 root_context->bind (name3,
897 obj.in ());
898 root_context->bind (name4,
899 obj.in ());
901 // List the content of the Naming Context.
902 CosNaming::BindingIterator_var iter;
903 CosNaming::BindingList_var bindings_list;
904 root_context->list (1,
905 bindings_list.out (),
906 iter.out ());
907 if (CORBA::is_nil (iter.in ())
908 || bindings_list->length () != 1
909 || bindings_list[0u].binding_type != CosNaming::nobject)
910 ACE_ERROR_RETURN ((LM_ERROR,
911 "Iterator_Test -> CosNaming::list does not function properly. \
912 Should have returned one CosNaming::nobject element in bindings_list. Length = %d, \
913 Binding Type = %d\n", bindings_list->length (), bindings_list[0u].binding_type),
914 -1);
915 ACE_DEBUG ((LM_DEBUG,
916 "First binding: %C\n",
917 bindings_list[0u].binding_name[0u].id.in ()));
919 // Invoke operations on the iterator.
920 CosNaming::Binding_var binding;
921 iter->next_one (binding.out ());
922 if (binding->binding_type != CosNaming::nobject)
923 ACE_ERROR_RETURN ((LM_ERROR,
924 "CosNaming::next_one does not function properly\n"),
925 -1);
926 ACE_DEBUG ((LM_DEBUG,
927 "Second binding: %C\n",
928 binding->binding_name[0].id.in ()));
930 iter->next_n (2, bindings_list.out ());
931 if (bindings_list->length () != 2
932 || bindings_list[0u].binding_type != CosNaming::nobject
933 || bindings_list[1u].binding_type != CosNaming::nobject)
934 ACE_ERROR_RETURN ((LM_ERROR,
935 "CosNaming::BindingIterator does not function properly\n"),
936 -1);
937 ACE_DEBUG ((LM_DEBUG,
938 "Third binding: %C\n"
939 "Fourth binding: %C\n",
940 bindings_list[0u].binding_name[0].id.in (),
941 bindings_list[1u].binding_name[0].id.in ()));
943 // We already iterated over all the bindings, so the following
944 // should return false.
945 CORBA::Boolean result = iter->next_one (binding.out ());
946 if (result)
947 ACE_ERROR_RETURN ((LM_ERROR,
948 "CosNaming::BindingIterator does not function properly\n"),
949 -1);
950 iter->destroy ();
952 catch (const CORBA::Exception& ex)
954 ex._tao_print_exception (
955 "Unexpected exception in Iterator test");
956 return -1;
959 return 0;
962 Destroy_Test::Destroy_Test(PortableServer::POA_ptr poa)
963 : Naming_Test (poa)
968 Destroy_Test::execute (TAO_Naming_Client &root_context)
972 // Create a context and bind an object under it.
974 CosNaming::NamingContext_var my_context;
975 my_context = root_context->new_context ();
977 // Bind a dummy object foo under my_context.
978 My_Test_Object *impl = new My_Test_Object;
979 PortableServer::ObjectId_var id_act =
980 this->poa_->activate_object (impl);
982 CORBA::Object_var object_act = this->poa_->id_to_reference (id_act.in ());
984 Test_Object_var obj = Test_Object::_narrow (object_act.in ());
985 impl->_remove_ref ();
987 CosNaming::Name object_name;
988 object_name.length (1);
989 object_name[0].id = CORBA::string_dup ("foo");
990 my_context->bind (object_name,
991 obj.in ());
993 // Do the testing.
994 not_empty_test (my_context);
996 my_context->unbind (object_name);
997 my_context->destroy ();
999 not_exist_test (my_context);
1002 catch (const CORBA::Exception& ex)
1004 ex._tao_print_exception (
1005 "Unexpected exception in Destroy test");
1006 return -1;
1009 return 0;
1012 void
1013 Destroy_Test::not_empty_test (CosNaming::NamingContext_var &ref)
1017 ref->destroy ();
1020 catch (const CosNaming::NamingContext::NotEmpty&)
1022 ACE_DEBUG ((LM_DEBUG,
1023 "NotEmpty exception works properly\n"));
1027 void
1028 Destroy_Test::not_exist_test (CosNaming::NamingContext_var &ref)
1032 ref->destroy ();
1035 catch (const CORBA::OBJECT_NOT_EXIST&)
1037 ACE_DEBUG ((LM_DEBUG,
1038 "Destroy works properly\n"));
1042 Persistent_Test_Begin::Persistent_Test_Begin (CORBA::ORB_ptr orb,
1043 PortableServer::POA_ptr poa,
1044 FILE * ior_output_file)
1045 : Naming_Test (poa),
1046 orb_ (orb),
1047 file_ (ior_output_file)
1051 Persistent_Test_Begin::~Persistent_Test_Begin ()
1056 Persistent_Test_Begin::execute (TAO_Naming_Client &root_context)
1060 // Create a name structure we will reuse.
1061 CosNaming::Name test_name;
1062 test_name.length (1);
1063 test_name[0].id = CORBA::string_dup ("level1");
1065 // Create and bind a naming context under the <root> context.
1066 CosNaming::NamingContext_var level1_context =
1067 root_context->bind_new_context (test_name);
1069 // Create and bind a naming context under <level1> context.
1070 test_name[0].id = CORBA::string_dup ("level2");
1071 CosNaming::NamingContext_var level2_context =
1072 level1_context->bind_new_context (test_name);
1074 // Log the ior of <level1_context> for use by <Persistent_Test_End>.
1075 CORBA::String_var ior =
1076 orb_->object_to_string (level1_context.in ());
1078 ACE_OS::fprintf (this->file_,
1079 "%s",
1080 ior.in ());
1081 ACE_OS::fclose (this->file_);
1083 ACE_DEBUG ((LM_DEBUG, "Persistent Naming test (part 1) OK.\n"));
1085 catch (const CORBA::Exception& ex)
1087 ex._tao_print_exception (
1088 "Unexpected exception in Persistent Test (part 1)");
1089 return -1;
1092 return 0;
1095 Persistent_Test_End::Persistent_Test_End (CORBA::ORB_ptr orb,
1096 PortableServer::POA_ptr poa,
1097 const ACE_TCHAR *ior)
1098 : Naming_Test (poa),
1099 orb_ (orb),
1100 ior_ (ior)
1104 Persistent_Test_End::~Persistent_Test_End ()
1109 Persistent_Test_End::execute (TAO_Naming_Client &root_context)
1113 // Create a name structure we will reuse.
1114 CosNaming::Name test_name;
1115 test_name.length (1);
1116 test_name[0].id = CORBA::string_dup ("level2");
1118 // Convert stringified ior we got from <Persistent_Test_Begin>
1119 // for <level1> Naming Context to Naming Context reference.
1120 CORBA::Object_var obj =
1121 orb_->string_to_object (ior_);
1123 CosNaming::NamingContext_var level1_context =
1124 CosNaming::NamingContext::_narrow (obj.in ());
1126 if (CORBA::is_nil (level1_context.in ()))
1127 ACE_ERROR_RETURN ((LM_ERROR,
1128 "Cannot narrow object to Naming Context\n"),
1129 -1);
1131 // Resolve for <level2> context through the persistent ior we
1132 // got from part 1 of this test.
1133 obj = level1_context->resolve (test_name);
1135 // Now, resolve for <level2> context using the <root> context
1136 // reference which we obtained through <resolve_initial_references>.
1137 test_name.length (2);
1138 test_name[0].id = CORBA::string_dup ("level1");
1139 test_name[1].id = CORBA::string_dup ("level2");
1140 CORBA::Object_var obj2 =
1141 root_context->resolve (test_name);
1143 // Make sure we got the same answer through both methods.
1144 if (obj2->_is_equivalent (obj.in ()))
1145 ACE_DEBUG ((LM_DEBUG, "Persistent Naming test (part 2) OK.\n"));
1147 catch (const CORBA::Exception& ex)
1149 ex._tao_print_exception (
1150 "Unexpected exception in Persistent Test (part 2)");
1151 return -1;
1154 return 0;
1157 Persistent_List_Test::Persistent_List_Test (CORBA::ORB_ptr orb,
1158 PortableServer::POA_ptr poa)
1159 : Naming_Test (poa),
1160 orb_ (orb)
1164 Persistent_List_Test::~Persistent_List_Test ()
1169 * @brief Verify functionality of a Naming Service's list() iterator
1170 * when dealing with a flat-file persistent NS.
1172 * Uses information stored in the NS in prior tests, then adds its
1173 * own bindings and runs additional tests iterating through them.
1176 Persistent_List_Test::execute (TAO_Naming_Client &root_context)
1180 // Create a name structure we will reuse.
1181 CosNaming::Name test_name;
1182 test_name.length (1);
1183 test_name[0].id = CORBA::string_dup ("level1");
1184 CORBA::Object_var obj1 =
1185 root_context->resolve (test_name);
1187 CosNaming::NamingContext_var level1_context =
1188 CosNaming::NamingContext::_narrow (obj1.in ());
1190 // Resolve for <level2> context through level1
1191 test_name.length (1);
1192 test_name[0].id = CORBA::string_dup ("level2");
1193 CORBA::Object_var obj2 =
1194 level1_context->resolve (test_name);
1196 CosNaming::NamingContext_var level2_context =
1197 CosNaming::NamingContext::_narrow (obj2.in ());
1199 // List the content of the Naming Context.
1200 CosNaming::BindingIterator_var iter;
1201 CosNaming::BindingList_var bindings_list;
1202 CosNaming::Binding_var binding;
1203 unsigned int objects_found = 0;
1204 unsigned int contexts_found = 0;
1206 root_context->list (0,
1207 bindings_list.out (),
1208 iter.out ());
1209 if (CORBA::is_nil (iter.in ())
1210 || bindings_list->length () != 0)
1211 ACE_ERROR_RETURN ((LM_ERROR,
1212 "Persistent_List_Test -> CosNaming::list does not function properly. Should have returned an iterator.\n"),
1213 -1);
1215 while (iter->next_one (binding.out ()))
1217 if (binding->binding_type == CosNaming::nobject)
1218 ++objects_found;
1219 if (binding->binding_type == CosNaming::ncontext)
1220 ++contexts_found;
1223 if (objects_found != 0 && contexts_found != 1)
1225 ACE_ERROR_RETURN ((LM_ERROR,
1226 "CosNaming::next_one does not function properly\n"),
1227 -1);
1230 iter->destroy ();
1232 contexts_found = 0;
1234 // Instantiate four dummy objects.
1235 My_Test_Object *impl = new My_Test_Object;
1236 Test_Object_var obj = impl->_this ();
1237 impl->_remove_ref ();
1239 // Bind objects to the naming context.
1240 CosNaming::Name name1;
1241 name1.length (1);
1242 name1[0].id = CORBA::string_dup ("foo1");
1243 CosNaming::Name name2;
1244 name2.length (1);
1245 name2[0].id = CORBA::string_dup ("foo2");
1246 CosNaming::Name name3;
1247 name3.length (1);
1248 name3[0].id = CORBA::string_dup ("foo3");
1249 CosNaming::Name name4;
1250 name4.length (1);
1251 name4[0].id = CORBA::string_dup ("foo4");
1253 level1_context->bind (name1,
1254 obj.in ());
1255 level1_context->bind (name2,
1256 obj.in ());
1257 level1_context->bind (name3,
1258 obj.in ());
1259 level1_context->bind (name4,
1260 obj.in ());
1262 level1_context->list (0,
1263 bindings_list.out (),
1264 iter.out ());
1265 if (CORBA::is_nil (iter.in ())
1266 || bindings_list->length () != 0)
1267 ACE_ERROR_RETURN ((LM_ERROR,
1268 "Persistent_List_Test -> CosNaming::list does not function properly. Should have returned an iterator.\n"),
1269 -1);
1271 while (iter->next_one (binding.out ()))
1273 if (binding->binding_type == CosNaming::nobject)
1274 ++objects_found;
1275 if (binding->binding_type == CosNaming::ncontext)
1276 ++contexts_found;
1279 if (objects_found != 4 && contexts_found != 1)
1281 ACE_ERROR_RETURN ((LM_ERROR,
1282 "CosNaming::next_one does not function properly\n"),
1283 -1);
1286 iter->destroy ();
1288 ACE_DEBUG ((LM_DEBUG, "Persistent Naming test (part 3) OK.\n"));
1290 catch (const CORBA::Exception& ex)
1292 ex._tao_print_exception (
1293 "Unexpected exception in Persistent Test (part 3)");
1294 return -1;
1297 return 0;
1300 // This function runs the test.
1303 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
1305 CosNaming_Client cosnaming_client;
1307 if (cosnaming_client.init (argc, argv) == -1)
1308 return 1;
1310 return cosnaming_client.run ();