001: package jimm.datavision.test;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.test.mock.source.MockDataSource;
005: import jimm.util.XMLWriter;
006: import java.awt.Color;
007: import java.io.*;
008:
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011: import junit.framework.Test;
012:
013: public class XMLWriterTest extends TestCase {
014:
015: protected static final File EXAMPLE_REPORT = new File(AllTests
016: .testDataFile("test.xml"));
017: protected static final File PARAM_INPUT_FILE = new File(AllTests
018: .testDataFile("test_parameters.xml"));
019:
020: protected StringWriter s;
021: protected XMLWriter out;
022: protected String linesep;
023:
024: public static Test suite() {
025: return new TestSuite(XMLWriterTest.class);
026: }
027:
028: public XMLWriterTest(String name) {
029: super (name);
030: }
031:
032: public void setUp() {
033: s = new StringWriter();
034: out = new XMLWriter(s, false, 1); // Indent level == 1
035: linesep = System.getProperty("line.separator").toString();
036: }
037:
038: public void testXML_1() {
039: Color c = new Color(12, 34, 56);
040: out.xmlDecl("UTF-8");
041: out.startElement("foo");
042: out.startElement("bar");
043: out.endElement();
044: out.startElement("bletch");
045: out.attr("color", c);
046: out.endElement();
047: out.endElement();
048: out.close();
049:
050: assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
051: + linesep + "<foo>" + linesep + " <bar/>" + linesep
052: + " <bletch color=\"" + c.getRed() + ';' + c.getGreen()
053: + ';' + c.getBlue() + ';' + c.getAlpha() + "\"/>"
054: + linesep + "</foo>" + linesep, s.toString());
055: }
056:
057: public void testXML_2() {
058: out.xmlDecl("UTF-8");
059: out.startElement("foo");
060: out.attr("a1", 1);
061: out.attr("a2", 3.5);
062: out.attr("a3", 'x');
063: out.startElement("bar");
064: out.endElement();
065: out.textElement("text", "contents");
066: out.cdataElement("cdata", "cdata contents");
067: out.startElement("bletch");
068: out.endElement();
069: out.endElement();
070: out.close();
071:
072: assertEquals(
073: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + linesep
074: + "<foo a1=\"1\" a2=\"3.5\" a3=\"x\">"
075: + linesep + " <bar/>" + linesep
076: + " <text>contents</text>" + linesep
077: + " <cdata><![CDATA[cdata contents]]></cdata>"
078: + linesep + " <bletch/>" + linesep + "</foo>"
079: + linesep, s.toString());
080: }
081:
082: public void testReportWrite() {
083: Report report = null;
084: try {
085: report = new Report();
086: report.setDataSource(new MockDataSource(report));
087: report.read(EXAMPLE_REPORT); // Must come after setting password
088: report.setParameterXMLInput(PARAM_INPUT_FILE);
089:
090: File f = File.createTempFile("xml-writer-test", ".xml");
091: f.deleteOnExit();
092: report.writeFile(f.getPath());
093: report.read(f);
094: f.delete();
095: } catch (Exception e) {
096: fail(e.toString());
097: e.printStackTrace();
098: }
099: }
100:
101: public static void main(String[] args) {
102: junit.textui.TestRunner.run(suite());
103: System.exit(0);
104: }
105:
106: }
|