KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > AbstractAcceptanceTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.io.xml.XppDriver;
5 import junit.framework.TestCase;
6
7 import java.lang.reflect.Array JavaDoc;
8
9 public abstract class AbstractAcceptanceTest extends TestCase {
10
11     protected XStream xstream = new XStream(new XppDriver());
12
13     protected Object JavaDoc assertBothWays(Object JavaDoc root, String JavaDoc xml) {
14         String JavaDoc resultXml = xstream.toXML(root);
15         assertEquals(xml, resultXml);
16         Object JavaDoc resultRoot = xstream.fromXML(resultXml);
17         compareObjects(root, resultRoot);
18         return resultRoot;
19     }
20
21     protected void compareObjects(Object JavaDoc expected, Object JavaDoc actual) {
22         if (expected == null) {
23             assertNull(actual);
24         } else {
25             assertNotNull(actual);
26             if (actual.getClass().isArray()) {
27                 assertArrayEquals(expected, actual);
28             } else {
29                 assertEquals(expected.getClass(), actual.getClass());
30                 assertEquals(expected, actual);
31             }
32         }
33     }
34
35     protected void assertArrayEquals(Object JavaDoc expected, Object JavaDoc actual) {
36         assertEquals(Array.getLength(expected), Array.getLength(actual));
37         for (int i = 0; i < Array.getLength(expected); i++) {
38             assertEquals(Array.get(expected, i), Array.get(actual, i));
39         }
40     }
41 }
42
Popular Tags