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
[])
138 // Initialize the ORB.
139 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
141 // Obtain the RootPOA.
142 CORBA::Object_var object
=
143 orb
->resolve_initial_references ("RootPOA");
146 PortableServer::POA_var root_poa
=
147 PortableServer::POA::_narrow (object
.in ());
149 // Get the POAManager of the RootPOA.
150 PortableServer::POAManager_var poa_manager
=
151 root_poa
->the_POAManager ();
153 // Policies for the new POA.
154 CORBA::PolicyList
policies (3);
157 // Request Processing Policy.
159 root_poa
->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT
);
161 // Id Uniqueness Policy.
163 root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
165 // Servant Retention Policy.
167 root_poa
->create_servant_retention_policy (PortableServer::NON_RETAIN
);
169 // Create POA to host default servant.
170 ACE_CString name
= "Default Servant";
171 PortableServer::POA_var default_servant_poa
=
172 root_poa
->create_POA (name
.c_str (),
177 for (CORBA::ULong i
= 0;
178 i
< policies
.length ();
181 CORBA::Policy_ptr policy
= policies
[i
];
185 // Activate POA manager.
186 poa_manager
->activate ();
188 test_reference_to_servant_active_object(root_poa
.in ());
192 CORBA::ULong expected_refcount
= 1;
194 (void) test_get_servant_with_no_set (default_servant_poa
.in());
196 (void) test_get_servant_manager (default_servant_poa
.in());
198 (void) test_set_servant_manager (default_servant_poa
.in());
200 // Register default servant.
201 default_servant_poa
->set_servant (&test
);
205 PortableServer::ObjectId_var id
=
206 PortableServer::string_to_ObjectId ("id");
208 // Create dummy object.
210 default_servant_poa
->create_reference ("IDL:test:1.0");
212 // Invoke id_to_servant(). Should retrieve default servant.
213 PortableServer::ServantBase_var servant
=
214 default_servant_poa
->id_to_servant (id
.in ());
217 // Assert correctness.
218 ACE_ASSERT (&test
== servant
.in());
220 // Invoke reference_to_servant(). Should retrieve default servant.
222 default_servant_poa
->reference_to_servant (object
.in ());
225 // Assert correctness.
226 ACE_ASSERT (&test
== servant
.in());
229 ACE_DEBUG ((LM_DEBUG
,
230 "Default_Servant test successful\n"));
232 CORBA::ULong refcount
=
233 test
._refcount_value ();
235 ACE_UNUSED_ARG (refcount
);
236 ACE_UNUSED_ARG (expected_refcount
);
237 ACE_ASSERT (expected_refcount
== refcount
);
242 catch (const CORBA::Exception
& ex
)
244 ex
._tao_print_exception ("Exception caught");