Codebase list libxstream-java / upstream/1.4.8 xstream / src / test / com / thoughtworks / acceptance / ImplicitArrayTest.java
upstream/1.4.8

Tree @upstream/1.4.8 (Download .tar.gz)

ImplicitArrayTest.java @upstream/1.4.8raw · history · blame

  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
/*
 * Copyright (C) 2011, 2012, 2013 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 *
 * Created on 29. July 2011 by Joerg Schaible
 */
package com.thoughtworks.acceptance;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.thoughtworks.acceptance.objects.StandardObject;
import com.thoughtworks.xstream.InitializationException;

/**
 * @author Jörg Schaible
 */
public class ImplicitArrayTest extends AbstractAcceptanceTest {

    protected void setUp() throws Exception {
        super.setUp();
        xstream.alias("farm", Farm.class);
        xstream.alias("animal", Animal.class);
        xstream.alias("MEGA-farm", MegaFarm.class);
        xstream.ignoreUnknownElements();
    }

    public static class Farm extends StandardObject {
        private transient int idx;
        Animal[] animals;
    }

    public static class Animal extends StandardObject implements Comparable {
        String name;

        public Animal(String name) {
            this.name = name;
        }

        public int compareTo(Object o) {
            return name.compareTo(((Animal)o).name);
        }
    }
    
    public void testWithDirectType() {
        Farm farm = new Farm();
        farm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };

        String expected = "" +
                "<farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "</farm>";

        xstream.addImplicitArray(Farm.class, "animals");
        assertBothWays(farm, expected);
    }

    public static class MegaFarm extends Farm {
        String separator = "---";
        String[] names;
    }

    public void testInheritsImplicitArrayFromSuperclass() {
        Farm farm = new MegaFarm(); // subclass
        farm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };

        String expected = "" +
                "<MEGA-farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "  <separator>---</separator>\n" +
                "</MEGA-farm>";

        xstream.addImplicitCollection(Farm.class, "animals");
        assertBothWays(farm, expected);
    }

    public void testSupportsInheritedAndDirectDeclaredImplicitArraysAtOnce() {
        MegaFarm farm = new MegaFarm(); // subclass
        farm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };
        farm.names = new String[] {
            "McDonald",
            "Ponte Rosa"
        };
        
        String expected = "" +
                "<MEGA-farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "  <separator>---</separator>\n" +
                "  <name>McDonald</name>\n" +
                "  <name>Ponte Rosa</name>\n" +
                "</MEGA-farm>";

        xstream.addImplicitArray(Farm.class, "animals");
        xstream.addImplicitArray(MegaFarm.class, "names", "name");
        assertBothWays(farm, expected);
    }

    public void testInheritedAndDirectDeclaredImplicitArraysAtOnceIsNotDeclarationSequenceDependent() {
        MegaFarm farm = new MegaFarm(); // subclass
        farm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };
        farm.names = new String[] {
            "McDonald",
            "Ponte Rosa"
        };
        
        String expected = "" +
                "<MEGA-farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "  <separator>---</separator>\n" +
                "  <name>McDonald</name>\n" +
                "  <name>Ponte Rosa</name>\n" +
                "</MEGA-farm>";

        xstream.addImplicitArray(MegaFarm.class, "names", "name");
        xstream.addImplicitArray(Farm.class, "animals");
        assertBothWays(farm, expected);
    }

    public void testAllowsSubclassToOverrideImplicitCollectionInSuperclass() {
        Farm farm = new MegaFarm(); // subclass
        farm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };

        String expected = "" +
                "<MEGA-farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "  <separator>---</separator>\n" +
                "</MEGA-farm>";

        xstream.addImplicitCollection(MegaFarm.class, "animals");
        assertBothWays(farm, expected);
    }

    public void testAllowDifferentImplicitArrayDefinitionsInSubclass() {
        Farm farm = new Farm();
        farm.animals = new Animal[] {
           new Animal("Cod"), 
           new Animal("Salmon") 
        };
        MegaFarm megaFarm = new MegaFarm(); // subclass
        megaFarm.animals = new Animal[] {
            new Animal("Cow"),
            new Animal("Sheep")
        };
        megaFarm.names = new String[] {
            "McDonald",
            "Ponte Rosa"
        };
        
        List list = new ArrayList();
        list.add(farm);
        list.add(megaFarm);
        String expected = "" +
                "<list>\n" +
                "  <farm>\n" +
                "    <fish>\n" +
                "      <name>Cod</name>\n" +
                "    </fish>\n" +
                "    <fish>\n" +
                "      <name>Salmon</name>\n" +
                "    </fish>\n" +
                "  </farm>\n" +
                "  <MEGA-farm>\n" +
                "    <animal>\n" +
                "      <name>Cow</name>\n" +
                "    </animal>\n" +
                "    <animal>\n" +
                "      <name>Sheep</name>\n" +
                "    </animal>\n" +
                "    <separator>---</separator>\n" +
                "    <name>McDonald</name>\n" +
                "    <name>Ponte Rosa</name>\n" +
                "  </MEGA-farm>\n" +
                "</list>";

        xstream.addImplicitArray(Farm.class, "animals", "fish");
        xstream.addImplicitArray(MegaFarm.class, "animals");
        xstream.addImplicitArray(MegaFarm.class, "names", "name");
        assertBothWays(list, expected);
    }

    public static class House extends StandardObject {
        private Room[] rooms;
        private String separator = "---";
        private Person[] people;
        
        public List getPeople() {
            return Arrays.asList(people);
        }
        
        public List getRooms() {
            return Arrays.asList(rooms);
        }
    }

    public static class Room extends StandardObject {
        private String name;

        public Room(String name) {
            this.name = name;
        }
    }

    public static class Person extends StandardObject {
        private String name;
        private String[] emailAddresses;

        public Person(String name) {
            this.name = name;
        }
    }

    public void testMultipleArraysBasedOnDifferentType() {
        House house = new House();
        house.rooms = new Room[] {
            new Room("kitchen"),
            new Room("bathroom")
        };
        Person joe = new Person("joe");
        joe.emailAddresses = new String[]{
            "joe@house.org",
            "joe.farmer@house.org"
        };
        Person jaimie = new Person("jaimie");
        jaimie.emailAddresses = new String[]{
            "jaimie@house.org",
            "jaimie.farmer@house.org",
            "jaimie.ann.farmer@house.org"
        };
        house.people = new Person[]{
            joe,
            jaimie
        };

        String expected = ""
                + "<house>\n"
                + "  <room>\n"
                + "    <name>kitchen</name>\n"
                + "  </room>\n"
                + "  <room>\n"
                + "    <name>bathroom</name>\n"
                + "  </room>\n"
                + "  <separator>---</separator>\n"
                + "  <person>\n"
                + "    <name>joe</name>\n"
                + "    <email>joe@house.org</email>\n"
                + "    <email>joe.farmer@house.org</email>\n"
                + "  </person>\n"
                + "  <person>\n"
                + "    <name>jaimie</name>\n"
                + "    <email>jaimie@house.org</email>\n"
                + "    <email>jaimie.farmer@house.org</email>\n"
                + "    <email>jaimie.ann.farmer@house.org</email>\n"
                + "  </person>\n"
                + "</house>";

        xstream.alias("room", Room.class);
        xstream.alias("house", House.class);
        xstream.alias("person", Person.class);
        xstream.addImplicitArray(House.class, "rooms");
        xstream.addImplicitArray(House.class, "people");
        xstream.addImplicitArray(Person.class, "emailAddresses", "email");

        House serializedHouse = (House)assertBothWays(house, expected);
        assertEquals(house.getPeople(), serializedHouse.getPeople());
        assertEquals(house.getRooms(), serializedHouse.getRooms());
    }

    public static class NumberedRoom extends Room {
        private int number;

        public NumberedRoom(int number) {
            super("room");
            this.number = number;
        }
    }

    public void testArraysWithDerivedElements() {
        House house = new House();
        house.rooms = new Room[] {
            new Room("kitchen"),
            new NumberedRoom(13)
        };

        String expected = ""
                + "<house>\n"
                + "  <room>\n"
                + "    <name>kitchen</name>\n"
                + "  </room>\n"
                + "  <room class=\"chamber\" number=\"13\">\n"
                + "    <name>room</name>\n"
                + "  </room>\n"
                + "  <separator>---</separator>\n"
                + "</house>";

        xstream.alias("house", House.class);
        xstream.alias("chamber", NumberedRoom.class);
        xstream.addImplicitArray(House.class, "rooms", "room");
        xstream.useAttributeFor(int.class);

        House serializedHouse = (House)assertBothWays(house, expected);
        assertEquals(house.getRooms(), serializedHouse.getRooms());
    }

    public static class Aquarium extends StandardObject {
        private String name;
        private String[] fish;

        public Aquarium(String name) {
            this.name = name;
        }
    }

    public void testWithExplicitItemNameMatchingTheNameOfTheFieldWithTheArray() {
        Aquarium aquarium = new Aquarium("hatchery");
        aquarium.fish = new String[]{
            "salmon",
            "halibut",
            "snapper"
        };

        String expected = "" +
                "<aquarium>\n" +
                "  <name>hatchery</name>\n" +
                "  <fish>salmon</fish>\n" +
                "  <fish>halibut</fish>\n" +
                "  <fish>snapper</fish>\n" +
                "</aquarium>";

        xstream.alias("aquarium", Aquarium.class);
        xstream.addImplicitArray(Aquarium.class, "fish", "fish");

        assertBothWays(aquarium, expected);
    }
    
    public void testWithImplicitNameMatchingTheNameOfTheFieldWithTheArray() {
        Aquarium aquarium = new Aquarium("hatchery");
        aquarium.fish = new String[]{
            "salmon",
            "halibut",
            "snapper"
        };

        String expected = "" +
                "<aquarium>\n" +
                "  <name>hatchery</name>\n" +
                "  <fish>salmon</fish>\n" +
                "  <fish>halibut</fish>\n" +
                "  <fish>snapper</fish>\n" +
                "</aquarium>";

        xstream.alias("aquarium", Aquarium.class);
        xstream.alias("fish", String.class);
        xstream.addImplicitArray(Aquarium.class, "fish");

        assertBothWays(aquarium, expected);
    }
    
    public void testWithAliasedItemNameMatchingTheAliasedNameOfTheFieldWithTheArray() {
        Aquarium aquarium = new Aquarium("hatchery");
        aquarium.fish = new String[]{
            "salmon",
            "halibut",
            "snapper"
        };

        String expected = "" +
                "<aquarium>\n" +
                "  <name>hatchery</name>\n" +
                "  <animal>salmon</animal>\n" +
                "  <animal>halibut</animal>\n" +
                "  <animal>snapper</animal>\n" +
                "</aquarium>";

        xstream.alias("aquarium", Aquarium.class);
        xstream.aliasField("animal", Aquarium.class, "fish");
        xstream.addImplicitArray(Aquarium.class, "fish", "animal");

        assertBothWays(aquarium, expected);
    }

    public void testCanBeDeclaredOnlyForMatchingType() {
        try {
            xstream.addImplicitArray(Animal.class, "name");
            fail("Thrown " + InitializationException.class.getName() + " expected");
        } catch (final InitializationException e) {
            assertTrue(e.getMessage().indexOf("declares no collection") >= 0);
        }
    }

    public void testCanBeDeclaredOnlyForMatchingComponentType() {
        try {
            xstream.addImplicitArray(Aquarium.class, "fish", Farm.class);
            fail("Thrown " + InitializationException.class.getName() + " expected");
        } catch (final InitializationException e) {
            assertTrue(e.getMessage().indexOf("array type is not compatible") >= 0);
        }
    }

    public void testWithNullElement() {
        Farm farm = new Farm();
        farm.animals = new Animal[] {
            new Animal("Cow"),
            null,
            new Animal("Sheep")
        };

        String expected = "" +
                "<farm>\n" +
                "  <animal>\n" +
                "    <name>Cow</name>\n" +
                "  </animal>\n" +
                "  <null/>\n" +
                "  <animal>\n" +
                "    <name>Sheep</name>\n" +
                "  </animal>\n" +
                "</farm>";

        xstream.addImplicitArray(Farm.class, "animals");
        assertBothWays(farm, expected);
    }

    public void testWithAliasAndNullElement() {
        Farm farm = new Farm();
        farm.animals = new Animal[] {
            null,
            new Animal("Sheep")
        };

        String expected = "" +
                "<farm>\n" +
                "  <null/>\n" +
                "  <beast>\n" +
                "    <name>Sheep</name>\n" +
                "  </beast>\n" +
                "</farm>";

        xstream.addImplicitArray(Farm.class, "animals", "beast");
        assertBothWays(farm, expected);
    }
    
    static class PrimitiveArray {
        int[] ints; 
    };

    public void testWithPrimitiveArray() {
        PrimitiveArray pa = new PrimitiveArray();
        pa.ints = new int[]{ 47, 11 };

        String expected = "" +
                "<primitives>\n" +
                "  <int>47</int>\n" +
                "  <int>11</int>\n" +
                "</primitives>";

        xstream.alias("primitives", PrimitiveArray.class);
        xstream.addImplicitArray(PrimitiveArray.class, "ints");
        assertBothWays(pa, expected);
    }

    static class MultiDimenstionalArrays {
        Object[][] multiObject; 
        String[][] multiString; 
        int[][] multiInt; 
    };

    public void testMultiDimensionalDirectArray() {
        MultiDimenstionalArrays multiDim = new MultiDimenstionalArrays();
        multiDim.multiString = new String[][]{ 
            new String[]{ "1", "2" }, 
            new String[]{ "a", "b", "c" }
        };

        String expected = "" +
                "<N>\n" +
                "  <string-array>\n" +
                "    <string>1</string>\n" +
                "    <string>2</string>\n" +
                "  </string-array>\n" +
                "  <string-array>\n" +
                "    <string>a</string>\n" +
                "    <string>b</string>\n" +
                "    <string>c</string>\n" +
                "  </string-array>\n" +
                "</N>";

        xstream.alias("N", MultiDimenstionalArrays.class);
        xstream.addImplicitArray(MultiDimenstionalArrays.class, "multiString");
        assertBothWays(multiDim, expected);
    }

    public void testMultiDimensionalPrimitiveArray() {
        MultiDimenstionalArrays multiDim = new MultiDimenstionalArrays();
        multiDim.multiInt = new int[][]{ 
            new int[]{ 1, 2 }, 
            new int[]{ 0, -1, -2 }
        };

        String expected = "" +
                "<N>\n" +
                "  <int-array>\n" +
                "    <int>1</int>\n" +
                "    <int>2</int>\n" +
                "  </int-array>\n" +
                "  <int-array>\n" +
                "    <int>0</int>\n" +
                "    <int>-1</int>\n" +
                "    <int>-2</int>\n" +
                "  </int-array>\n" +
                "</N>";

        xstream.alias("N", MultiDimenstionalArrays.class);
        xstream.addImplicitArray(MultiDimenstionalArrays.class, "multiInt");
        assertBothWays(multiDim, expected);
    }

    public void testMultiDimensionalArrayWithAlias() {
        MultiDimenstionalArrays multiDim = new MultiDimenstionalArrays();
        multiDim.multiObject = new Object[][]{ 
            new String[]{ "1" }, 
            new Object[]{ "a", Boolean.FALSE }
        };

        String expected = "" +
                "<N>\n" +
                "  <M class=\"string-array\">\n" +
                "    <string>1</string>\n" +
                "  </M>\n" +
                "  <M>\n" +
                "    <string>a</string>\n" +
                "    <boolean>false</boolean>\n" +
                "  </M>\n" +
                "</N>";

        xstream.alias("N", MultiDimenstionalArrays.class);
        xstream.addImplicitArray(MultiDimenstionalArrays.class, "multiObject", "M");
        assertBothWays(multiDim, expected);
    }
}