Package com.skaringa.javaxml.test

Source Code of com.skaringa.javaxml.test.JavaXMLTestClass

package com.skaringa.javaxml.test;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;

import com.skaringa.javaxml.DeserializerException;
import com.skaringa.javaxml.ObjectTransformer;
import com.skaringa.javaxml.ObjectTransformerFactory;
import com.skaringa.javaxml.PropertyKeys;
import com.skaringa.util.SAXValidator;

/**
* JUnit test case for Skaringa.
*
*/
public final class JavaXMLTestClass extends TestCase {
  private static final String XML = "<?xml version=\"1.0\"";
  private static final String XSD_NS =
    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
  private static final String XSI_NS =
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
  private static final String XML_NS = XSD_NS + " " + XSI_NS;

  private ObjectTransformer trans;

  /**
   * Main
   * @param args
   */
  public static void main(String[] args) {
    junit.textui.TestRunner.run(JavaXMLTestClass.class);
  }

  /**
   * @see TestCase#TestCase(java.lang.String)
   */
  public JavaXMLTestClass(String name) {
    super(name);
  }

  /**
  * @return test suite.
  */
  public static Test suite() {
    return new TestSuite(JavaXMLTestClass.class);
  }

  /**
   * @see TestCase#setUp()
   */
  protected void setUp() throws Exception {
    super.setUp();
    trans = ObjectTransformerFactory.getInstance().getImplementation();
  }

  /**
   * Check the serialized XML string for some components.
   * An exact string match isn't possible because different transformers
   * produce slightly different XML (order of attributes, ...).
   * @param testName The name of the test.
   * @param serial The serialized string.
   * @param value The value element string that is expected to be in serial.
   * @throws Exception If the test failes.
   */
  private void checkSerializedString(
    String testName,
    String serial,
    String value)
    throws Exception {
    assertTrue(
      testName + ": doesn't start with XML declaration: " + serial,
      serial.startsWith(XML));
    assertTrue(
      testName + ": doesn't contain XSD namespace decl: " + serial,
      serial.indexOf(XSD_NS) > 0);
    assertTrue(
      testName + ": doesn't contain XSI namespace decl: " + serial,
      serial.indexOf(XSI_NS) > 0);
    assertTrue(
      testName + ": doesn't contain " + value + ": " + serial,
      serial.indexOf(value) > 0);
  }

  /**
   * Check the serialized XML string for some components.
   * An exact string match isn't possible because different transformers
   * produce slightly different XML (order of attributes, ...).
   * @param testName The name of the test.
   * @param serial The serialized string.
   * @param value The value element string that is expected to be in serial.
   * @throws Exception If the test failes.
   */
  private void checkSerializedStringNoXmlns(
    String testName,
    String serial,
    String value)
    throws Exception {
    assertTrue(
      testName + ": doesn't start with XML declaration: " + serial,
      serial.startsWith(XML));
    assertTrue(
      testName + ": contains XSD namespace decl: " + serial,
      serial.indexOf(XSD_NS) == -1);
    assertTrue(
      testName + ": contains XSI namespace decl: " + serial,
      serial.indexOf(XSI_NS) == -1);
    assertTrue(
      testName + ": doesn't contain " + value + ": " + serial,
      serial.indexOf(value) > 0);
  }

  /**
   * Test (de)serialization of int.
   * @throws Exception If the test failes.
   */
  public void testInt() throws Exception {
    IntObj x = new IntObj(10);
    String value = "<value xsi:type=\"xsd:int\">10</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testInt", serial, value);
    IntObj y = (IntObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of boolean.
   * @throws Exception If the test failes.
   */
  public void testBoolean() throws Exception {
    BooleanObj x = new BooleanObj(true);
    String value = "<value xsi:type=\"xsd:boolean\">true</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testBoolean", serial, value);
    BooleanObj y = (BooleanObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of char.
   * @throws Exception If the test failes.
   */
  public void testChar() throws Exception {
    CharObj x = new CharObj('X');
    String value = "<value xsi:type=\"char\">X</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testChar", serial, value);
    CharObj y = (CharObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of control characters.
   * @throws Exception If the test failes.
   */
  public void testControlChar() throws Exception {
    for (int i = 0; i < 32; ++i) {
      CharObj x = new CharObj((char) i);
      String serial = trans.serializeToString(x);
      CharObj y = (CharObj) trans.deserializeFromString(serial);
      assertEquals("wrong deserializer for char #" + i, x, y);
    }
  }

  /**
   * Test (de)serialization of double.
   * @throws Exception If the test failes.
   */
  public void testDouble() throws Exception {
    DoubleObj x = new DoubleObj(1.23456789);
    String value = "<value xsi:type=\"xsd:double\">1.23456789</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testDouble", serial, value);
    DoubleObj y = (DoubleObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of float.
   * @throws Exception If the test failes.
   */
  public void testFloat() throws Exception {
    FloatObj x = new FloatObj(1.234f);
    String value = "<value xsi:type=\"xsd:float\">1.234</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testFloat", serial, value);
    FloatObj y = (FloatObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of long.
   * @throws Exception If the test failes.
   */
  public void testLong() throws Exception {
    LongObj x = new LongObj(10);
    String value = "<value xsi:type=\"xsd:long\">10</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testLong", serial, value);
    LongObj y = (LongObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of short.
   * @throws Exception If the test failes.
   */
  public void testShort() throws Exception {
    ShortObj x = new ShortObj(10);
    String value = "<value xsi:type=\"xsd:short\">10</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testShort", serial, value);
    ShortObj y = (ShortObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of string.
   * @throws Exception If the test failes.
   */
  public void testString() throws Exception {
    StringObj x = new StringObj();
    String value =
      "<value xsi:type=\"xsd:string\">&lt;yxcvbnm,.-#���<?char 8?>lkjhgfdsaqwertzuiop�?=)(/&amp;%$�\"!���</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testString", serial, value);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of strings that contain XML markup.
   * @throws Exception If the test failes.
   */
  public void testXMLString() throws Exception {
    String xmlstr = trans.serializeToString(new StringObj());
    StringObj x = new StringObj(xmlstr);
    String serial = trans.serializeToString(x);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of dates in time zones east of Greenwich.
   * @throws Exception If the test failes.
   */
  public void testDateEast() throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
    DateObj x = new DateObj(0);
    String value =
      "<value xsi:type=\"xsd:dateTime\">1970-01-01T01:00:00+01:00</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testDateEast", serial, value);
    DateObj y = (DateObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of dates in time zones west of Greenwich.
   * @throws Exception If the test failes.
   */
  public void testDateWest() throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("GMT-05"));
    DateObj x = new DateObj(2000);
    String value =
      "<value xsi:type=\"xsd:dateTime\">1969-12-31T19:00:02-05:00</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testDateWest", serial, value);
    DateObj y = (DateObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of BigDecimal.
   * @throws Exception If the test failes.
   */
  public void testBigDecimal() throws Exception {
    BigDecimal x = new BigDecimal("42.27");
    String serial = trans.serializeToString(x);
    checkSerializedString("testBigDecimal", serial, "xsi:type=\"xsd:decimal");
    checkSerializedString("testBigDecimal", serial, "42.27</xsd:decimal>");
    BigDecimal y = (BigDecimal) trans.deserializeFromString(serial);
    assertEquals("deserialization error,", x, y);
  }

  /**
   * Test (de)serialization of BigInteger.
   * @throws Exception If the test failes.
   */
  public void testBigInteger() throws Exception {
    BigInteger x = new BigInteger("4227");
    String serial = trans.serializeToString(x);
    checkSerializedString("testBigInteger", serial, "xsi:type=\"xsd:integer\"");
    checkSerializedString("testBigInteger", serial, "4227</xsd:integer>");
    BigInteger y = (BigInteger) trans.deserializeFromString(serial);
    assertEquals("deserialization error,", x, y);
  }

  /**
   * Test (de)serialization of objects with final members.
   * @throws Exception If the test failes.
   */
  public void testFinalVar() throws Exception {
    FinalVarObj x = new FinalVarObj();
    String value = "<value xsi:type=\"xsd:int\">77</value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testFinalVar", serial, value);
    FinalVarObj y = (FinalVarObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of an empty string.
   * @throws Exception If the test failes.
   */
  public void testEmptyString() throws Exception {
    StringObj x = new StringObj("");
    String serial = trans.serializeToString(x);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x.value, y.value);
  }

  /**
   * Test (de)serialization of null strings.
   * @throws Exception If the test failes.
   */
  public void testNullString() throws Exception {
    StringObj x = new StringObj(null);
    String serial = trans.serializeToString(x);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x.value, y.value);
  }

  /**
   * Test (de)serialization of null arrays.
   * @throws Exception If the test failes.
   */
  public void testNullArray() throws Exception {
    ArrayObj x = new ArrayObj(null);
    String serial = trans.serializeToString(x);
    ArrayObj y = (ArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of arrays with null elements.
   * @throws Exception If the test failes.
   */
  public void testNullArrayElements() throws Exception {
    ComplexArrayObj x = new ComplexArrayObj(new StringObj[3]);
    String serial = trans.serializeToString(x);
    ComplexArrayObj y = (ComplexArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of null values.
   * @throws Exception If the test failes.
   */
  public void testNull() throws Exception {
    Object x = null;
    String serial = trans.serializeToString(x);
    Object y = trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of null collection.
   * @throws Exception If the test failes.
   */
  public void testNullCollection() throws Exception {
    VectorObj x = new VectorObj(null);
    String serial = trans.serializeToString(x);
    VectorObj y = (VectorObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of collections with null elements.
   * @throws Exception If the test failes.
   */
  public void testNullCollectionElements() throws Exception {

    Collection x = new ArrayList();
    x.add(null);
    x.add("1");
    String serial = trans.serializeToString(x);
    Collection y = (Collection) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization with PropertyKeys.OMIT_XSI_NIL.
   * @throws Exception If the test failes.
   */
  public void testOmitNil() throws Exception {
    StringObj x = new StringObj(null);
    String value = "<value xsi:type=\"xsd:string\"/>";
    trans.setProperty(PropertyKeys.OMIT_XSI_NIL, "true");
    String serial = trans.serializeToString(x);
    checkSerializedString("testOmitNil", serial, value);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    // y is not equal to x because of OMIT_NIL_ELEMENTS!!
  }

  /**
   * Test (de)serialization with PropertyKeys.OMIT_XSI_TYPE.
   * @throws Exception If the test failes.
   */
  public void testOmitType() throws Exception {
    StringObj x = new StringObj("string");
    String value = "<value>string</value>";
    trans.setProperty(PropertyKeys.OMIT_XSI_TYPE, "true");
    trans.setProperty(PropertyKeys.OMIT_XSI_NIL, "true");
    String serial = trans.serializeToString(x);
    checkSerializedStringNoXmlns("testOmitType", serial, value);
    StringObj y = (StringObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of arrays.
   * @throws Exception If the test failes.
   */
  public void testArray() throws Exception {
    ArrayObj x = new ArrayObj(3);
    String value =
      "<value xsi:type=\"array_of_int\"><el xsi:type=\"xsd:int\">0</el><el xsi:type=\"xsd:int\">1</el><el xsi:type=\"xsd:int\">2</el></value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testArray", serial, value);
    ArrayObj y = (ArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of arrays as root elements.
   * @throws Exception If the test failes.
   */
  public void testArrayAsRoot() throws Exception {
    String[] x = new String[] { "alpha", "beta", "gamma" };
    String serial = trans.serializeToString(x);
    String[] y = (String[]) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", Arrays.asList(x), Arrays.asList(y));
  }

  /**
   * Test if arrays with the old "array" type name can be still deserialized.
   * @throws Exception If the test failes.
   */
  public void testArrayBackwardCompatibility() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "yes");
    ArrayObj x = new ArrayObj(3);
    String name = x.getClass().getName();

    String serial =
      "<"
        + name
        + " xsi:type=\""
        + name
        + "\" "
        + XML_NS
        + "><value xsi:type=\"array\"><el xsi:type=\"xsd:int\">0</el><el xsi:type=\"xsd:int\">1</el><el xsi:type=\"xsd:int\">2</el></value></"
        + name
        + ">";

    ArrayObj y = (ArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of arrays with PropertyKeys.OMIT_XSI_TYPE.
   * @throws Exception If the test failes.
   */
  public void testArrayOmitType() throws Exception {
    ArrayObj x = new ArrayObj(3);
    String value = "<value><el>0</el><el>1</el><el>2</el></value>";
    trans.setProperty(PropertyKeys.OMIT_XSI_TYPE, "true");
    trans.setProperty(PropertyKeys.OMIT_XSI_NIL, "true");
    String serial = trans.serializeToString(x);
    checkSerializedStringNoXmlns("testArrayOmitType", serial, value);
    ArrayObj y = (ArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of huge arrays.
   * @throws Exception If the test failes.
   */
  public void testHugeArray() throws Exception {
    ArrayObj x = new ArrayObj(10000);
    String serial = trans.serializeToString(x);
    ArrayObj y = (ArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of vector.
   * @throws Exception If the test failes.
   */
  public void testVector() throws Exception {
    VectorObj x = new VectorObj(3);
    String value =
      "<value xsi:type=\"java.util.Vector\"><cel xsi:type=\"xsd:string\">a</cel><cel xsi:type=\"xsd:string\">b</cel><cel xsi:type=\"xsd:string\">c</cel></value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testVector", serial, value);
    VectorObj y = (VectorObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of set.
   * @throws Exception If the test failes.
   */
  public void testSet() throws Exception {
    SetObj x = new SetObj(3);
    String value =
      "<value xsi:type=\"java.util.TreeSet\"><cel xsi:type=\"xsd:string\">0</cel><cel xsi:type=\"xsd:string\">1</cel><cel xsi:type=\"xsd:string\">2</cel></value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testSet", serial, value);
    SetObj y = (SetObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of int vector.
   * @throws Exception If the test failes.
   */
  public void testIntVector() throws Exception {
    IntVectorObj x = new IntVectorObj(3);
    String value =
      "<value xsi:type=\"java.util.Vector\"><cel xsi:type=\"xsd:int\">1</cel><cel xsi:type=\"xsd:int\">2</cel><cel xsi:type=\"xsd:int\">3</cel></value>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testIntVector", serial, value);
    IntVectorObj y = (IntVectorObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of map.
   * @throws Exception If the test failes.
   */
  public void testMap() throws Exception {
    MapObj x = new MapObj(3);
    String value =
      "<myMap xsi:type=\"java.util.TreeMap\"><mapentry xsi:type=\"MapEntry\"><key xsi:type=\"xsd:string\">key1</key><value xsi:type=\"xsd:string\">value1</value></mapentry><mapentry xsi:type=\"MapEntry\"><key xsi:type=\"xsd:string\">key2</key><value xsi:type=\"xsd:string\">value2</value></mapentry><mapentry xsi:type=\"MapEntry\"><key xsi:type=\"xsd:string\">key3</key><value xsi:type=\"java.lang.Object\" xsi:nil=\"true\"/></mapentry></myMap>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testMap", serial, value);
    MapObj y = (MapObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of complex map.
   * @throws Exception If the test failes.
   */
  public void testComplexMap() throws Exception {
    ComplexMapObj x = new ComplexMapObj(3);
    String serial = trans.serializeToString(x);
    ComplexMapObj y = (ComplexMapObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of Locale.
   * java.util.Locale has three constructors; test them all.
   * @throws Exception If the test failes.
   */
  public void testLocale() throws Exception {
    Locale x = Locale.ENGLISH;
    String serial = trans.serializeToString(x);
    Locale y = (Locale) trans.deserializeFromString(serial);
    assertEquals("Error serializing 1-arg Locale: ", x, y);

    x = new Locale("en", "US");
    serial = trans.serializeToString(x);
    y = (Locale) trans.deserializeFromString(serial);
    assertEquals("Error serializing 2-arg Locale: ", x, y);

    x = new Locale("en", "US", "Skaringa");
    serial = trans.serializeToString(x);
    y = (Locale) trans.deserializeFromString(serial);
    assertEquals("Error serializing 3-arg Locale: ", x, y);
  }

  /**
   * Test (de)serialization of complex array.
   * @throws Exception If the test failes.
   */
  public void testComplexArray() throws Exception {
    ComplexArrayObj x = new ComplexArrayObj(3);
    String serial = trans.serializeToString(x);
    ComplexArrayObj y = (ComplexArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of complex array with PropertyKeys.OMIT_XSI_TYPE.
   * @throws Exception If the test failes.
   */
  public void testComplexArrayOmitType() throws Exception {
    ComplexArrayObj x = new ComplexArrayObj(10);
    trans.setProperty(PropertyKeys.OMIT_XSI_TYPE, "true");
    trans.setProperty(PropertyKeys.OMIT_XSI_NIL, "true");
    trans.setProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
    String serial = trans.serializeToString(x);
    ComplexArrayObj y = (ComplexArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of complex object.
   * @throws Exception If the test failes.
   */
  public void testComplex() throws Exception {
    ComplexObj x = new ComplexObj(3);
    String serial = trans.serializeToString(x);

    //OutputStream out = new FileOutputStream("complex.xml");
    //trans.serialize(x, new StreamResult(out));

    ComplexObj y = (ComplexObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of derived classes.
   * @throws Exception If the test failes.
   */
  public void testDerived() throws Exception {
    DerivedObj x = new DerivedObj(1, "hi");
    String serial = trans.serializeToString(x);
    DerivedObj y = (DerivedObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of derived array.
   * @throws Exception If the test failes.
   */
  public void testDerivedArray() throws Exception {
    DerivedArrayObj x = new DerivedArrayObj(5);
    String serial = trans.serializeToString(x);
    DerivedArrayObj y = (DerivedArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test (de)serialization of objects with references.
   * @throws Exception If the test failes.
   */
  public void testReferences() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "no");
    RefObj x = new RefObj(new DoubleObj(1.23));
    String serial = trans.serializeToString(x);
    RefObj y = (RefObj) trans.deserializeFromString(serial);
    assertTrue(
      "Members of RefObj doesn't point to the same instance.",
      y.getA() == y.getB());
  }
 
  /**
   * Test the behaviour if serializing references with omit id.
   * (Bug 1149984)
   * @throws Exception If the test failes.
   */
  public void testReferencesAndOmitId() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "yes");
   
    StringObj s = new StringObj("test");
    ArrayList x = new ArrayList();
    x.add(s);
    x.add(s);
    String serial = trans.serializeToString(x);
    ArrayList y = (ArrayList) trans.deserializeFromString(serial);
    assertEquals("length", x.size(), y.size());
    for (int i = 0; i < x.size(); ++i)
    assertEquals("el " + i, x.get(i), y.get(i));
  }

  /**
   * Test (de)serialization of unknown types.
   * @throws Exception If the test failes.
   */
  public void testDeserializeInvalidType() throws Exception {
    String serial =
      "<invalid xsi:type=\"gnulf\" "
        + XML_NS
        + "><_id xsi:type=\"xsd:string\">qwertz</_id></invalid>";

    try {
      Object obj = trans.deserializeFromString(serial);
      fail("an exception should has been thrown.");
    }
    catch (DeserializerException e) {
      //System.err.println(e); // OK
    }
  }

  /**
   * Test the serialization into a DOM tree
   * @throws Exception If the test failes.
   */
  public void testSerializeToDOM() throws Exception {
    DocumentBuilder docBuilder =
      DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    ComplexObj x = new ComplexObj(3);
    trans.serialize(x, new DOMResult(doc));

    ComplexObj y = (ComplexObj) trans.deserialize(new DOMSource(doc));
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test schema generation and validation.
   * @throws Exception If the test failes.
   */
  public void testGenerateSchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(IntObj.class, new StreamResult(str));
    String schema = str.toString();
    assertTrue("schema: starts with XML declaration", schema.startsWith(XML));
    assertTrue(
      "schema: contains XSD namespace decl",
      schema.indexOf(XSD_NS) > 0);
    assertTrue(
      "schema: contains <xsd:schema ",
      schema.indexOf("<xsd:schema ") > 0);
    // validate
    IntObj x = new IntObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation into a DOM tree.
   * @throws Exception If the test failes.
   */
  public void testGenerateSchemaToDOM() throws Exception {
    DocumentBuilder docBuilder =
      DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    trans.writeXMLSchema(IntObj.class, new DOMResult(doc));
  }

  /**
   * Test schema generation and validation of complex objects.
   * @throws Exception If the test failes.
   */
  public void testGenerateComplexSchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(ComplexObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    ComplexObj x = new ComplexObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of char objects.
   * @throws Exception If the test failes.
   */
  public void testGenerateCharSchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(CharObj.class, new StreamResult(str));
    String schema = str.toString();
    //System.out.println(schema);

    // validate
    CharObj x = new CharObj('x');
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of maps.
   * @throws Exception If the test failes.
   */
  public void testGenerateMapSchema() throws Exception {
    StringWriter str = new StringWriter();
    Vector componentTypes = new Vector();
    componentTypes.add(TreeMap.class);
    trans.writeXMLSchema(MapObj.class, componentTypes, new StreamResult(str));
    String schema = str.toString();

    // validate
    MapObj x = new MapObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of complex maps.
   * @throws Exception If the test failes.
   */
  public void testGenerateComplexMapSchema() throws Exception {
    StringWriter str = new StringWriter();
    Collection componentTypes = new Vector();
    componentTypes.add(StringObj.class);
    componentTypes.add(ComplexObj.class);
    componentTypes.add(Collection.class);
    componentTypes.add(ArrayList.class);
    componentTypes.add(DateObj.class);
    componentTypes.add(HashMap.class);
    trans.writeXMLSchema(
      ComplexMapObj.class,
      componentTypes,
      new StreamResult(str));
    String schema = str.toString();

    // validate
    ComplexMapObj x = new ComplexMapObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of complex arrays.
   * @throws Exception If the test failes.
   */
  public void testGenerateComplexArraySchema() throws Exception {
    StringWriter str = new StringWriter();
    Collection componentTypes = new Vector();
    componentTypes.add(StringObj.class);
    trans.writeXMLSchema(
      ComplexArrayObj.class,
      componentTypes,
      new StreamResult(str));
    String schema = str.toString();

    // validate
    ComplexArrayObj x = new ComplexArrayObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of arrays.
   * @throws Exception If the test failes.
   */
  public void testGenerateArraySchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(ArrayObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    ArrayObj x = new ArrayObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of derived objects.
   * @throws Exception If the test failes.
   */
  public void testGenerateDerivedObjSchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(DerivedObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    DerivedObj x = new DerivedObj(0, "0");
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of sets.
   * @throws Exception If the test failes.
   */
  public void testGenerateSetSchema() throws Exception {
    StringWriter str = new StringWriter();
    Collection componentTypes = new Vector();
    componentTypes.add(java.util.TreeSet.class);
    trans.writeXMLSchema(SetObj.class, componentTypes, new StreamResult(str));
    String schema = str.toString();

    // validate
    SetObj x = new SetObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test schema generation and validation of objects with references.
   * @throws Exception If the test failes.
   */
  public void testGenerateSchemaWithReferences() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(RefObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    RefObj x = new RefObj(new DoubleObj(1.23));
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Validate an instance against its schema.
   * @param schema The schema.
   * @param serial The instance.
   * @throws IOException If instance or schema can't be read.
   */
  private void validate(String schema, String serial) throws IOException {
    try {
      SAXValidator.validate(
        new InputSource(new StringReader(serial)),
        new InputSource(new StringReader(schema)));
    }
    catch (SAXNotRecognizedException ex) {
      System.out.println(
        "WARNING: can't validate against schema. Need a JAXP 1.2 parser for this!");
    }
    catch (ParserConfigurationException ex) {
      System.out.println(
        "WARNING: can't validate against schema. Need a validation parser for this!");
    }
    catch (SAXException ex) {
      System.err.println("Instance doesn't validate against the schema: " + ex);
      System.err.println(schema);
      System.err.println(serial);
      fail("Instance doesn't validate against the schema: " + ex);
    }
  }

  /**
   * Test the postprocessing transformation.
   * @throws Exception If the test failes.
   */
  public void testTransformCopy() throws Exception {
    // an xsl stylesheet that simply copies the input to the output
    String xsltCopy =
      "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
        + "<xsl:template match=\"@*|node()\">"
        + " <xsl:copy>"
        + "   <xsl:apply-templates select=\"@*|node()\"/>"
        + " </xsl:copy>"
        + "</xsl:template>"
        + "</xsl:stylesheet>";

    IntObj x = new IntObj(3333);
    trans.setPostprocessorInstruction(
      new StreamSource(new StringReader(xsltCopy)));
    IntObj y = (IntObj) trans.transform(x);

    assertEquals("transformation copy failed", x, y);

    // again
    y = (IntObj) trans.transform(x);
    assertEquals("2nd transformation copy failed", x, y);
  }

  /**
  * Test the handling of cyclic references.
  * @throws Exception If the test failes.
  */
  public void testCyclic() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "no");
    ParentObj x = new ParentObj();
    x.createChild();
    String serial = trans.serializeToString(x);
    ParentObj y = (ParentObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
  * Test the schema generation for classes with cyclic references.
  * @throws Exception If the test failes.
  */
  public void testCyclicSchema() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "no");
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(ParentObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    ParentObj x = new ParentObj();
    x.createChild();
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test deserialization of the "strange" XML data types that
   * Skaringa doesn't use for serialization.
   * @throws Exception If the test failes.
   */
  public void testOtherTypes() throws Exception {
    String serial =
      "<Strange xsi:type=\"com.skaringa.javaxml.test.StrangeTypesObj\" "
        + XML_NS
        + ">"
        + " <nonPosInt xsi:type=\"xsd:nonPositiveInteger\">-27</nonPosInt>"
        + " <nonNegInt xsi:type=\"xsd:nonNegativeInteger\">27</nonNegInt>"
        + " <posInt xsi:type=\"xsd:positiveInteger\">42</posInt>"
        + " <negInt xsi:type=\"xsd:negativeInteger\">-42</negInt>"
        + " <uLong xsi:type=\"xsd:unsignedLong\">42424242</uLong>"
        + " <uInt xsi:type=\"xsd:unsignedInt\">424242</uInt>"
        + " <uShort xsi:type=\"xsd:unsignedShort\">4242</uShort>"
        + " <uByte xsi:type=\"xsd:unsignedByte\">42</uByte>"
        + " <date xsi:type=\"xsd:date\">2003-04-01</date>"
        + "</Strange>";

    StrangeTypesObj y = (StrangeTypesObj) trans.deserializeFromString(serial);

    assertEquals(new Integer(-27), y.getNonPosInt());
    assertEquals(new Integer(27), y.getNonNegInt());
    assertEquals(new Integer(42), y.getPosInt());
    assertEquals(new Integer(-42), y.getNegInt());
    assertEquals(new Long(42424242), y.getULong());
    assertEquals(new Integer(424242), y.getUInt());
    assertEquals(new Short((short) 4242), y.getUShort());
    assertEquals(new Byte((byte) 42), y.getUByte());
    assertNotNull(y.getDate());

    Calendar cal = Calendar.getInstance();
    cal.setTime(y.getDate());
    cal.setTimeZone(TimeZone.getDefault());
    assertEquals(2003, cal.get(Calendar.YEAR));
    assertEquals(Calendar.APRIL, cal.get(Calendar.MONTH));
    assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
  }

  /**
   * Test that an XSLT transformer can figure out whether a nil
   * attribute in Skaringa's output has a value of true.  This
   * implicitly tests namespace processing since type attributes are
   * in the http://www.w3.org/2001/XMLSchema-instance namespace.
   * @throws Exception If the test failes.
   */
  public void testTransform() throws Exception {
    String yesNil = "ATTR IS NIL";
    String noNil = "ATTR NOT NIL";
    String serial =
      "<Strange xsi:type=\"com.skaringa.javaxml.test.StrangeTypesObj\" "
        + XML_NS
        + ">"
        + " <nonPosInt xsi:type=\"xsd:nonPositiveInteger\">-27</nonPosInt>"
        + " <nonNegInt xsi:type=\"xsd:nonNegativeInteger\">27</nonNegInt>"
        + " <posInt xsi:type=\"xsd:positiveInteger\">42</posInt>"
        + " <negInt xsi:type=\"xsd:negativeInteger\" xsi:nil=\"true\" />"
        + " <uLong xsi:type=\"xsd:unsignedLong\">42424242</uLong>"
        + " <uInt xsi:type=\"xsd:unsignedInt\">424242</uInt>"
        + " <uShort xsi:type=\"xsd:unsignedShort\">4242</uShort>"
        + " <uByte xsi:type=\"xsd:unsignedByte\">42</uByte>"
        + " <date xsi:type=\"xsd:date\">2003-04-01</date>"
        + "</Strange>";

    // an xsl stylesheet that tests if the negInt element has an
    // xsi:nil attribute
    String xsltCheck =
      "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
        + XML_NS
        + " version=\"1.0\" >"
        + " <xsl:template match=\"/"
        + StrangeTypesObj.class.getName()
        + "/negInt\">"
        + "  <xsl:choose>"
        + "   <xsl:when test=\"@xsi:nil = 'true'\">"
        + "    <Result xsi:type=\"xsd:string\">"
        + yesNil
        + "</Result>"
        + "   </xsl:when>"
        + "   <xsl:otherwise>"
        + "    <Result xsi:type=\"xsd:string\">"
        + noNil
        + "</Result>"
        + "   </xsl:otherwise>"
        + "  </xsl:choose>"
        + " </xsl:template>"
        + "</xsl:stylesheet>";

    StrangeTypesObj y = (StrangeTypesObj) trans.deserializeFromString(serial);
    assertNotNull(y);
    assertNull(y.getNegInt());

    trans.setPostprocessorInstruction(
      new StreamSource(new StringReader(xsltCheck)));
    String z = (String) trans.transform(y);
    assertEquals(
      "transformer didn't match @xsi:nil in skaringa output,",
      yesNil,
      z);
  }

  /**
   * Test Preprocessing.
   * @throws Exception If the test failes.
   */
  public void testPreprocessing() throws Exception {
    String serial =
      "<items>"
        + "<item>a</item>"
        + "<item>b</item>"
        + "<item>c</item>"
        + "</items>";

    String xslt =
      "<xsl:transform xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
        + XML_NS
        + " version=\"1.0\" >"
        + "<xsl:template match = \"items\">"
        + " <com.skaringa.javaxml.test.VectorObj xsi:type = \"com.skaringa.javaxml.test.VectorObj\">"
        + "  <value xsi:type = \"java.util.Vector\">"
        + "   <xsl:apply-templates select = \"item\"/>"
        + "  </value>"
        + " </com.skaringa.javaxml.test.VectorObj>"
        + "</xsl:template>"
        + "<xsl:template match = \"item\">"
        + " <cel xsi:type = \"xsd:string\">"
        + "  <xsl:value-of select = \".\"/>"
        + " </cel>"
        + "</xsl:template>"
        + "</xsl:transform>";

    trans.setPreprocessorInstruction(new StreamSource(new StringReader(xslt)));
    VectorObj y = (VectorObj) trans.deserializeFromString(serial);

    VectorObj x = new VectorObj(3);
    assertEquals("preprocessing failed", x, y);

    // again
    assertEquals(
      "preprocessing failed 2",
      x,
      trans.deserializeFromString(serial));
  }

  /**
   * Test Postprocessing.
   * @throws Exception If the test failes.
   */
  public void testPostprocessing() throws Exception {
    String expectedXML =
      "<items>"
        + "<item>a</item>"
        + "<item>b</item>"
        + "<item>c</item>"
        + "</items>";

    String xslt =
      "<xsl:transform xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
        + " version=\"1.0\" >"
        + "<xsl:output omit-xml-declaration=\"yes\"/>"
        + "<xsl:template match = \"com.skaringa.javaxml.test.VectorObj\">"
        + " <items>"
        + "   <xsl:apply-templates select = \"value/cel\"/>"
        + " </items>"
        + "</xsl:template>"
        + "<xsl:template match = \"value/cel\">"
        + " <item>"
        + "  <xsl:value-of select = \".\"/>"
        + " </item>"
        + "</xsl:template>"
        + "</xsl:transform>";

    VectorObj x = new VectorObj(3);

    trans.setPostprocessorInstruction(new StreamSource(new StringReader(xslt)));
    String serial = trans.serializeToString(x);

    assertEquals("postprocessing failed", expectedXML, serial);

    // again
    serial = trans.serializeToString(x);
    assertEquals("postprocessing failed 2", expectedXML, serial);
  }

  /**
   * Test skaReadResolve and skaWriteReplace
   * @throws Exception If the test failes.
   */
  public void testResolveAndReplace() throws Exception {
    PersonObj p = new PersonObj("name", "password");
    String serial = trans.serializeToString(p);
    PersonObj q = (PersonObj) trans.deserializeFromString(serial);
    assertEquals("(de)serialization failed", p._name, q._name);
    assertEquals("skaReadResolve failed", 4, q._nameLen);
    assertEquals("skaWriteReplace failed", "", q._password);
  }

  /**
   * Test skaReadResolve and skaWriteReplace with references
   * @throws Exception If the test failes.
   */
  public void testResolveAndReplaceWithReferences() throws Exception {
    Collection persons = new ArrayList();
    PersonObj p = new PersonObj("name", "password");
    persons.add(p);
    persons.add(p);
    String serial = trans.serializeToString(persons);

    PersonObj._readResolveCount = 0;
    Collection qpersons = (Collection) trans.deserializeFromString(serial);
    assertEquals(2, persons.size());
    assertEquals("readResolveCount", 1, PersonObj._readResolveCount);

    Iterator ipq = qpersons.iterator();
    PersonObj q = (PersonObj) ipq.next();
    assertEquals("(de)serialization failed (1)", p._name, q._name);
    assertEquals("skaReadResolve failed (1)", 4, q._nameLen);
    assertEquals("skaWriteReplace failed (1)", "", q._password);
    PersonObj r = (PersonObj) ipq.next();
    assertEquals("(de)serialization failed (2)", p._name, r._name);
    assertEquals("skaReadResolve failed (2)", 4, r._nameLen);
    assertEquals("skaWriteReplace failed (2)", "", r._password);
  }

  /**
   * Test skaReadResolve and skaWriteReplace with references and omit id.
   * (Bug 118822)
   * @throws Exception If the test failes.
   */
  public void testResolveAndReplaceWithReferencesAndOmitId() throws Exception {
    trans.setProperty(PropertyKeys.OMIT_ID, "yes");
   
    Collection persons = new ArrayList();
    PersonObj p = new PersonObj("name", "password");
    persons.add(p);
    persons.add(p);
    String serial = trans.serializeToString(persons);

    PersonObj._readResolveCount = 0;
    Collection qpersons = (Collection) trans.deserializeFromString(serial);
    assertEquals(2, persons.size());
    assertEquals("readResolveCount", 2, PersonObj._readResolveCount);

    Iterator ipq = qpersons.iterator();
    PersonObj q = (PersonObj) ipq.next();
    assertEquals("(de)serialization failed (1)", p._name, q._name);
    assertEquals("skaReadResolve failed (1)", 4, q._nameLen);
    assertEquals("skaWriteReplace failed (1)", "", q._password);
    PersonObj r = (PersonObj) ipq.next();
    assertEquals("(de)serialization failed (2)", p._name, r._name);
    assertEquals("skaReadResolve failed (2)", 4, r._nameLen);
    assertEquals("skaWriteReplace failed (2)", "", r._password);
  }

  /**
   * Test (de)serialization of multi arrays.
   * @throws Exception If the test failes.
   */
  public void testMultiArrayObj() throws Exception {
    MultiArrayObj ma = new MultiArrayObj(4);
    String serial = trans.serializeToString(ma);
    MultiArrayObj ma2 = (MultiArrayObj) trans.deserializeFromString(serial);
    assertEquals(ma, ma2);
  }

  /**
   * Test schema generation and validation of multi arrays.
   * @throws Exception If the test failes.
   */
  public void testMultiArraySchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(MultiArrayObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    MultiArrayObj x = new MultiArrayObj(3);
    String serial = trans.serializeToString(x);
    validate(schema, serial);
  }

  /**
   * Test (de)serialization of inner classes.
   * @throws Exception If the test failes.
   */
  public void testInnerClass() throws Exception {
    InnerClassObj ic = new InnerClassObj("gnulf");
    String serial = trans.serializeToString(ic);
    InnerClassObj ic2 = (InnerClassObj) trans.deserializeFromString(serial);
    assertEquals(ic, ic2);
  }

  /**
   * Test schema generation and validation of inner classes.
   * @throws Exception If the test failes.
   */
  public void testInnerClassSchema() throws Exception {
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(InnerClassObj.class, new StreamResult(str));
    String schema = str.toString();

    // validate
    InnerClassObj ic = new InnerClassObj("gnulf");
    String serial = trans.serializeToString(ic);
    validate(schema, serial);
  }

  /**
   * Make a test collection.
   * @param coll An empty collection of the concrete type.
   * @return The collection filled with some values.
   */
  private Collection makeTestCollection(Collection coll) {
    coll.add("x1");
    coll.add("x2");
    coll.add("x3");
    return coll;
  }

  /**
   * Test (de)serialization of a collection.
   * @param coll The collection to test.
   * @throws Exception If the test failes.
   */
  private void subTestCollectionType(Collection coll) throws Exception {
    Collection in = makeTestCollection(coll);
    String serial = trans.serializeToString(in);
    Collection out = (Collection) trans.deserializeFromString(serial);
    assertEquals(in.getClass(), out.getClass());
    assertEquals(in.getClass().getName(), in, out);
  }

  /**
   * Test (de)serialization of ArrayList.
   * @throws Exception If the test failes.
   */
  public void testArrayList() throws Exception {
    subTestCollectionType(new ArrayList());
  }

  /**
   * Test (de)serialization of LinkedList.
   * @throws Exception If the test failes.
   */
  public void testLinkedList() throws Exception {
    subTestCollectionType(new LinkedList());
  }

  /**
   * Test (de)serialization of HashSet.
   * @throws Exception If the test failes.
   */
  public void testHashSet() throws Exception {
    subTestCollectionType(new HashSet());
  }

  /**
   * Test (de)serialization of TreeSet.
   * @throws Exception If the test failes.
   */
  public void testTreeSet() throws Exception {
    subTestCollectionType(new TreeSet());
  }

  /**
   * Make a test map.
   * @param map An empty map of the concrete type.
   * @return The map filled with some values.
   */
  private Map makeTestMap(Map map) {
    map.put("k1", "v1");
    map.put("k2", "v2");
    map.put("k3", new Integer(3));
    map.put("k4", new Double(3.14));
    return map;
  }

  /**
   * Test (de)serialization of a map.
   * @param map The map to test.
   * @throws Exception If the test failes.
   */
  private void subTestMapType(Map map) throws Exception {
    Map in = makeTestMap(map);
    String serial = trans.serializeToString(in);
    Map out = (Map) trans.deserializeFromString(serial);
    assertEquals(in.getClass(), out.getClass());
    assertEquals(in.getClass().getName(), in, out);
  }

  /**
   * Test (de)serialization of Hashtable.
   * @throws Exception If the test failes.
   */
  public void testHashtable() throws Exception {
    subTestMapType(new Hashtable());
  }

  /**
   * Test (de)serialization of HashMap.
   * @throws Exception If the test failes.
   */
  public void testHashMap() throws Exception {
    subTestMapType(new HashMap());
  }

  /**
   * Test (de)serialization of TreeMap.
   * @throws Exception If the test failes.
   */
  public void testTreeMap() throws Exception {
    subTestMapType(new TreeMap());
  }

  /**
   * Test if collections with the type "java.util.Collection"
   * can be still deserialized.
   * @throws Exception If the test failes.
   */
  public void testCollectionBackwardCompatibility() throws Exception {
    Collection x = makeTestCollection(new ArrayList());

    String serial =
      "<java.util.Collection"
        + " xsi:type=\""
        + "java.util.Collection"
        + "\" "
        + XML_NS
        + "><cel xsi:type=\"xsd:string\">x1</cel><cel xsi:type=\"xsd:string\">x2</cel><cel xsi:type=\"xsd:string\">x3</cel>"
        + "</java.util.Collection>";

    Collection y = (Collection) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);
  }

  /**
   * Test the field order using skaFieldOrder.
   * @throws Exception If the test failes.
   */
  public void testSkaFieldOrder() throws Exception {
    SkaFieldOrderObj x = new SkaFieldOrderObj();
    String value =
      "<alpha xsi:type=\"xsd:int\">3</alpha><v xsi:type=\"xsd:int\">1</v><a xsi:type=\"xsd:int\">2</a>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testSkaFieldOrder", serial, value);
    SkaFieldOrderObj y = (SkaFieldOrderObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);

    // validate
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(SkaFieldOrderObj.class, new StreamResult(str));
    String schema = str.toString();
    validate(schema, serial);
  }

  /**
   * Test the lexicographical field ordering.
   * @throws Exception If the test failes.
   */
  public void testLexFieldOrder() throws Exception {
    trans.setProperty(PropertyKeys.SORT_FIELDS, "yes");
    LexFieldOrderObj x = new LexFieldOrderObj();
    String value =
      "<a xsi:type=\"xsd:int\">2</a><alpha xsi:type=\"xsd:int\">3</alpha><v xsi:type=\"xsd:int\">1</v>";
    String serial = trans.serializeToString(x);
    checkSerializedString("testLexFieldOrder", serial, value);
    LexFieldOrderObj y = (LexFieldOrderObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer", x, y);

    // validate
    StringWriter str = new StringWriter();
    trans.writeXMLSchema(LexFieldOrderObj.class, new StreamResult(str));
    String schema = str.toString();
    validate(schema, serial);
  }

  /**
   * Test the usage of different class loaders.
   * @throws Exception If the test failes.
   */
  public void testClassLoader() throws Exception {
    ComplexArrayObj x = new ComplexArrayObj(3);
    String serial = trans.serializeToString(x);

    ComplexArrayObj y1 = (ComplexArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer 1", x, y1);

    try {
      trans.setClassLoader(null);
      ComplexArrayObj y2 =
        (ComplexArrayObj) trans.deserializeFromString(serial);
      fail("DeserializerException not thrown.");
    }
    catch (DeserializerException e) {
      // OK
    }

    trans.setClassLoader(getClass().getClassLoader());
    ComplexArrayObj y3 = (ComplexArrayObj) trans.deserializeFromString(serial);
    assertEquals("wrong deserializer 3", x, y3);
  }

  /**
   * Test if unknown fields in the XML
   * are skipped during deserialization.
   * @throws Exception If the test failes.
   */
  public void testSkipUnknownFields() throws Exception {
    String serial =
      "<IntObj xsi:type=\"com.skaringa.javaxml.test.IntObj\" "
        + XML_NS
        + "><value xsi:type=\"xsd:int\">100</value>"
        + "<unknownField xsi:type=\"xsd:string\">Unknown</unknownField>"
        + "</IntObj>";

    try {
      IntObj obj = (IntObj) trans.deserializeFromString(serial);
      fail("an exception should have been thrown.");
    }
    catch (DeserializerException e) {
      // OK
    }

    trans.setProperty(PropertyKeys.SKIP_UNKNOWN_FIELDS, "yes");
    IntObj obj = (IntObj) trans.deserializeFromString(serial);
    assertNotNull(obj);
    assertEquals(100, obj.getValue());

  }

  /**
   * Test if xsi and xsd namespace attribute occur exactly once in the output.
   * @throws Exception if the test fails.
   */
  public void testDuplicateNsAttr() throws Exception {
    String serial = trans.serializeToString("Test");
    int i = serial.indexOf("xmlns:xsd=");
    assertTrue("no xmlns:xsd attribute", i > -1);
    assertEquals(
      "duplicate xmlns:xsd attribute",
      -1,
      serial.indexOf("xmlns:xsd=", i + 1));
    i = serial.indexOf("xmlns:xsi=");
    assertTrue("no xmlns:xsi attribute", i > -1);
    assertEquals(
      "duplicate xmlns:xsi attribute",
      -1,
      serial.indexOf("xmlns:xsi=", i + 1));
  }
}
TOP

Related Classes of com.skaringa.javaxml.test.JavaXMLTestClass

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.