/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

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