Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / DynAny_Test / test_dynunion.cpp
blob125a533b0faa55aac08e63ded7edfa4c2aaff51f
1 //=============================================================================
2 /**
3 * @file test_dynunion.cpp
5 * Implementation of the simple DynUnion test
7 * @author Jeff Parsons <parsons@cs.wustl.edu>
8 */
9 //=============================================================================
11 #include "da_testsC.h"
12 #include "test_dynunion.h"
13 #include "data.h"
14 #include "analyzer.h"
15 #include "tao/DynamicAny/DynamicAny.h"
16 #include "ace/OS_NS_string.h"
18 Test_DynUnion::Test_DynUnion (CORBA::ORB_var orb, int debug)
19 : orb_ (orb),
20 test_name_ (CORBA::string_dup ("test_dynunion")),
21 error_count_ (0),
22 debug_ (debug)
26 Test_DynUnion::~Test_DynUnion ()
28 CORBA::string_free (this->test_name_);
29 this->test_name_ = 0;
32 const char*
33 Test_DynUnion::test_name () const
35 return this->test_name_;
38 int
39 Test_DynUnion::run_test ()
41 Data data (this->orb_);
43 DynAnyTests::test_union tu;
45 try
47 ACE_DEBUG ((LM_DEBUG,
48 "\t*=*=*=*= %C =*=*=*=*\n",
49 data.labels[12]));
51 CORBA::Object_var factory_obj =
52 this->orb_->resolve_initial_references ("DynAnyFactory");
54 DynamicAny::DynAnyFactory_var dynany_factory =
55 DynamicAny::DynAnyFactory::_narrow (factory_obj.in ());
57 if (CORBA::is_nil (dynany_factory.in ()))
59 ACE_ERROR_RETURN ((LM_ERROR,
60 "Nil dynamic any factory after narrow\n"),
61 -1);
64 DynAnyAnalyzer analyzer(this->orb_.in(), dynany_factory.in(), debug_);
66 ACE_DEBUG ((LM_DEBUG,
67 "testing: constructor(Any)/from_any/to_any with string\n"));
69 DynAnyTests::test_union tstring;
70 tstring._d (DynAnyTests::TE_THIRD);
71 tstring.str (CORBA::string_dup (data.m_string1));
72 CORBA::Any in_any4;
73 in_any4 <<= tstring;
74 DynamicAny::DynAny_var dp4 =
75 dynany_factory->create_dyn_any (in_any4);
76 DynamicAny::DynUnion_var fa4 =
77 DynamicAny::DynUnion::_narrow (dp4.in ());
79 analyzer.analyze(fa4.in());
81 CORBA::Any_var out_any5 = fa4->to_any ();
83 DynamicAny::DynAny_var dp5 =
84 dynany_factory->create_dyn_any (out_any5.in());
86 ACE_DEBUG ((LM_DEBUG,
87 "testing: constructor(Any)/insert/get\n"));
89 tu._d (DynAnyTests::TE_SECOND);
90 tu.tc (data.m_typecode2);
91 CORBA::Any in_any1;
92 in_any1 <<= tu;
93 DynamicAny::DynAny_var dp1 =
94 dynany_factory->create_dyn_any (in_any1);
95 DynamicAny::DynUnion_var fa1 =
96 DynamicAny::DynUnion::_narrow (dp1.in ());
97 fa1->seek (1);
98 fa1->insert_typecode (data.m_typecode1);
100 CORBA::TypeCode_var s_out1 = fa1->get_typecode ();
102 CORBA::Boolean const equal_tc1 =
103 s_out1->equal (data.m_typecode1);
105 if (equal_tc1)
107 ACE_DEBUG ((LM_DEBUG,
108 "++ OK ++\n"));
110 else
112 ++this->error_count_;
115 ACE_DEBUG ((LM_DEBUG,
116 "testing: constructor(TypeCode)/from_any/to_any\n"));
118 analyzer.analyze (fa1.in());
120 CORBA::Any_var out_any1 = fa1->to_any ();
121 DynamicAny::DynAny_var ftc1_base =
122 dynany_factory->create_dyn_any_from_type_code (DynAnyTests::_tc_test_union);
124 DynamicAny::DynUnion_var ftc1 =
125 DynamicAny::DynUnion::_narrow (ftc1_base.in ());
127 if (CORBA::is_nil (ftc1.in ()))
129 ACE_ERROR_RETURN ((LM_ERROR,
130 "DynUnion::_narrow() returned nil\n"),
131 -1);
134 ftc1->from_any (out_any1.in ());
136 ftc1->seek (1);
138 CORBA::TypeCode_var s_out2 =
139 ftc1->get_typecode ();
141 CORBA::Boolean const equal_tc2 =
142 s_out2->equal (data.m_typecode1);
144 if (equal_tc2)
146 ACE_DEBUG ((LM_DEBUG,
147 "++ OK ++\n"));
149 else
151 ++this->error_count_;
154 ACE_DEBUG ((LM_DEBUG,
155 "testing: constructor(TypeCode alias)/from_any/to_any\n"));
157 analyzer.analyze( fa1.in());
159 CORBA::Any_var out_any2 = fa1->to_any ();
161 CORBA::TypeCode_var s_out3;
165 DynamicAny::DynAny_var ftc2_base =
166 dynany_factory->create_dyn_any_from_type_code
167 (DynAnyTests::_tc_test_union_alias);
169 DynamicAny::DynUnion_var ftc2 =
170 DynamicAny::DynUnion::_narrow (ftc2_base.in ());
172 if (CORBA::is_nil (ftc2.in ()))
174 ACE_ERROR_RETURN ((LM_ERROR,
175 "DynUnion::_narrow() returned nil\n"),
176 -1);
179 ftc2->from_any (out_any2.in ());
181 ftc2->seek (1);
183 s_out3 = ftc2->get_typecode ();
185 catch (const CORBA::TypeCode::BadKind& )
187 // Failed to create
190 if (!CORBA::is_nil (s_out3.in ()))
192 CORBA::Boolean const equal_tc =
193 s_out3->equal (data.m_typecode1);
195 if (equal_tc)
197 ACE_DEBUG ((LM_DEBUG,
198 "++ OK ++\n"));
200 else
202 ++this->error_count_;
205 else
207 ++this->error_count_;
210 ACE_DEBUG ((LM_DEBUG,
211 "testing:discriminator/discriminator_kind\n"));
213 DynamicAny::DynAny_var dp2 =
214 ftc1->get_discriminator ();
216 CORBA::TypeCode_var tc2 =
217 dp2->type ();
219 CORBA::TCKind tc1kind =
220 ftc1->discriminator_kind ();
222 CORBA::TCKind tc2kind = tc2->kind ();
224 if (tc2kind == tc1kind)
226 ACE_DEBUG ((LM_DEBUG,
227 "++ OK ++\n"));
229 else
231 ++this->error_count_;
234 ACE_DEBUG ((LM_DEBUG,
235 "testing:member/member_kind/member_name\n"));
237 DynamicAny::DynAny_var dp3 =
238 ftc1->member ();
240 CORBA::String_var m_nm =
241 ftc1->member_name ();
243 CORBA::TCKind tk =
244 ftc1->member_kind ();
246 CORBA::TypeCode_var tc3 =
247 dp3->get_typecode ();
249 CORBA::Boolean const equal_tc3 =
250 data.m_typecode1->equal (tc3.in ());
252 if (!ACE_OS::strcmp (m_nm.in (), "tc")
253 && tk == CORBA::tk_TypeCode
254 && equal_tc3)
256 ACE_DEBUG ((LM_DEBUG,
257 "++ OK ++\n"));
259 else
261 ++this->error_count_;
264 ACE_DEBUG ((LM_DEBUG,
265 "testing: set_to_default_member\n"));
267 ftc1->set_to_default_member ();
269 ftc1->seek (1);
271 ftc1->insert_short (data.m_short1);
273 DynamicAny::DynAny_var mem =
274 ftc1->member ();
276 CORBA::Short out_s =
277 mem->get_short ();
279 if (out_s == data.m_short1)
281 ACE_DEBUG ((LM_DEBUG,
282 "++ OK ++\n"));
284 else
286 ++this->error_count_;
289 fa1->destroy ();
291 ftc1->destroy ();
293 ACE_DEBUG ((LM_DEBUG,
294 "testing: create_dyn_any with _default()\n"));
295 DynAnyTests::test_implicit_def test_implicit_def_union;
296 CORBA::Any any_union;
298 test_implicit_def_union._default();
300 any_union <<= test_implicit_def_union;
302 DynamicAny::DynAny_var da_union =
303 dynany_factory->create_dyn_any (any_union);
304 ACE_DEBUG ((LM_DEBUG,
305 "++ OK ++\n"));
307 ACE_DEBUG ((LM_DEBUG,
308 "testing: union_no_active_member destroy\n"));
310 // Test fix for bug on destroy when there is no active member in union.
311 DynAnyTests::test_union_no_active_member l_union_value_no_active_member;
313 CORBA::Any corba_union;
314 corba_union <<= l_union_value_no_active_member;
316 DynamicAny::DynAny_var l_dynany = dynany_factory->create_dyn_any (corba_union);
319 l_dynany->destroy ();
320 ACE_DEBUG ((LM_DEBUG,
321 "++ OK ++\n"));
323 catch (...)
325 ++this->error_count_;
329 // if we get here the create_dyn_any worked.
331 catch (const CORBA::Exception& ex)
333 ex._tao_print_exception ("test_dynunion::run_test");
334 ++this->error_count_;
337 ACE_DEBUG ((LM_DEBUG,
338 "\n%d errors\n",
339 this->error_count_));
341 return !!error_count_;