package org.bifrost.xmlio.config.test;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.InputStream;
import org.bifrost.xmlio.XmlException;
import org.bifrost.xmlio.XmlReader;
import org.bifrost.xmlio.XmlWriter;
import org.bifrost.xmlio.config.ObjectMap;
import org.bifrost.xmlio.config.PropertyMap;
import org.bifrost.xmlio.config.XmlIOConfig;
import org.bifrost.xmlio.test.helpers.TestHelperSimple;
import org.custommonkey.xmlunit.Diff;
import junit.framework.TestCase;
/**
* <p></p>
* <p>
* Created: Feb 26, 2004<br/>
* Copyright: Copyright (c) 2004<br/>
* Assumptions: none<br/>
* Requires: nothing<br/>
* Required by: nothing<br/>
* Revision History:<br/>
* <br/>
* </p>
* <p>Example:</p>
* <pre>
* </pre>
* <p>Conventions:<br/>
* <ul>
* </ul>
* </p>
* @author Donald Kittle <donald@bifrost.org> (last commit: $Author: donald $)
* @version 1.0 (revision $Revision: 1.5 $)
* @stereotype ??
*/
public class FunctionalTestXmlIOConfig extends TestCase
{
/**
* CVS version information.
*/
private final static String _VERSION =
"$Id: FunctionalTestXmlIOConfig.java,v 1.5 2004/05/05 15:40:34 donald Exp $";
/**
* Strings that should be the result of serializing various object graphs.
*/
private static final String SIMPLE_TEST = "<TestHelperSimple><theString>mdk</theString><theNumber>808</theNumber></TestHelperSimple>";
private static final String SIMPLE_ALIASED_TYPES = "<TestHelperAliasedTypes><name>test object</name></TestHelperAliasedTypes>";
private XmlIOConfig config;
public void setUp()
{
config = XmlIOConfig.getInstance();
config.reset();
}
/**
* Set up some simple property mappings from the tag name 'theString' to
* the object property called 'string' and from the tag name 'theNumber' to
* the object property called 'number'.
*/
private void configSimpleTest()
{
PropertyMap map = new PropertyMap();
map.setPropertyXmlName("theString");
map.setPropertyName("string");
map.setPropertyType(String.class);
config.addPropertyMap(map);
map = new PropertyMap();
map.setPropertyXmlName("theNumber");
map.setPropertyName("number");
map.setPropertyType(Long.class);
config.addPropertyMap(map);
}
/**
* Set up some simple property mappings for a specific object.
*
*/
private void configSimpleTestFromObject()
{
ObjectMap oMap = new ObjectMap();
oMap.setName("TestHelperSimple");
oMap.setXmlName("TestHelperSimple");
PropertyMap map = new PropertyMap();
map.setPropertyXmlName("theString");
map.setPropertyName("string");
map.setPropertyType(String.class);
oMap.addPropertyMap(map);
map = new PropertyMap();
map.setPropertyXmlName("theNumber");
map.setPropertyName("number");
map.setPropertyType(Long.class);
oMap.addPropertyMap(map);
config.addObjectMap(oMap);
}
public void testReadingUnmappedProperties()
{
String configString = SIMPLE_TEST;
InputStream reader = new ByteArrayInputStream(configString.getBytes());
XmlReader xmlReader = null;
try
{
xmlReader = new XmlReader(reader, "org.bifrost.xmlio.test.helpers");
}
catch (XmlException e)
{
return;
}
fail("The test should have failed and not found the attributes in the XML");
}
public void testReadingGloballyMappedProperties()
{
configSimpleTest();
PropertyMap map = new PropertyMap();
String configString = SIMPLE_TEST;
InputStream reader = new ByteArrayInputStream(configString.getBytes());
XmlReader xmlReader = null;
try
{
xmlReader = new XmlReader(reader, "org.bifrost.xmlio.test.helpers");
}
catch (XmlException e)
{
fail("Unexpected error: " + e.toString());
}
assertNotNull(xmlReader);
TestHelperSimple helper = (TestHelperSimple)xmlReader.getRootObject();
assertNotNull(helper);
assertTrue(helper.getNumber() == 808);
assertTrue("mdk".equals(helper.getString()));
}
public void testReadingObjectMappedProperties()
{
configSimpleTestFromObject();
PropertyMap map = new PropertyMap();
String configString = SIMPLE_TEST;
InputStream reader = new ByteArrayInputStream(configString.getBytes());
XmlReader xmlReader = null;
try
{
xmlReader = new XmlReader(reader, "org.bifrost.xmlio.test.helpers");
}
catch (XmlException e)
{
fail("Unexpected error: " + e.toString());
}
assertNotNull(xmlReader);
TestHelperSimple helper = (TestHelperSimple)xmlReader.getRootObject();
assertNotNull(helper);
assertTrue(helper.getNumber() == 808);
assertTrue("mdk".equals(helper.getString()));
}
public void testWritingGloballyMappedProperties()
{
configSimpleTest();
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());
}
/**
* Test serializing an object that has aliased property names.
*/
public void testAliasedTypes()
{
// CharArrayWriter output = new CharArrayWriter();
// assertNotNull(output);
// TestHelperAliasedTypes helper = new TestHelperAliasedTypes();
// assertNotNull(helper);
// helper.setName("test object");
// helper.setStartDate(new Date(System.currentTimeMillis()));
// helper.setEndDate(new Date(System.currentTimeMillis() + 50000));
// Diff myDiff = null;
// try
// {
// XmlWriter xmlWriter = new XmlWriter(output);
// assertNotNull(xmlWriter);
// xmlWriter.setRootObject(helper);
// myDiff = new Diff(SIMPLE_ALIASED_TYPES, 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()
}