Package org.bifrost.xmlio.test

Source Code of org.bifrost.xmlio.test.FunctionalTestXmlWriter

package org.bifrost.xmlio.test;

import java.io.CharArrayWriter;
import java.io.File;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

import junit.framework.TestCase;
import junit.textui.TestRunner;

import org.bifrost.xmlio.XmlException;
import org.bifrost.xmlio.XmlWriter;
import org.bifrost.xmlio.config.DateConverter;
import org.bifrost.xmlio.config.GregorianCalendarConverter;
import org.bifrost.xmlio.config.ObjectMap;
import org.bifrost.xmlio.config.PropertyMap;
import org.bifrost.xmlio.config.TimestampConverter;
import org.bifrost.xmlio.config.XmlIOConfig;
import org.bifrost.xmlio.test.helpers.TestHelperArray;
import org.bifrost.xmlio.test.helpers.TestHelperArrayList;
import org.bifrost.xmlio.test.helpers.TestHelperBoolean;
import org.bifrost.xmlio.test.helpers.TestHelperCollection;
import org.bifrost.xmlio.test.helpers.TestHelperCompany;
import org.bifrost.xmlio.test.helpers.TestHelperComplex;
import org.bifrost.xmlio.test.helpers.TestHelperComplexArray;
import org.bifrost.xmlio.test.helpers.TestHelperDate;
import org.bifrost.xmlio.test.helpers.TestHelperEmpty;
import org.bifrost.xmlio.test.helpers.TestHelperParent;
import org.bifrost.xmlio.test.helpers.TestHelperPerson;
import org.bifrost.xmlio.test.helpers.TestHelperSimple;
import org.bifrost.xmlio.test.helpers.TestHelperTypes;
import org.bifrost.xmlio.test.helpers.inheritance.TestHelperBoat;
import org.bifrost.xmlio.test.helpers.inheritance.TestHelperCar;
import org.bifrost.xmlio.test.helpers.inheritance.TestHelperOwner;
import org.custommonkey.xmlunit.Diff;

/**
* <p>Unit tests to validate the XmlWriter package.</p>
* <p>
* Created: Feb 24, 2003<br/>
* Copyright: Copyright (c) 2003<br/>
* Assumptions: none<br/>
* Requires: TestHelperSimple, TestHelperComplex, TestHelperBroken<br/>
* Required by: nothing<br/>
* Revision History:<br/>
* </p>
* @author Donald Kittle <donald@bifrost.org>  (last commit: $Author: donald $)
* @version 1.0 (revision $Revision: 1.12 $)
*/

public class FunctionalTestXmlWriter extends TestCase
{

  /**
   * Strings that should be the result of serializing various object graphs.
   */
  private static final String SIMPLE_TEST = "<TestHelperSimple><string>mdk</string><number>808</number></TestHelperSimple>";
  private static final String NULL_TEST = "<TestHelperSimple><string><null></null></string><number>808</number></TestHelperSimple>";
  private static final String COMPLEX_TEST = "<TestHelperComplex><TestHelperSimple><number>333</number><string>ccc</string></TestHelperSimple><TestHelperSimple><number>444</number><string>ddd</string></TestHelperSimple></TestHelperComplex>";
  private static final String FORMATTING_INDENT_TEST = "<TestHelperComplex>.<TestHelperSimple>..<number>333</number>..<string>ccc</string>.</TestHelperSimple>.<TestHelperSimple>..<number>444</number>..<string>ddd</string>.</TestHelperSimple></TestHelperComplex>";
  private static final String FORMATTING_INDENT_TEST2 = "<TestHelperComplex>.<TestHelperSimple>..<string>ccc</string>..<number>333</number>.</TestHelperSimple>.<TestHelperSimple>..<string>ddd</string>..<number>444</number>.</TestHelperSimple></TestHelperComplex>";
  private static final String FORMATTING_LINE_BREAK_TEST = "<TestHelperComplex>.<TestHelperSimple>.<number>333</number>.<string>ccc</string>.</TestHelperSimple>.<TestHelperSimple>.<number>444</number>.<string>ddd</string>.</TestHelperSimple>.</TestHelperComplex>.";
  private static final String FORMATTING_LINE_BREAK_TEST2 = "<TestHelperComplex>.<TestHelperSimple>.<string>ccc</string>.<number>333</number>.</TestHelperSimple>.<TestHelperSimple>.<string>ddd</string>.<number>444</number>.</TestHelperSimple>.</TestHelperComplex>.";
  private static final String SIMPLE_ARRAY_TEST = "<TestHelperArray><number>123</number><string>aaa</string><string>bbb</string><string>ccc</string></TestHelperArray>";
  private static final String COMPLEX_ARRAY_TEST = "<TestHelperComplexArray><number>123</number><TestHelperSimple><string>aaa</string><number>222</number></TestHelperSimple><TestHelperSimple><string>bbb</string><number>333</number></TestHelperSimple><TestHelperSimple><string>ccc</string><number>444</number></TestHelperSimple></TestHelperComplexArray>";
  private static final String BOOLEAN_TEST = "<TestHelperBoolean><fooled>true</fooled><rich>false</rich></TestHelperBoolean>";
  private static final String EMPTY_TEST = "<TestHelperEmpty></TestHelperEmpty>";
  private static final String FULL_PACKAGE_TEST = "<org.bifrost.xmlio.test.helpers.TestHelperSimple><string>mdk</string><number>808</number></org.bifrost.xmlio.test.helpers.TestHelperSimple>";
  private static final String TYPES_TEST = "<TestHelperTypes><string>mdk</string><pint>1</pint><integer>2</integer><plong>3</plong><olong>4</olong><pbool>true</pbool><obool>true</obool><pfloat>5.2</pfloat><ofloat>6.2</ofloat><pshort>7</pshort><oshort>8</oshort><pbyte>9</pbyte><obyte>10</obyte><pchar>a</pchar><ochar>b</ochar><pdouble>11.2</pdouble><odouble>12.2</odouble><date>2003-10-20 10:32:53</date></TestHelperTypes>";
  private static final String TEST_OBJECT_PROPERTY_MAPPING = "<TestHelperComplex><obj><a>aaa</a><b>222</b></obj><obj><a>bbb</a><b>333</b></obj><obj><a>ccc</a><b>444</b></obj></TestHelperComplex>";
  private static final String SIMPLE_ATTRIBUTES = "<TestHelperSimple string=\"mdk\" number=\"808\"/>";
  private static final String MIXED_ATTRIBUTES = "<TestHelperSimple number=\"808\"><string>mdk</string></TestHelperSimple>";
  private static final String COMPLEX_ATTRIBUTES = "<TestHelperComplex><TestHelperSimple number=\"333\" string =\"ccc\"/><TestHelperSimple number=\"444\" string=\"ddd\"/></TestHelperComplex>";
  private static final String DATE_TEST_SIMPLE = "<TestHelperDate><created>2003-10-20 10:32</created><startDate>2002-06-16</startDate><startTime>10:32:54</startTime><endTime>23:04:47</endTime><calendar>2002-09-21 23:47:34</calendar></TestHelperDate>";
  private static final String COLLECTION_TEST = "<TestHelperCollection><widget>waazoo</widget><widget>wizbang</widget><widget>hugug</widget><cheese>cheddar</cheese><cheese>swiss</cheese><book>Dantes Inferno</book><book>Cantebury Tales</book><movie>LotR</movie><movie>Fight Club</movie></TestHelperCollection>";
  private static final String ARRAYLIST_TEST = "<TestHelperArrayList></TestHelperArrayList>";
  private static final String SIMPLE_AMP = "<TestHelperSimple><string>a&amp;b</string><number>808</number></TestHelperSimple>";
  private static final String SIMPLE_BRACKETS = "<TestHelperSimple><string>a&lt;b&gt;c</string><number>808</number></TestHelperSimple>";
  private static final String SIMPLE_CHILD = "<TestHelperParent><variable>hello world</variable><TestHelperParent><variable>salut le monde</variable></TestHelperParent></TestHelperParent>";
  private static final String DEEP_CHILD = "<TestHelperParent><variable>a1</variable><TestHelperParent><variable>b2</variable><TestHelperParent><variable>c3</variable><TestHelperParent><variable>d4</variable></TestHelperParent></TestHelperParent></TestHelperParent></TestHelperParent>";
  private static final String SIMPLE_DASHES = "<test-helper-simple><number>808</number><string>mdk</string></test-helper-simple>";
  private static final String DASHES_AND_INHERITANCE = "<test-helper-owner><name>Jane Smith</name><test-helper-car><vin>A880BBC64</vin><kms>18768</kms><number-doors>5</number-doors></test-helper-car><test-helper-boat><vin>6674C</vin><kms>7248</kms><top-speed-in-knots>54</top-speed-in-knots></test-helper-boat></test-helper-owner>";
  private static final String COMPANY_NO_PERSON = "<TestHelperCompany><name>Company Name</name></TestHelperCompany>";

  /**
   * Runs all tests (test* methods) using a console based user interface.
   */
  public static void main(String args[])
  {
    TestRunner.run(FunctionalTestXmlWriter.class);
  } // end main()

  /**
   * Constructor for TestCase
   */
  public FunctionalTestXmlWriter(String s) {
    super(s);
  } // end Constructor()

  /**
   * Sets up the tests.
   */
  public void setUp()
  {
    // Reset to default configuration between each test
    XmlIOConfig.getInstance().reset();
  }

  /**
   * Removes the test information from the system.
   */
  public void tearDown()
  {
  }

  /**
   * Test serializing an object made of of all the types that are considered
   * primative.
   */
  public void testAllTypes()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperTypes types = new TestHelperTypes();
    assertNotNull(types);
    types.setPint(1);
    types.setInteger(new Integer(2));
    types.setString("mdk");
    types.setPlong(3l);
    types.setOlong(new Long(4));
    types.setPbool(true);
    types.setObool(new Boolean(true));
    types.setPfloat(5.2f);
    types.setOfloat(new Float(6.2));
    types.setPshort((short)7);
    types.setOshort(new Short((short)8));
    types.setPbyte((byte)9);
    types.setObyte(new Byte((byte)10));
    types.setPchar('a');
    types.setOchar(new Character('b'));
    types.setPdouble(11.2d);
    types.setOdouble(new Double(12.2));

    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2003);
    cal.set(Calendar.MONTH, 9);
    cal.set(Calendar.DAY_OF_MONTH, 20);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 32);
    cal.set(Calendar.SECOND, 53);

    types.setDate(cal.getTime());
   
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(types);
      myDiff = new Diff(TYPES_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output: " + se);
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testAllTypes()

  /**
   * Test serializing a simple object made of of primative attributes.
   */
  public void testSimpleXml()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("mdk");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleXml()
 
  /**
   * Test serializing an object which has a collection of objects of a type the
   * same as it's own type.
   */
  public void testSimpleParentChild()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperParent simple = new TestHelperParent();
    assertNotNull(simple);
    simple.setVariable("hello world");
    TestHelperParent child = new TestHelperParent();
    assertNotNull(child);
    child.setVariable("salut le monde");
    simple.addTestHelperParent(child);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_CHILD, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleParentChild()

  /**
   * Test serializing an object which has a collection of objects of a type the
   * same as it's own type.
   */
  public void testDeepParentChild()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperParent deep = new TestHelperParent();
    TestHelperParent parent = deep;
    assertNotNull(deep);
    deep.setVariable("a1");
    TestHelperParent child = new TestHelperParent();
    assertNotNull(child);
    child.setVariable("b2");
    parent.addTestHelperParent(child);
    parent = child;
    child = new TestHelperParent();
    assertNotNull(child);
    child.setVariable("c3");
    parent.addTestHelperParent(child);
    parent = child;
    child = new TestHelperParent();
    assertNotNull(child);
    child.setVariable("d4");
    parent.addTestHelperParent(child);
    parent = child;
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(deep);
      myDiff = new Diff(DEEP_CHILD, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleParentChild()

  /**
   * Test serializing a simple object which has a string that has an
   * ampersand in it.
   */
  public void testSimpleWithAmpersand()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("a&b");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_AMP, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleWithAmpersand()

  /**
   * Test serializing a simple object which has a string that has a less-than
   * and greater-than sign in it.
   */
  public void testSimpleWithLtGt()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("a<b>c");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_BRACKETS, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleWithLtGt()

  /**
   * Test serializing a simple object made of of primative attributes that
   * outputs the fully qualified classname for object elements.
   */
  public void testFullPackageNameXml()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("mdk");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setIncludePackageName(true);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(FULL_PACKAGE_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleXml()

  /**
   * Test serializing a simple object made of of primative attributes.
   */
  public void testEmptyXml()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperEmpty empty = new TestHelperEmpty();
    assertNotNull(empty);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(empty);
      myDiff = new Diff(EMPTY_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testEmptyXml()

  /**
   * Test serializing a simple object made of of primative boolean attributes.
   */
  public void testBooleanXml()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperBoolean simple = new TestHelperBoolean();
    assertNotNull(simple);
    simple.setFooled(true);
    simple.setRich(false);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(BOOLEAN_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testBooleanXml()

  /**
   * Test serializing a simple object made of of primative boolean attributes.
   */
  public void testCollections()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperCollection helper = new TestHelperCollection();
    assertNotNull(helper);
    helper.addWidget("waazoo");
    helper.addWidget("wizbang");
    helper.addWidget("hugug");
    helper.setCheese("cheddar");
    helper.setCheese("swiss");
    helper.addBook("Dantes Inferno");
    helper.addBook("Cantebury Tales");
    helper.addMovie("LotR");
    helper.addMovie("Fight Club");
   
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(helper);
      myDiff = new Diff(COLLECTION_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testBooleanXml()

  /**
   * Test walking a graph where the root object has other, non-primative objects in it.
   */
  public void testComplexXml()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperComplex complex = new TestHelperComplex();
    assertNotNull(complex);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(333);
    simple.setString("ccc");
    complex.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(444);
    simple.setString("ddd");
    complex.setTestHelperSimple(simple);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(complex);
      myDiff = new Diff(COMPLEX_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
   
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testComplexXml()

  /**
   * Test serializing an object with date and timestamp properties.
   */
  public void testDateComplex()
  {
    // Configure date/timestamp formats
    XmlIOConfig config = XmlIOConfig.getInstance();
    // Override the default date and timestamp converters to use a different
    // format.
    DateConverter dconv = new DateConverter("yyyy-MM-dd");
    config.addConverter(dconv);
    TimestampConverter tconv = new TimestampConverter("yyyy-MM-dd HH:mm");
    config.addConverter(tconv);
   
    List plist = new LinkedList();
    PropertyMap pmap = new PropertyMap("created");
    plist.add(pmap);
    pmap = new PropertyMap("startDate");
    plist.add(pmap);
    pmap = new PropertyMap("startTime");
    dconv = new DateConverter("HH:mm:ss");
    pmap.setConverter(dconv);
    plist.add(pmap);
    pmap = new PropertyMap("endTime");
    pmap.setConverter(dconv);
    plist.add(pmap);
    pmap = new PropertyMap("calendar");
    pmap.setConverter(new GregorianCalendarConverter());
    plist.add(pmap);
    ObjectMap omap = new ObjectMap("TestHelperDate", "TestHelperDate", plist);
    config.addObjectMap(omap);
   
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperDate helper = new TestHelperDate();
    assertNotNull(helper);
   
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2003);
    cal.set(Calendar.MONTH, 9);
    cal.set(Calendar.DAY_OF_MONTH, 20);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 32);
    helper.setCreated(new Timestamp(cal.getTime().getTime()));

    cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2002);
    cal.set(Calendar.MONTH, 5);
    cal.set(Calendar.DAY_OF_MONTH, 16);
    helper.setStartDate(cal.getTime());
   
    cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 32);
    cal.set(Calendar.SECOND, 54);
    helper.setStartTime(cal.getTime());

    cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 4);
    cal.set(Calendar.SECOND, 47);
    helper.setEndTime(cal.getTime());

    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(Calendar.YEAR, 2002);
    gcal.set(Calendar.MONTH, 8);
    gcal.set(Calendar.DAY_OF_MONTH, 21);
    gcal.set(Calendar.HOUR_OF_DAY, 23);
    gcal.set(Calendar.MINUTE, 47);
    gcal.set(Calendar.SECOND, 34);
    helper.setCalendar(gcal);

    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(helper);
      myDiff = new Diff(DATE_TEST_SIMPLE, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testDate()

  /**
   * Test serializing a simple object made of of primative attributes.
   */
  public void testSimpleArray()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperArray array = new TestHelperArray();
    assertNotNull(array);
    array.setNumber(123);
    array.setString("aaa");
    array.setString("bbb");
    array.setString("ccc");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(array);
      myDiff = new Diff(SIMPLE_ARRAY_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleXml()

  /**
   * Test serializing an object with an array list whose first element is NULL
   */
  public void testArrayList()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperArrayList helper = new TestHelperArrayList();
    assertNotNull(helper);
    Diff myDiff = null;
    helper.addList(null);
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(helper);
      myDiff = new Diff(ARRAYLIST_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleXml()

  /**
   * Test serializing a simple object made of of primative attributes.
   */
  public void testComplexArray()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperComplexArray array = new TestHelperComplexArray();
    assertNotNull(array);
    array.setNumber(123);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(222);
    simple.setString("aaa");
    array.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(333);
    simple.setString("bbb");
    array.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(444);
    simple.setString("ccc");
    array.setTestHelperSimple(simple);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(array);
      myDiff = new Diff(COMPLEX_ARRAY_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleXml()


  /**
   * Test serializing a simple object made of of primative properties, where the
   * properties are serialized to XML attributes, not elements.
   */
  public void testSimpleAttributes()
  {
    XmlIOConfig config = XmlIOConfig.getInstance();
    // Set up mapping
    ObjectMap oMap =
      ObjectMap.createFromClassAsAttributes(TestHelperSimple.class);
    config.addObjectMap(oMap);

    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("mdk");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_ATTRIBUTES, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleAttributes()

  /**
   * Test serializing a simple object made of of primative properties, where the
   * properties are serialized to XML attributes, not elements.
   */
  public void testMixedAttributes()
  {
    XmlIOConfig config = XmlIOConfig.getInstance();
    // Set up mapping
    ObjectMap oMap = ObjectMap.createFromClass(TestHelperSimple.class);
    PropertyMap pMap = oMap.getPropertyMapFromName("number");
    pMap.setWriteAsAttribute(true);
    config.addObjectMap(oMap);

    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("mdk");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(MIXED_ATTRIBUTES, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleAttributes()

  /**
   * Test writing out a complex object which is a wrapper around a list of
   * more simple objects where the tag names of the simple objects and simple
   * object attributes are mapped from their java names to different xml names.
   */
  public void testObjectPropertyMapping()
  {
//    String configString = createXml(TEST_OBJECT_PROPERTY_MAPPING);
    XmlIOConfig config = XmlIOConfig.getInstance();
    // Set up mapping
    List list = new LinkedList();
    list.add(new PropertyMap("a", "string"));
    list.add(new PropertyMap("b", "number"));
    ObjectMap oMap = new ObjectMap("obj", "TestHelperSimple", list);
    config.addObjectMap(oMap);
    list = new LinkedList();
    list.add(new PropertyMap("obj", "testHelperSimple"));
    oMap = new ObjectMap("TestHelperComplex", "TestHelperComplex", list);
    config.addObjectMap(oMap);
    // Create the object graph
    TestHelperComplex helper = new TestHelperComplex();
    assertNotNull(helper);
    TestHelperSimple simple = new TestHelperSimple(222l, "aaa");
    helper.setTestHelperSimple(simple);
    simple = new TestHelperSimple(333l, "bbb");
    helper.setTestHelperSimple(simple);
    simple = new TestHelperSimple(444l, "ccc");
    helper.setTestHelperSimple(simple);
    // Serialize graph and compare
    CharArrayWriter output = new CharArrayWriter();
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(helper);
      myDiff = new Diff(TEST_OBJECT_PROPERTY_MAPPING, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testObjectPropertyMapping()

  /**
   * Test walking a graph where the root object has other, non-primative objects
   * in it.  The wrapper object outputs in the inner objects as elements, but
   * the properties of the inner objects are output as XML attributes.
   */
  public void testComplexAttributes()
  {
    XmlIOConfig config = XmlIOConfig.getInstance();
    ObjectMap oMap =
      ObjectMap.createFromClassAsAttributes(TestHelperSimple.class);
    config.addObjectMap(oMap);

    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperComplex complex = new TestHelperComplex();
    assertNotNull(complex);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(333);
    simple.setString("ccc");
    complex.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(444);
    simple.setString("ddd");
    complex.setTestHelperSimple(simple);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(complex);
      myDiff = new Diff(COMPLEX_ATTRIBUTES, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
   
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testComplexAttributes()

  /**
   * Test the ability to have indentation characters in the xml output.
   */
  public void testIndentedOutput()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperComplex complex = new TestHelperComplex();
    assertNotNull(complex);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(333);
    simple.setString("ccc");
    complex.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(444);
    simple.setString("ddd");
    complex.setTestHelperSimple(simple);
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setBeautify(true);
      xmlWriter.setBreakString("");
      xmlWriter.setIndentString(".");
      xmlWriter.setRootObject(complex);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    assertTrue("Got " + output.toString(),
      FORMATTING_INDENT_TEST.equals(output.toString()) ||
      FORMATTING_INDENT_TEST2.equals(output.toString())
      );
  } // end testIndentOutput()

  /**
   * Test the ability to have line separator characters in the xml output.
   */
  public void testLineBreakOutput()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperComplex complex = new TestHelperComplex();
    assertNotNull(complex);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(333);
    simple.setString("ccc");
    complex.setTestHelperSimple(simple);
    simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(444);
    simple.setString("ddd");
    complex.setTestHelperSimple(simple);
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setBeautify(true);
      xmlWriter.setBreakString(".");
      xmlWriter.setIndentString("");
      xmlWriter.setRootObject(complex);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    assertTrue("Got " + output.toString(),
      FORMATTING_LINE_BREAK_TEST.equals(output.toString()) ||
      FORMATTING_LINE_BREAK_TEST2.equals(output.toString())
      );
  } // end testLineBreakOutput()

  /**
   * Test the case where an object getting serialized has an attribute that is null.
   */
  public void testObjectWithNullAttribute()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString(null);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(NULL_TEST, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testInvalidObject()

  /**
   * Test the case where there is a 'primative' type within a List or Set attribute.  This
   * condition should throw and XmlException.
   */
  public void testInvalidCollection()
  {
//    CharArrayWriter output = new CharArrayWriter();
//    assertNotNull(output);
//    TestHelperBroken broken = new TestHelperBroken();
//    assertNotNull(broken);
//    TestHelperSimple simple = new TestHelperSimple();
//    assertNotNull(simple);
//    simple.setString("test");
//    broken.setList(simple);
//    broken.setList(new Long(123));
//    try
//    {
//      XmlWriter xmlWriter = new XmlWriter(output);
//      assertNotNull(xmlWriter);
//      xmlWriter.setRootObject(broken);
//    }
//    catch (XmlException e)
//    {
//      String test = e.toString().toLowerCase();
//      assertTrue(test.indexOf(XmlWriter.EX_PRIMATIVE.toLowerCase()) >= 0);
//      return;
//    }
//    fail("Should have gotten an XmlException");
  } // end testInvalidCollection()

  /**
   * Test to make sure that an invalid filename throws an XmlException
   */
  public void testInvalidFile()
  {
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setString("test");
    try
    {
      XmlWriter xmlWriter = new XmlWriter("/foobardpath/foobarfile.xml");
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
    }
    catch (XmlException e)
    {
      String test = e.toString().toLowerCase();
      assertTrue(test.indexOf(XmlWriter.EX_FILE.toLowerCase()) >= 0);
      return;
    }
    fail("Should have gotten an XmlException");
  } // end testInvalidFile()

  /**
   * Test to make sure that a valid filename does not throw an XmlException
   */
  public void testValidFile()
  {
    String filename = "./foobarfile.xml";
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setString("test");
    simple.setNumber(345);
    try
    {
      XmlWriter xmlWriter = new XmlWriter(filename);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
    }
    catch (XmlException e)
    {
      fail("Should not have gotten an XmlException");
    }
    File file = new File(filename);
    file.delete();
  } // end testInvalidFile()

  /**
   * Test serializing a simple object made of of primative attributes but use
   * names with dashes between words instead of camel casing
   */
  public void testSimpleDashes()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperSimple simple = new TestHelperSimple();
    assertNotNull(simple);
    simple.setNumber(808);
    simple.setString("mdk");
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setDashedNames(true);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(SIMPLE_DASHES, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testSimpleDashes()

  /**
   * Test serializing a simple object made of of primative attributes.
   */
  public void testDashesAndInheritance()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperOwner owner = new TestHelperOwner();
    assertNotNull(owner);
    owner.setName("Jane Smith");
    TestHelperCar car = new TestHelperCar();
    car.setKms(18768);
    car.setNumberDoors("5");
    car.setVin("A880BBC64");
    owner.addTestHelperVehicle(car);
    TestHelperBoat boat = new TestHelperBoat();
    boat.setKms(7248);
    boat.setVin("6674C");
    boat.setTopSpeedInKnots("54");
    owner.addTestHelperVehicle(boat);
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setDashedNames(true);
      xmlWriter.setRootObject(owner);
      myDiff = new Diff(DASHES_AND_INHERITANCE, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testDashesAndInheritance()

  /**
   *
   */
  public void testCompanyNoPerson()
  {
    CharArrayWriter output = new CharArrayWriter();
    assertNotNull(output);
    TestHelperCompany simple = new TestHelperCompany();
    assertNotNull(simple);
    simple.setName("Company Name");
//    simple.setTestHelperPerson(new TestHelperPerson());
    Diff myDiff = null;
    try
    {
      XmlWriter xmlWriter = new XmlWriter(output);
      assertNotNull(xmlWriter);
      xmlWriter.setRootObject(simple);
      myDiff = new Diff(COMPANY_NO_PERSON, output.toString());
      assertNotNull(myDiff);
    }
    catch (XmlException e)
    {
      fail(e.toString());
    }
    catch (Exception se)
    {
      fail("Could not parse XmlWriter output.");
    }
    assertTrue("Got " + output.toString(), myDiff.similar());
  } // end testCompanyNoPerson()

} // end FunctionalTestXmlWriter Class
TOP

Related Classes of org.bifrost.xmlio.test.FunctionalTestXmlWriter

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.