2 //=============================================================================
4 * @file Default_Servant.cpp
6 * This program tests the behavior of POA::id_to_servant() and
7 * POA::reference_to_servant() with the use of default servants.
9 * @author Irfan Pyarali
11 //=============================================================================
15 #include "ace/SString.h"
16 #include "tao/PortableServer/ServantManagerC.h"
18 class test_i
: public POA_test
23 test_get_servant_manager (PortableServer::POA_ptr poa
)
28 // Getting the servant manager should give a wrong policy exception
30 PortableServer::ServantManager_ptr servant_manager
=
31 poa
->get_servant_manager ();
33 ACE_UNUSED_ARG (servant_manager
);
35 catch (const PortableServer::POA::WrongPolicy
& )
39 catch (const CORBA::Exception
&)
46 "(%t) ERROR, get servant manager failed, should give an exception\n"));
51 test_set_servant_manager (PortableServer::POA_ptr poa
)
56 // Setting the servant manager should give a wrong policy exception
58 poa
->set_servant_manager (PortableServer::ServantManager::_nil());
60 catch (const PortableServer::POA::WrongPolicy
& )
64 catch (const CORBA::Exception
&)
71 "(%t) ERROR, set servant manager failed, should give an exception\n"));
76 test_get_servant_with_no_set (PortableServer::POA_ptr poa
)
81 // Getting the default servant without one set whould give a NoServant
83 PortableServer::Servant servant
=
86 ACE_UNUSED_ARG (servant
);
88 catch (const PortableServer::POA::NoServant
& )
92 catch (const CORBA::Exception
&)
99 "(%t) ERROR, get servant without one set failed\n"));
104 test_reference_to_servant_active_object(PortableServer::POA_ptr root_poa
)
107 CORBA::ULong expected_refcount
= 1;
109 PortableServer::ObjectId_var id
=
110 root_poa
->activate_object (&test
);
113 CORBA::Object_var object
=
114 root_poa
->id_to_reference (id
.in ());
116 PortableServer::ServantBase_var servant
=
117 root_poa
->reference_to_servant (object
.in ());
120 root_poa
->deactivate_object (id
.in ());
123 CORBA::ULong refcount
=
124 test
._refcount_value ();
126 ACE_UNUSED_ARG (refcount
);
127 ACE_UNUSED_ARG (expected_refcount
);
128 ACE_ASSERT (expected_refcount
== refcount
);
133 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
137 // Initialize the ORB.
138 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
140 // Obtain the RootPOA.
141 CORBA::Object_var object
=
142 orb
->resolve_initial_references ("RootPOA");
145 PortableServer::POA_var root_poa
=
146 PortableServer::POA::_narrow (object
.in ());
148 // Get the POAManager of the RootPOA.
149 PortableServer::POAManager_var poa_manager
=
150 root_poa
->the_POAManager ();
152 // Policies for the new POA.
153 CORBA::PolicyList
policies (3);
156 // Request Processing Policy.
158 root_poa
->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT
);
160 // Id Uniqueness Policy.
162 root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
164 // Servant Retention Policy.
166 root_poa
->create_servant_retention_policy (PortableServer::NON_RETAIN
);
168 // Create POA to host default servant.
169 ACE_CString name
= "Default Servant";
170 PortableServer::POA_var default_servant_poa
=
171 root_poa
->create_POA (name
.c_str (),
176 for (CORBA::ULong i
= 0;
177 i
< policies
.length ();
180 CORBA::Policy_ptr policy
= policies
[i
];
184 // Activate POA manager.
185 poa_manager
->activate ();
187 test_reference_to_servant_active_object(root_poa
.in ());
191 CORBA::ULong expected_refcount
= 1;
193 (void) test_get_servant_with_no_set (default_servant_poa
.in());
195 (void) test_get_servant_manager (default_servant_poa
.in());
197 (void) test_set_servant_manager (default_servant_poa
.in());
199 // Register default servant.
200 default_servant_poa
->set_servant (&test
);
204 PortableServer::ObjectId_var id
=
205 PortableServer::string_to_ObjectId ("id");
207 // Create dummy object.
209 default_servant_poa
->create_reference ("IDL:test:1.0");
211 // Invoke id_to_servant(). Should retrieve default servant.
212 PortableServer::ServantBase_var servant
=
213 default_servant_poa
->id_to_servant (id
.in ());
216 // Assert correctness.
217 ACE_ASSERT (&test
== servant
.in());
219 // Invoke reference_to_servant(). Should retrieve default servant.
221 default_servant_poa
->reference_to_servant (object
.in ());
224 // Assert correctness.
225 ACE_ASSERT (&test
== servant
.in());
228 ACE_DEBUG ((LM_DEBUG
,
229 "Default_Servant test successful\n"));
231 CORBA::ULong refcount
=
232 test
._refcount_value ();
234 ACE_UNUSED_ARG (refcount
);
235 ACE_UNUSED_ARG (expected_refcount
);
236 ACE_ASSERT (expected_refcount
== refcount
);
241 catch (const CORBA::Exception
& ex
)
243 ex
._tao_print_exception ("Exception caught");