01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow;
06:
07: import com.opensymphony.workflow.loader.DTDEntityResolver;
08: import com.opensymphony.workflow.loader.WorkflowDescriptor;
09:
10: import org.custommonkey.xmlunit.Diff;
11: import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
12: import org.custommonkey.xmlunit.XMLTestCase;
13:
14: import org.w3c.dom.Document;
15:
16: import org.xml.sax.InputSource;
17: import org.xml.sax.SAXException;
18:
19: import java.io.*;
20:
21: import java.net.URL;
22:
23: import javax.xml.parsers.DocumentBuilder;
24: import javax.xml.parsers.DocumentBuilderFactory;
25: import javax.xml.parsers.ParserConfigurationException;
26:
27: /**
28: * Test that when you save a descriptor, it is identical to the original.
29: * Identical means with the same xml syntax, not the exact same text wise.
30: *
31: * @author Hani Suleiman (hani@formicary.net)
32: * @author Eric Pugh (epugh@upstate.com)
33: * Date: May 5, 2003
34: * Time: 12:43:08 PM
35: */
36: public class SaveDescriptorTestCase extends XMLTestCase {
37: //~ Instance fields ////////////////////////////////////////////////////////
38:
39: private DocumentBuilder documentBuilder;
40:
41: //~ Constructors ///////////////////////////////////////////////////////////
42:
43: public SaveDescriptorTestCase(String s)
44: throws ParserConfigurationException {
45: super (s);
46: documentBuilder = DocumentBuilderFactory.newInstance()
47: .newDocumentBuilder();
48: documentBuilder.setEntityResolver(new DTDEntityResolver());
49: }
50:
51: //~ Methods ////////////////////////////////////////////////////////////////
52:
53: /**
54: *Test if a saved descriptor is identical to the original
55: */
56: public void testSave() throws Exception {
57: URL url = getClass().getResource("/samples/saved.xml");
58: Document inputDocument = documentBuilder.parse(url.toString());
59: WorkflowDescriptor descriptor = DescriptorLoader
60: .getDescriptor(url.toString());
61: StringWriter out = new StringWriter();
62: PrintWriter writer = new PrintWriter(out);
63: writer.println(WorkflowDescriptor.XML_HEADER);
64: writer.println(WorkflowDescriptor.DOCTYPE_DECL);
65: descriptor.writeXML(new PrintWriter(out), 0);
66:
67: // PrintWriter blah = new PrintWriter(new FileWriter("foo.xml"));
68: // descriptor.writeXML(blah, 0);
69: // blah.flush();
70: Document outputDocument = getDocument(out.toString());
71:
72: Diff diff = new Diff(inputDocument, outputDocument);
73: diff
74: .overrideElementQualifier(new ElementNameAndTextQualifier());
75: assertTrue(diff.toString(), diff.similar());
76: assertXMLEqual(
77: "But they are equal when an ElementQualifier controls which test element is compared with each control element",
78: diff, true);
79: assertTrue(diff.toString(), diff.similar());
80: }
81:
82: private Document getDocument(String xml) throws IOException,
83: SAXException {
84: InputSource source = new InputSource(new StringReader(xml));
85:
86: return documentBuilder.parse(source);
87: }
88: }
|