summaryrefslogtreecommitdiff
path: root/java/test/IceXML/encoding/Client.java
blob: 95356fc0cd0a642214742afbcdaae06e30d6df00 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// **********************************************************************
//
// Copyright (c) 2001
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

public class Client
{
    private 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\">";
    private static final String footer = "</ice:data>";

    private static void
    test(boolean b)
    {
        if(!b)
        {
            throw new RuntimeException("Test failed!");
        }
    }

    private static void
    TestString(Ice.Communicator communicator)
    {
        final String element = "s";
        final String[] strings =
        {
            "hello world",
            "hello & world",
            "\"hello world\"",
            "'hello world'",
            "hello <world",
            "hello >world",
            "hello >>world",
            "hello <<>>world",
            "hello &&''\"\"<<>>world",
        };
        for(int i = 0; i < strings.length; ++i)
        {
            java.io.StringWriter sw = new java.io.StringWriter();
            java.io.PrintWriter pw = new java.io.PrintWriter(sw);
            pw.print("<data>");
            Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
            ostream.writeString(element, strings[i]);
            pw.print("</data>");
            pw.flush();

            java.io.StringReader sr = new java.io.StringReader(sw.toString());
            Ice.Stream istream = new IceXML.StreamI(communicator, sr);
            String result = istream.readString(element);
            test(result.equals(strings[i]));
        }
    }

    private static void
    TestStruct1(Ice.Communicator communicator)
    {
        final String element = "Test.Struct1";
        Test.Struct1 sin = new Test.Struct1();
        sin.l = 10;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        sin.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Struct1 sout = new Test.Struct1();
        sout.ice_unmarshal(element, istream);
        test(sin.equals(sout));
    }

    private static void
    TestStruct2(Ice.Communicator communicator)
    {
        final String element = "Test.Struct2";
        Test.Struct2 sin = new Test.Struct2();
        sin.s1 = new Test.Struct1();
        sin.s1.l = 10;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        sin.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Struct2 sout = new Test.Struct2();
        sout.ice_unmarshal(element, istream);
        test(sin.equals(sout));
    }

    private static void
    TestStruct3(Ice.Communicator communicator)
    {
        final String element = "Test.Struct3";
        Test.Struct3 sin = new Test.Struct3();
        sin.l = 20;
        sin.s2 = new Test.Struct2();
        sin.s2.s1 = new Test.Struct1();
        sin.s2.s1.l = 10;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        sin.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Struct3 sout = new Test.Struct3();
        sout.ice_unmarshal(element, istream);
        test(sin.equals(sout));
    }

    private static void
    TestStruct4(Ice.Communicator communicator)
    {
        final String element = "Test.Struct4";
        Test.Struct4 sin = new Test.Struct4();
        sin.l = 30;
        sin.s3 = new Test.Struct3();
        sin.s3.l = 20;
        sin.s3.s2 = new Test.Struct2();
        sin.s3.s2.s1 = new Test.Struct1();
        sin.s3.s2.s1.l = 10;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        sin.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Struct4 sout = new Test.Struct4();
        sout.ice_unmarshal(element, istream);
        test(sin.equals(sout));
    }

    private static void
    TestStruct4Seq(Ice.Communicator communicator)
    {
        final String element = "Test.Struct4Seq";
        Test.Struct4[] seqin = new Test.Struct4[2];

        Test.Struct4 sin = new Test.Struct4();
        sin.l = 30;
        sin.s3 = new Test.Struct3();
        sin.s3.l = 20;
        sin.s3.s2 = new Test.Struct2();
        sin.s3.s2.s1 = new Test.Struct1();
        sin.s3.s2.s1.l = 10;
        seqin[0] = sin;

        sin = new Test.Struct4();
        sin.l = 40;
        sin.s3 = new Test.Struct3();
        sin.s3.l = 30;
        sin.s3.s2 = new Test.Struct2();
        sin.s3.s2.s1 = new Test.Struct1();
        sin.s3.s2.s1.l = 20;
        seqin[1] = sin;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        Test.Struct4SeqHelper.ice_marshal(element, ostream, seqin);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Struct4[] seqout = Test.Struct4SeqHelper.ice_unmarshal(element, istream);
        test(seqout.length == seqin.length);
        for(int i = 0; i < seqin.length; i++)
        {
            test(seqin[i].equals(seqout[i]));
        }
    }

    private static void
    TestStringStruct4Dict(Ice.Communicator communicator)
    {
        final String element = "Test.StringStruct4Dict";
        java.util.Map dictin = new java.util.HashMap();

        Test.Struct4 sin = new Test.Struct4();
        sin.l = 30;
        sin.s3 = new Test.Struct3();
        sin.s3.l = 20;
        sin.s3.s2 = new Test.Struct2();
        sin.s3.s2.s1 = new Test.Struct1();
        sin.s3.s2.s1.l = 10;
        dictin.put("1", sin);

        sin = new Test.Struct4();
        sin.l = 40;
        sin.s3 = new Test.Struct3();
        sin.s3.l = 30;
        sin.s3.s2 = new Test.Struct2();
        sin.s3.s2.s1 = new Test.Struct1();
        sin.s3.s2.s1.l = 20;
        dictin.put("2", sin);

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        Test.StringStruct4DictHelper.ice_marshal(element, ostream, dictin);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        java.util.Map dictout = Test.StringStruct4DictHelper.ice_unmarshal(element, istream);
        test(dictout.size() == dictin.size());
        java.util.Iterator p = dictin.entrySet().iterator();
        while(p.hasNext())
        {
            java.util.Map.Entry e = (java.util.Map.Entry)p.next();
            Test.Struct4 val = (Test.Struct4)dictout.get(e.getKey());
            test(val != null);
            test(val.equals(e.getValue()));
        }
    }

    private static void
    TestColor(Ice.Communicator communicator)
    {
        final String element = "Test.Color";
        Test.Color ein = Test.Color.Red;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        ein.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Color eout = Test.Color.ice_unmarshal(element, istream);
        test(ein == eout);
    }

    private static void
    TestColorSeq(Ice.Communicator communicator)
    {
        final String element = "Test.ColorSeq";
        Test.Color[] seqin = new Test.Color[3];

        seqin[0] = Test.Color.Red;
        seqin[1] = Test.Color.Green;
        seqin[2] = Test.Color.Blue;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        Test.ColorSeqHelper.ice_marshal(element, ostream, seqin);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Color[] seqout = Test.ColorSeqHelper.ice_unmarshal(element, istream);
        test(seqout.length == seqin.length);
        for(int i = 0; i < seqin.length; i++)
        {
            test(seqin[i] == seqout[i]);
        }
    }

    private static void
    TestClass1(Ice.Communicator communicator)
    {
        final String element = "Test.Class1";
        Test.Class1 in = new Test.Class1();
        in.c = Test.Color.Red;
        in.name = "Red";

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Class1 out = (Test.Class1)Test.Class1.ice_unmarshal(element, istream);
        test(in.c == out.c && in.name.equals(out.name));
    }

    private static void
    TestClass2(Ice.Communicator communicator)
    {
        final String element = "Test.Class2";
        Test.Class2 in = new Test.Class2();
        in.c = Test.Color.Blue;
        in.name = "Blue";

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Class2 out = (Test.Class2)Test.Class2.ice_unmarshal(element, istream);
        test(in.c == out.c && in.name.equals(out.name));
    }

    private static void
    TestClass2Rec(Ice.Communicator communicator)
    {
        final String element = "Test.Class2";
        Test.Class2 in = new Test.Class2();
        in.c = Test.Color.Blue;
        in.name = "Blue";
        Test.Class2 in2 = new Test.Class2();
        in2.c = Test.Color.Green;
        in2.name = "Green";
        in2.r = in;
        in.r = in2;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Class2 out = (Test.Class2)Test.Class2.ice_unmarshal(element, istream);
        test(out.r != null);
        test(out.r.r != null);
        test(in.c == out.c && in.name.equals(out.name));
        test(in.r.c == out.r.c && in.r.name.equals(out.r.name));
        test(in == in.r.r);
    }

    private static class Class3I extends Test.Class3
    {
        public void
        if1(Ice.Current current)
        {
        }
    }

    private static class Class3Factory extends Ice.LocalObjectImpl implements Ice.ObjectFactory
    {
        public Ice.Object
        create(String type)
        {
            test(type.equals(Test.Class3.ice_staticId()));
            return new Class3I();
        }

        public void
        destroy()
        {
        }

    }

    private static void
    TestClass3(Ice.Communicator communicator)
    {
        final String element = "Test.Class3";
        Test.Class3 in = new Class3I();
        in.c = Test.Color.Blue;
        in.name = "Blue";

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Ice.Object o = Ice.ObjectImpl.ice_unmarshal(element, istream);

        Test.Class3 out = (Test.Class3)o;
        test(in.c == out.c && in.name.equals(out.name));
    }

    private static void
    TestClass3Rec(Ice.Communicator communicator)
    {
        final String element = "Test.Class3";
        Test.Class3 in = new Class3I();
        in.c = Test.Color.Blue;
        in.name = "Blue";
        Test.Class3 in2 = new Class3I();
        in2.c = Test.Color.Green;
        in2.name = "Green";
        in2.r = in;
        in.r = in2;

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Ice.Object o = Ice.ObjectImpl.ice_unmarshal(element, istream);
        Test.Class3 out = (Test.Class3)o;
        test(out.r != null);
        test(out.r.r != null);
        test(in.c == out.c && in.name.equals(out.name));
        test(in.r.c == out.r.c && in.r.name.equals(out.r.name));
        test(in == in.r.r);
    }

    private static void
    TestFacets(Ice.Communicator communicator)
    {
        final String element = "Test.Class2";
        Test.Class2 in = new Test.Class2();
        in.c = Test.Color.Blue;
        in.name = "Blue";
        Test.Class2 in2 = new Test.Class2();
        in2.c = Test.Color.Green;
        in2.name = "Green";
        in2.r = in;
        in.r = in2;

        Test.Class1 facet = new Test.Class1();
        facet.c = Test.Color.Red;
        facet.name = "Red";
        in.ice_addFacet(facet, "red");

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Class2 out = (Test.Class2)Test.Class2.ice_unmarshal(element, istream);
        test(out.r != null);
        test(out.r.r != null);
        test(in.c == out.c && in.name.equals(out.name));
        test(in.r.c == out.r.c && in.r.name.equals(out.r.name));
        test(in == in.r.r);
        Ice.Object obj = out.ice_findFacet("red");
        test(obj != null);
        Test.Class1 outFacet = (Test.Class1)obj;
        test(facet.c == outFacet.c && facet.name.equals(outFacet.name));
    }

    private static void
    TestException1(Ice.Communicator communicator)
    {
        final String element = "Test.Exception1";
        Test.Exception1 in = new Test.Exception1();

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Exception1 out = new Test.Exception1();
        out.ice_unmarshal(element, istream);
    }

    private static void
    TestException2(Ice.Communicator communicator)
    {
        final String element = "Test.Exception2";
        Test.Exception2 in = new Test.Exception2();
        in.msg = "hello world";

        java.io.StringWriter sw = new java.io.StringWriter();
        java.io.PrintWriter pw = new java.io.PrintWriter(sw);
        pw.print(header);
        Ice.Stream ostream = new IceXML.StreamI(communicator, pw);
        in.ice_marshal(element, ostream);
        pw.print(footer);
        pw.flush();

        java.io.StringReader sr = new java.io.StringReader(sw.toString());
        Ice.Stream istream = new IceXML.StreamI(communicator, sr);
        Test.Exception2 out = new Test.Exception2();
        out.ice_unmarshal(element, istream);
        test(in.msg.equals(out.msg));
    }

    private static int
    run(String[] args, Ice.Communicator communicator)
    {
        //
        // If the test is run out of the CWD then adjust the location of
        // Test.xsd appropriately.
        //
        if(args.length > 0)
        {
            int pos = header.indexOf("Test.xsd");
            assert(pos != -1);
            header = header.substring(0, pos) + args[0] + "/" + header.substring(pos);
        }

        System.out.print("testing string... ");
        System.out.flush();
        TestString(communicator);
        System.out.println("ok");

        System.out.print("testing struct... ");
        System.out.flush();
        TestStruct1(communicator);
        TestStruct2(communicator);
        TestStruct3(communicator);
        TestStruct4(communicator);
        System.out.println("ok");

        System.out.print("testing sequence... ");
        System.out.flush();
        TestStruct4Seq(communicator);
        System.out.println("ok");

        System.out.print("testing dictionaries... ");
        System.out.flush();
        TestStringStruct4Dict(communicator);
        System.out.println("ok");

        System.out.print("testing enumerations... ");
        System.out.flush();
        TestColor(communicator);
        TestColorSeq(communicator);
        System.out.println("ok");

        System.out.print("testing class... ");
        System.out.flush();
        TestClass1(communicator);
        TestClass2(communicator);
        TestClass2Rec(communicator);
        try
        {
            TestClass3(communicator);
            test(false);
        }
        catch(Ice.NoObjectFactoryException ex)
        {
        }

        communicator.addObjectFactory(new Class3Factory(), Test.Class3.ice_staticId());

        TestClass3(communicator);
        TestClass3Rec(communicator);
        TestFacets(communicator);
        System.out.println("ok");

        System.out.print("testing exception... ");
        System.out.flush();
        TestException1(communicator);
        TestException2(communicator);
        System.out.println("ok");

        return 0;
    }

    public static void
    main(String[] args)
    {
        int status = 0;
        Ice.Communicator communicator = null;

        try
        {
            communicator = Ice.Util.initialize(args);
            status = run(args, communicator);
        }
        catch(Ice.LocalException ex)
        {
            ex.printStackTrace();
            status = 1;
        }

        if(communicator != null)
        {
            try
            {
                communicator.destroy();
            }
            catch(Ice.LocalException ex)
            {
                ex.printStackTrace();
                status = 1;
            }
        }

        System.exit(status);
    }
}