PageRenderTime 51ms CodeModel.GetById 23ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-client/src/test/java/com/google/sitebricks/client/transport/XmlTransportTest.java

http://github.com/dhanji/sitebricks
Java | 81 lines | 64 code | 14 blank | 3 comment | 22 complexity | c43bb14992b8dc7bd1474dfe6033e377 MD5 | raw file
 1package com.google.sitebricks.client.transport;
 2
 3import com.thoughtworks.xstream.XStream;
 4import org.testng.annotations.DataProvider;
 5import org.testng.annotations.Test;
 6
 7import java.io.ByteArrayInputStream;
 8import java.io.ByteArrayOutputStream;
 9import java.io.IOException;
10import java.util.Date;
11
12/**
13 * Unit test for the xml transport supported out of the box.
14 */
15public class XmlTransportTest {
16  private static final String ROBOTS = "robots";
17
18  @DataProvider(name = ROBOTS)
19  Object[][] objects() {
20    return new Object[][] {
21        { new Robot("megatron", new Date(), 333, 12887L, null) },
22        { new Robot("meg aoskdoaks", new Date(192839), 3, 12887L,
23            new Robot("egatron", new Date(), 2193833, 12312887L, null)) },
24        { new Robot("iaisdja aijsd", new Date(1293891283), 333, 12887L, null) },
25    };
26  }
27
28  @Test(dataProvider = ROBOTS)
29  public final void xmlTransport(Robot robot) throws IOException {
30    ByteArrayOutputStream out = new ByteArrayOutputStream();
31    new XStreamXmlTransport(new XStream()).out(out, Robot.class, robot);
32    Robot in = new XStreamXmlTransport(new XStream()).in(new ByteArrayInputStream(out.toByteArray()), Robot.class);
33
34    assert robot.equals(in) : "Xml transport was not balanced";
35  }
36
37  public static class Robot {
38    public Robot() {
39    }
40
41    public Robot(String name, Date time, int age, long looong, Robot pet) {
42      this.name = name;
43      this.time = time;
44      this.age = age;
45      this.looong = looong;
46      this.pet = pet;
47    }
48
49    private String name;
50    private Date time;
51    private int age;
52    private long looong = 123L;
53    private Robot pet;
54
55    @Override
56    public boolean equals(Object o) {
57      if (this == o) return true;
58      if (o == null || getClass() != o.getClass()) return false;
59
60      Robot that = (Robot) o;
61
62      if (age != that.age) return false;
63      if (looong != that.looong) return false;
64      if (name != null ? !name.equals(that.name) : that.name != null) return false;
65      if (pet != null ? !pet.equals(that.pet) : that.pet != null) return false;
66      if (time != null ? !time.equals(that.time) : that.time != null) return false;
67
68      return true;
69    }
70
71    @Override
72    public int hashCode() {
73      int result = name != null ? name.hashCode() : 0;
74      result = 31 * result + (time != null ? time.hashCode() : 0);
75      result = 31 * result + age;
76      result = 31 * result + (int) (looong ^ (looong >>> 32));
77      result = 31 * result + (pet != null ? pet.hashCode() : 0);
78      return result;
79    }
80  }
81}