More tests update
[ACE_TAO.git] / TAO / tests / POA / Default_Servant / Default_Servant.cpp
blobf3a54ca0129076c8be7fb0b45209780e735faec1
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[])
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");
145 // Narrow to POA.
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);
155 policies.length (3);
157 // Request Processing Policy.
158 policies[0] =
159 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
161 // Id Uniqueness Policy.
162 policies[1] =
163 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
165 // Servant Retention Policy.
166 policies[2] =
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 (),
173 poa_manager.in (),
174 policies);
176 // Destroy policies.
177 for (CORBA::ULong i = 0;
178 i < policies.length ();
179 ++i)
181 CORBA::Policy_ptr policy = policies[i];
182 policy->destroy ();
185 // Activate POA manager.
186 poa_manager->activate ();
188 test_reference_to_servant_active_object(root_poa.in ());
190 // Test servant.
191 test_i test;
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);
202 expected_refcount++;
204 // Create dummy id.
205 PortableServer::ObjectId_var id =
206 PortableServer::string_to_ObjectId ("id");
208 // Create dummy object.
209 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 ());
215 expected_refcount++;
217 // Assert correctness.
218 ACE_ASSERT (&test == servant.in());
220 // Invoke reference_to_servant(). Should retrieve default servant.
221 servant =
222 default_servant_poa->reference_to_servant (object.in ());
223 expected_refcount++;
225 // Assert correctness.
226 ACE_ASSERT (&test == servant.in());
228 // Report success.
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);
239 // Destroy the ORB.
240 orb->destroy ();
242 catch (const CORBA::Exception& ex)
244 ex._tao_print_exception ("Exception caught");
245 return -1;
248 return 0;