Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Client_Protocol / client.cpp
blobf46560e5472b3371697bdbba8e77dc14db9a2ced
1 #include "testC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_unistd.h"
4 #include "tao/RTCORBA/RTCORBA.h"
5 #include "tao/Policy_ManagerC.h"
6 #include "tao/Policy_CurrentC.h"
8 #include "tao/Strategies/advanced_resource.h"
10 const ACE_TCHAR *ior1 = ACE_TEXT("file://test1.ior");
11 const ACE_TCHAR *ior2 = ACE_TEXT("file://test2.ior");
12 CORBA::ULong protocol_type = 0;
14 int
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("s:c:p:"));
18 int c, result;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 's':
24 ior1 = get_opts.opt_arg ();
25 break;
26 case 'c':
27 ior2 = get_opts.opt_arg ();
28 break;
29 case 'p':
30 result = ::sscanf (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()),
31 "%u",
32 &protocol_type);
33 if (result == 0 || result == EOF)
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "Unable to process <-p> option"),
36 -1);
37 break;
38 case '?':
39 default:
40 ACE_ERROR_RETURN ((LM_ERROR,
41 "usage: %s "
42 "-s <ior> "
43 "-c <ior> "
44 "-p <protocol_type> "
45 "\n",
46 argv [0]),
47 -1);
50 return 0;
53 int
54 check_for_nil (CORBA::Object_ptr obj, const char *msg)
56 if (CORBA::is_nil (obj))
57 ACE_ERROR_RETURN ((LM_ERROR,
58 "ERROR: Object reference <%C> is nil\n",
59 msg),
60 -1);
61 else
62 return 0;
65 void
66 exception_test (Test_ptr server,
67 const char *msg)
69 try
71 server->test_method ();
73 catch (const CORBA::INV_POLICY& )
75 ACE_DEBUG ((LM_DEBUG,
76 "INV_POLICY exception is caught as expected.\n"));
78 catch (const CORBA::Exception&)
80 ACE_DEBUG ((LM_DEBUG, msg));
81 throw;
85 int
86 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
88 try
90 // Initialize the ORB, resolve references and parse arguments.
92 // ORB.
93 CORBA::ORB_var orb =
94 CORBA::ORB_init (argc, argv);
96 // Parse arguments.
97 if (parse_args (argc, argv) != 0)
98 return -1;
100 // RTORB.
101 CORBA::Object_var object =
102 orb->resolve_initial_references ("RTORB");
103 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
104 if (check_for_nil (rt_orb.in (), "RTORB") == -1)
105 return -1;
107 // PolicyManager.
108 object = orb->resolve_initial_references ("ORBPolicyManager");
109 CORBA::PolicyManager_var policy_manager =
110 CORBA::PolicyManager::_narrow (object.in ());
111 if (check_for_nil (policy_manager.in (), "PolicyManager")
112 == -1)
113 return -1;
115 // PolicyCurrent.
116 object = orb->resolve_initial_references ("PolicyCurrent");
117 CORBA::PolicyCurrent_var policy_current =
118 CORBA::PolicyCurrent::_narrow (object.in ());
119 if (check_for_nil (policy_current.in (), "PolicyCurrent")
120 == -1)
121 return -1;
123 // Test object 1 (ClientProtocolPolicy set on server).
124 object = orb->string_to_object (ior1);
125 Test_var server1 = Test::_narrow (object.in ());
126 if (check_for_nil (server1.in (), "server1") == -1)
127 return -1;
129 // Test object 2 (no client-exposed ClientProtocolPolicy).
130 object = orb->string_to_object (ior2);
131 Test_var server2 = Test::_narrow (object.in ());
132 if (check_for_nil (server2.in (), "server2") == -1)
133 return -1;
135 // Do testing.
137 // Test 1: Invoke operation on the object that has
138 // ClientProtocolPolicy set on the server side.
139 ACE_DEBUG ((LM_DEBUG,
140 "\n Test 1\n"));
141 server1->test_method ();
143 // Test 2: Set the ORB-level ClientProtocolPolicy override, and
144 // attempt the same invocation again. Should get
145 // CORBA::INV_POLICY exception since the policy is set on both
146 // client and server sides.
147 ACE_DEBUG ((LM_DEBUG,
148 "\n Test 2\n"));
149 RTCORBA::ProtocolList protocols;
150 protocols.length (1);
151 protocols[0].protocol_type = protocol_type;
152 protocols[0].transport_protocol_properties =
153 RTCORBA::ProtocolProperties::_nil ();
154 protocols[0].orb_protocol_properties =
155 RTCORBA::ProtocolProperties::_nil ();
157 CORBA::PolicyList policy_list;
158 policy_list.length (1);
159 policy_list[0] =
160 rt_orb->create_client_protocol_policy (protocols);
162 policy_manager->set_policy_overrides (policy_list,
163 CORBA::SET_OVERRIDE);
165 exception_test (server1.in (),
166 "ERROR: Test 2 failed\n");
168 // Test 3: Attempt the invocation on the second object reference
169 // (the one that didn't have ClientProtocolPolicy set on the
170 // server side). This should succeed since there are no
171 // conflicts.
172 ACE_DEBUG ((LM_DEBUG,
173 "\n Test 3\n"));
174 server2->test_method ();
176 // Test 4: Override ClientProtocolPolicy on the Current level.
177 // For the override value, use the sequence of protocols, none
178 // of which are available in the server ORB. Attempt an
179 // invocation on the second object. Should get
180 // CORBA::INV_POLICY exception since none of the protocols
181 // specified in the policy are available.
182 ACE_DEBUG ((LM_DEBUG,
183 "\n Test 4\n"));
184 // Hardcode a sequence of nonexistent protocols.
185 protocols.length (3);
186 protocols[0].protocol_type = 3;
187 protocols[1].protocol_type = 4;
188 protocols[2].protocol_type = 5;
189 policy_list[0] =
190 rt_orb->create_client_protocol_policy (protocols);
192 policy_current->set_policy_overrides (policy_list,
193 CORBA::SET_OVERRIDE);
195 exception_test (server2.in (),
196 "ERROR: Test 4 failed\n");
198 // Test 5: Override ClientProtocolPolicy on the Current level
199 // again. This time use the sequence in which the first
200 // protocol isn't available and the second one is. The
201 // invocation should succeed through using the second protocol.
202 ACE_DEBUG ((LM_DEBUG,
203 "\n Test 5\n"));
204 protocols.length (2);
205 protocols[0].protocol_type = 3;
206 protocols[1].protocol_type = protocol_type;
207 policy_list[0] =
208 rt_orb->create_client_protocol_policy (protocols);
210 policy_current->set_policy_overrides (policy_list,
211 CORBA::SET_OVERRIDE);
213 server2->test_method ();
215 // Testing over. Shut down server ORB.
216 ACE_DEBUG ((LM_DEBUG,
217 "\n Testing over\n"));
218 server2->shutdown ();
220 // Needed for SHMIOP to work fine. Please dont remove. Please
221 // see Bug 1197 for details.
222 ACE_OS::sleep (5);
224 catch (const CORBA::Exception& ex)
226 ex._tao_print_exception (
227 "Unexpected exception caught in ClientProtocolPolicy test client:");
228 return -1;
231 return 0;