1 package jimm.datavision.test; 2 import jimm.datavision.Report; 3 import jimm.datavision.test.mock.source.MockDataSource; 4 import jimm.util.XMLWriter; 5 import java.awt.Color ; 6 import java.io.*; 7 8 import junit.framework.TestCase; 9 import junit.framework.TestSuite; 10 import junit.framework.Test; 11 12 public class XMLWriterTest extends TestCase { 13 14 protected static final File EXAMPLE_REPORT = 15 new File(AllTests.testDataFile("test.xml")); 16 protected static final File PARAM_INPUT_FILE = 17 new File(AllTests.testDataFile("test_parameters.xml")); 18 19 protected StringWriter s; 20 protected XMLWriter out; 21 protected String linesep; 22 23 public static Test suite() { 24 return new TestSuite(XMLWriterTest.class); 25 } 26 27 public XMLWriterTest(String name) { 28 super(name); 29 } 30 31 public void setUp() { 32 s = new StringWriter(); 33 out = new XMLWriter(s, false, 1); linesep = System.getProperty("line.separator").toString(); 35 } 36 37 public void testXML_1() { 38 Color c = new Color (12, 34, 56); 39 out.xmlDecl("UTF-8"); 40 out.startElement("foo"); 41 out.startElement("bar"); 42 out.endElement(); 43 out.startElement("bletch"); 44 out.attr("color", c); 45 out.endElement(); 46 out.endElement(); 47 out.close(); 48 49 assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + linesep 50 + "<foo>" + linesep 51 + " <bar/>" + linesep 52 + " <bletch color=\"" + c.getRed() + ';' + c.getGreen() 53 + ';' + c.getBlue() + ';' + c.getAlpha() + "\"/>" + linesep 54 + "</foo>" + linesep, 55 s.toString()); 56 } 57 58 public void testXML_2() { 59 out.xmlDecl("UTF-8"); 60 out.startElement("foo"); 61 out.attr("a1", 1); 62 out.attr("a2", 3.5); 63 out.attr("a3", 'x'); 64 out.startElement("bar"); 65 out.endElement(); 66 out.textElement("text", "contents"); 67 out.cdataElement("cdata", "cdata contents"); 68 out.startElement("bletch"); 69 out.endElement(); 70 out.endElement(); 71 out.close(); 72 73 assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + linesep 74 + "<foo a1=\"1\" a2=\"3.5\" a3=\"x\">" + linesep 75 + " <bar/>" + linesep 76 + " <text>contents</text>" + linesep 77 + " <cdata><![CDATA[cdata contents]]></cdata>" + linesep 78 + " <bletch/>" + linesep 79 + "</foo>" + linesep, 80 s.toString()); 81 } 82 83 public void testReportWrite() { 84 Report report = null; 85 try { 86 report = new Report(); 87 report.setDataSource(new MockDataSource(report)); 88 report.read(EXAMPLE_REPORT); report.setParameterXMLInput(PARAM_INPUT_FILE); 90 91 File f = File.createTempFile("xml-writer-test", ".xml"); 92 f.deleteOnExit(); 93 report.writeFile(f.getPath()); 94 report.read(f); 95 f.delete(); 96 } 97 catch (Exception e) { 98 fail(e.toString()); 99 e.printStackTrace(); 100 } 101 } 102 103 public static void main(String [] args) { 104 junit.textui.TestRunner.run(suite()); 105 System.exit(0); 106 } 107 108 } 109 | Popular Tags |