001: package org.obe.test.standalone;
002:
003: import java.util.Date;
004: import java.util.HashMap;
005: import java.util.Map;
006: import junit.framework.TestCase;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.dom4j.DocumentException;
010: import org.obe.XMLException;
011: import org.obe.xpdl.model.data.SchemaType;
012: import org.obe.xpdl.model.misc.ExtendedAttributes;
013: import org.w3c.dom.Document;
014:
015: /**
016: *
017: *
018: * @author Adrian Price
019: */
020: public class XMLFragmentTest extends TestCase {
021: private static final Log _logger = LogFactory
022: .getLog(XMLFragmentTest.class);
023: private static final String SCHEMA_TEXT = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
024: + "elementFormDefault=\"qualified\" "
025: + "attributeFormDefault=\"unqualified\">\n"
026: + " <xsd:element name=\"Status\">\n"
027: + " <xsd:simpleType>\n"
028: + " <xsd:restriction base=\"xsd:NMTOKEN\">\n"
029: + " <xsd:enumeration value=\"ValidData\"/>\n"
030: + " <xsd:enumeration value=\"InvalidData\"/>\n"
031: + " <xsd:enumeration value=\"Accept\"/>\n"
032: + " <xsd:enumeration value=\"BadCredit\"/>\n"
033: + " <xsd:enumeration value=\"OverLimit\"/>\n"
034: + " <xsd:enumeration value=\"BadDataFormat\"/>\n"
035: + " </xsd:restriction>\n"
036: + " </xsd:simpleType>\n"
037: + " </xsd:element>\n"
038: + "</xsd:schema>";
039: private static final String ATTRS_NS_TEXT = "<xpdl:ExtendedAttributes"
040: + " xmlns:xpdl=\"http://www.wfmc.org/2002/XPDL1.0\" "
041: + " xmlns:xyz=\"http://www.xyzeorder.com/workflow\">\n"
042: + " <xpdl:ExtendedAttribute Name=\"Coordinates\">\n"
043: + " <xyz:Coordinates xpos=\"10\" ypos=\"20\"/>\n"
044: + " </xpdl:ExtendedAttribute>\n"
045: + "</xpdl:ExtendedAttributes>\n";
046: private static final String ATTRS_DEFAULT_NS_TEXT = "<ExtendedAttributes "
047: + " xmlns=\"http://www.wfmc.org/2002/XPDL1.0\""
048: + " xmlns:xyz=\"http://www.xyzeorder.com/workflow\">\n"
049: + " <ExtendedAttribute Name=\"Coordinates\">\n"
050: + " <xyz:Coordinates xpos=\"10\" ypos=\"20\"/>\n"
051: + " </ExtendedAttribute>\n" + "</ExtendedAttributes>\n";
052: protected static final String MALFORMED_XML = "<bad-xml></awful-xml>";
053: private static final Object[][] _simpleAttrs = { { "Null", null },
054: { "String", "Hello, World" },
055: { "Integer", new Integer(123) },
056: { "Boolean", Boolean.TRUE }, { "Date", new Date() }, };
057: private static final Map _simpleAttrMap = new HashMap();
058:
059: static {
060: for (int i = 0; i < _simpleAttrs.length; i++)
061: _simpleAttrMap.put(_simpleAttrs[i][0], _simpleAttrs[i][1]);
062: }
063:
064: public XMLFragmentTest(String string) {
065: super (string);
066: }
067:
068: public void testSchemaType() throws XMLException, DocumentException {
069: SchemaType schemaType = new SchemaType();
070: schemaType.setText(SCHEMA_TEXT);
071: Document document = schemaType.getDocument();
072: schemaType.setDocument(document);
073: _logger.info("setDocument(SCHEMA_TEXT)/getText:\n"
074: + schemaType.getText());
075:
076: // Test null text.
077: schemaType.setText(null);
078: assertNull(schemaType.getText());
079: assertNull(schemaType.getDocument());
080:
081: // Test malformed XML.
082: try {
083: schemaType.setText(MALFORMED_XML);
084: fail("Malformed XML failed to throw exception");
085: } catch (XMLException e) {
086: }
087:
088: // Test invalid document element name.
089: try {
090: schemaType
091: .setText("<xsd:badschema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"/>");
092: fail("Invalid schema failed to throw exception");
093: } catch (XMLException e) {
094: }
095:
096: // Test invalid document element name.
097: try {
098: schemaType
099: .setText("<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema-bad\"/>");
100: fail("Invalid xsd: namespace failed to throw exception");
101: } catch (XMLException e) {
102: }
103: }
104:
105: public void testExtendedAttributes() throws XMLException,
106: DocumentException {
107:
108: ExtendedAttributes extAttrs = new ExtendedAttributes();
109: extAttrs.setText(ATTRS_NS_TEXT);
110: Document document = extAttrs.getDocument();
111: extAttrs.setDocument(document);
112: _logger.info("setDocument(ATTRS_NS_TEXT)/getText:\n"
113: + extAttrs.getText());
114:
115: extAttrs.setText(ATTRS_DEFAULT_NS_TEXT);
116: document = extAttrs.getDocument();
117: extAttrs.setDocument(document);
118: _logger.info("setDocument(ATTRS_DEFAULT_NS_TEXT)/getText:\n"
119: + extAttrs.getText());
120:
121: // Test null text.
122: extAttrs.setText(null);
123: assertNull("text is non-null", extAttrs.getText());
124: assertNull("document is non-null", extAttrs.getDocument());
125: assertNotNull("map is null", extAttrs.getMap());
126: assertEquals("map is not empty", 0, extAttrs.getMap().size());
127:
128: extAttrs.setMap(_simpleAttrMap);
129: String text = extAttrs.getText();
130: _logger.info("setMap/getText:\n" + text);
131:
132: extAttrs.setText(text);
133: _logger.info("setText/getMap:\n" + extAttrs.getMap());
134:
135: // Test malformed XML.
136: try {
137: extAttrs.setText(MALFORMED_XML);
138: fail("Malformed XML failed to throw exception");
139: } catch (XMLException e) {
140: }
141:
142: // Test invalid document element name.
143: try {
144: extAttrs.setText("<Bad-ExtendedAttributes/>");
145: fail("Invalid document element failed to throw exception");
146: } catch (XMLException e) {
147: }
148:
149: // Test invalid document element name.
150: try {
151: extAttrs
152: .setText("<ExtendedAttributes xmlns=\"http://www.wfmc.org/2002/XPDL1.0-bad\"/>");
153: fail("Invalid xpdl: namespace failed to throw exception");
154: } catch (XMLException e) {
155: }
156: }
157: }
|