Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Non_Retain_System_Id / test.cpp
blob7a22cc244e7871211ebb729b52f9fbd5ebc99ec6
2 //=============================================================================
3 /**
4 * @file test.cpp
6 * This program verifies that a POA using the NON_RETAIN policy creates
7 * unique system IDs, also that objects created with a USER id policy is
8 * not given a bo
10 * @author Phil Mesnier
12 //=============================================================================
15 #include "testS.h"
16 #include "ace/SString.h"
17 #include "tao/PortableServer/POA_Current.h"
19 class ID_Check_i : public POA_ID_Check
21 public:
22 ID_Check_i (int num);
23 ~ID_Check_i ();
25 void set_id (int index, PortableServer::ObjectId_var oid);
27 CORBA::Boolean check_servant_id (CORBA::Short index);
29 private:
30 int count_;
31 PortableServer::ObjectId_var * oids_;
32 PortableServer::Current_var current_;
35 ID_Check_i::ID_Check_i (int num)
36 : count_ (num),
37 oids_ (0),
38 current_ ()
40 int argc = 0;
41 char **argv = 0;
42 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
43 CORBA::Object_var obj = orb->resolve_initial_references ("POACurrent");
44 current_ = PortableServer::Current::_narrow (obj.in());
46 oids_ = new PortableServer::ObjectId_var[num];
49 ID_Check_i::~ID_Check_i ()
51 delete [] oids_;
54 void
55 ID_Check_i::set_id (int index, PortableServer::ObjectId_var oid)
57 this->oids_[index] = oid;
60 CORBA::Boolean
61 ID_Check_i::check_servant_id (CORBA::Short index)
63 if (index < 0 || index >= this->count_)
65 ACE_ERROR ((LM_ERROR,
66 ACE_TEXT ("check_servant_id: requested index outside of range\n")));
67 return false;
69 PortableServer::ObjectId_var oid = this->current_->get_object_id();
71 ACE_DEBUG ((LM_DEBUG,
72 ACE_TEXT ("check_servant_id: index = %d current objectid ")
73 ACE_TEXT ("len %d octets: "),
74 index, oid->length()));
75 for (size_t i = 0; i < oid->length () && i < 16; i++)
76 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("%02x "), oid[i]));
77 ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("\n")));
79 if (oid->length() != this->oids_[index]->length())
80 return false;
81 for (size_t i = 0; i < oid->length(); i++)
83 if (oid[i] != this->oids_[index][i])
85 ACE_ERROR ((LM_ERROR,
86 ACE_TEXT ("check_servant_id: id[%d] mismatch at position %d, ")
87 ACE_TEXT ("got %d, expected %d\n"),
88 index, i, oid[i], this->oids_[index][i]));
89 return false;
92 return true;
95 int
96 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
98 try
100 // Initialize the ORB.
101 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
103 // Obtain the RootPOA.
104 CORBA::Object_var object =
105 orb->resolve_initial_references ("RootPOA");
107 // Narrow to POA.
108 PortableServer::POA_var root_poa =
109 PortableServer::POA::_narrow (object.in ());
111 // Get the POAManager of the RootPOA.
112 PortableServer::POAManager_var poa_manager =
113 root_poa->the_POAManager ();
115 // Policies for the new POA.
116 CORBA::PolicyList policies (4);
117 policies.length (3);
119 // Request Processing Policy.
120 policies[0] =
121 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
123 // Id Uniqueness Policy.
124 policies[1] =
125 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
127 // Servant Retention Policy.
128 policies[2] =
129 root_poa->create_servant_retention_policy (PortableServer::NON_RETAIN);
131 // Create POA to host default servant.
132 ACE_CString name = "System IDs";
133 PortableServer::POA_var sys_id_poa =
134 root_poa->create_POA (name.c_str (),
135 poa_manager.in (),
136 policies);
138 policies.length (4);
139 policies[3] =
140 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
142 name = "User IDs";
143 PortableServer::POA_var user_id_poa =
144 root_poa->create_POA (name.c_str (),
145 poa_manager.in (),
146 policies);
148 // Destroy policies.
149 for (CORBA::ULong i = 0; i < policies.length (); ++i)
151 CORBA::Policy_ptr policy = policies[i];
152 policy->destroy ();
155 int objcount = 30;
157 ID_Check_i *servant = new ID_Check_i (objcount);
158 sys_id_poa->set_servant (servant);
159 user_id_poa->set_servant (servant);
161 // Activate POA manager.
162 poa_manager->activate ();
164 // Create object array.
165 ID_Check_var *refs = new ID_Check_var[objcount];
166 PortableServer::ObjectId_var obj_id;
167 char *user_id_str = new char[10];
169 for (int i = 0; i < objcount; i++)
171 if (i < objcount / 2)
173 object = sys_id_poa->create_reference ("IDL:ID_Check:1.0");
174 refs[i] = ID_Check::_narrow (object.in());
175 obj_id = sys_id_poa->reference_to_id (object.in());
176 servant->set_id (i, obj_id);
178 else
180 ACE_OS::sprintf (user_id_str, "id %d", i);
181 obj_id = PortableServer::string_to_ObjectId (user_id_str);
182 object = user_id_poa->create_reference_with_id (obj_id.in(),
183 "IDL:ID_Check:1.0");
184 refs[i] = ID_Check::_narrow (object.in());
185 servant->set_id (i, obj_id);
189 int successes = 0;
190 for (int i = 0; i < objcount; i++)
192 successes += refs[i]->check_servant_id (i) ? 1 : 0;
195 orb->destroy ();
197 if (successes != objcount)
199 ACE_ERROR ((LM_ERROR,
200 ACE_TEXT ("FAILURE: only got %d ids correct out of %d\n"),
201 successes, objcount));
202 return -1;
204 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Success: All ids match\n")));
206 catch (const CORBA::Exception& ex)
208 ex._tao_print_exception ("Exception caught");
209 return -1;
212 return 0;