summaryrefslogtreecommitdiff
path: root/cpp/test/IceXML/encoding/Client.cpp
blob: 9bc31fcca3bb08456b774dc625d224d78a22b75c (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/Ice.h>
#include <TestCommon.h>
#include <Test.h>

#include <IceXML/StreamI.h>

using namespace std;

static string header = "<ice:data xmlns=\"http://www.noorg.org/schemas\""
                             " xmlns:ice=\"http://www.mutablerealms.com/schemas\""
                             " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
                             " xsi:schemaLocation=\"http://www.noorg.org/schemas Test.xsd\">";
static string footer = "</ice:data>";

void
TestString(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "s";
    static const string strings[] =
    {
	"hello world",
	"hello & world",
	"\"hello world\"",
	"'hello world'",
	"hello <world",
	"hello >world",
	"hello >>world",
	"hello <<>>world",
	"hello &&''\"\"<<>>world",
	""
    };
    for(int i = 0; !strings[i].empty(); ++i)
    {
	ostringstream os;
	os << "<data>";
	Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
	ostream->writeString(element, strings[i]);
	os << "</data>";;
	
	istringstream is(os.str());
	Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
	string result = istream->readString(element);
	test(result == strings[i]);
    }
}

void
TestStruct1(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct1";
    Test::Struct1 sin;
    sin.l = 10;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    sin.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct1 sout;
    sout.ice_unmarshal(element, istream);
    test(sin == sout);
}

void
TestStruct2(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct2";
    Test::Struct2 sin;
    sin.s1.l = 10;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    sin.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct2 sout;
    sout.ice_unmarshal(element, istream);
    test(sin == sout);
}

void
TestStruct3(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct3";
    Test::Struct3 sin;
    sin.l = 20;
    sin.s2.s1.l = 10;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    sin.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct3 sout;
    sout.ice_unmarshal(element, istream);
    test(sin == sout);
}

void
TestStruct4(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct4";
    Test::Struct4 sin;
    sin.l = 30;
    sin.s3.l = 20;
    sin.s3.s2.s1.l = 10;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    sin.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct4 sout;
    sout.ice_unmarshal(element, istream);
    test(sin == sout);
}

void
TestStruct4Seq(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct4Seq";
    Test::Struct4Seq seqin;

    Test::Struct4 sin;
    sin.l = 30;
    sin.s3.l = 20;
    sin.s3.s2.s1.l = 10;
    seqin.push_back(sin);

    sin.l = 40;
    sin.s3.l = 30;
    sin.s3.s2.s1.l = 20;
    seqin.push_back(sin);

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    Test::Struct4SeqHelper::ice_marshal(element, ostream, seqin);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct4Seq seqout;
    Test::Struct4SeqHelper::ice_unmarshal(element, istream, seqout);
    test(seqout.size() == seqin.size());
    while(!seqin.empty())
    {
	Test::Struct4 sin = seqin.back();
	Test::Struct4 sout = seqout.back();
	test(sin == sout);
	seqout.pop_back();
	seqin.pop_back();
    }
}

void
TestStringStruct4Dict(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.StringStruct4Dict";
    Test::StringStruct4Dict dictin;

    Test::Struct4 sin;
    sin.l = 30;
    sin.s3.l = 20;
    sin.s3.s2.s1.l = 10;
    dictin["1"] = sin;

    sin.l = 40;
    sin.s3.l = 30;
    sin.s3.s2.s1.l = 20;
    dictin["2"] = sin;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    Test::StringStruct4DictHelper::ice_marshal(element, ostream, dictin);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::StringStruct4Dict dictout;
    Test::StringStruct4DictHelper::ice_unmarshal(element, istream, dictout);
    test(dictout.size() == dictin.size());
    for(Test::StringStruct4Dict::const_iterator p = dictin.begin(); p != dictin.end(); ++p)
    {
	Test::StringStruct4Dict::const_iterator q = dictout.find(p->first);
	test(q != dictout.end());
	test(q->second == p->second);
    }
}

void
TestStruct3Struct4Dict(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Struct3Struct4Dict";
    Test::Struct3Struct4Dict dictin;

    Test::Struct3 s3in;
    s3in.l = 20;
    s3in.s2.s1.l = 10;

    Test::Struct4 s4in;
    s4in.l = 30;
    s4in.s3.l = 20;
    s4in.s3.s2.s1.l = 10;
    dictin[s3in] = s4in;

    s3in.l = 30;
    s3in.s2.s1.l = 20;

    s4in.l = 40;
    s4in.s3.l = 30;
    s4in.s3.s2.s1.l = 20;
    dictin[s3in] = s4in;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    Test::Struct3Struct4DictHelper::ice_marshal(element, ostream, dictin);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Struct3Struct4Dict dictout;
    Test::Struct3Struct4DictHelper::ice_unmarshal(element, istream, dictout);
    test(dictout.size() == dictin.size());
    for(Test::Struct3Struct4Dict::const_iterator p = dictin.begin(); p != dictin.end(); ++p)
    {
	Test::Struct3Struct4Dict::const_iterator q = dictout.find(p->first);
	test(q != dictout.end());
	test(q->second == p->second);
    }
}

void
TestColor(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Color";
    Test::Color ein = Test::Red;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    ::Test::ice_marshal(element, ostream, ein);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Color eout;
    ::Test::ice_unmarshal(element, istream, eout);
    test(ein == eout);
}

void
TestColorSeq(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.ColorSeq";
    Test::ColorSeq seqin;

    seqin.push_back(Test::Red);
    seqin.push_back(Test::Green);
    seqin.push_back(Test::Blue);

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    Test::ColorSeqHelper::ice_marshal(element, ostream, seqin);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::ColorSeq seqout;
    Test::ColorSeqHelper::ice_unmarshal(element, istream, seqout);
    test(seqout.size() == seqin.size());
    while(!seqin.empty())
    {
	Test::Color sin = seqin.back();
	Test::Color sout = seqout.back();
	test(sin == sout);
	seqout.pop_back();
	seqin.pop_back();
    }
}

void
TestClass1(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Class1";
    Test::Class1Ptr in = new Test::Class1();
    in->c = Test::Red;
    in->name = "Red";

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in->ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Class1Ptr out;
    Test::Class1::ice_unmarshal(element, istream, out);
    test(in->c == out->c && in->name == out->name);
}

void
TestClass2(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Class2";
    Test::Class2Ptr in = new Test::Class2();
    in->c = Test::Blue;
    in->name = "Blue";

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in->ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Class2Ptr out;
    Test::Class2::ice_unmarshal(element, istream, out);
    test(in->c == out->c && in->name == out->name);
}

void
TestClass2Rec(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Class2";
    Test::Class2Ptr in = new Test::Class2();
    in->c = Test::Blue;
    in->name = "Blue";
    Test::Class2Ptr in2 = new Test::Class2();
    in2->c = Test::Green;
    in2->name = "Green";
    in2->r = in;
    in->r = in2;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in->ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Class2Ptr out;
    Test::Class2::ice_unmarshal(element, istream, out);
    test(out->r);
    test(out->r->r);
    test(in->c == out->c && in->name == out->name);
    test(in->r->c == out->r->c && in->r->name == out->r->name);
    test(in == in->r->r);
}

class Class3I : public Test::Class3
{
public:

    virtual void if1(const ::Ice::Current&)
    {
    }
};

class Class3Factory : public ::Ice::ObjectFactory
{
public:

    virtual ::Ice::ObjectPtr
    create(const ::std::string& type)
    {
	test(type == ::Test::Class3::ice_staticId());
	return new Class3I;
    }

    virtual void
    destroy()
    {
    }

};

void
TestClass3(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Class3";
    Test::Class3Ptr in = new Class3I();
    in->c = Test::Blue;
    in->name = "Blue";

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in->ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    ::Ice::ObjectPtr o;
    ::Ice::Object::ice_unmarshal(element, istream, o);

    Test::Class3Ptr out = ::Test::Class3Ptr::dynamicCast(o);
    test(in->c == out->c && in->name == out->name);
}

void
TestClass3Rec(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Class3";
    Test::Class3Ptr in = new Class3I();
    in->c = Test::Blue;
    in->name = "Blue";
    Test::Class3Ptr in2 = new Class3I();
    in2->c = Test::Green;
    in2->name = "Green";
    in2->r = in;
    in->r = in2;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in->ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    ::Ice::ObjectPtr o;
    ::Ice::Object::ice_unmarshal(element, istream, o);
    Test::Class3Ptr out = ::Test::Class3Ptr::dynamicCast(o);
    test(out->r);
    test(out->r->r);
    test(in->c == out->c && in->name == out->name);
    test(in->r->c == out->r->c && in->r->name == out->r->name);
    test(in == in->r->r);
}

void
TestException1(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Exception1";
    Test::Exception1 in;

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Exception1 out;
    out.ice_unmarshal(element, istream);
}

void
TestException2(const Ice::CommunicatorPtr& communicator)
{
    static const string element = "Test.Exception2";
    Test::Exception2 in;
    in.msg = "hello world";

    ostringstream os;
    os << header;
    Ice::StreamPtr ostream = new IceXML::StreamI(communicator, os);
    in.ice_marshal(element, ostream);
    os << footer;

    istringstream is(os.str());
    Ice::StreamPtr istream = new IceXML::StreamI(communicator, is);
    Test::Exception2 out;
    out.ice_unmarshal(element, istream);
    test(in.msg == out.msg);
}

int
run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
{
    //
    // If the test is run out of the CWD then adjust the location of
    // Test.xsd appropriately.
    //
    if(argc > 1)
    {
	string dir(argv[1]);
	dir += '/';
	size_t pos = header.find("Test.xsd");
	assert(pos != string::npos);
	header.insert(pos, dir);
    }

    cout << "testing string... ";
    TestString(communicator);
    cout << "ok" << endl;

    cout << "testing struct... ";
    TestStruct1(communicator);
    TestStruct2(communicator);
    TestStruct3(communicator);
    TestStruct4(communicator);
    cout << "ok" << endl;

    cout << "testing sequence... ";
    TestStruct4Seq(communicator);
    cout << "ok" << endl;

    cout << "testing dictionaries... ";
    TestStringStruct4Dict(communicator);
    TestStruct3Struct4Dict(communicator);
    cout << "ok" << endl;

    cout << "testing enumerations... ";
    TestColor(communicator);
    TestColorSeq(communicator);
    cout << "ok" << endl;

    cout << "testing class... ";
    TestClass1(communicator);
    TestClass2(communicator);
    TestClass2Rec(communicator);
    try
    {
	TestClass3(communicator);
	test(false);
    }
    catch(const ::Ice::NoObjectFactoryException&)
    {
    }

    communicator->addObjectFactory(new Class3Factory, ::Test::Class3::ice_staticId());

    TestClass3(communicator);
    TestClass3Rec(communicator);
    cout << "ok" << endl;

    cout << "testing exception... ";
    TestException1(communicator);
    TestException2(communicator);
    cout << "ok" << endl;

    return EXIT_SUCCESS;
}

int
main(int argc, char* argv[])
{
    int status;
    Ice::CommunicatorPtr communicator;

    try
    {
	communicator = Ice::initialize(argc, argv);
	status = run(argc, argv, communicator);
    }
    catch(const Ice::Exception& ex)
    {
	cerr << ex << endl;
	status = EXIT_FAILURE;
    }

    if(communicator)
    {
	try
	{
	    communicator->destroy();
	}
	catch(const Ice::Exception& ex)
	{
	    cerr << ex << endl;
	    status = EXIT_FAILURE;
	}
    }

    return status;
}