001: /*
002: * Copyright (C) 2004 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 08. July 2004 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.testutil.TimeZoneChanger;
016:
017: import java.util.ArrayList;
018: import java.util.Calendar;
019: import java.util.List;
020: import java.util.TimeZone;
021:
022: public class DefaultImplementationTest extends AbstractAcceptanceTest {
023:
024: public static class Farm extends StandardObject {
025: int size;
026: List animals = new ArrayList();
027: String name;
028:
029: public Farm(int size, String name) {
030: this .size = size;
031: this .name = name;
032: }
033:
034: public void add(Animal animal) {
035: animals.add(animal);
036: }
037: }
038:
039: public static class Animal extends StandardObject {
040: String name;
041:
042: public Animal(String name) {
043: this .name = name;
044: }
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: TimeZoneChanger.change("GMT");
050: xstream.alias("farm", Farm.class);
051: xstream.alias("animal", Animal.class);
052: xstream.alias("age", Age.class);
053: }
054:
055: /**
056: * @see junit.framework.TestCase#tearDown()
057: */
058: protected void tearDown() throws Exception {
059: TimeZoneChanger.reset();
060: super .tearDown();
061: }
062:
063: public void testArrayList() {
064: Farm farm = new Farm(100, "Old McDonald's");
065: farm.add(new Animal("Cow"));
066: farm.add(new Animal("Sheep"));
067:
068: String expected = "" + "<farm>\n" + " <size>100</size>\n"
069: + " <animals>\n" + " <animal>\n"
070: + " <name>Cow</name>\n" + " </animal>\n"
071: + " <animal>\n" + " <name>Sheep</name>\n"
072: + " </animal>\n" + " </animals>\n"
073: + " <name>Old McDonald's</name>\n" + "</farm>";
074:
075: assertBothWays(farm, expected);
076: }
077:
078: public static class Age extends StandardObject {
079: java.util.Date date;
080:
081: public Age(java.util.Date age) {
082: this .date = age;
083: }
084: }
085:
086: public void testCustomDate() {
087: Calendar cal = Calendar
088: .getInstance(TimeZone.getTimeZone("UTC"));
089: cal.clear();
090: cal.set(2007, Calendar.DECEMBER, 18);
091: Age age = new Age(new java.sql.Date(cal.getTime().getTime()));
092:
093: xstream.addDefaultImplementation(java.sql.Date.class,
094: java.util.Date.class);
095:
096: String expected = "" + "<age>\n"
097: + " <date>2007-12-18</date>\n" + "</age>";
098:
099: assertBothWays(age, expected);
100: }
101: }
|