summaryrefslogtreecommitdiff
path: root/TAO/tests/IORManipulation/IORTest.cpp
blob: 54c840cfcf522e7fd91c47f59429dcca4a630b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// $Id$

//========================================================================
//
// = LIBRARY
//     TAO/tests/IORManipultion
//
//
// = FILENAME
//     IORTest.cpp
//
// = DESCRIPTION
//   This program tests the basic functionality of the
//   IOR Manipultion interface.
//
// = AUTHOR
//     Fred Kuhns
//
//=========================================================================


#include "tao/corbafwd.h"
#include "tao/ORB.h"
#include "tao/IORManipulation.h"
#include "tao/corbafwd.h"
#include "tao/ORB.h"
#include "tao/IORManipulation.h"
#include "tao/IORS.h"
#include "ace/SString.h"

ACE_RCSID(IORManipluation, IORTest, "$Id$")

int
main (int argc, char **argv)
{

  int Status = 0;
  CORBA::ORB_var orb_;
  ACE_DEBUG ((LM_DEBUG, "----------------------------------\n"));
  ACE_DEBUG ((LM_DEBUG, "Running the IORManipulation Tests.\n"));

  ACE_DECLARE_NEW_CORBA_ENV;

  ACE_TRY
    {
      // Retrieve the ORB.
      orb_ = CORBA::ORB_init (argc,
                              argv,
                              0,
                              ACE_TRY_ENV);
      ACE_TRY_CHECK;
      // ***********************************************************************

      // Get an object reference for the ORBs IORManipultion object!
      CORBA_Object_ptr IORM =
        orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
                                          0,
                                          ACE_TRY_ENV);
      ACE_TRY_CHECK;

      TAO_IOP::TAO_IOR_Manipulation_ptr iorm =
               TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM, ACE_TRY_ENV);
      ACE_TRY_CHECK;
      // ***********************************************************************

      // Create a few fictitious IORs
      CORBA_Object_ptr name1 =
        orb_->string_to_object ("iiop://acme.cs.wustl.edu:6060/xyz", ACE_TRY_ENV);
      ACE_TRY_CHECK;
      CORBA_Object_ptr name2 =
        orb_->string_to_object ("iiop://tango.cs.wustl.edu:7070/xyz", ACE_TRY_ENV);
      ACE_TRY_CHECK;
      // ***********************************************************************

      // Get the string reps for these IORs and show them to the user
      ACE_CString iorm_ior = orb_->object_to_string (IORM, ACE_TRY_ENV);
      ACE_TRY_CHECK;
      ACE_DEBUG ((LM_DEBUG, "\tIORManipultion IOR string = %s\n", iorm_ior.c_str()));

      ACE_CString name1_ior = orb_->object_to_string (name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;
      ACE_DEBUG ((LM_DEBUG, "\tFirst made up IOR = %s\n", name1_ior.c_str()));

      ACE_CString name2_ior = orb_->object_to_string (name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;
      ACE_DEBUG ((LM_DEBUG, "\tSecond made up IOR = %s\n", name2_ior.c_str()));

      // ***********************************************************************
      // Create IOR list for use with merge_iors.
      TAO_IOP::TAO_IOR_Manipulation::IORList iors (2);
      iors.length(2);
      iors [0] = name1;
      iors [1] = name2;

      CORBA_Object_ptr merged = iorm->merge_iors (iors, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_CString merged_ior = orb_->object_to_string (merged, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::ULong count1 = iorm->get_profile_count (iors [0], ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::ULong count2 = iorm->get_profile_count (iors [1], ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::ULong count = iorm->get_profile_count (merged, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count != (count1 + count2))
        ACE_DEBUG ((LM_DEBUG, "**ERROR (merge_profiles): Merged profile count incorrect!\n"));

      ACE_DEBUG ((LM_DEBUG, "\tMerged IOR(%d) = %s\n", count, merged_ior.c_str()));

      // is_in_ior throws an exception if the intersection of the two
      // IORs is NULL.
      CORBA::ULong in_count = iorm->is_in_ior (merged, name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count1 != in_count)
        printf ("**ERROR (merge_iors): name1 is_in_ior returned profile count bad (%d)!\n",
                in_count);

      in_count = iorm->is_in_ior (merged, name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count2 != in_count)
        printf ("**ERROR (merge_iors): name2 is_in_ior returned profile count bad (%d)!\n",
                in_count);
      // ***********************************************************************

      // Verify ability to remove profiles from an IOR
      // First remove the second IOR from the merged IOR
      CORBA_Object_ptr just1 = iorm->remove_profiles (merged, name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_CString just1_ior = orb_->object_to_string (just1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      count = iorm->get_profile_count (just1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count1 != count)
        ACE_DEBUG ((LM_DEBUG, "**ERROR (merge_profiles): removing last IOR after a merge_iors ()\n"));

      ACE_DEBUG ((LM_DEBUG, "\tJust 1 IOR(%d) = %s\n", count, just1_ior.c_str ()));

      in_count = iorm->is_in_ior (just1, name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count1 != in_count)
        printf ("**ERROR (merge_profiles): incorrect count returned for is_in_ior\
                \nafter removal (%d)\n", in_count);
      // ***********************************************************************

      // Now try the add_profiles interface.
      CORBA_Object_ptr merged2 = iorm->add_profiles (just1, name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      count = iorm->get_profile_count (merged2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count != (count1 + count2))
        ACE_DEBUG ((LM_DEBUG, "**ERROR: add_profile failed profile count test!\n"));

      ACE_CString merged2_ior = orb_->object_to_string (merged2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "\tAdding 2 back in IOR(%d) = %s\n", count, merged2_ior.c_str ()));

      // is_in_ior throws an exception if the intersection of the two
      // IORs is NULL.
      in_count = iorm->is_in_ior (merged2, name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count1 != in_count)
        printf ("**ERROR (add_profiles): is_in_ior returned profile count bad (%d)!\n",
                 in_count);

      in_count = iorm->is_in_ior (merged2, name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count2 != in_count)
        ACE_DEBUG ((LM_DEBUG, "**ERROR (add_profiles): is_in_ior returned profile count bad!\n"));
      // ***********************************************************************

      CORBA_Object_ptr just2 = iorm->remove_profiles (merged2, name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_CString just2_ior = orb_->object_to_string (just2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      count = iorm->get_profile_count (just2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count2 != count)
        ACE_DEBUG ((LM_DEBUG, "ERROR removing last IOR after a merge_iors ()\n"));

      ACE_DEBUG ((LM_DEBUG, "\tJust 2 IOR(%d) = %s\n", count, just2_ior.c_str ()));

      in_count = iorm->is_in_ior (just2, name2, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (count2 != in_count)
        ACE_DEBUG ((LM_DEBUG,
                    "**ERROR (add_profiles): incorrect count (%d) "
                    "returned for is_in_ior after removal\n",
                    in_count));

      // all the primary tests worked, set status to 1
      Status = 1;
      // Finally generate an exception and quit.
      // This will generate a NotFound exception.
      in_count = iorm->is_in_ior (just2, name1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

    }
    ACE_CATCH (TAO_IOP::TAO_IOR_Manipulation::EmptyProfileList, userex)
      {
        ACE_UNUSED_ARG (userex);
        ACE_DEBUG ((LM_DEBUG, "Unexpected EmptyProfileList Exception!\n"));
        return -1;
      }
    ACE_CATCH (TAO_IOP::TAO_IOR_Manipulation::NotFound, userex)
      {
        ACE_UNUSED_ARG (userex);
        if (Status == 1)
          {
            Status = 2;
            ACE_DEBUG ((LM_DEBUG, "Caught NotFound Exception!\n"));
          }
        else
          {
            ACE_DEBUG ((LM_DEBUG, "Unexpected NotFound Exception!\n"));
            return -1;
          }
      }
    ACE_CATCH (TAO_IOP::TAO_IOR_Manipulation::Duplicate, userex)
      {
        ACE_UNUSED_ARG (userex);
        ACE_DEBUG ((LM_DEBUG, "Unexpected Duplicate Exception!\n"));
        return -1;
      }
    ACE_CATCH (TAO_IOP::TAO_IOR_Manipulation::Invalid_IOR, userex)
      {
        ACE_UNUSED_ARG (userex);
        ACE_DEBUG ((LM_DEBUG, "Unexpected Invalid_IOR Exception!\n"));
        return -1;
      }
    ACE_CATCH (CORBA::SystemException, sysex)
      {
        ACE_UNUSED_ARG (sysex);
        ACE_DEBUG ((LM_DEBUG, "Unexpected system Exception!!\n"));
        return -1;
      }
    ACE_CATCHANY
      {
        ACE_DEBUG ((LM_DEBUG, "Unexpected ACE_CATCHANY Exception!\n"));
        return -1;
      }
  ACE_ENDTRY;

  if (Status == 1)
    {
      // then exception didn't work!
      ACE_DEBUG ((LM_DEBUG, "**ERROR: Unable to generate the NotFound exception!\n"));
      Status = 0;
    }
  if (Status == 0)
    ACE_DEBUG ((LM_DEBUG, "An ERROR occured during the tests!\n"));
  else
    ACE_DEBUG ((LM_DEBUG, "Tests Successfully Completed!\n"));
  ACE_DEBUG ((LM_DEBUG, "--------------------------------------\n"));

  return 0;
}