Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / POA / Default_Servant / Default_Servant.cpp
blobb1054c535d58b1d1e32e8e7c57e1fda091fc8cdc
2 //=============================================================================
3 /**
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 //=============================================================================
14 #include "testS.h"
15 #include "ace/SString.h"
16 #include "tao/PortableServer/ServantManagerC.h"
18 class test_i : public POA_test
22 void
23 test_get_servant_manager (PortableServer::POA_ptr poa)
25 bool succeed = false;
26 try
28 // Getting the servant manager should give a wrong policy exception
29 // exception
30 PortableServer::ServantManager_ptr servant_manager =
31 poa->get_servant_manager ();
33 ACE_UNUSED_ARG (servant_manager);
35 catch (const PortableServer::POA::WrongPolicy& )
37 succeed = true;
39 catch (const CORBA::Exception&)
43 if (!succeed)
45 ACE_ERROR ((LM_ERROR,
46 "(%t) ERROR, get servant manager failed, should give an exception\n"));
50 void
51 test_set_servant_manager (PortableServer::POA_ptr poa)
53 bool succeed = false;
54 try
56 // Setting the servant manager should give a wrong policy exception
57 // exception
58 poa->set_servant_manager (PortableServer::ServantManager::_nil());
60 catch (const PortableServer::POA::WrongPolicy& )
62 succeed = true;
64 catch (const CORBA::Exception&)
68 if (!succeed)
70 ACE_ERROR ((LM_ERROR,
71 "(%t) ERROR, set servant manager failed, should give an exception\n"));
75 void
76 test_get_servant_with_no_set (PortableServer::POA_ptr poa)
78 bool succeed = false;
79 try
81 // Getting the default servant without one set whould give a NoServant
82 // exception
83 PortableServer::Servant servant =
84 poa->get_servant ();
86 ACE_UNUSED_ARG (servant);
88 catch (const PortableServer::POA::NoServant& )
90 succeed = true;
92 catch (const CORBA::Exception&)
96 if (!succeed)
98 ACE_ERROR ((LM_ERROR,
99 "(%t) ERROR, get servant without one set failed\n"));
103 void
104 test_reference_to_servant_active_object(PortableServer::POA_ptr root_poa)
106 test_i test;
107 CORBA::ULong expected_refcount = 1;
109 PortableServer::ObjectId_var id =
110 root_poa->activate_object (&test);
111 expected_refcount++;
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 ());
118 ++expected_refcount;
120 root_poa->deactivate_object (id.in ());
121 --expected_refcount;
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");
144 // Narrow to POA.
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);
154 policies.length (3);
156 // Request Processing Policy.
157 policies[0] =
158 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
160 // Id Uniqueness Policy.
161 policies[1] =
162 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
164 // Servant Retention Policy.
165 policies[2] =
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 (),
172 poa_manager.in (),
173 policies);
175 // Destroy policies.
176 for (CORBA::ULong i = 0;
177 i < policies.length ();
178 ++i)
180 CORBA::Policy_ptr policy = policies[i];
181 policy->destroy ();
184 // Activate POA manager.
185 poa_manager->activate ();
187 test_reference_to_servant_active_object(root_poa.in ());
189 // Test servant.
190 test_i test;
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);
201 expected_refcount++;
203 // Create dummy id.
204 PortableServer::ObjectId_var id =
205 PortableServer::string_to_ObjectId ("id");
207 // Create dummy object.
208 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 ());
214 expected_refcount++;
216 // Assert correctness.
217 ACE_ASSERT (&test == servant.in());
219 // Invoke reference_to_servant(). Should retrieve default servant.
220 servant =
221 default_servant_poa->reference_to_servant (object.in ());
222 expected_refcount++;
224 // Assert correctness.
225 ACE_ASSERT (&test == servant.in());
227 // Report success.
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);
238 // Destroy the ORB.
239 orb->destroy ();
241 catch (const CORBA::Exception& ex)
243 ex._tao_print_exception ("Exception caught");
244 return -1;
247 return 0;